File size: 4,781 Bytes
4a98c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""Generate a pre-registered allocation table: one row per participant id.

Why a file rather than seeding from the pid on the fly:

  * a seed gives RANDOM assignment, not BALANCED assignment. Over 200
    participants a coin flip lands anywhere from about 86/114 to 114/86 between
    groups. A table fixes the split exactly.
  * it can be inspected, committed and cited before collection starts, which is
    what makes the counterbalancing pre-registered rather than asserted.
  * a participant's condition is then a lookup, immune to code changes,
    server restarts, and anything else that happens between now and testing.

Everything not fixed here (trial order within a block, which side is WORK, the
partner avatar) is still derived from the pid, but now via a STABLE seed
(effort_design._seed_from_pid uses crc32, not Python's per-process hash()).

    uv run python make_allocation.py --n 200 --out allocation.csv
"""
import argparse
import csv
import itertools
import random

import effort_design as D

#: Participant groups. These are RECRUITED, not randomised -- group is a
#: property of the person, encoded in the first digit of the pid exactly as
#: next_id.py and orders.py already assume ("0042" -> group 0, "1007" -> 1).
#: Every randomised arm below is therefore balanced WITHIN each group, so the
#: night manipulation stays orthogonal to the clinical comparison.
GROUPS = {"0": "control / healthy", "1": "CU"}

#: The between-participant arms, as explicit (night, night_order) cells.
#:
#: NOT a full crossing, because night_order only exists for the 'both' arm --
#: crossing it against the others would create cells that differ in name only.
#:
#: There is deliberately no block-order factor. Blocks 0 and 1 are two shuffles
#: of the same 24 cells with a break between them; beneficiary is interleaved
#: WITHIN each block, so there is no self-block/other-block order to balance.
#:
#: night_order matters because effort_stages.build_experiment() lists
#: _night_stage(1) before _night_stage(2), so without this every 'both'
#: participant meets Night 1 first and the Night1-vs-Night2 contrast is
#: confounded with fatigue and practice.
ARMS = [
    ("", ""),          # classic two-block task, no night conditions
    ("1", ""),         # Night 1 only
    ("2", ""),         # Night 2 only
    ("both", "12"),    # both nights, Night 1 first
    ("both", "21"),    # both nights, Night 2 first
]
THEME = "harvest"


def build(per_group=100, seed=20260827, arms=None, groups=None):
    """Balanced, shuffled allocation: `per_group` participants in each group.

    Balanced by construction -- the arm list is cycled to fill each group, then
    shuffled within it. That guarantees the split rather than hoping for it, and
    guarantees it separately per group so night is not confounded with group.
    """
    arms = arms or ARMS
    groups = groups or GROUPS
    rng = random.Random(seed)

    rows = []
    for g in sorted(groups):
        seq = [arms[i % len(arms)] for i in range(per_group)]
        rng.shuffle(seq)
        for i, (night, night_order) in enumerate(seq, start=1):
            pid = f"{g}{i:03d}"
            rows.append({
                "pid": pid,
                "group": g,
                "group_label": groups[g],
                "night": night,
                "night_order": night_order,
                "theme": THEME,
                "seed": D._seed_from_pid(pid),
            })
    return rows


def link_for(row, base="https://princessamygdala-harvestfortwo.hf.space/"):
    q = [f"pid={row['pid']}"]
    if row.get("night"):
        q.append(f"night={row['night']}")
    if row.get("night_order") == "21":
        q.append("night_order=21")
    return base + "?" + "&".join(q)


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--per-group", type=int, default=100,
                    help="participants per group (default 100 -> 200 total)")
    ap.add_argument("--out", default="allocation.csv")
    ap.add_argument("--seed", type=int, default=20260827)
    a = ap.parse_args()

    rows = build(a.per_group, a.seed)
    fields = ["pid", "group", "group_label", "night", "night_order",
              "theme", "seed", "link"]
    with open(a.out, "w", newline="", encoding="utf-8") as f:
        w = csv.DictWriter(f, fieldnames=fields)
        w.writeheader()
        for r in rows:
            w.writerow({**r, "link": link_for(r)})

    print(f"wrote {a.out}  ({len(rows)} participants)")
    counts = {}
    for r in rows:
        key = (r["group"], r["night"] or "(none)", r["night_order"] or "-")
        counts[key] = counts.get(key, 0) + 1
    for k in sorted(counts):
        print(f"  group {k[0]}  night={k[1]:6s} order={k[2]}   n={counts[k]}")


if __name__ == "__main__":
    main()