-
Notifications
You must be signed in to change notification settings - Fork 24
/
create-cadence-partners-csv.py
71 lines (57 loc) · 2.03 KB
/
create-cadence-partners-csv.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
import json
import csv
# Turn a JSON file into a Python dict or list
def data_from_json(filename):
with open(filename) as f:
return json.loads(f.read())
# Convert the comma-separated list of partners, like ",3,", into a Python list
# of partners, like ["America's Frontline Doctors"]
def get_partners(partner_lookup, patient):
partners = []
partner_ids = patient["partner"].split(",")
for partner_id in partner_ids:
if partner_id != "":
partners.append(partner_lookup[int(partner_id)])
return partners
# Export a CSV that lists Cadence partners
def main():
partner_rows = []
# Load the Cadence patient data
patients_data = data_from_json(
"data/horse_around_find_out/cadence_allpatients_all.json"
)
# Load the Cadence partners data
partners_data = data_from_json(
"data/horse_around_find_out/cadence_health_partners.json"
)
# Create a dictionary that maps a partner ID with its name
partner_lookup = {}
for partner in partners_data:
partner_lookup[partner["id"]] = partner["name"]
# Loop through all of the partners
for partner in partners_data:
# Count how many patients use this partner
patients = 0
for patient in patients_data["patients"]:
patient_partners = get_partners(partner_lookup, patient)
for patient_partner in patient_partners:
if patient_partner == partner["name"]:
patients += 1
# Add the partner's row
partner_rows.append(
{
"ID": partner["id"],
"Name": partner["name"],
"Domain": partner["domain"],
"Patients": patients,
}
)
# Write the CSV file
headers = ["ID", "Name", "Domain", "Patients"]
csv_filename = "cadence-partners.csv"
with open(csv_filename, "w") as f:
writer = csv.DictWriter(f, headers)
writer.writeheader()
writer.writerows(partner_rows)
if __name__ == "__main__":
main()