""" Tell the experimenter the next participant ID to assign, per group, by reading the randomization log from GitLab. Orders are fully randomised per ID, so assignment is just sequential within each group; this also prints a quick balance check. uv run python next_id.py """ import csv import io from collections import Counter from urllib.parse import quote from dotenv import load_dotenv load_dotenv() import gitlab_save # noqa: E402 import orders # noqa: E402 GROUPS = {"0": "control / healthy", "1": "CU"} def _parse_stim_by_block(s): """'0:A5-D21_1:A14-B6_...' -> list of (block, letter, stim_id).""" out = [] for chunk in (s or "").split("_"): if ":" not in chunk: continue b, rest = chunk.split(":", 1) for part in rest.split("-"): if len(part) < 2: continue out.append((int(b), part[0], int(part[1:]))) return out def fetch_log(path="randomization_log.csv"): token, project, base, branch = gitlab_save._env() if not token or not project: return None pid = quote(project, safe="") branch = branch or gitlab_save._default_branch(base, pid, token) or "main" return gitlab_save._get_raw(base, pid, token, branch, path) def main(): txt = fetch_log() rows = list(csv.DictReader(io.StringIO(txt))) if txt else [] print(f"Randomization log: {len(rows)} participant(s) recorded so far.\n") for g, label in GROUPS.items(): nums = [int(r["orderID"]) for r in rows if str(r.get("gid")) == g and str(r.get("orderID", "")).isdigit()] nxt = (max(nums) + 1) if nums else 1 print(f" group {g} ({label}): {len(nums)} done -> NEXT ID = {g}{nxt:03d}") if rows: # every one of the 24 chests is unique to its (block, letter) slot within a session now # (no persistent look-per-profile to tally), so the balance check that matters is # whether a CHEST COLOUR ends up systematically confounded with reward valence across # the sample. orders.py's stim-slot rotation now GUARANTEES this is exactly even per # complete 24-ID cycle (within a group) -- this print is a live confirmation of that # guarantee (and would catch a real bug), not a hope that a random draw stays even. print("\nColour-vs-reward-valence balance across the sample " "(guaranteed even per complete 24-ID cycle within a group):") self_good = Counter() self_bad = Counter() # Only real groups (0/1) -- orders.py's rotation is only guaranteed balanced WITHIN a # group, so a stray/test row (group 9, or any other non-recruitment gid) has no cycle # to belong to and would just add noise to this count rather than reflect a real # imbalance. for r in rows: if str(r.get("gid")) not in GROUPS: continue for _, letter, sid in _parse_stim_by_block(r.get("stim_by_block", "")): li = orders.LETTERS.index(letter) colour = sid % orders.N_COLORS (self_good if orders.LETTER_SELF_GOOD[li] else self_bad)[colour] += 1 for colour in sorted(set(self_good) | set(self_bad)): print(f" colour {colour}: self-good {self_good.get(colour, 0)}x, " f"self-bad {self_bad.get(colour, 0)}x") else: print("\n(No participants logged yet, start with 0001 / 1001.)") if __name__ == "__main__": main()