File size: 6,283 Bytes
bf22a94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
123
124
125
126
"""The predetermined pool of randomisation ids, and how a study draws from it.

A `rando` id is one entry in a fixed pool of designs. Give a session a rando and its whole
design follows from that number alone: the pid becomes a label, free to be any number in
any order with any gaps, and two people who share a pid get two designs because they hold
two pool entries.

WHY A POOL AND NOT THE pid
orders.make_order builds three cyclic rotations over `number`: the block order over 12
entry positions, the chest-slot assignment over 24 stimuli, the house layout over 5 maps.
Reading `number` out of the pid means the pid has to behave like a counter for those to
come out even, and a recruitment identifier does not. The SONA sample is the demonstration:
its pids are HMAC hashes, so its numbers are arbitrary and its rotations came out balanced
only in expectation. Drawing from a pool puts the counter back where it belongs.

WHAT EVEN LOOKS LIKE
The rotations have periods 12, 24 and 5, and 12 divides 24, so a design is fixed by
(number mod 24, number mod 5). Those are independent and repeat every lcm(24,5) = 120.
So a run of 120 consecutive ids covers every (chest rotation, layout) combination exactly
once. POOL_SIZE is not a multiple of 120: it is sized by the parent gap instead, for the
reason given at the constant. Measured over candidate id sets, min and max uses of
each cell:

    id set                          block /12    chest /24    layout /5
    1..120 consecutive              10-10        5-5          24-24
    1..100 consecutive              8-9          4-5          20-20
    every 7th (1,8,15,...)          8-9          4-5          20-20
    100 ids picked at random        5-12         0-7          15-27
    every 10th (1,11,21,...)        0-17         0-9          0-100

Consecutive is not the requirement. A constant step coprime to 24 and 5 is just as even,
which is why every 7th matches 1..100. A step sharing a factor is what collapses: every
10th puts all 100 sessions on one layout. Consecutive is step 1, which is coprime to
everything, so it is the choice that cannot be got wrong.

ALLOCATING TO CHILDREN AND PARENTS
Two blocks of consecutive ids, PARENT_GAP apart. The gap has to be coprime to 24 and 5 or
a parent lands on some of their child's rotations: 100 leaves every dyad on one layout
(100 % 5 == 0) and 120 gives every dyad the identical design (120 is the whole period).
103 shifts the block order by 7, the chest slots by 7 and the layout by 3.

    uv run python randos.py --children 100 --parents 100
"""
import argparse

N_BLOCK, N_CHEST, N_LAYOUT = 12, 24, 5
PERIOD = 120                    # lcm(N_CHEST, N_LAYOUT); N_BLOCK divides N_CHEST

#: Distance from a child's id to their parent's. Coprime to 24 and 5, so the parent moves
#: on all three rotations at once. Large enough that a 100-child block and a 100-parent
#: block do not overlap.
PARENT_GAP = 103


#: Exactly the dyads PARENT_GAP allows, and no more. Children take 1..C and parents
#: 1+PARENT_GAP..C+PARENT_GAP, so the two blocks collide once C passes PARENT_GAP: the
#: ceiling is 103 dyads whatever the pool holds, and 2 * PARENT_GAP is that ceiling written
#: down. A larger pool only adds entries no dyad can reach. To run more than 103 dyads,
#: raise PARENT_GAP to another value coprime to 12, 24 and 5 and set this to twice it.
#:
#: It is not a whole number of PERIODs, so the pool TAKEN AS A WHOLE is not exactly even.
#: That is not the quantity that matters: children and parents are analysed as two samples,
#: each drawn as a consecutive run, and each run is even on its own (see report()).
POOL_SIZE = 2 * PARENT_GAP

def pool(size=POOL_SIZE):
    """The pool: ids 1..size, in order. Hand them out in order."""
    return list(range(1, size + 1))


def allocate(n_children, n_parents=0, size=POOL_SIZE):
    """(child_ids, parent_ids). Two consecutive blocks PARENT_GAP apart."""
    if n_children < 1:
        raise ValueError("need at least one child")
    kids = list(range(1, n_children + 1))
    parents = [n + PARENT_GAP for n in range(1, n_parents + 1)]
    used = kids + parents
    if set(kids) & set(parents):
        raise ValueError(f"{n_children} children and {n_parents} parents overlap at a gap "
                         f"of {PARENT_GAP}; widen PARENT_GAP or shrink the blocks")
    over = [i for i in used if i > size]
    if over:
        raise ValueError(f"ids {min(over)}-{max(over)} fall outside a pool of {size}; "
                         f"raise POOL_SIZE to a multiple of {PERIOD}")
    return kids, parents


def balance(ids):
    """{'block': (min,max), 'chest': (min,max), 'layout': (min,max)} uses per cell."""
    out = {}
    for name, m in (("block", N_BLOCK), ("chest", N_CHEST), ("layout", N_LAYOUT)):
        counts = [sum(1 for i in ids if i % m == cell) for cell in range(m)]
        out[name] = (min(counts), max(counts))
    return out


def report(n_children, n_parents=0, size=POOL_SIZE):
    kids, parents = allocate(n_children, n_parents, size)
    lines = [f"pool of {size} ids, {PERIOD} per complete cycle",
             f"  children  {kids[0]}-{kids[-1]}  ({len(kids)})"]
    if parents:
        lines.append(f"  parents   {parents[0]}-{parents[-1]}  ({len(parents)})")
    lines.append("")
    lines.append(f"{'':12s}{'block /12':>14s}{'chest /24':>14s}{'layout /5':>14s}")
    for name, ids in (("children", kids), ("parents", parents), ("both", kids + parents)):
        if not ids:
            continue
        b = balance(ids)
        lines.append(f"{name:12s}" + "".join(
            f"{b[k][0]}-{b[k][1]:<12d}" for k in ("block", "chest", "layout")))
    if parents:
        lines.append("")
        lines.append(f"every dyad differs on all three rotations: "
                     f"{all(PARENT_GAP % m for m in (N_BLOCK, N_CHEST, N_LAYOUT))}")
    return "\n".join(lines)


if __name__ == "__main__":
    ap = argparse.ArgumentParser(description=__doc__,
                                 formatter_class=argparse.RawDescriptionHelpFormatter)
    ap.add_argument("--children", type=int, required=True)
    ap.add_argument("--parents", type=int, default=0)
    ap.add_argument("--size", type=int, default=POOL_SIZE)
    a = ap.parse_args()
    print(report(a.children, a.parents, a.size))