{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3f90fefb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Done! Generated 30 repeats × 9 modules × 5 DVs = 1350 files in './moduleStimuli/'\n",
      "BackEnd_CurrentCount.txt written with 1350 entries.\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "import os\n",
    "\n",
    "DVs = ['Aes', 'Liking', 'Pawe', 'Tawe', 'PF']\n",
    "\n",
    "# 5 styles × 27 each = 135 stimuli\n",
    "styles = ['bauhaus', 'Celtic knot patterns', 'ornamental', 'kandinsky', 'logo']\n",
    "num_per_style = 27\n",
    "num_stimuli = len(styles) * num_per_style  # 135\n",
    "\n",
    "# Build master stimulus list: \"bauhaus_001\", \"bauhaus_002\", ...\n",
    "stim_ids_master = []\n",
    "for style in styles:\n",
    "    for i in range(1, num_per_style + 1):\n",
    "        stim_ids_master.append(f\"{style}_{i:03d}\")\n",
    "\n",
    "# Two factors (3 × 3 = 9 conditions)\n",
    "complexity_levels = ['low', 'medium', 'high']\n",
    "group_size_levels = ['individual', 'medium', 'large']\n",
    "\n",
    "num_repeats = 30  # Each stimulus rated by 30 participants per condition\n",
    "\n",
    "# Output\n",
    "output_folder = \"./moduleStimuli\"\n",
    "os.makedirs(output_folder, exist_ok=True)\n",
    "\n",
    "# ============ GENERATION ============\n",
    "def generate_modules_all_dvs(repeat_num):\n",
    "    stim_ids = stim_ids_master[:]\n",
    "    random.shuffle(stim_ids)\n",
    "\n",
    "    # Split into 3 groups of 45 for complexity counterbalancing\n",
    "    comp_groups = [stim_ids[i * 45:(i + 1) * 45] for i in range(3)]\n",
    "\n",
    "    # Within each complexity group, split into 3 subgroups of 15 for group_size\n",
    "    # cells[(i, j)] = 15 stimuli; i = complexity group, j = group_size group\n",
    "    cells = {}\n",
    "    for i in range(3):\n",
    "        for j in range(3):\n",
    "            cells[(i, j)] = comp_groups[i][j * 15:(j + 1) * 15]\n",
    "\n",
    "    # Graeco-Latin square: 3 rotations for complexity × 3 rotations for group_size = 9 modules\n",
    "    for dv in DVs:\n",
    "        for m_c in range(3):        # complexity rotation\n",
    "            for m_g in range(3):    # group_size rotation\n",
    "                assignment = []\n",
    "                for i in range(3):\n",
    "                    for j in range(3):\n",
    "                        comp = complexity_levels[(i + m_c) % 3]\n",
    "                        gs = group_size_levels[(j + m_g) % 3]\n",
    "                        for stim in cells[(i, j)]:\n",
    "                            assignment.append(f\"{stim}_{comp}_{gs}.jpg\")\n",
    "\n",
    "                random.shuffle(assignment)  # Randomize presentation order\n",
    "\n",
    "                module_idx = m_c * 3 + m_g  # 0-8\n",
    "                filename = os.path.join(\n",
    "                    output_folder,\n",
    "                    f\"{dv}_Module_{repeat_num}-{module_idx}.txt\"\n",
    "                )\n",
    "                with open(filename, \"w\") as f:\n",
    "                    f.write(\"\\n\".join(assignment))\n",
    "\n",
    "# ============ MAIN ============\n",
    "for rep in range(num_repeats):\n",
    "    generate_modules_all_dvs(rep)\n",
    "\n",
    "print(f\"Done! Generated {num_repeats} repeats × 9 modules × {len(DVs)} DVs = \"\n",
    "      f\"{num_repeats * 9 * len(DVs)} files in '{output_folder}/'\")\n",
    "\n",
    "# ============ BACKEND COUNTER FILE ============\n",
    "filenames = [f[:-4] for f in os.listdir(output_folder)\n",
    "             if f.endswith(\".txt\") and f != \"BackEnd_CurrentCount.txt\"]\n",
    "random.shuffle(filenames)\n",
    "\n",
    "output_file = \"BackEnd_CurrentCount.txt\"\n",
    "with open(output_file, \"w\") as f:\n",
    "    f.write(\"\\n\".join(f\"{name} 0\" for name in filenames))\n",
    "\n",
    "print(f\"BackEnd_CurrentCount.txt written with {len(filenames)} entries.\")"
   ]
  }
 ],
 "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
}
