-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_gformula_script.py
More file actions
268 lines (235 loc) · 11.7 KB
/
3_gformula_script.py
File metadata and controls
268 lines (235 loc) · 11.7 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import numpy as np
import pandas as pd
import gc
import datetime
import joblib
from pygformula import ParametricGformula
from pygformula.parametric_gformula.interventions import static
from sklearn.preprocessing import StandardScaler
import os
import shutil
import glob
# Start timer
startTime = datetime.datetime.now()
# Setup info for saving path purposes and others
analysis_method = 'gformula'
missing_assumption = 'mar' # 'struct_miss' or 'mar'
data_fraction = 'complete' # 'complete' or 'sample'
modelling = 'ML' # 'GLM' or 'ML'
save_results = True
normalize_data = False
parallel = True
# Print some params
print('Model type: {}'.format(modelling))
print('Normalize data: {}'.format(normalize_data))
print('Data: {}'.format(data_fraction))
# Loop over files
for file in glob.glob("/mnt/dicoms/borja_files/CovidVax_DM/data/currentData02092025/chunks_100_{}/*.csv".format(missing_assumption)):
# Read data
data = pd.read_csv(file)
# If sampling
if data_fraction == 'sample':
data = data.sample(frac=0.1)
data.reset_index(inplace=True, drop=True)
# Number of Monte-Carlo simulations
#n_simul = int(np.ceil(0.1*len(data)))
n_simul = len(data)
###### PIVOT DATA
for i in range(1,4):
data.rename({'VACUNA_{}_DATA'.format(i): 'VACUNA_DATA_{}'.format(i)}, axis=1, inplace=True)
data.rename({'VACUNA_{}_MOTIU'.format(i): 'VACUNA_MOTIU_{}'.format(i)}, axis=1, inplace=True)
data.rename({'VACUNA_{}_DATA_pp'.format(i): 'VACUNA_DATA_pp_{}'.format(i)}, axis=1, inplace=True)
stubnames = ['VACUNA_DATA_1', 'VACUNA_MOTIU_1', 'VACUNA_DATA_2',
'VACUNA_MOTIU_2', 'VACUNA_DATA_3', 'VACUNA_MOTIU_3', 'VACUNA_DATA_pp_1',
'VACUNA_DATA_pp_2', 'VACUNA_DATA_pp_3',
'test_date_covid_1', 'test_res_covid_1',
'test_date_covid_2', 'test_res_covid_2', 'test_date_covid_3',
'test_res_covid_3', 'test_date_imc_1', 'test_res_imc_1',
'test_date_imc_2', 'test_res_imc_2', 'test_date_imc_3',
'test_res_imc_3', 'test_date_sp_1', 'test_res_sp_1', 'test_date_sp_2',
'test_res_sp_2', 'test_date_sp_3', 'test_res_sp_3', 'test_date_dp_1',
'test_res_dp_1', 'test_date_dp_2', 'test_res_dp_2', 'test_date_dp_3',
'test_res_dp_3', 'test_date_abdo_1', 'test_res_abdo_1',
'test_date_abdo_2', 'test_res_abdo_2', 'test_date_abdo_3',
'test_res_abdo_3', 'test_date_bg_1', 'test_res_bg_1', 'test_date_bg_2',
'test_res_bg_2', 'test_date_bg_3', 'test_res_bg_3', 'test_date_chol_1',
'test_res_chol_1', 'test_date_chol_2', 'test_res_chol_2',
'test_date_chol_3', 'test_res_chol_3', 'test_date_smoking_1',
'test_res_smoking_1', 'test_date_smoking_2', 'test_res_smoking_2',
'test_date_smoking_3', 'test_res_smoking_3', 'test_date_gma_1',
'test_res_gma_1', 'test_date_gma_2', 'test_res_gma_2',
'test_date_gma_3', 'test_res_gma_3', 'Vacuna_1', 'Vacuna_2', 'Vacuna_3',
'Vacuna_assign_1', 'Vacuna_assign_2', 'Vacuna_assign_3']
for i in range(0,len(stubnames)):
stubnames[i] = stubnames[i][0:-1]
data_piv = pd.wide_to_long(data, list(set(stubnames)), i='NIA', j='time')
data_piv.rename({'Vacuna_':'Vacuna'}, axis=1, inplace=True)
data_piv.reset_index(inplace=True, drop=False)
data_piv.time = data_piv.time - 1
del data
gc.collect()
# G-formula params
time_name = 'time'
id = 'NIA'
time_points = np.max(np.unique(data_piv[time_name])) + 1
covnames = [
'test_res_sp_',
'test_res_smoking_',
'test_res_chol_',
'test_res_abdo_',
'test_res_dp_',
'test_res_imc_',
'test_res_bg_',
'test_res_covid_',
'test_res_gma_',
'Vacuna']
if missing_assumption == 'mar':
if modelling == 'ML':
covtypes = [
'unknown-continuous',
'unknown-categorical',
'unknown-continuous',
'unknown-continuous',
'unknown-continuous',
'unknown-continuous',
'unknown-continuous',
'unknown-binary',
'unknown-continuous',
'unknown-binary']
ymodel_type = 'ML'
elif modelling == 'GLM':
covtypes = [
'normal',
'categorical',
'normal',
'normal',
'normal',
'normal',
'normal',
'binary',
'normal',
'binary']
ymodel_type = 'GLM'
else:
raise('Wrong modelling option, choose between "ML" or "GLM"')
covmodels = [
'test_res_sp_ ~ lag1_test_res_sp_ + lag1_test_res_imc_ + data_naixement + C(pais_c) + C(lag1_test_res_smoking_) + C(test_res_sociostat_1) + lag1_Vacuna + time',
'test_res_smoking_ ~ C(lag1_test_res_smoking_) + data_naixement + C(pais_c) + C(test_res_sociostat_1) + lag1_Vacuna + time',
'test_res_chol_ ~ lag1_test_res_chol_ + data_naixement + C(pais_c) + C(test_res_sociostat_1) + lag1_Vacuna + time',
'test_res_abdo_ ~ lag1_test_res_abdo_ + lag1_test_res_imc_ + data_naixement + C(pais_c) + C(lag1_test_res_smoking_) + lag1_Vacuna + time',
'test_res_dp_ ~ lag1_test_res_dp_ + lag1_test_res_imc_ + data_naixement + C(pais_c) + C(lag1_test_res_smoking_) + C(test_res_sociostat_1) + lag1_Vacuna + time',
'test_res_imc_ ~ lag1_test_res_imc_ + data_naixement + C(pais_c) + C(lag1_test_res_smoking_) + C(test_res_sociostat_1) + lag1_Vacuna + time',
'test_res_bg_ ~ lag1_test_res_bg_ + lag1_test_res_imc_ + data_naixement + C(pais_c) + C(test_res_sociostat_1) + lag1_Vacuna + time',
'test_res_covid_ ~ lag1_test_res_covid_ + lag1_test_res_imc_ + data_naixement + C(pais_c) + lag1_test_res_bg_ + C(lag1_test_res_smoking_) + C(test_res_sociostat_1) + lag1_Vacuna + time',
'test_res_gma_ ~ lag1_test_res_gma_ + lag1_test_res_imc_ + data_naixement + lag1_test_res_dp_ + lag1_test_res_sp_ + lag1_test_res_chol_ + C(pais_c) + lag1_test_res_bg_ + C(lag1_test_res_smoking_) + lag1_Vacuna + time',
'Vacuna ~ lag1_Vacuna + test_res_sp_ + C(test_res_smoking_) + test_res_chol_ + test_res_abdo_ + test_res_dp_ + test_res_imc_ + test_res_bg_ + test_res_covid_ + test_res_gma_ \
+ C(abs_c) + C(pais_c) + sexe + data_naixement + C(test_res_sociostat_1) + time']
ymodel = 'DM ~ C(abs_c) + C(pais_c) + sexe + data_naixement + C(test_res_sociostat_1)+\
test_res_sp_ +\
C(test_res_smoking_) +\
test_res_chol_ +\
test_res_abdo_ +\
test_res_dp_ +\
test_res_imc_ +\
test_res_bg_ +\
test_res_covid_ +\
test_res_gma_ +\
Vacuna + lag1_Vacuna + lag2_Vacuna + time'
elif missing_assumption == 'struct_miss':
if modelling == 'ML':
covtypes = [
'unknown-continuous',
'unknown-categorical',
'unknown-continuous',
'unknown-categorical',
'unknown-continuous',
'unknown-continuous',
'unknown-continuous',
'unknown-binary',
'unknown-continuous',
'unknown-binary']
ymodel_type = 'ML'
elif modelling == 'GLM':
covtypes = [
'normal',
'categorical',
'normal',
'categorical',
'normal',
'normal',
'normal',
'binary',
'normal',
'binary']
ymodel_type = 'GLM'
else:
raise('Wrong modelling option, choose between "ML" or "GLM"')
covmodels = [
'test_res_sp_ ~ lag1_test_res_sp_ + lag1_test_res_imc_ + data_naixement + C(pais_c) + C(lag1_test_res_smoking_) + C(test_res_sociostat_1) + lag1_Vacuna + time',
'test_res_smoking_ ~ C(lag1_test_res_smoking_) + data_naixement + C(pais_c) + C(test_res_sociostat_1) + lag1_Vacuna + time',
'test_res_chol_ ~ lag1_test_res_chol_ + data_naixement + C(pais_c) + C(test_res_sociostat_1) + lag1_Vacuna + time',
'test_res_abdo_ ~ C(lag1_test_res_abdo_) + lag1_test_res_imc_ + data_naixement + C(pais_c) + C(lag1_test_res_smoking_) + lag1_Vacuna + time',
'test_res_dp_ ~ lag1_test_res_dp_ + lag1_test_res_imc_ + data_naixement + C(pais_c) + C(lag1_test_res_smoking_) + C(test_res_sociostat_1) + lag1_Vacuna + time',
'test_res_imc_ ~ lag1_test_res_imc_ + data_naixement + C(pais_c) + C(lag1_test_res_smoking_) + C(test_res_sociostat_1) + lag1_Vacuna + time',
'test_res_bg_ ~ lag1_test_res_bg_ + lag1_test_res_imc_ + data_naixement + C(pais_c) + C(test_res_sociostat_1) + lag1_Vacuna + time',
'test_res_covid_ ~ lag1_test_res_covid_ + lag1_test_res_imc_ + data_naixement + C(pais_c) + lag1_test_res_bg_ + C(lag1_test_res_smoking_) + C(test_res_sociostat_1) + lag1_Vacuna + time',
'test_res_gma_ ~ lag1_test_res_gma_ + lag1_test_res_imc_ + data_naixement + lag1_test_res_dp_ + lag1_test_res_sp_ + lag1_test_res_chol_ + C(pais_c) + lag1_test_res_bg_ + C(lag1_test_res_smoking_) + lag1_Vacuna + time',
'Vacuna ~ lag1_Vacuna + test_res_sp_ + C(test_res_smoking_) + test_res_chol_ + C(test_res_abdo_) + test_res_dp_ + test_res_imc_ + test_res_bg_ + test_res_covid_ + test_res_gma_ \
+ C(abs_c) + C(pais_c) + sexe + data_naixement + C(test_res_sociostat_1) + time']
ymodel = 'DM ~ C(abs_c) + C(pais_c) + sexe + data_naixement + C(test_res_sociostat_1)+\
test_res_sp_ +\
C(test_res_smoking_) +\
test_res_chol_ +\
C(test_res_abdo_) +\
test_res_dp_ +\
test_res_imc_ +\
test_res_bg_ +\
test_res_covid_ +\
test_res_gma_ +\
Vacuna + lag1_Vacuna + lag2_Vacuna + time'
else:
raise('Wrong missing assumption option, choose between "mar" or "struct_miss"')
trunc_params = ['NA', 'NA', 'NA', 'NA', 'NA', 'NA','NA', 'NA', 'NA', 'NA']
basecovs = ['abs_c', 'pais_c', 'sexe', 'data_naixement', 'test_res_sociostat_1']
outcome_name = 'DM'
outcome_type = 'binary_eof'
if outcome_type=='binary_eof':
# Transform outcome for binary eof
data_piv.loc[(data_piv.time==0)|(data_piv.time==1), 'DM'] = np.NaN
# Interventions
int_descript = ['Never treat', 'Treat on Vacuna only at t1', 'Treat on Vacuna only at t1 & t2', 'Treat on Vacuna at t1, t2 & t3']
Intervention1_Vacuna = [static, (0, 0, 0), [0, 1, 2]]
Intervention2_Vacuna = [static, (1, 0, 0), [0, 1, 2]]
Intervention3_Vacuna = [static, (1, 1, 0), [0, 1, 2]]
Intervention4_Vacuna = [static, (1, 1, 1), [0, 1, 2]]
# Normalize data
if normalize_data:
data_piv[[covnames[covtypes=='normal']] + ['data_naixement', 'test_res_sociostat_1']] = StandardScaler().fit_transform(data_piv[[covnames[covtypes=='normal']] + ['data_naixement', 'test_res_sociostat_1']])
# G-formula package call
g = ParametricGformula(obs_data=data_piv, id=id, time_name=time_name, time_points=time_points,
covnames=covnames, covtypes=covtypes, covmodels=covmodels, basecovs=basecovs,
outcome_name=outcome_name, outcome_type=outcome_type, ymodel=ymodel, ymodel_type=ymodel_type,
int_descript=int_descript,
Intervention1_Vacuna = Intervention1_Vacuna,
Intervention2_Vacuna = Intervention2_Vacuna,
Intervention3_Vacuna = Intervention3_Vacuna,
Intervention4_Vacuna = Intervention4_Vacuna,
trunc_params=trunc_params, nsamples=0, parallel=parallel, parallel_bootstrap=False, ncores=32, n_simul=n_simul, save_results=save_results,
save_path=f'Results/{data_fraction}_data/{analysis_method}/{missing_assumption}/out_modeltype_{modelling}/presplit_chunks_100')
g.fit()
#joblib.dump(g, f'pygformula_object_{c}.pickle')
# Remove temporary files etc.
for folder in ['/home/bvelasco/CovidVax_DM/temp/', '/home/bvelasco/.local/share/Trash/files']:
for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))
del data_piv
gc.collect()
# Print time
print(datetime.datetime.now() - startTime)