This repository has been archived by the owner on May 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
get.py
373 lines (335 loc) · 12.4 KB
/
get.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import pandas as pd
import numpy as np
import os
import json
def convert_to_int(value):
return int(value)
def get_word_epidemic(date):
"""
根据日期获取所有国家对应的累计确诊、累计死亡、累计治愈
以及最大的累计确诊值和最小的累计确诊值
格式: [["country",[confirmed,death,recovered]],...]
"""
date_name = date.replace("/", "-")
directory = "data/world-epidemic/"
if os.path.isfile(directory + date_name + ".csv"):
file = open(os.path.join(directory, date_name + ".csv"),
'r', encoding="utf-8")
data = pd.read_csv(file)
file.close()
max = data["Confirmed"].max()
min = data["Confirmed"].min()
result_list = []
for index in range(0, data.shape[0]):
item = data.iloc[index]
result_list.append([item["Country_Region"], [int(item["Confirmed"]),
int(item["Deaths"]), int(item["Recovered"])]])
return int(max), int(min), result_list
else:
return None, None, None
def get_country_epidemic(date, country):
"""
获取国家的疫情数据
:param date: 04/25/2020
:param country: China
:return: list
"""
date_name = date.replace("/", "-")
file_name = "data/country-epidemic/" \
+ date_name + "-" + country + ".csv"
if os.path.isfile(file_name):
data = pd.read_csv(
os.path.join("data/country-epidemic/", date_name + "-" + country + ".csv"),
encoding="utf-8")
confirmed = data["Confirmed"].astype(int)
max = confirmed.max()
min = confirmed.min()
result = []
for index in range(0, data.shape[0]):
result.append(data.iloc[index].tolist())
return max, min, result
else:
return None
def get_country_epidemic_summary(country):
"""
获取国家疫情概述数据
:param country:
:return: list
"""
file_name = "data/country-epidemic-summary/" + country + ".csv"
if os.path.exists(file_name):
file = open(file_name, mode="r", encoding="utf-8")
data = pd.read_csv(file, dtype=np.object)
file.close()
confirmed_list = []
confirmed_change_list = []
death_list = []
death_change_list = []
date_list = []
for index in range(0, data.shape[0]):
item = data.iloc[index]
date_list.append(item["Updated"])
confirmed_list.append(item["Confirmed"])
confirmed_change_list.append(item["ConfirmedChange"])
death_list.append(item["Deaths"])
death_change_list.append(item["DeathsChange"])
return date_list, confirmed_list, confirmed_change_list, \
death_list, death_change_list
else:
return None
# "Côte d'Ivoire.csv
def get_confirmed_distribution(country, date):
"""
获取累计确诊的分布情况
:param country: China
:param date: 04/25/2020
:return: list
"""
date_name = date.replace("/", "-")
file_name = "data/country-epidemic/" \
+ date_name + "-" + country + ".csv"
world = "Worldwide"
if os.path.isfile(file_name):
file = open(os.path.join("data/country-epidemic/",
date_name + "-" + country + ".csv"),
"r", encoding="utf-8")
data = pd.read_csv(file)
file.close()
if country == world:
data = data[["Country_Region", "Confirmed"]]
else:
data = data[["AdminRegion1", "Confirmed"]]
data.sort_values(by="Confirmed", inplace=True, ascending=False)
result = []
if data.shape[0] > 20:
data1 = data.head(20)
confirmed = data.tail(-20)["Confirmed"].sum()
dict1 = {}
if country == world:
dict1["Country_Region"] = "其它"
else:
dict1["AdminRegion1"] = "其它"
dict1["Confirmed"] = confirmed
data = data1.append(dict1, ignore_index=True)
if country == world:
for index in range(0, data.shape[0]):
row = data.iloc[index]
result.append([row["Country_Region"], int(row["Confirmed"])])
else:
for index in range(0, data.shape[0]):
row = data.iloc[index]
result.append([row["AdminRegion1"], int(row["Confirmed"])])
return result
else:
return None
def get_new_confirmed_top5(country, date):
"""
根据日期和国家获取新增确诊的top5
:param country:"China"
:param date: "04/24/2020
:return: list
"""
date_name = date.replace("/", "-")
file_name = "data/country-epidemic/" \
+ date_name + "-" + country + ".csv"
if os.path.isfile(file_name):
file = open(file_name, "r", encoding="utf-8")
data = pd.read_csv(file, dtype=np.object)
file.close()
if data.shape[0] == 0:
return None
data.sort_values(by="ConfirmedChange", ascending=False, inplace=True)
result = []
area = []
if data.shape[0] > 5:
data = data.head(5)
for index in range(0, data.shape[0]):
item = data.iloc[index]
area.append(item["AdminRegion1"])
result.append(item["ConfirmedChange"])
return [area, result]
else:
return None
def get_region_comparion(country, date, attribute):
"""
获取地区对比的疫情数据
:param country: "China"
:param date: 04/25/2020
:param attribbute: 属性名称{Confirmed|Deaths|Recovered|ConfirmedChange}
:return:Nothing
"""
date_name = date.replace("/", "-")
file_name1 = "data/country-epidemic/" \
+ date_name + "-" + country + ".csv"
file_name2 = "data/region-comparision/" + country + ".csv"
if os.path.isfile(file_name1) and os.path.isfile(file_name2):
file1 = open(file_name1, "r", encoding="utf-8")
data = pd.read_csv(file1, dtype=np.object)
file1.close()
if attribute not in data.columns:
return None
data.sort_values(by=attribute, inplace=True, ascending=False)
if data.shape[0] > 5:
data = data.head(5)
region_list = data["AdminRegion1"].tolist()
data = pd.read_csv(
open(os.path.join("data/region-comparision/", country + ".csv"),
"r", encoding="utf-8"), dtype=np.object)
region_column = data["AdminRegion1"]
result = {}
for region in region_list:
item = data[region_column == region][["Updated", attribute]]
date_list = item["Updated"].tolist()
data_list = item[attribute].tolist()
result[region] = [date_list, data_list]
return result
else:
return None
def get_date_list():
"""
"""
file_name = "data/country-epidemic-summary/Worldwide.csv"
if os.path.isfile(file_name):
return pd.read_csv(file_name)["Updated"].tolist()
else:
return None
def get_time_axis_data(date):
"""
:return:
"""
date_name = date.replace("/", "-")
file_name = "data/world-epidemic/" + date_name + ".csv"
if os.path.isfile(file_name):
data = pd.read_csv(file_name, encoding="utf-8")
data.sort_values(by="Confirmed", ascending=False, inplace=True)
result = []
max = int(data.head(1)["Confirmed"])
min = int(data.tail(1)["Confirmed"])
for index in range(0, data.shape[0]):
row = data.iloc[index]
result.append([str(row["Country_Region"]),
[int(row["Confirmed"]), str(row["Country_Region"])]])
return max, min, result
def get_world_confirmed():
"""
"""
result = []
date_list = get_date_list()
for date in date_list:
date_name = date.replace("/", "-")
file_name = "data/world-epidemic/" + date_name + ".csv"
data = pd.read_csv(file_name, encoding="utf-8")
result.append(int(data.iloc[0]["Confirmed"]))
return result
def get_country_status(country="China"):
"""
"""
file_name = "data/country-status/country_status.csv"
if os.path.isfile(file_name):
file = open(file_name, "r", encoding="utf-8")
data = pd.read_csv(file, dtype=np.str)
file.close()
data = data[data["Country_Region"] == country]
if data.shape[0] == 0:
return None
data = data.iloc[0].tolist()
c = int(data[0])
nc = int(data[1])
d = int(data[2])
nd = int(data[3])
r = int(data[4])
nr = int(data[5])
total = c + r + d
total_today = nc + nd + nr
dict = {
'total': format(total, ','),
'total_today': format(total_today, ','),
'confirm_total': format(c, ','),
'confirm_today': format(nc, ','),
'recover_total': format(d, ','),
'recover_today': format(nd, ','),
'death_total': format(r, ','),
'death_today': format(nr, ',')
}
return dict
else:
return None
def get_country_list_with_data():
"""
"""
file_name = "data/country-status/country_status.csv"
if os.path.isfile(file_name):
data = pd.read_csv(file_name, encoding="utf-8")
country_list = []
tmp = []
for index in range(0, data.shape[0]):
row = data.iloc[index]
total = int(row["Confirmed"]) \
+ int(row["Deaths"]) \
+ int(row["Recovered"])
country = row["Country_Region"]
tmp.append([country, total])
tmp_df = pd.DataFrame(data=tmp, columns=["country", "total"])
tmp_df.sort_values(by="total", inplace=True, ascending=False)
for index in range(0, tmp_df.shape[0]):
row = tmp_df.iloc[index]
country_list.append({"name": row["country"],
"number": format(row["total"], ',')})
return country_list
else:
return None
def get_today():
"""
返回最新日期
:return str
"""
file_name = "data/country-epidemic-summary/Worldwide.csv"
if os.path.isfile(file_name):
return str(pd.read_csv(file_name)["Updated"].tolist()[-1])
else:
return None
def get_treemap_data(today):
"""
"""
data_list = []
source = pd.read_csv("data/Bing-COVID19-Data.csv", encoding="utf-8")
source = source[source.Updated == today]
country_list = source["Country_Region"].unique()
country_list = country_list[1:]
for country_name in country_list:
data = source[source.Country_Region == country_name]
if data.shape[0] == 0:
continue
data = data[["Confirmed", "Country_Region", "AdminRegion1", "AdminRegion2"]]
country_value = int(data[data["AdminRegion1"].isna()]["Confirmed"])
if data.shape[0] <= 1:
data_list.append({"name": str(country_name), "value": int(country_value), "children": []})
continue
ar1_list = []
tmp = data[data["AdminRegion2"].isna()].fillna(value="nan")
for index in range(0, tmp.shape[0]):
row = tmp.iloc[index]
if row["AdminRegion1"] == "nan":
continue
tmp1 = data[data.AdminRegion1 == row["AdminRegion1"]]
if tmp1.shape[0] == 0:
ar1_list.append({"name": str(row["AdminRegion1"]), "value": int(row["Confirmed"]), "children": []})
continue
ar2_list = []
tmp1 = tmp1[tmp1["AdminRegion2"].notna()]
for j in range(0, tmp1.shape[0]):
item = tmp1.iloc[j]
ar2_list.append({"name": str(item["AdminRegion2"]), "value": int(item["Confirmed"]), "children": []})
ar1_list.append({"name": str(row["AdminRegion1"]), "value": int(row["Confirmed"]), "children": ar2_list})
data_list.append({"name": str(country_name), "value": int(country_value), "children": ar1_list})
return {"children": data_list}
def get_confirmed_pre(country):
file_name = "prediction_data/" + country + ".csv"
if os.path.isfile(file_name):
data = pd.read_csv(file_name, encoding="utf-8")
confirmed = []
for index in range(0, data.shape[0]):
confirmed.append(int(data.iloc[index]["Confirmed"]))
return confirmed
else:
return None