-
Notifications
You must be signed in to change notification settings - Fork 0
/
DailyReports.py
171 lines (155 loc) · 6.81 KB
/
DailyReports.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
# data from https://github.com/CSSEGISandData/COVID-19
import os
import pandas
import numpy as np
from datetime import datetime
import state_codes
class DailyReports(object):
def __init__(self, reports_directory='../COVID-19/csse_covid_19_data/csse_covid_19_daily_reports'):
self.reports_directory = reports_directory
files = os.listdir(self.reports_directory)
files.sort()
self.reports = []
self.dates = []
for f in files:
if f.endswith('.csv'):
report = pandas.read_csv(os.path.join(self.reports_directory, f))
self.reports.append(report)
# Rename columns from the older format
if 'Province/State' in report.columns:
self.fix_old_column_names(report)
#self.fix_dates(report)
self.fix_country_names(report)
self.dates.append(f[0:2] + '/' + f[3:5] + '/' + f[8:10])
self.dates = [datetime.strptime(d, "%m/%d/%y").date() for d in self.dates]
def fix_old_column_names(self, report):
report.rename(columns = {'Country/Region':'Country_Region',
'Last Update':'Last_Update'},
inplace = True)
# split up the Province/State
counties = []
states = []
for ps in report['Province/State']:
county = np.nan
state = np.nan
if isinstance(ps, str):
ss = ps.split(', ')
if len(ss) == 2:
# For names like 'Contra Costa County, CA'
county = ss[0]
if county.endswith(' County'):
county = county[:-7]
state = state_codes.state_codes_reverse.get(ss[1], ss[1])
elif len(ss) == 1:
state = ss[0]
counties.append(county)
states.append(state)
report['Province_State'] = states
report['Admin2'] = counties
def fix_dates(self, report):
# Fixes up the dates, using the format MM/DD/YY
fixed_dates = []
for date in report['Last_Update']:
if date.find('/') > -1:
# old format dates, split off the time
sdate = date.split()
fixed_dates.append(sdate[0])
else:
fdate = date[5:7].lstrip('0') + '/' + date[8:10] + '/' + date[2:4]
fixed_dates.append(fdate)
report['Last_Update'] = fixed_dates
def fix_country_names(self, report):
countries = []
for country in report['Country_Region']:
if country == 'Mainland China':
country = 'China'
if country == 'South Korea' or country == 'Republic of Korea':
country = 'Korea, South'
if country == 'UK':
country = 'United Kingdom'
countries.append(country)
report['Country_Region'] = countries
def data_from_report(self, column, name, which, report):
case = None
if report[column].isnull().all():
# All NaNs, no data for this date
return case
df = report[report[column] == name]
if len(df) > 0:
case = 0
for index in df.index:
value = report[which][index]
if not np.isnan(value):
case += int(value)
return case
def county_data(self, county, which='Confirmed', state='California'):
cases = []
dates = []
for date, report in zip(self.dates, self.reports):
report = report[report['Province_State'] == state]
case = self.data_from_report('Admin2', county, which, report)
if case is not None:
cases.append(case)
dates.append(date)
return np.array(cases, dtype=float), dates
def state_data(self, state, which='Confirmed', country='US'):
cases = []
dates = []
for date, report in zip(self.dates, self.reports):
report = report[report['Country_Region'] == country]
case = self.data_from_report('Province_State', state, which, report)
if case is not None:
cases.append(case)
dates.append(date)
return np.array(cases, dtype=float), dates
def country_data(self, country, which='Confirmed'):
cases = []
dates = []
for date, report in zip(self.dates, self.reports):
case = self.data_from_report('Country_Region', country, which, report)
if case is not None:
cases.append(case)
dates.append(date)
return np.array(cases, dtype=float), dates
def find_max_regions(self, which, column, ncases=10, population_df=None, get_regions=None, mincases=0):
maxregions = []
maxcases = []
report = self.reports[-1]
if get_regions is not None:
report = get_regions(report)
for region in report[column].unique():
cases = self.data_from_report(column, region, which, report)
if cases is None:
continue
if cases < mincases:
continue
if population_df is not None:
population = population_df[population_df['Name'] == region]['Population']
if len(population) == 0:
continue
population = int(population)
cases = cases/population
if len(maxcases) < ncases:
maxregions.append(region)
maxcases.append(cases)
elif cases > min(maxcases):
ii = np.argmin(maxcases)
maxregions[ii] = region
maxcases[ii] = cases
# sort in descending order
ii = np.argsort(maxcases)[::-1]
result = []
for i in ii:
result.append(maxregions[i])
return result
def find_max_countries(self, which, ncases=10, population_df=None, mincases=0):
return self.find_max_regions(which, 'Country_Region', ncases, population_df,
mincases=mincases)
def find_max_states(self, which, ncases=10, population_df=None, country='US', mincases=0):
return self.find_max_regions(which, 'Province_State', ncases, population_df,
get_regions=lambda report : report[report['Country_Region'] == country],
mincases=mincases)
def find_max_counties(self, which, ncases=10, population_df=None, state='California', mincases=0):
return self.find_max_regions(which, 'Admin2', ncases, population_df,
get_regions=lambda report : report[report['Province_State'] == state],
mincases=mincases)