Update dataset_utils.py
Browse files- dataset_utils.py +28 -16
dataset_utils.py
CHANGED
@@ -77,7 +77,7 @@ def vocab(task,diag_flag,proc_flag,out_flag,chart_flag,med_flag,lab_flag):
|
|
77 |
|
78 |
return len(condVocabDict),len(procVocabDict),len(medVocabDict),len(outVocabDict),len(chartVocabDict),len(labVocabDict),ethVocabDict,genderVocabDict,ageVocabDict,insVocabDict
|
79 |
|
80 |
-
def concat_data(data,task,feat_cond,feat_proc,feat_out,feat_chart,feat_meds,feat_lab
|
81 |
meds=data['Med']
|
82 |
proc = data['Proc']
|
83 |
out = data['Out']
|
@@ -98,7 +98,9 @@ def concat_data(data,task,feat_cond,feat_proc,feat_out,feat_chart,feat_meds,feat
|
|
98 |
##########COND#########
|
99 |
if (feat_cond):
|
100 |
#get all conds
|
101 |
-
|
|
|
|
|
102 |
features=pd.DataFrame(np.zeros([1,len(conds)]),columns=conds['COND'])
|
103 |
|
104 |
#onehot encode
|
@@ -120,11 +122,13 @@ def concat_data(data,task,feat_cond,feat_proc,feat_out,feat_chart,feat_meds,feat
|
|
120 |
|
121 |
##########PROC#########
|
122 |
if (feat_proc):
|
|
|
|
|
123 |
|
124 |
if proc :
|
125 |
feat=proc.keys()
|
126 |
proc_val=[proc[key] for key in feat]
|
127 |
-
procedures=pd.DataFrame(
|
128 |
features=pd.DataFrame(np.zeros([1,len(procedures)]),columns=procedures['PROC'])
|
129 |
features.columns=pd.MultiIndex.from_product([["PROC"], features.columns])
|
130 |
procs=pd.DataFrame(columns=feat)
|
@@ -140,11 +144,13 @@ def concat_data(data,task,feat_cond,feat_proc,feat_out,feat_chart,feat_meds,feat
|
|
140 |
|
141 |
##########OUT#########
|
142 |
if (feat_out):
|
|
|
|
|
143 |
|
144 |
if out :
|
145 |
feat=out.keys()
|
146 |
out_val=[out[key] for key in feat]
|
147 |
-
outputs=pd.DataFrame(
|
148 |
features=pd.DataFrame(np.zeros([1,len(outputs)]),columns=outputs['OUT'])
|
149 |
features.columns=pd.MultiIndex.from_product([["OUT"], features.columns])
|
150 |
outs=pd.DataFrame(columns=feat)
|
@@ -153,60 +159,68 @@ def concat_data(data,task,feat_cond,feat_proc,feat_out,feat_chart,feat_meds,feat
|
|
153 |
outs.columns=pd.MultiIndex.from_product([["OUT"], outs.columns])
|
154 |
out_df = pd.concat([features,outs],ignore_index=True).fillna(0)
|
155 |
else:
|
156 |
-
outputs=pd.DataFrame(
|
157 |
features=pd.DataFrame(np.zeros([1,len(outputs)]),columns=outputs['OUT'])
|
158 |
features.columns=pd.MultiIndex.from_product([["OUT"], features.columns])
|
159 |
out_df=features.fillna(0)
|
160 |
|
161 |
##########CHART#########
|
162 |
if (feat_chart):
|
|
|
|
|
|
|
163 |
if chart:
|
164 |
charts=chart['val']
|
165 |
feat=charts.keys()
|
166 |
chart_val=[charts[key] for key in feat]
|
167 |
-
charts=pd.DataFrame(
|
168 |
features=pd.DataFrame(np.zeros([1,len(charts)]),columns=charts['CHART'])
|
169 |
features.columns=pd.MultiIndex.from_product([["CHART"], features.columns])
|
|
|
170 |
chart=pd.DataFrame(columns=feat)
|
171 |
for c,v in zip(feat,chart_val):
|
172 |
chart[c]=v
|
173 |
chart.columns=pd.MultiIndex.from_product([["CHART"], chart.columns])
|
174 |
chart_df = pd.concat([features,chart],ignore_index=True).fillna(0)
|
175 |
-
|
176 |
else:
|
177 |
-
charts=pd.DataFrame(
|
178 |
features=pd.DataFrame(np.zeros([1,len(charts)]),columns=charts['CHART'])
|
179 |
features.columns=pd.MultiIndex.from_product([["CHART"], features.columns])
|
180 |
chart_df=features.fillna(0)
|
181 |
|
182 |
##########LAB#########
|
183 |
if (feat_lab):
|
|
|
|
|
|
|
184 |
if chart:
|
185 |
charts=chart['val']
|
186 |
feat=charts.keys()
|
187 |
chart_val=[charts[key] for key in feat]
|
188 |
-
charts=pd.DataFrame(
|
189 |
features=pd.DataFrame(np.zeros([1,len(charts)]),columns=charts['LAB'])
|
190 |
features.columns=pd.MultiIndex.from_product([["LAB"], features.columns])
|
191 |
|
192 |
-
|
193 |
chart=pd.DataFrame(columns=feat)
|
194 |
for c,v in zip(feat,chart_val):
|
195 |
chart[c]=v
|
196 |
chart.columns=pd.MultiIndex.from_product([["LAB"], chart.columns])
|
197 |
chart_df = pd.concat([features,chart],ignore_index=True).fillna(0)
|
198 |
else:
|
199 |
-
charts=pd.DataFrame(
|
200 |
features=pd.DataFrame(np.zeros([1,len(charts)]),columns=charts['LAB'])
|
201 |
features.columns=pd.MultiIndex.from_product([["LAB"], features.columns])
|
202 |
chart_df=features.fillna(0)
|
203 |
-
|
204 |
###MEDS
|
205 |
if (feat_meds):
|
|
|
|
|
|
|
206 |
if meds:
|
207 |
feat=meds['signal'].keys()
|
208 |
med_val=[meds['amount'][key] for key in feat]
|
209 |
-
meds=pd.DataFrame(
|
210 |
features=pd.DataFrame(np.zeros([1,len(meds)]),columns=meds['MEDS'])
|
211 |
features.columns=pd.MultiIndex.from_product([["MEDS"], features.columns])
|
212 |
|
@@ -216,7 +230,7 @@ def concat_data(data,task,feat_cond,feat_proc,feat_out,feat_chart,feat_meds,feat
|
|
216 |
med.columns=pd.MultiIndex.from_product([["MEDS"], med.columns])
|
217 |
meds_df = pd.concat([features,med],ignore_index=True).fillna(0)
|
218 |
else:
|
219 |
-
meds=pd.DataFrame(
|
220 |
features=pd.DataFrame(np.zeros([1,len(meds)]),columns=meds['MEDS'])
|
221 |
features.columns=pd.MultiIndex.from_product([["MEDS"], features.columns])
|
222 |
meds_df=features.fillna(0)
|
@@ -330,8 +344,6 @@ def generate_ml(dyn,stat,demo,concat_cols,concat):
|
|
330 |
else:
|
331 |
dyn_df=pd.concat([dyn_df,agg],axis=0)
|
332 |
dyn_df=dyn_df.T
|
333 |
-
print(dyn_df.columns)
|
334 |
-
print(dyn_df)
|
335 |
dyn_df.columns = dyn_df.iloc[0]
|
336 |
dyn_df=dyn_df.iloc[1:,:]
|
337 |
|
|
|
77 |
|
78 |
return len(condVocabDict),len(procVocabDict),len(medVocabDict),len(outVocabDict),len(chartVocabDict),len(labVocabDict),ethVocabDict,genderVocabDict,ageVocabDict,insVocabDict
|
79 |
|
80 |
+
def concat_data(data,task,feat_cond,feat_proc,feat_out,feat_chart,feat_meds,feat_lab):
|
81 |
meds=data['Med']
|
82 |
proc = data['Proc']
|
83 |
out = data['Out']
|
|
|
98 |
##########COND#########
|
99 |
if (feat_cond):
|
100 |
#get all conds
|
101 |
+
with open("./data/dict/"+task+"/condVocab", 'rb') as fp:
|
102 |
+
conDict = pickle.load(fp)
|
103 |
+
conds=pd.DataFrame(conDict,columns=['COND'])
|
104 |
features=pd.DataFrame(np.zeros([1,len(conds)]),columns=conds['COND'])
|
105 |
|
106 |
#onehot encode
|
|
|
122 |
|
123 |
##########PROC#########
|
124 |
if (feat_proc):
|
125 |
+
with open("./data/dict/"+task+"/procVocab", 'rb') as fp:
|
126 |
+
procDic = pickle.load(fp)
|
127 |
|
128 |
if proc :
|
129 |
feat=proc.keys()
|
130 |
proc_val=[proc[key] for key in feat]
|
131 |
+
procedures=pd.DataFrame(procDic,columns=['PROC'])
|
132 |
features=pd.DataFrame(np.zeros([1,len(procedures)]),columns=procedures['PROC'])
|
133 |
features.columns=pd.MultiIndex.from_product([["PROC"], features.columns])
|
134 |
procs=pd.DataFrame(columns=feat)
|
|
|
144 |
|
145 |
##########OUT#########
|
146 |
if (feat_out):
|
147 |
+
with open("./data/dict/"+task+"/outVocab", 'rb') as fp:
|
148 |
+
outDic = pickle.load(fp)
|
149 |
|
150 |
if out :
|
151 |
feat=out.keys()
|
152 |
out_val=[out[key] for key in feat]
|
153 |
+
outputs=pd.DataFrame(outDic,columns=['OUT'])
|
154 |
features=pd.DataFrame(np.zeros([1,len(outputs)]),columns=outputs['OUT'])
|
155 |
features.columns=pd.MultiIndex.from_product([["OUT"], features.columns])
|
156 |
outs=pd.DataFrame(columns=feat)
|
|
|
159 |
outs.columns=pd.MultiIndex.from_product([["OUT"], outs.columns])
|
160 |
out_df = pd.concat([features,outs],ignore_index=True).fillna(0)
|
161 |
else:
|
162 |
+
outputs=pd.DataFrame(outDic,columns=['OUT'])
|
163 |
features=pd.DataFrame(np.zeros([1,len(outputs)]),columns=outputs['OUT'])
|
164 |
features.columns=pd.MultiIndex.from_product([["OUT"], features.columns])
|
165 |
out_df=features.fillna(0)
|
166 |
|
167 |
##########CHART#########
|
168 |
if (feat_chart):
|
169 |
+
with open("./data/dict/"+task+"/chartVocab", 'rb') as fp:
|
170 |
+
chartDic = pickle.load(fp)
|
171 |
+
|
172 |
if chart:
|
173 |
charts=chart['val']
|
174 |
feat=charts.keys()
|
175 |
chart_val=[charts[key] for key in feat]
|
176 |
+
charts=pd.DataFrame(chartDic,columns=['CHART'])
|
177 |
features=pd.DataFrame(np.zeros([1,len(charts)]),columns=charts['CHART'])
|
178 |
features.columns=pd.MultiIndex.from_product([["CHART"], features.columns])
|
179 |
+
|
180 |
chart=pd.DataFrame(columns=feat)
|
181 |
for c,v in zip(feat,chart_val):
|
182 |
chart[c]=v
|
183 |
chart.columns=pd.MultiIndex.from_product([["CHART"], chart.columns])
|
184 |
chart_df = pd.concat([features,chart],ignore_index=True).fillna(0)
|
|
|
185 |
else:
|
186 |
+
charts=pd.DataFrame(chartDic,columns=['CHART'])
|
187 |
features=pd.DataFrame(np.zeros([1,len(charts)]),columns=charts['CHART'])
|
188 |
features.columns=pd.MultiIndex.from_product([["CHART"], features.columns])
|
189 |
chart_df=features.fillna(0)
|
190 |
|
191 |
##########LAB#########
|
192 |
if (feat_lab):
|
193 |
+
with open("./data/dict/"+task+"/labsVocab", 'rb') as fp:
|
194 |
+
chartDic = pickle.load(fp)
|
195 |
+
|
196 |
if chart:
|
197 |
charts=chart['val']
|
198 |
feat=charts.keys()
|
199 |
chart_val=[charts[key] for key in feat]
|
200 |
+
charts=pd.DataFrame(chartDic,columns=['LAB'])
|
201 |
features=pd.DataFrame(np.zeros([1,len(charts)]),columns=charts['LAB'])
|
202 |
features.columns=pd.MultiIndex.from_product([["LAB"], features.columns])
|
203 |
|
|
|
204 |
chart=pd.DataFrame(columns=feat)
|
205 |
for c,v in zip(feat,chart_val):
|
206 |
chart[c]=v
|
207 |
chart.columns=pd.MultiIndex.from_product([["LAB"], chart.columns])
|
208 |
chart_df = pd.concat([features,chart],ignore_index=True).fillna(0)
|
209 |
else:
|
210 |
+
charts=pd.DataFrame(chartDic,columns=['LAB'])
|
211 |
features=pd.DataFrame(np.zeros([1,len(charts)]),columns=charts['LAB'])
|
212 |
features.columns=pd.MultiIndex.from_product([["LAB"], features.columns])
|
213 |
chart_df=features.fillna(0)
|
214 |
+
|
215 |
###MEDS
|
216 |
if (feat_meds):
|
217 |
+
with open("./data/dict/"+task+"/medVocab", 'rb') as fp:
|
218 |
+
medDic = pickle.load(fp)
|
219 |
+
|
220 |
if meds:
|
221 |
feat=meds['signal'].keys()
|
222 |
med_val=[meds['amount'][key] for key in feat]
|
223 |
+
meds=pd.DataFrame(medDic,columns=['MEDS'])
|
224 |
features=pd.DataFrame(np.zeros([1,len(meds)]),columns=meds['MEDS'])
|
225 |
features.columns=pd.MultiIndex.from_product([["MEDS"], features.columns])
|
226 |
|
|
|
230 |
med.columns=pd.MultiIndex.from_product([["MEDS"], med.columns])
|
231 |
meds_df = pd.concat([features,med],ignore_index=True).fillna(0)
|
232 |
else:
|
233 |
+
meds=pd.DataFrame(medDic,columns=['MEDS'])
|
234 |
features=pd.DataFrame(np.zeros([1,len(meds)]),columns=meds['MEDS'])
|
235 |
features.columns=pd.MultiIndex.from_product([["MEDS"], features.columns])
|
236 |
meds_df=features.fillna(0)
|
|
|
344 |
else:
|
345 |
dyn_df=pd.concat([dyn_df,agg],axis=0)
|
346 |
dyn_df=dyn_df.T
|
|
|
|
|
347 |
dyn_df.columns = dyn_df.iloc[0]
|
348 |
dyn_df=dyn_df.iloc[1:,:]
|
349 |
|