{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c728bcb8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "import os\n",
    "\n",
    "# Basic configuration\n",
    "DVs = ['HairColor']\n",
    "num_stimuli = 234\n",
    "stim_ids_master = [f\"WF{i:03d}\" for i in range(1, num_stimuli + 1)]\n",
    "conditions = ['_individual', '_medium', '_large']\n",
    "num_repeats = 30  # Number of randomized modules\n",
    "\n",
    "# Create output folder\n",
    "output_folder = \"./moduleStimuli\"\n",
    "os.makedirs(output_folder, exist_ok=True)\n",
    "\n",
    "# Function to generate and save one module set for all DVs\n",
    "def generate_modules_all_dvs(repeat_num):\n",
    "    stim_ids = stim_ids_master[:]  # Copy the master list\n",
    "    random.shuffle(stim_ids)\n",
    "\n",
    "    group1 = stim_ids[0:78]\n",
    "    group2 = stim_ids[78:156]\n",
    "    group3 = stim_ids[156:234]\n",
    "\n",
    "    modules = [\n",
    "        (group1, group2, group3),  # Initial\n",
    "        (group1, group2, group3),  # Rotation 1\n",
    "        (group1, group2, group3)   # Rotation 2\n",
    "    ]\n",
    "\n",
    "    rotations = [\n",
    "        [0, 1, 2],  # _individual, _medium, _large\n",
    "        [1, 2, 0],  # _medium, _large, _individual\n",
    "        [2, 0, 1]   # _large, _individual, _medium\n",
    "    ]\n",
    "\n",
    "    for dv in DVs:\n",
    "        for m in range(3):\n",
    "            rotated = rotations[m]\n",
    "            assignment = []\n",
    "            for group, cond_idx in zip(modules[m], rotated):\n",
    "                assignment += [stim + conditions[cond_idx] for stim in group]\n",
    "            filename = os.path.join(output_folder, f\"{dv}_Module_{repeat_num}-{m}.txt\")\n",
    "            with open(filename, \"w\") as f:\n",
    "                for i, stim in enumerate(assignment):\n",
    "                    if i < len(assignment) - 1:\n",
    "                        f.write(f\"{stim}\\n\")\n",
    "                    else:\n",
    "                        f.write(f\"{stim}\")\n",
    "\n",
    "# Main loop\n",
    "for rep in range(num_repeats):\n",
    "    generate_modules_all_dvs(rep)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1bcdddce",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import random\n",
    "\n",
    "# Use the correct path where files were previously saved\n",
    "input_folder = \"moduleStimuli\"\n",
    "output_file = \"BackEnd_CurrentCount.txt\"\n",
    "\n",
    "# Get all .txt files and extract base names\n",
    "filenames = [f[:-4] for f in os.listdir(input_folder) if f.endswith(\".txt\") and f != \"BackEnd_CurrentCount.txt\"]\n",
    "\n",
    "# Shuffle them!\n",
    "random.shuffle(filenames)\n",
    "\n",
    "# Write to the new file with the specified format\n",
    "with open(output_file, \"w\") as f:\n",
    "    for i, name in enumerate(filenames):\n",
    "        if i < len(filenames) - 1:\n",
    "            f.write(f\"{name} 0\\n\")\n",
    "        else:\n",
    "            f.write(f\"{name} 0\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
