This repository has been archived by the owner on Jan 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getPatientInfo.py
72 lines (63 loc) · 2.81 KB
/
getPatientInfo.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
def interpretPy(kind, value):
if "NA": return None
answer = {"SEX": {True: "Male", False: "Female"},
"VITAL_STATUS": {True: "ALIVE", False: "DECEASED"},
"SMOKING_HISTORY": {1: "Prev/Curr Smoker", 0: "Never", -1: "Unknown"},
"OS_STATUS": {True: "LIVING", False: "DECEASED"}}
return answer[kind][value]
def interpretMan(kind, value):
if None: return "NA"
answer = {"SEX": {"Male": True, "Female": False},
"VITAL_STATUS": {"ALIVE": True, "DECEASED": False},
"SMOKING_HISTORY": {"Prev/Curr Smoker": 1, "Never": 0, "Unknown": -1},
"OS_STATUS": {"LIVING": True, "DECEASED": False}}
return answer[kind][value]
def selectPatient(patient, kind, value):
value = interpretMan(kind, value)
names = list(patient.keys())
for name in names:
if patient[name][kind] != value:
del(patient[name])
return patient
def getPatientInfo(fileName, review=True):
patient = dict()
infomations = list()
infomationsFile = open(fileName, "r")
for _ in range(4): assert infomationsFile.readline()
infomations = infomationsFile.readline().split()
while True:
line = infomationsFile.readline()
if not line: break
line = line.strip().split("\t")
name = None
for i, val_i in enumerate(line):
if i == 0:
assert val_i not in patient
patient[val_i] = dict()
name = val_i
elif i == 1:
if val_i == "Male": patient[name][infomations[i]] = True
elif val_i == "Female": patient[name][infomations[i]] = False
else: raise WrongSexInfo
elif i == 2 or i == 5:
if val_i == "ALIVE" or val_i == "LIVING": patient[name][infomations[i]] = True
elif val_i == "DECEASED": patient[name][infomations[i]] = False
elif val_i == "NA": patient[name][infomations[i]] = val_i
else: raise WrongAliveInfo
elif i == 3:
if val_i == "Prev/Curr Smoker": patient[name][infomations[i]] = 1
elif val_i == "Never": patient[name][infomations[i]] = 0
elif val_i == "Unknown": patient[name][infomations[i]] = -1
elif val_i == "NA": patient[name][infomations[i]] = val_i
else: raise WrongSmokingInfo
elif i == 4:
if val_i == "NA": patient[name][infomations[i]] = val_i
else: patient[name][infomations[i]] = float(val_i)
else: raise WrongInfo
infomationsFile.close()
return patient
if __name__ == "__main__":
info = getPatientInfo("../msk_impact_2017/data_clinical_patient.txt")
print(len(info))
info = selectPatient(info, "SMOKING_HISTORY", "Never")
print(len(info))