-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample_data.py
More file actions
executable file
·58 lines (52 loc) · 2.18 KB
/
Copy pathsample_data.py
File metadata and controls
executable file
·58 lines (52 loc) · 2.18 KB
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
#!/usr/bin/env python3
# Write a new set of training containing a given percentage of the
# individuals from the original data set.
# Filenames will include the sampled percentage, for example, a
# 1% sample of the va dataset will have files following the
# pattern va1_activity_locations.csv.gz
import argparse
import csv
import random
import gzip
from pathlib import Path
FILENAMES = [
"%s_person%s.csv.gz",
"%s_activity_locations%s.csv.gz",
"%s_activity_location_assignment%s.csv.gz",
"%s_disease_outcome_training%s.csv.gz",
"%s_disease_outcome_target%s.csv.gz",
]
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--prefix", default="va", help="The prefix for input data filenames")
parser.add_argument("--input", default=".", help="The directory containing input data")
parser.add_argument("--percentage", default=1, type=int, help="Percentage of individuals to sample")
parser.add_argument("--shards", default=1, type=int, help="Split the output into the given number of shards, by pid")
flags = parser.parse_args()
for filename in FILENAMES:
p = Path(flags.input).joinpath(Path(filename % (flags.prefix, "")))
with gzip.open(p, "rt") as input:
if flags.shards == 1:
outputs = [gzip.open(filename % (flags.prefix + str(flags.percentage), ""), "wt")]
else:
outputs = [gzip.open(filename % (flags.prefix + str(flags.percentage), ".%d" % i), "wt") for i in range(0, flags.shards)]
r = csv.reader(input)
ws = [csv.writer(o) for o in outputs]
headers = next(r)
if "pid" in headers:
pid = headers.index("pid")
for w in ws:
w.writerow(headers)
for row in r:
if int(row[pid]) % 100 <= flags.percentage:
ws[hash(row[pid]) % flags.shards].writerow(row)
else:
for w in ws:
w.writerow(headers)
for row in r:
for w in ws:
w.writerow(row)
for o in outputs:
o.close()
if __name__ == "__main__":
main()