-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomize.py
39 lines (29 loc) · 901 Bytes
/
randomize.py
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
import hashlib
import random
import csv
import os
import sys
input_path = sys.argv[1]
print "Generating assignments for %s..." % input_path
print "========"
input_dir, input_fname = os.path.split(input_path)
input_noext = os.path.splitext(input_fname)[0]
output_fname = '%s-output.csv' % input_noext
output_path = os.path.join(input_dir, output_fname)
with open(input_path) as f:
r = csv.reader(f)
participants = [l[0] for l in r]
h = hashlib.sha256()
h.update('\n'.join(participants))
i = int(h.hexdigest(), 16)
random.seed(i)
def randgroup():
return 'CONTROL' if random.randint(1, 2) == 1 else 'TREATMENT'
assignments = map(lambda p: [p, randgroup()], participants)
with open(output_path, 'w') as f:
writer = csv.writer(f)
for a in assignments:
writer.writerow(a)
print "%s, %s" % (a[0], a[1])
print "========"
print "Wrote output to: %s" % output_path