Spaces:
Running
Running
Download orders.py from princessamygdala/HarvestForTwo: direct link, hf CLI and curl.
- Browser
- Download file 8.02 kB
-
https://huggingface.co/spaces/princessamygdala/HarvestForTwo/resolve/main/orders.py
- Command line
-
hf download hf://spaces/princessamygdala/HarvestForTwo/orders.py
-
curl -L -o orders.py https://huggingface.co/spaces/princessamygdala/HarvestForTwo/resolve/main/orders.py
8.02 kB
| """ | |
| Deterministic, counterbalanced order generation for Duo Treasure Hunt. | |
| The participant ID *is* the order: parsing it gives the group and a number, and from | |
| those we deterministically build that participant's whole session — reproducibly. | |
| ID format: first digit = group (0 = control/healthy, 1 = CU), the rest = a number. | |
| e.g. "0042" -> group 0, number 42 ; "1007" -> group 1, number 7. | |
| Counterbalancing: | |
| * COLOUR <-> reward-profile is *systematically* counterbalanced (4 Latin-square | |
| conditions = number % 4), because colour is the salient cue. | |
| * Block order, pair->house assignment, chest sides, and the exact outcome shuffles are | |
| *randomised but reproducible* per ID (a fixed seed from the ID) — randomisation | |
| decorrelates them from the pairs without needing a huge condition count. | |
| Outcomes are EXACT per block per recipient: 15 good (+100), 3 bad (-100), 2 neutral (0). | |
| Everything here is plain numpy and fully testable (run `python orders.py`). | |
| """ | |
| import numpy as np | |
| N_PLACES = 12 | |
| N_TRIALS = 20 | |
| LETTERS = ["A", "B", "C", "D"] | |
| PAIRS = [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)] # AB AC AD BC BD CD | |
| PLACE_PAIRS = PAIRS + PAIRS # 12 places (each pair twice) | |
| # fixed reward profiles per letter (self_good, other_good) | |
| LETTER_SELF_GOOD = [1, 1, 0, 0] # A,B good for self | |
| LETTER_OTHER_GOOD = [1, 0, 1, 0] # A,C good for partner | |
| # exact outcome counts over 20 trials (matches the OG 15 / 3 / 2) | |
| N_PLUS, N_MINUS, N_ZERO = 15, 3, 2 | |
| COIN = 100 | |
| # 24 distinct box stimuli; each participant gets 4 of them randomly assigned to the 4 | |
| # reward-profiles (letters A,B,C,D). The look is an arbitrary, randomised label. | |
| N_STIM = 24 | |
| N_COLORS = 13 # must match len(sprites.STIM_PALETTE): stimulus colour = sid % N_COLORS | |
| def _distinct_stims(rng, k=4): | |
| """Pick k stimuli with all-different colours, so a participant's boxes never look alike.""" | |
| chosen, colors = [], set() | |
| for sid in rng.permutation(N_STIM): | |
| col = int(sid) % N_COLORS | |
| if col not in colors: | |
| chosen.append(int(sid)) | |
| colors.add(col) | |
| if len(chosen) == k: | |
| break | |
| return np.array(chosen, dtype=np.int32) | |
| # candidate house positions on the 25x25 world (pick & shuffle 12 per participant) | |
| CANDIDATE_POS = np.array([ | |
| [3, 4], [2, 15], [5, 9], [4, 21], [9, 2], [8, 18], | |
| [6, 12], [11, 23], [16, 6], [17, 16], [21, 3], [20, 20], | |
| [13, 5], [7, 22], [22, 12], [3, 20], [19, 9], [14, 18], | |
| ], dtype=np.int32) | |
| def parse_pid(pid): | |
| """'0042' -> (group=0, number=42). Robust to stray characters.""" | |
| s = "".join(ch for ch in str(pid).strip() if ch.isdigit()) | |
| if not s: | |
| return 0, 0 | |
| group = int(s[0]) | |
| number = int(s[1:]) if len(s) > 1 else 0 | |
| return group, number | |
| def _seed(pid): | |
| group, number = parse_pid(pid) | |
| return group * 1_000_003 + number + 1 | |
| def _exact_sequence(good, rng): | |
| """A length-20 sequence with exactly 15/3/2, signed by good vs bad profile.""" | |
| plus, minus = (COIN, -COIN) if good else (-COIN, COIN) | |
| seq = [plus] * N_PLUS + [minus] * N_MINUS + [0] * N_ZERO | |
| seq = np.array(seq, dtype=np.int32) | |
| rng.shuffle(seq) | |
| return seq | |
| def make_order(pid): | |
| """Build the full, deterministic order for one participant ID.""" | |
| group, number = parse_pid(pid) | |
| rng = np.random.default_rng(_seed(pid)) | |
| # assign 4 distinct-coloured boxes (of 24) to the 4 reward-profiles (letters A,B,C,D) | |
| stim_map = _distinct_stims(rng, 4) # letter -> stimulus id | |
| # block order: shuffle the 12 places (each pair appears twice) | |
| block_order = rng.permutation(N_PLACES) | |
| place_pairs = np.array([PLACE_PAIRS[i] for i in block_order], dtype=np.int32) # (12,2) letters | |
| pair_code = np.array([PAIRS.index(tuple(PLACE_PAIRS[i])) for i in block_order], dtype=np.int32) | |
| # pair -> house: assign the 12 distinct houses to the 12 places | |
| house_type = rng.permutation(N_PLACES).astype(np.int32) # (12,) distinct house ids 0..11 | |
| # house positions on the map | |
| pos_idx = rng.permutation(len(CANDIDATE_POS))[:N_PLACES] | |
| house_pos = CANDIDATE_POS[pos_idx].astype(np.int32) # (12,2) | |
| # which option is on the LEFT each trial (0 => place_pairs[:,0] on left) | |
| side_schedule = rng.integers(0, 2, size=(N_PLACES, N_TRIALS)).astype(np.int32) | |
| # exact outcomes: [place, option(0/1), recipient(0 self/1 partner), trial] | |
| outcomes = np.zeros((N_PLACES, 2, 2, N_TRIALS), dtype=np.int32) | |
| for p in range(N_PLACES): | |
| for o in range(2): | |
| letter = int(place_pairs[p, o]) | |
| outcomes[p, o, 0] = _exact_sequence(LETTER_SELF_GOOD[letter], rng) | |
| outcomes[p, o, 1] = _exact_sequence(LETTER_OTHER_GOOD[letter], rng) | |
| return dict( | |
| pid=str(pid), group=group, number=number, | |
| orderID=number, stim_map=stim_map, | |
| block_order=block_order.tolist(), | |
| block_pairs_str="_".join(LETTERS[a] + LETTERS[b] for a, b in place_pairs.tolist()), | |
| place_pairs=place_pairs, pair_code=pair_code, house_type=house_type, | |
| house_pos=house_pos, side_schedule=side_schedule, outcomes=outcomes, | |
| ) | |
| def make_practice_order(pid="practice"): | |
| """Practice order: one nearby 'forest hut', the AB pair, clear outcomes. | |
| Structure is fixed (so practice always teaches the same thing), but the chest sides | |
| and the outcome order are randomised PER PARTICIPANT (seeded from the PID) so there's | |
| no shared, memorisable practice pattern. | |
| """ | |
| o = make_order("90000") | |
| rng = np.random.default_rng(_seed(pid) ^ 0x9E3779B9) # distinct practice stream per pid | |
| o["house_pos"] = o["house_pos"].copy() | |
| o["house_pos"][0] = [12, 16] # near the start tile | |
| o["place_pairs"] = o["place_pairs"].copy() | |
| o["place_pairs"][0] = [0, 1] # pair AB (A=good/good, B=good/bad) | |
| o["pair_code"] = o["pair_code"].copy() | |
| o["pair_code"][0] = 0 | |
| o["house_type"] = o["house_type"].copy() | |
| o["house_type"][0] = 12 # practice-only forest hut | |
| o["stim_map"] = o["stim_map"].copy() | |
| o["stim_map"][0], o["stim_map"][1] = 24, 25 # practice-only boxes (not in main pool) | |
| o["side_schedule"] = o["side_schedule"].copy() | |
| o["side_schedule"][0] = rng.integers(0, 2, size=N_TRIALS).astype(np.int32) # randomise sides | |
| o["outcomes"] = o["outcomes"].copy() | |
| for opt, letter in enumerate([0, 1]): # rebuild place-0 outcomes for AB | |
| o["outcomes"][0, opt, 0] = _exact_sequence(LETTER_SELF_GOOD[letter], rng) | |
| o["outcomes"][0, opt, 1] = _exact_sequence(LETTER_OTHER_GOOD[letter], rng) | |
| return o | |
| def manifest_row(pid): | |
| """One flat dict summarising a participant's randomisation (for the rando log).""" | |
| o = make_order(pid) | |
| sm = o["stim_map"].tolist() | |
| return dict( | |
| pid=o["pid"], gid=o["group"], orderID=o["orderID"], | |
| A_stim=sm[0], B_stim=sm[1], C_stim=sm[2], D_stim=sm[3], # profile -> stimulus id | |
| block_order=o["block_pairs_str"], | |
| house_assignment="_".join(str(h) for h in o["house_type"].tolist()), | |
| ) | |
| if __name__ == "__main__": | |
| # --- self-tests --- | |
| o = make_order("0042") | |
| assert o["group"] == 0 and o["number"] == 42 | |
| # exact counts hold for every place/option/recipient | |
| for p in range(N_PLACES): | |
| for opt in range(2): | |
| for r in range(2): | |
| seq = o["outcomes"][p, opt, r] | |
| assert (seq == COIN).sum() + (seq == -COIN).sum() == 18 | |
| assert (seq == 0).sum() == 2 | |
| # reproducible | |
| assert np.array_equal(make_order("0042")["outcomes"], o["outcomes"]) | |
| # 4 distinct stimuli drawn from the 24-box pool | |
| sm = o["stim_map"] | |
| assert len(set(sm.tolist())) == 4 and all(0 <= x < N_STIM for x in sm) | |
| print("orders.py self-tests passed.\n") | |
| print("Sample manifest rows:") | |
| for pid in ["0001", "0002", "0003", "0004", "1001", "1002"]: | |
| print(" ", manifest_row(pid)) | |