File size: 899 Bytes
804f155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
"""Reproducible per-participant sampling and presentation order."""
import hashlib
import random
import uuid


def make_assignment(study, participant_id):
    participant_id = str(uuid.UUID(participant_id))
    seed = int.from_bytes(hashlib.sha256(f"{study['version']}/{participant_id}".encode()).digest()[:8], "big") % (2 ** 53)
    rng = random.Random(seed)
    trial_order, option_order = [], {}
    for kind in ["audio", "video"]:
        block = [t["id"] for t in study["trials"] if t["kind"] == kind]
        rng.shuffle(block)
        trial_order.extend(block)
    for trial in study["trials"]:
        baselines = [c["id"] for c in trial["candidates"] if c["id"] != trial["ours"]]
        options = [trial["ours"], *rng.sample(baselines, 3)]
        rng.shuffle(options)
        option_order[trial["id"]] = options
    return seed, {"trial_order": trial_order, "option_order": option_order}