-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-states.py
188 lines (163 loc) · 6.21 KB
/
run-states.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
import datetime
import json
import numpy as np
from src.api import get_states_daily, fill_data
from src.loss_evaluation import mean_absolute_error, weighted_mae_loss
from src.pipeline import Predictor
# STATES = ['AK']
from src.save_parameters import save_to_json
NUMBER_OF_DAYS_TRAINING = 35
NUMBER_OF_DAYS_PREDICTING = 25
BEGIN_DATE_TRAINING = '2020-06-20'
BEGIN_DATE_PREDICTING = '2020-07-25'
# STATES = ['AS']
STATES = ['AK', 'AL', 'AR', 'AS', 'AZ', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'GA', 'GU', 'HI', 'IA', 'ID', 'IL', 'IN',
'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MI', 'MN', 'MO', 'MP', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM',
'NV', 'NY', 'OH', 'OK', 'OR', 'PA', 'PR', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VI', 'VT', 'WA', 'WI',
'WV', 'WY']
STATE_POPULATIONS = {
'AK': 734002,
'AL': 4908620,
'AR': 3039000,
'AS': 55212,
'AZ': 7378490,
'CA': 39937500,
'CO': 5845530,
'CT': 3563080,
'DC': 720687,
'DE': 982895,
'FL': 21993000,
'GA': 10736100,
'GU': 168485,
'HI': 1412690,
'IA': 3179850,
'ID': 1826160,
'IL': 12659700,
'IN': 6745350,
'KS': 2910360,
'KY': 4499690,
'LA': 4645180,
'MA': 6976600,
'MD': 6083120,
'ME': 1345790,
'MI': 10045000,
'MN': 5700670,
'MO': 6169270,
'MP': 57581,
'MS': 2989260,
'MT': 1086760,
'NC': 10611900,
'ND': 761723,
'NE': 1952570,
'NH': 1371250,
'NJ': 8936570,
'NM': 2096640,
'NV': 3139660,
'NY': 19440500,
'OH': 11747700,
'OK': 3954820,
'OR': 4301090,
'PA': 12820900,
'PR': 3032160,
'RI': 1056160,
'SC': 5210100,
'SD': 903027,
'TN': 6897580,
'TX': 29472300,
'UT': 3282120,
'VA': 8626210,
'VI': 104425,
'VT': 628061,
'WA': 7797100,
'WI': 5851750,
'WV': 1778070,
'WY': 567025
}
param_ranges = {
'beta': (0.0001, 2), # Rate of transmission
'sigma': (1 / 14, 1), # Rate of progression
'gamma': (1 / 10, 1), # Rate of recoveryrecovery
'mu_I': (0.0001, 1 / 10), # Rate of DEATH
'xi': (0.0001, 0.0001) # Rate of re-susceptibility
}
genetic_params = {
'max_gen': 30,
'stop_cond': 10000,
'mut_range': 0.1,
'p_regen': 0.2,
'p_mut': 0.4
}
us_results_training = {
'S': np.zeros(NUMBER_OF_DAYS_TRAINING, dtype=np.float),
'E': np.zeros(NUMBER_OF_DAYS_TRAINING, dtype=np.float),
'I': np.zeros(NUMBER_OF_DAYS_TRAINING, dtype=np.float),
'R': np.zeros(NUMBER_OF_DAYS_TRAINING, dtype=np.float),
'F': np.zeros(NUMBER_OF_DAYS_TRAINING, dtype=np.float)
}
us_results_predicting = {
'S': np.zeros(NUMBER_OF_DAYS_PREDICTING, dtype=np.float),
'E': np.zeros(NUMBER_OF_DAYS_PREDICTING, dtype=np.float),
'I': np.zeros(NUMBER_OF_DAYS_PREDICTING, dtype=np.float),
'R': np.zeros(NUMBER_OF_DAYS_PREDICTING, dtype=np.float),
'F': np.zeros(NUMBER_OF_DAYS_PREDICTING, dtype=np.float)
}
data = get_states_daily()
data = fill_data(data, '2020-07-25')
for state in STATES:
print("Predicting for {}...".format(state))
predictor = Predictor(loss_days=NUMBER_OF_DAYS_TRAINING, init_date=BEGIN_DATE_TRAINING, state=state,
param_ranges=param_ranges,
genetic_params=genetic_params, states_data=data, state_population=STATE_POPULATIONS[state])
iterations = predictor.run(verbose=0)
training_seir = predictor.generate_data_for_plots(BEGIN_DATE_TRAINING, NUMBER_OF_DAYS_TRAINING)
prediction_seir = predictor.generate_data_for_plots(BEGIN_DATE_PREDICTING, NUMBER_OF_DAYS_PREDICTING)
for n in training_seir:
us_results_training[n] += np.array(training_seir[n])
us_results_predicting[n] += np.array(prediction_seir[n])
report_data = predictor.report(BEGIN_DATE_PREDICTING, NUMBER_OF_DAYS_TRAINING)
save_to_json(training_seir, path='results/states/', file_name='training_seir_{}_{}'.format(state, predictor.best))
save_to_json(prediction_seir, path='results/states/',
file_name='prediction_seir_{}_{}'.format(state, predictor.best))
save_to_json(iterations, path='results/states/', file_name='iterations_{}_{}'.format(state, predictor.best))
save_to_json(report_data, path='results/states/', file_name='report_{}_{}'.format(state, predictor.best))
# predictor.report()
print("Done!..")
us_predictor = Predictor(loss_days=NUMBER_OF_DAYS_TRAINING, init_date=BEGIN_DATE_TRAINING, param_ranges=param_ranges,
genetic_params=genetic_params)
real_data = us_predictor.US_daily
start = datetime.datetime.strptime(us_predictor.from_this_day_to_predict, '%Y-%m-%d')
start = start + datetime.timedelta(days=1)
time_delta = datetime.timedelta(days=us_predictor.loss_days - 1)
end = start + time_delta
real_positives = []
real_recovered = []
step = datetime.timedelta(days=1)
while start <= end:
day = start.strftime('%Y-%m-%d')
real_positives.append(int(us_predictor.US_daily[day]['positive'].values[0])) # date()
real_recovered.append(int(us_predictor.US_daily[day]['recovered'].values[0]))
start += step
print("Predicted infected: {}\nReal infected: {}\n\nPredicted recovered: {}\nTrue recovered: {}".format(
us_results_training['I'],
real_positives,
us_results_training['R'],
real_recovered))
print("MAE for merged data: {}\nWeighted MAE for merged data: {}".format(
mean_absolute_error(real_positives, us_results_training['I']),
weighted_mae_loss(us_results_training['I'], real_positives)))
with open("results/states/training_states.json", "w+") as json_file:
json.dump({"S": list(us_results_training["S"]),
"E": list(us_results_training["E"]),
"I": list(us_results_training["I"]),
"R": list(us_results_training["R"]),
"F": list(us_results_training["F"]),
"real_I": list(real_positives),
"real_R": list(real_recovered)},
json_file)
with open("results/states/predicting_states.json", "w+") as json_file:
json.dump({"S": list(us_results_predicting["S"]),
"E": list(us_results_predicting["E"]),
"I": list(us_results_predicting["I"]),
"R": list(us_results_predicting["R"]),
"F": list(us_results_predicting["F"])},
json_file)