-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_select.py
263 lines (209 loc) · 11.4 KB
/
model_select.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
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
import numpy as np
import matplotlib.pyplot as plt
import SEIR
import argparse
class model_selection():
def __init__(self, smooth=False):
# ========================================== #
# Hyper-Parameters values to test
# ========================================== #
self.w_1 = [1, 2]
self.w_2 = [1, 2]
self.w_3 = [1, 2]
self.w_4 = [1, 2]
self.w_5 = [1, 2]
# Binomial smoother
self.binom_smoother = [2, 3, 4, 5]
# Optimizer to choose:
self.optimizer = 'LBFGSB'
# Step size (if LBFGSB)
self.step_size = [0.1, 0.05, 0.01]
self.model = SEIR.SEIR()
self.smoothing = smooth
if self.smoothing:
self.model.smoothing = True
else:
self.model.smoothing = False
self.model.import_dataset()
self.model.fit_type = 'type_1'
def perform(self, select_part=1, optimizer='LBFGSB'):
iter = 0
total_iter = len(self.w_1) ** 4 * len(self.binom_smoother)
if optimizer=='LBFGSB':
total_iter *= len(self.step_size)
self.model.optimizer = optimizer
if select_part == 1:
self.model.w_1 = self.w_1[0]
else:
self.model.w_1 = self.w_1[1]
for w_2 in self.w_2:
self.model.w_2 = w_2
for w_3 in self.w_3:
self.model.w_3 = w_3
for w_4 in self.w_4:
self.model.w_4 = w_4
for w_5 in self.w_5:
self.model.w_5 = w_5
for binom_smoother in self.binom_smoother:
self.model.binom_smoother = binom_smoother
if self.model.optimizer == 'LBFGSB':
for step_size in self.step_size:
self.model.opti_step = step_size
iter += 1
print('iter {} / {}'.format(iter, total_iter))
# Reinit the model
self.model.beta = 0.3 # Contamination rate
self.model.sigma = 0.8 # Incubation rate
self.model.gamma = 0.15 # Recovery rate
self.model.hp = 0.05 # Hospit rate
self.model.hcr = 0.2 # Hospit recovery rate
self.model.pc = 0.1 # Critical rate
self.model.pd = 0.1 # Critical recovery rate
self.model.pcr = 0.3 # Critical mortality
self.model.s = 0.765 # Sensitivity
self.model.t = 0.75 # Testing rate in symptomatical
# Fit the model:
self.model.fit()
# Get SEIR parameters value:
param_seir = self.model.get_parameters()
# Get model's Hyper parameters
h_param = self.model.get_hyper_parameters()
# Get score:
raw = self.model.score(output='raw')
mean_test = str(np.mean(raw[:, 0]))
sum_test = str(np.sum(raw[:, 0]))
std_test = str(np.std(raw[:, 0]))
mean_hospit = str(np.mean(raw[:, 1]))
sum_hospit = str(np.sum(raw[:, 1]))
std_hospit = str(np.std(raw[:, 1]))
mean_critical = str(np.mean(raw[:, 2]))
sum_critical = str(np.sum(raw[:, 2]))
std_critical = str(np.std(raw[:, 2]))
mean_fata = str(np.mean(raw[:, 3]))
sum_fata = str(np.sum(raw[:, 3]))
std_fata = str(np.std(raw[:, 3]))
mean_tot = str(np.mean(raw))
sum_tot = str(np.sum(raw))
std_tot = str(np.std(raw))
# Write in file:
# Make a list of informations:
str_lst = []
str_lst.append(sum_tot)
for item in param_seir:
str_lst.append(item)
for item in h_param:
str_lst.append(item)
str_lst.append(mean_tot)
str_lst.append(sum_tot)
str_lst.append(std_tot)
str_lst.append(mean_test)
str_lst.append(sum_test)
str_lst.append(std_test)
str_lst.append(mean_hospit)
str_lst.append(sum_hospit)
str_lst.append(std_hospit)
str_lst.append(mean_critical)
str_lst.append(sum_critical)
str_lst.append(std_critical)
str_lst.append(mean_fata)
str_lst.append(sum_fata)
str_lst.append(std_fata)
convert_str_lst = []
for i in range(0, len(str_lst)):
convert_str_lst.append(str(str_lst[i]))
print(convert_str_lst)
final_str = ';'.join(convert_str_lst)
file = open(
'mod_select_result_part{}-{}-{}.csv'.format(select_part, self.smoothing, self.model.optimizer), "a")
file.write(final_str)
file.write('\n')
file.close()
else:
iter += 1
print('iter {} / {}'.format(iter, total_iter))
# Reinit the model
self.model.beta = 0.3 # Contamination rate
self.model.sigma = 0.8 # Incubation rate
self.model.gamma = 0.15 # Recovery rate
self.model.hp = 0.05 # Hospit rate
self.model.hcr = 0.2 # Hospit recovery rate
self.model.pc = 0.1 # Critical rate
self.model.pd = 0.1 # Critical recovery rate
self.model.pcr = 0.3 # Critical mortality
self.model.s = 0.765 # Sensitivity
self.model.t = 0.75 # Testing rate in symptomatical
# Fit the model:
self.model.fit()
# Get SEIR parameters value:
param_seir = self.model.get_parameters()
# Get model's Hyper parameters
h_param = self.model.get_hyper_parameters()
# Get score:
raw = self.model.score(output='raw')
mean_test = str(np.mean(raw[:, 0]))
sum_test = str(np.sum(raw[:, 0]))
std_test = str(np.std(raw[:, 0]))
mean_hospit = str(np.mean(raw[:, 1]))
sum_hospit = str(np.sum(raw[:, 1]))
std_hospit = str(np.std(raw[:, 1]))
mean_critical = str(np.mean(raw[:, 2]))
sum_critical = str(np.sum(raw[:, 2]))
std_critical = str(np.std(raw[:, 2]))
mean_fata = str(np.mean(raw[:, 3]))
sum_fata = str(np.sum(raw[:, 3]))
std_fata = str(np.std(raw[:, 3]))
mean_tot = str(np.mean(raw))
sum_tot = str(np.sum(raw))
std_tot = str(np.std(raw))
# Write in file:
# Make a list of informations:
str_lst = []
str_lst.append(sum_tot)
for item in param_seir:
str_lst.append(item)
for item in h_param:
str_lst.append(item)
str_lst.append(mean_tot)
str_lst.append(sum_tot)
str_lst.append(std_tot)
str_lst.append(mean_test)
str_lst.append(sum_test)
str_lst.append(std_test)
str_lst.append(mean_hospit)
str_lst.append(sum_hospit)
str_lst.append(std_hospit)
str_lst.append(mean_critical)
str_lst.append(sum_critical)
str_lst.append(std_critical)
str_lst.append(mean_fata)
str_lst.append(sum_fata)
str_lst.append(std_fata)
convert_str_lst = []
for i in range(0, len(str_lst)):
convert_str_lst.append(str(str_lst[i]))
print(convert_str_lst)
final_str = ';'.join(convert_str_lst)
file = open(
'mod_select_result_part{}-{}-{}.csv'.format(select_part, self.smoothing, self.model.optimizer), "a")
file.write(final_str)
file.write('\n')
file.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='blabla')
# smooth: 0 = non, 1 = oui
parser.add_argument('--smooth', default=0)
# Parties 1 à 4
parser.add_argument('--part', default=1)
# otptimizer
parser.add_argument('--opti', default='LBFGSB')
args = parser.parse_args()
if args.smooth=='1':
selector = model_selection(smooth=True)
else:
selector = model_selection(smooth=False)
selected = 0
if args.part == '1':
selected = 1
if args.part == '2':
selected = 2
selector.perform(select_part=selected, optimizer=args.opti)