-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathModelSelection-Ablation-ManyCountries.py
145 lines (108 loc) · 3.59 KB
/
ModelSelection-Ablation-ManyCountries.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
"""
Creating Table C3: columns for CGP (Local)
"""
# !/usr/bin/env python
# coding: utf-8
# Creating Table C3: columns for CGP (Local)
import pickle
import numpy as np
import pandas as pds
import torch
from pyro.ops.stats import quantile
import data_loader
import pyro_model.helper
# ## loading data
countries_list = [
'United Kingdom',
'Italy',
'Germany',
'Spain',
'France',
'Korea, South',
'Brazil',
'Iran',
'Netherlands',
'Sweden',
'Mexico',
'India',
'Russia',
'Japan',
'South Africa',
'Egypt',
'Norway'
]
# prefix = ''
prefix = 'trained_models/'
# for country in countries_list:
country = countries_list[0]
countries = [country]
pad = 24
data_dict = data_loader.get_data_pyro(countries, smart_start=False, pad=pad)
data_dict = pyro_model.helper.smooth_daily(data_dict)
days = 14
train_len = data_dict['cum_death'].shape[0] - days
test_dates = data_dict['date_list'][train_len:]
print('Done')
err14_list = []
err7_list = []
for country in countries_list:
print(country)
countries = [country]
pad = 24
data_dict = data_loader.get_data_pyro(countries, smart_start=False, pad=pad)
data_dict = pyro_model.helper.smooth_daily(data_dict)
days = 14
train_len = data_dict['cum_death'].shape[0] - days
test_dates = data_dict['date_list'][train_len:]
predictive_list = []
samples_list = []
for seed in range(10):
model_id = country + '-ablation-day-{}-rng-{}'.format(days, seed)
try:
with open(prefix + 'AblationLoop{}/{}-predictive.pkl'.format(days, model_id), 'rb') as f:
predictive = pickle.load(f)
except Exception:
continue
predictive_list.append(predictive)
with open(prefix + 'AblationLoop{}/{}-samples.pkl'.format(days, model_id), 'rb') as f:
samples = pickle.load(f)
samples_list.append(samples)
val_window = 14
seir_error_list = []
for i in range(len(predictive_list)):
seir_train = quantile(predictive_list[i]['prediction'].squeeze(), 0.5, dim=0)[-val_window + 1:].numpy()
seir_train = np.diff(seir_train, axis=0)
seir_label = data_dict['daily_death'][train_len - val_window:train_len, :].numpy()
seir_error = np.abs(np.sum(seir_train, axis=0) - np.sum(seir_label, axis=0))
seir_error_list.append(seir_error)
seir_error = np.stack(seir_error_list, axis=0)
best_model = np.argmin(seir_error, axis=0)
test_len = 14
best_error_list = []
test_len = test_len - 1
for j, i in zip(range(len(countries)), best_model):
c = countries[j]
samples = samples_list[i]
p50 = quantile(torch.tensor(samples), 0.5, dim=0)[1:, :]
pred = p50[test_len, :]
truth = data_dict['actual_cum_death'][test_len - days]
err = (pred[j] - truth[j]).item()
best_error_list.append(err)
err_14 = best_error_list[0]
test_len = 7
best_error_list = []
test_len = test_len - 1
for j, i in zip(range(len(countries)), best_model):
c = countries[j]
samples = samples_list[i]
p50 = quantile(torch.tensor(samples), 0.5, dim=0)[1:, :]
pred = p50[test_len, :]
truth = data_dict['actual_cum_death'][test_len - days]
err = (pred[j] - truth[j]).item()
best_error_list.append(err)
err_7 = best_error_list[0]
err14_list.append(err_14)
err7_list.append(err_7)
print(country, err_14, err_7)
abl_df = pds.DataFrame({'country': countries_list, 'err7': err7_list, 'err14': err14_list})
abl_df.to_csv('tables/Table-C3-ablation-many-countries-d7-d14.csv')