-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
96 lines (79 loc) · 2.93 KB
/
Main.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
# -*- coding: utf-8 -*-
"""@author: Sami Safadi"""
from Patient import Patient
import pandas as pd
import numpy as np
import glob
demographicsFrame = pd.DataFrame(columns = ['MRN', 'age', 'gender', 'race', 'DOH'])
ptDict ={} # List containing all code values
Patients = {} # Dictonary, keys are MRN, values are Patient objects
def getNumPatients():
return len(Patients)
def getNumCodes():
return sum([Patients[p].codes.size for p in Patients])
def stageCKD(egfr):
if egfr < 15: return 5
elif egfr >= 15 and egfr < 30: return 4
elif egfr >= 30 and egfr < 60: return 3
elif egfr >= 60 and egfr < 90: return 2
else: return np.NaN
def createPtDict(df: pd.DataFrame):
global ptDict
uniqueMRN = np.unique(df.ix[:, 0])
for i in uniqueMRN:
if not(i in ptDict.keys()): ptDict[i] = list()
for i in range(len(df)):
mrn = df.ix[i, 0]
code = df.ix[i, 1]
date = df.ix[i, 2]
if not(np.isnan(mrn)):
ptDict[int(mrn)].append((code, str(date)))
return ptDict
def createPts(patients: dict):
global Patients
for key in patients:
codes = patients[key]
mrn = key
age, gender, race = 0, 0, 0
if any(demographicsFrame.MRN == key):
index = demographicsFrame.index[demographicsFrame.MRN == key][0]
age = demographicsFrame.Age[index]
gender = demographicsFrame.Gender[index]
race = demographicsFrame.Race[index]
encounterDate = demographicsFrame.DOH[index]
if len(codes) > 0: Patients[mrn] = Patient(mrn, codes, age, gender, race, encounterDate)
def getMasterTable(savetofile = False):
global Patients
keys = list(Patients.keys())
df = Patients[keys[0]].comorbiditiesFrame
for key in keys[1:]:
df = df.append(Patients[key].comorbiditiesFrame)
outputfile = "Output/Comorbidities.csv"
if savetofile: df.to_csv(outputfile, index = False)
else: print(df)
return df
def readDemographics(file: str):
global demographicsFrame
demographicsFrame = pd.read_csv(file)
def readCodes(files:[str]):
for file in files:
createPtDict(pd.read_csv(file))
createPts(ptDict)
def write():
global Patients
getMasterTable(savetofile = True)
if __name__ == "__main__":
demographicsFile = "Input/Demographics.csv"
try: readDemographics(demographicsFile)
except: print("Demographics.csv was not found")
message = "Loaded demographics file and identified {} patients...".format(demographicsFrame.MRN.size)
print(message)
ICD9Files = glob.glob("Input/Codes*.csv")
print("The following files are proccessed: ", ICD9Files)
readCodes(ICD9Files)
message = "Processed {} patients with {} comorbidities"\
.format(getNumPatients(), getNumCodes(),)
print(message)
print("Writing results to disk")
write()
input("Done, press enter to exit...")