-
Notifications
You must be signed in to change notification settings - Fork 2
/
Extract_time_windows.py
222 lines (178 loc) · 6.87 KB
/
Extract_time_windows.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#Author: Louis Gomez
#Health and AI Lab
'''
SCRIPT DESCRIPTION:
This file is used to extract the specificed time windows from the dataset. The final results
are all the time wndows appended together column-wise in a resulting csv file for all patients
Inputs: conscioussfn - command following file
inputdir - Folder that contains the data-files. Each data file is in the form [pid,timestamp,v1,v2...vn]
outputdir - Output folder
first - initial time index
second - end time index. note that first is bigger than second
dataset - SAH or ICH. Different pre-processing based on the way the dataset is saved
expec-Len: expected length of the extracted window
experiment - A (hospital), B(ICU), C(Neuro-ICU) subset
'''
import pandas as pd
import glob as glob
import numpy as np
import sys
import pickle
from dateutil.parser import parse
from datetime import timedelta
def clean(df,del_labels=[]):
for label in del_labels:
try:
df = df.drop(labels=label, axis=1)
except:
continue
return df
def to_keep(df,labels):
for col in df.columns[1:]:
if col not in labels:
df = df.drop(labels = col,axis = 1)
else:
pass
return df
def extract_windows(df,time,event,PID,first,second,elen,dataset):
'''
This function is used to extract time window based on the time slice index
provided.
Df - dataframe
time - the time of consciousness assessment in CF file
event - label
window - how large the extracted window should be
PID - simply the identifier of the patient file
first, second - time slices. First is ALWAYS bigger than second
'''
if dataset == "sah":
left = time - (first*60)
right = time - (second * 60)
dfextract = df.loc[(df["Time"] >= left) & (df["Time"] < right)].copy()
else:
left = time - timedelta(minutes = first)
right = time - timedelta(minutes = second)
dfextract = df.loc[(df["DateTime"] >= left) & (df["DateTime"] < right)].copy()
if len(dfextract) != elen:
return None
column_count = dfextract.count().to_dict()
l = len(dfextract)
cols_to_drop = []
for name,val in column_count.items():
if val < (round(0.80 * l)):
cols_to_drop.append(name)
#if the number of entries in the variable column is less than 80%, drop it
dfextract.drop(columns = cols_to_drop, inplace = True)
dfextract.dropna(axis = 1, how = "all", inplace = True)
if len(dfextract.columns.tolist()) == 1:
return None
#Operations to insert three new columns
Event = np.repeat(event,len(dfextract))
pid = np.repeat(PID,len(dfextract))
Event_Time = np.repeat(time,len(dfextract))
dfextract.insert(0,"PID",pid)
dfextract.insert(1,"Event",Event)
dfextract.insert(2,"Event_Time",Event_Time)
return dfextract
def main_sah():
df_bcd = pd.read_csv(CF_file)
data = None
for _, row in df_bcd.iterrows():
PID = int(row.PID)
time = int(row.timeafterbleed)
event = int(row.Event)
filename = "Patient_" + str(PID) + ".csv"
del_labels = ["GLU","LAC","LGR","LPR","PGR","PYR","GLU-panel"]
try:
df = pd.read_csv(inputdir + filename, sep = ',')
df = clean(df,del_labels)
df.rename(columns = {"DateTime":"Time"}, inplace = True)
except Exception as e:
#print(str(PID) + " EXCEPTION: " + str(e))
continue
df = to_keep(df,cols_to_keep)
if len(df.columns) == 1:
continue
extracted_data = extract_windows(df,time,event,PID,first,second,expec_len,dataset)
if extracted_data is None:
pass
else:
data = pd.concat([data,extracted_data],sort = False)
name = "Time_windows_SAH_" + experiment + ".csv"
#file = outputdir + name
#with open(file,"wb") as f:
#pickle.dump(data,f,pickle.HIGHEST_PROTOCOL)
data.to_csv(outputdir + name,index = False)
def main_ich():
df_bcd = pd.read_csv(CF_file)
data = None
for _,row in df_bcd.iterrows():
PID = int(row.PID)
time = row.Time
event = int(row.Event)
filename = "patient_"+str(PID)+".csv"
try:
df = pd.read_csv(inputdir + filename,parse_dates = ["DateTime"])
except Exception as e:
#print(str(PID) + " EXCEPTION: " + str(e))
continue
#check if time is a valid entry
try:
time = parse(time, fuzzy = False)
except:
print(str(PID) + " invalid time record: ",time)
continue
#check if the is 0:00, these are not comple time records and have to be ignored
if time.hour == 0 and time.minute == 0:
continue
#two columns - datetime and a variable columns must be at least present to be valid
if len(df.columns.to_list()) < 2:
continue
df = to_keep(df,cols_to_keep)
extracted_data = extract_windows(df,time,event,PID,first,second,expec_len,dataset)
if extracted_data is None:
pass
else:
data = pd.concat([data,extracted_data],sort = False)
name = "Time_windows_ICH_" + str(experiment) + ".csv"
data.to_csv(outputdir + name,index = False)
if __name__ == "__main__":
if len(sys.argv) == 9:
CF_file = sys.argv[1]
inputdir = sys.argv[2]
outputdir = sys.argv[3]
first = int(sys.argv[4]) #initial time index to begin time slice
second = int(sys.argv[5]) #end time index to end time slice
dataset = str(sys.argv[6]) #name of dataset
expec_len = int(sys.argv[7]) #expected length of data window.
experiment = sys.argv[8] #A or B or C
if dataset == "sah":
if experiment == "A":
cols_to_keep = ["SPO2%","RR","HR"]
elif experiment == "B":
cols_to_keep = ["SPO2%","RR","HR","MAP","CO2EX","TMP"]
else:
cols_to_keep = ["SPO2%","RR","HR","MAP","CO2EX","TMP","ICP","PbtO2","BrT"]
elif dataset == "ich":
if experiment == "A":
cols_to_keep = ["SPO2","RR","HR"]
else:
cols_to_keep = ["SPO2","RR","HR","MAP","EtCO2","TMP",'ICP']
else:
print("incorrect dataset name")
sys.exit(0)
print("index is: ",first)
print("index is: ",second)
if second > first:
print("Incorrect time slice. First should be bigger than second")
sys.exit(0)
if dataset == "sah":
main_sah()
elif dataset == "ich":
main_ich()
else:
print("incorrect dataset name")
sys.exit(0)
else:
print("Invalid input arguements")
print("python file-name command following file, inputdir, outputdir, windowm first second True/False")