-
Notifications
You must be signed in to change notification settings - Fork 8
/
linkid_to_patid.py
195 lines (173 loc) · 6.69 KB
/
linkid_to_patid.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python3
import argparse
import csv
import json
import os
import sys
from io import TextIOWrapper
from pathlib import Path
from zipfile import ZipFile
from utils.validate_metadata import get_metadata, verify_metadata
HEADERS = ["LINK_ID", "PATID"]
HH_HEADERS = ["HOUSEHOLD_ID", "PATID"]
def parse_arguments():
parser = argparse.ArgumentParser(
description="Tool for translating LINK_IDs back into PATIDs"
)
parser.add_argument("--sourcefile", help="Source pii-TIMESTAMP.csv file")
parser.add_argument("--linkszip", help="LINK_ID ZIP file from linkage agent")
parser.add_argument(
"--hhsourcefile",
help="Household PII csv, either inferred by households.py"
" or provided by data owner",
)
parser.add_argument(
"--hhlinkszip",
help="HOUSEHOLD_ID zip file from linkage agent",
)
parser.add_argument(
"-o",
"--outputdir",
dest="outputdir",
default="output",
help="Specify an output directory for links. Default is './output'",
)
parser.add_argument(
"--force",
"-f",
dest="force",
action="store_true",
default=False,
help="Attempt resolution of patids from linkids even if issues are found"
"in metadata file. USE ONLY AS LAST RESORT",
)
args = parser.parse_args()
return args
def parse_source_file(source_file):
pii_lines = []
with open(Path(source_file)) as source:
source_reader = csv.reader(source)
pii_lines = list(source_reader)
return pii_lines
def write_patid_links(args):
links_archive = Path(args.linkszip)
pii_lines = parse_source_file(args.sourcefile)
filepath = os.path.join(args.outputdir, "linkid_to_patid.csv")
with open(
filepath,
"w",
newline="",
encoding="utf-8",
) as csvfile:
writer = csv.writer(csvfile)
writer.writerow(HEADERS)
with ZipFile(links_archive) as link_zip:
links_list = list(filter(lambda x: ".csv" in x, link_zip.namelist()))
if len(links_list) > 1:
sys.exit(
f"ERROR: found more than one .csv "
f"file in link archive {links_archive.name}"
)
with link_zip.open(links_list[0]) as links:
links_reader = csv.reader(
TextIOWrapper(links, encoding="UTF-8", newline="")
)
# Skipping header
next(links_reader)
for row in links_reader:
link_id = row[0]
# The +1 accounts for the header row in spreadsheet index
patid = pii_lines[int(row[1]) + 1][0]
writer.writerow([link_id, patid])
print(f"Wrote {filepath}")
def write_hh_links(args):
hh_links_file = Path(args.hhlinkszip)
hh_pii_lines = parse_source_file(args.hhsourcefile)
filepath = os.path.join(args.outputdir, "householdid_to_patid.csv")
with open(
filepath,
"w",
newline="",
encoding="utf-8",
) as csvfile:
writer = csv.writer(csvfile)
writer.writerow(HH_HEADERS)
with ZipFile(hh_links_file) as hh_archive:
hh_links_list = list(filter(lambda x: ".csv" in x, hh_archive.namelist()))
if len(hh_links_list) > 1:
print(
f"WARNING: found more than one .csv "
f"file in link archive {hh_links_file.name}"
)
print(f"\tUsing {hh_links_list[0]}")
with hh_archive.open(hh_links_list[0]) as links:
links_reader = csv.reader(
TextIOWrapper(links, encoding="UTF-8", newline="")
)
# Skipping header
next(links_reader)
for row in links_reader:
household_id = row[0]
household_position = row[1]
# The +1 accounts for the header row in spreadsheet index
# HH_PII headers:
# family_name,phone_number,household_street_address,household_zip,record_ids
record_ids = hh_pii_lines[int(household_position) + 1][4]
record_ids_list = record_ids.split(",")
for record_id in record_ids_list:
writer.writerow([household_id, record_id])
print(f"Wrote {filepath}")
def translate_linkids(args):
if args.linkszip and args.sourcefile:
source_metadata_filename = Path(args.sourcefile).parent / Path(
args.sourcefile
).name.replace("pii", "metadata").replace(".csv", ".json")
with open(source_metadata_filename) as source_metadata_file:
source_metadata = json.load(source_metadata_file)
link_metadata = get_metadata(args.linkszip)["input_system_metadata"]
metadata_issues = verify_metadata(
source_metadata,
link_metadata,
source_name=source_metadata_filename,
linkage_name=args.linkszip,
)
if len(metadata_issues) > 0:
print(
f"{'WARNING' if args.force else 'ERROR'}: "
f"Inconsistencies found in source "
f"metadata file {args.sourcefile}"
f" and linkage archive metadata in {args.linkszip}:"
)
for issue in metadata_issues:
print("\t" + issue)
if len(metadata_issues) == 0 or args.force:
write_patid_links(args)
if args.hhlinkszip and args.hhsourcefile:
source_metadata_filename = Path(args.hhsourcefile).parent / Path(
args.hhsourcefile
).name.replace("pii", "metadata").replace(".csv", ".json")
with open(source_metadata_filename) as source_metadata_file:
source_metadata = json.load(source_metadata_file)
link_metadata = get_metadata(args.hhlinkszip)["input_system_metadata"]
metadata_issues = verify_metadata(
source_metadata,
link_metadata,
source_name=source_metadata_filename,
linkage_name=args.hhlinkszip,
)
if len(metadata_issues) > 0:
print(
f"{'WARNING' if args.force else 'ERROR'}: "
f"Inconsistencies found in source "
f"metadata file {args.sourcefile}"
f" and linkage archive metadata in {args.linkszip}:"
)
for issue in metadata_issues:
print("\t" + issue)
if len(metadata_issues) == 0 or args.force:
write_hh_links(args)
def main():
args = parse_arguments()
translate_linkids(args)
if __name__ == "__main__":
main()