-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVisualization_Severe.py
315 lines (216 loc) · 10.9 KB
/
Visualization_Severe.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/env python
# coding: utf-8
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pulp
import glob
from matplotlib import rcParams
rcParams['font.family'] = 'sans-serif'
rcParams['font.sans-serif'] = ['Hiragino Maru Gothic Pro', 'Yu Gothic', 'Meirio', 'Takao', 'IPAexGothic', 'IPAPGothic', 'VL PGothic', 'Noto Sans CJK JP']
# MODE = 'all'
MODE = 'normal'
filenames = glob.glob('data_severe/x_*')
filenames.sort()
forecast_dates = [filename.split('_')[-1].split('.')[0] for filename in filenames]
def visualization(gamma,x_type,forecast_date):
print("forcasted date ={0}".format(f_date))
# 重みの入力
df_w = pd.read_csv('data_Kokudo/w_distance.csv',index_col=0)
W= df_w.values
w_pulp = W.T.reshape(-1)
# x, x_q0025も計算
df_x0975 = pd.read_csv('data_severe/x0975_{0}.csv'.format(forecast_date),index_col=0 )
df_x0025 = pd.read_csv('data_severe/x0025_{0}.csv'.format(forecast_date),index_col=0 )
df_xmean = pd.read_csv('data_severe/x_{0}.csv'.format(forecast_date),index_col=0 )
gammas = np.load('data_transport/gammas_{0}_{1:03}_{2}.npy'.format(x_type,int(gamma*100),forecast_date))
x_mean = df_xmean.values
x_q0975 = df_x0975.values
x_q0025 = df_x0025.values
N = x_mean.shape[1]
T = x_mean.shape[0]
L = np.kron(np.ones((1,N)),np.eye(N)) - np.kron(np.eye(N),np.ones((1,N)))
uv = np.load('data_transport/u_{0}_{1:03}_{2}.npy'.format(x_type,int(gamma*100),forecast_date))
y_mean = np.zeros(x_mean.shape)
y_q0975 = np.zeros(x_mean.shape)
y_q0025 = np.zeros(x_mean.shape)
y_mean[0] = x_mean[0]
y_q0975[0] = x_q0975[0]
y_q0025[0] = x_q0025[0]
sum_u = np.zeros(T)
sum_cost = np.zeros(T)
for k in range(T-1):
y_mean[k+1] = y_mean[k] + x_mean[k+1] - x_mean[k] + L.dot(uv[k])
y_q0975[k+1] = y_q0975[k] + x_q0975[k+1] - x_q0975[k] + L.dot(uv[k])
y_q0025[k+1] = y_q0025[k] + x_q0025[k+1] - x_q0025[k] + L.dot(uv[k])
sum_u[k+1] = np.sum(uv[k])
sum_cost[k+1] = np.sum(w_pulp*uv[k])
# ベット数の入力
df_severe_beds = pd.read_csv('data_Koro/severe_beds.csv',index_col=0)
dirnames = (df_severe_beds['japan_prefecture_code']+df_severe_beds['都道府県名']).values
names = df_severe_beds['都道府県名'].values
weeks = df_severe_beds.columns[2:].values
new_week = max(weeks)
M = df_severe_beds[new_week].values
times = pd.to_datetime(df_xmean.index)
date_s = min(times)
date_e = max(times)
# ## 全県の重症者数の表示
plt.figure(figsize = (6,4))
plt.fill_between(times,x_q0025.sum(axis=1),x_q0975.sum(axis=1),facecolor = 'lime',alpha = 0.3,label = '95%信頼区間')
plt.plot(times,x_mean.sum(axis=1),'*-',color = 'lime',label = '平均値')
plt.plot([date_s,date_e],np.ones(2)*0.8*M.sum(),"--",label = '重症病床使用率 80%',color = 'red',linewidth = 2.0)
plt.plot([date_s,date_e],np.ones(2)*M.sum(),"--",label = '重症病床使用率 100%',color = 'purple',linewidth = 2.0)
plt.gca().tick_params(axis='x', rotation= -60)
plt.title('全国の重症者数の予測値, 予測日={0}'.format(forecast_date),fontsize = 15)
plt.xlim([date_s,date_e])
plt.ylim([0, 1.5* M.sum(),])
plt.ylabel('重症者数 [人]')
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0)
plt.grid()
plt.savefig('resultB_google_prediction/all_severe_{0}.png'.format(forecast_date),bbox_inches='tight',dpi = 100)
if MODE == 'normal':
plt.savefig('resultB_google_prediction/all_severe.png',bbox_inches='tight',dpi = 100)
plt.close()
# 県ごとの感染者数の予測結果
plt.figure(figsize = (50,25))
plt.subplots_adjust(wspace=0.1, hspace=0.5)
for i in range(47):
plt.subplot(10,5,i+1)
plt.fill_between(times,x_q0025[:,i],x_q0975[:,i],facecolor = 'lime',alpha = 0.3,label = '95%信頼区間')
plt.plot(times,x_mean[:,i],'*-',color = 'lime',label = '平均値')
plt.plot([date_s,date_e],np.ones(2)*0.8*M[i],"--",label = '重症病床使用率 80%',color = 'red',linewidth = 2.0)
plt.plot([date_s,date_e],np.ones(2)*M[i],"--",label = '重症病床使用率 100%',color = 'purple',linewidth = 2.0)
plt.gca().tick_params(axis='x', rotation= -60)
plt.title(names[i],fontsize = 20)
plt.xlim([date_s,date_e])
plt.ylim([0, 5.0* M[i]])
plt.grid()
if i < 42:
plt.tick_params(labelbottom=False)
if i == 0:
plt.legend()
plt.savefig('resultB_google_prediction/each_severe_{0}.png'.format(forecast_date),bbox_inches='tight',dpi = 100)
if MODE == 'normal':
plt.savefig('resultB_google_prediction/each_severe.png',bbox_inches='tight',dpi = 100)
plt.close()
# 県ごとの感染者数の予測結果
plt.figure(figsize = (50,25))
plt.subplots_adjust(wspace=0.1, hspace=0.5)
for i in range(47):
plt.subplot(10,5,i+1)
max_beds = M[i]
# ベットの限界
plt.plot([date_s,date_e],[0.8*max_beds,0.8*max_beds],'--',label = '重症者病床使用率80%',color = 'red',linewidth = 2.0)
plt.plot([date_s,date_e],[max_beds,max_beds],'--',label = '重症者病床使用率100%',color = 'purple',linewidth = 2.0)
# 輸送なし
plt.fill_between(times,x_q0025[:,i],x_q0975[:,i],facecolor = 'lime',alpha = 0.5,label = '医療シェアリングなし',)
plt.plot(times,x_mean[:,i],"*-",linewidth = 2,color= 'lime')
# 輸送あり
plt.fill_between(times,y_q0025[:,i],y_q0975[:,i],facecolor = 'orange',alpha = 0.5,label = '医療シェアリングあり',)
plt.plot(times,y_mean[:,i],"*-",linewidth = 2,color = 'orange')
plt.xlim([date_s,date_e])
plt.ylim([0,5.0*max_beds])
plt.grid()
plt.gca().tick_params(axis='x', rotation= -60)
plt.title(names[i],fontsize = 20)
if i < 42:
plt.tick_params(labelbottom=False)
if i == 0:
plt.legend()
if MODE == 'normal':
plt.savefig('resultC_transport_strategy/main/each_severe_{0}_{1:03}.png'.format(x_type,int(gamma*100)),bbox_inches='tight',dpi = 100)
plt.savefig('resultC_transport_strategy/main/each_severe_{0}_{1:03}_{2}.png'.format(x_type,int(gamma*100),forecast_date),bbox_inches='tight',dpi = 100)
plt.close()
# コスト評価
times = pd.to_datetime(df_xmean.index)[:-1]
date_s = min(times)
date_e = max(times)
max_beds = M.sum()
# 輸送人数
plt.plot(times,sum_u[:-1],"*-",linewidth = 2,color= 'black',label = '重症者受け入れ依頼数')
plt.xlim([date_s,date_e])
plt.gca().tick_params(axis='x', rotation= -60)
# plt.title('',fontsize = 20)
plt.ylabel('毎日の重症者の受け入れ依頼数 [人]')
plt.legend()
if MODE == 'normal':
plt.savefig('resultC_transport_strategy/cost/num_{0}_{1:03}.png'.format(x_type,int(gamma*100)),bbox_inches='tight',dpi = 100)
plt.savefig('resultC_transport_strategy/cost/num_{0}_{1:03}_{2}.png'.format(x_type,int(gamma*100),forecast_date),bbox_inches='tight',dpi = 100)
plt.close()
times = pd.to_datetime(df_xmean.index)[:-1]
date_s = min(times)
date_e = max(times)
max_beds = M.sum()
# 輸送コスト
plt.plot(times,sum_cost[:-1],"*-",linewidth = 2,color= 'black',label = '医療シェアリングの依頼コスト')
plt.xlim([date_s,date_e])
plt.gca().tick_params(axis='x', rotation= -60)
plt.legend()
plt.ylabel('毎日の依頼コスト [km]')
if MODE == 'normal':
plt.savefig('resultC_transport_strategy/cost/cost_{0}_{1:03}.png'.format(x_type,int(gamma*100)),bbox_inches='tight',dpi = 100)
plt.savefig('resultC_transport_strategy/cost/cost_{0}_{1:03}_{2}.png'.format(x_type,int(gamma*100),forecast_date),bbox_inches='tight',dpi = 100)
plt.close()
times = pd.to_datetime(df_xmean.index)[:-1]
date_s = min(times)
date_e = max(times)
max_beds = M.sum()
# 輸送コスト
plt.plot(times,sum_cost[:-1]/sum_u[:-1],"*-",linewidth = 2,color= 'black',label = '重症者患者ごとの依頼コスト')
plt.xlim([date_s,date_e])
plt.gca().tick_params(axis='x', rotation= -60)
plt.legend()
plt.ylabel('重症者患者ごとのコスト [km/人]')
if MODE == 'normal':
plt.savefig('resultC_transport_strategy/cost/performance_{0}_{1:03}.png'.format(x_type,int(gamma*100)),bbox_inches='tight',dpi = 100)
plt.savefig('resultC_transport_strategy/cost/performance_{0}_{1:03}_{2}.png'.format(x_type,int(gamma*100),forecast_date),bbox_inches='tight',dpi = 100)
plt.close()
times = pd.to_datetime(df_xmean.index)
plt.plot(times,gammas*100)
plt.gca().tick_params(axis='x', rotation= -60)
plt.ylabel('重症病床利用率の上限 [%]')
if MODE == 'normal':
plt.savefig('resultC_transport_strategy/cost/gammas_{0}_{1:03}.png'.format(x_type,int(gamma*100)),bbox_inches='tight',dpi = 300)
plt.savefig('resultC_transport_strategy/cost/gammas_{0}_{1:03}_{2}.png'.format(x_type,int(gamma*100),forecast_date),bbox_inches='tight',dpi = 300)
plt.close()
# 各県の搬送数
U = uv.reshape(T,N,N)
U_sum = np.zeros(U.shape)
U_sum[0] = U[0]
for i in range(U_sum.shape[0]-1):
U_sum[i+1] = U_sum[i] + U[i+1]
times_U = np.sum(U_sum>0,axis=0)
for target in range(N):
# if sum(U[:,target,:].sum(0)>0) >0:
plt.figure(figsize = (10,6))
times = pd.to_datetime(df_xmean.index)[:-1]
num_U=np.sum(times_U[target] !=0)
index_U = np.argsort(times_U[target,:])[::-1]
tmp_names = names[index_U[:num_U]]
for i in range(tmp_names.shape[0]):
out_target = U_sum[:-1,target,index_U[i]]
plt.plot(times,out_target,'-*',label = tmp_names[i])
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0)
plt.grid()
plt.gca().tick_params(axis='x', rotation= -60)
plt.ylabel('重症者の医療シェア数 [人]')
plt.title(names[target]+'から他地域へのシェアリング')
if MODE == 'normal':
plt.savefig('resultC_transport_strategy/' + dirnames[target]+'/transport_{0}_{1:03}.png'.format(x_type,int(gamma*100)),bbox_inches='tight',dpi = 300)
plt.savefig('resultC_transport_strategy/' + dirnames[target]+'/transport_{0}_{1:03}_{2}.png'.format(x_type,int(gamma*100),forecast_date),bbox_inches='tight',dpi = 300)
plt.close()
# 重傷者病床上限率の設定
gamma_list = [0.8,1.0]
# 使う条件の設定
x_type_list = ['upper','mean','bottom']
if MODE == 'all':
for f_date in forecast_dates:
for gamma in gamma_list:
for x_type in x_type_list:
visualization(gamma,x_type,f_date)
elif MODE == 'normal':
f_date = max(forecast_dates)
for gamma in gamma_list:
for x_type in x_type_list:
visualization(gamma,x_type,f_date)