This repository has been archived by the owner on Feb 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
delta_calculator.py
272 lines (235 loc) · 8.76 KB
/
delta_calculator.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
import os
import datetime
import pandas as pd
API_DIST_CUM = 'https://data.covid19bharat.org/csv/latest/district_wise.csv'
API_DIST_TS = 'https://data.covid19bharat.org/csv/latest/districts.csv'
DELTA_TXT = os.path.join(os.path.dirname(os.path.abspath(__file__)), '_outputs', 'delta.txt')
DELTA_MAPPING = os.path.join(os.path.dirname(os.path.abspath(__file__)), '_meta', 'delta_mapping.meta')
def format_df(opt, df):
'''
Given dataframe, format print as example shown as below
---
Example: Lower Siang , Arunachal Pradesh, AR , -17 , Recovered
Format: <districtname>, <state name> , <state_code>, <delta>, <[Hospitalised, Recovered, Deceased, Migrated_Other]>
---
:param: <dict> - state config
:param: <pd.DataFrame> - delta dataframe
:returns: <dict> - deltas, other info and formatted data for printing
'''
cols = [
'district_name',
'state_name',
'state_code',
'delta',
'delta_type'
]
frmts = {
'Confirmed': 'Hospitalized',
'Recovered': 'Recovered',
'Deceased': 'Deceased',
'Migrated_Other': 'Migrated_Other'
}
dfs = []
for f in frmts:
frmt_df = pd.DataFrame(columns=cols)
frmt_df['district_name'] = df['District']
frmt_df['delta'] = df[f]
frmt_df['delta_type'] = frmts[f]
frmt_df['state_name'] = opt['name']
frmt_df['state_code'] = opt['state_code']
dfs.append(frmt_df)
result_df = pd.concat(dfs)
result_df = result_df[result_df['delta'] != 0] # drop rows with no deltas
return result_df
def ut_calculate_detlas(opt, live_data):
'''
This delta calculation is only for the state of UT. This has been added because UT pdf
cumulatives now provide `total since 1st Jan, 2022`.
'''
api_df = pd.read_csv(API_DIST_TS)
state_df = api_df[api_df['State'] == opt['name']].rename(columns={'Other': 'Migrated_Other'})
# done_df = state_df[state_df['Date'] == datetime.date.today().strftime('%Y-%m-%d')]
# is data already entered for today?
# if done_df.empty == False:
# return {
# 'delta_totals': {
# 'confirmed': 0,
# 'recovered': 0,
# 'deceased': 0,
# 'migrated': 0
# },
# 'deltas': pd.DataFrame(),
# 'api_state_data': done_df[['District', 'Confirmed', 'Recovered', 'Deceased', 'Migrated_Other']],
# 'for_sheets': pd.DataFrame()
# }
# 0. get meta info
meta_df = pd.read_csv(DELTA_MAPPING, sep=',', encoding='utf-8', header=None, names=['state_name', 'from_dist', 'to_dist'])
state_meta_df = meta_df[meta_df['state_name'] == opt['name']][[
'from_dist',
'to_dist'
]].apply(lambda x: x.str.strip()) # strip whitespaces
dist_to_rename = state_meta_df.set_index('from_dist').to_dict().get('to_dist')
# 1. get cumulative until 31st Dec
dt_dec = datetime.date(2021, 12, 31)
dt_dec_str = dt_dec.strftime('%Y-%m-%d')
dec_df = state_df[api_df['Date'] == dt_dec_str].rename(columns={'Other': 'Migrated_Other'})
dec_df = dec_df[[
'District',
'Confirmed',
'Recovered',
'Deceased',
'Migrated_Other'
]].set_index('District').sort_index(ascending=True)
# 2. get cumulative from 1st jan until today (live_data)
live_df = pd.DataFrame.from_dict(live_data)
live_df.rename(columns={
'confirmed': 'Confirmed',
'recovered': 'Recovered',
'deceased' : 'Deceased',
'migrated': 'Migrated_Other'
}, inplace=True)
live_df.replace(dist_to_rename, inplace=True)
live_df = live_df.set_index('districtName').sort_index(ascending=True)
# 3. calculate (#1) + (#2)
today_cum_df = dec_df + live_df
# 4. get cumulatives for (today - 1) i.e. until yesterday
dt_yest = datetime.date.today() - datetime.timedelta(days=1)
dt_yest_str = dt_yest.strftime('%Y-%m-%d')
yest_cum_df = api_df[
(api_df['State'] == opt['name']) &
(api_df['Date'] == dt_yest_str)
].rename(columns={'Other': 'Migrated_Other'})
yest_cum_df = yest_cum_df[[
'District',
'Confirmed',
'Recovered',
'Deceased',
'Migrated_Other'
]].set_index('District').sort_index(ascending=True)
# --> substract individually for each district
custom_diffs = {
'District': [
'Almora',
'Bageshwar',
'Chamoli',
'Champawat',
'Dehradun',
'Haridwar',
'Nainital',
'Pauri Garhwal',
'Pithoragarh',
'Rudraprayag',
'Tehri Garhwal',
'Udham Singh Nagar',
'Uttarkashi'
],
'Confirmed': [
6,
4,
2,
3,
135,
30,
70,
7,
12,
0,
7,
24,
2
]
}
custom_df = pd.DataFrame(data=custom_diffs)
custom_df.set_index('District', inplace=True)
# 5. calculate (#3) - (#4)
delta_df = today_cum_df - yest_cum_df
# --> take custom diff for each district
delta_df['Confirmed'] = delta_df['Confirmed'] - custom_df['Confirmed']
# 6. format data frame
delta_df.fillna(0, inplace=True)
delta_df = delta_df.astype(int).reset_index()
delta_df.rename(columns={
'districtName': 'District',
'index': 'District'
}, inplace=True)
# 7. drop rows with no deltas
delta_df = delta_df.drop(delta_df[delta_df['District'].str.contains('Total')].index)
return {
'delta_totals': {
'confirmed': delta_df['Confirmed'].sum(),
'recovered': delta_df['Recovered'].sum(),
'deceased': delta_df['Deceased'].sum(),
'migrated': delta_df['Migrated_Other'].sum()
},
'deltas': delta_df,
'api_state_data': today_cum_df.reset_index(),
'for_sheets': format_df(opt, delta_df)
}
def calculate_deltas(opt, live_data, dt=datetime.date.today()):
'''
Calculate difference b/w current data vs API/latest data and return deltas
:param: <dict> - `opt` as selected state's config
:param: <dict> - currently read data from input
:param: <pd.DataFrame> - dataframe for a particular date
:param: <datetime> - date to calculate deltas against
:returns: <pd.DataFrame> - calculated difference dataframe
'''
# 0. exception for UT delta calculation
if opt['state_code'] == 'UT':
return ut_calculate_detlas(opt, live_data)
# 1. get updated API data & filter for selected state & sort
if dt != datetime.date.today():
api_df = pd.read_csv(API_DIST_TS)
dt_str = dt.strftime('%Y-%m-%d')
state_df = api_df[
(api_df['State'] == opt['name']) &
(api_df['Date'] == dt_str)
].rename(columns={'Other': 'Migrated_Other'})
else:
api_df = pd.read_csv(API_DIST_CUM)
state_df = api_df[api_df['State'] == opt['name']]
state_df = state_df[[
'District',
'Confirmed',
'Recovered',
'Deceased',
'Migrated_Other'
]].set_index('District').sort_index(ascending=True)
# 2. read meta file to map district names
meta_df = pd.read_csv(DELTA_MAPPING, sep=',', encoding='utf-8', header=None, names=['state_name', 'from_dist', 'to_dist'])
state_meta_df = meta_df[meta_df['state_name'] == opt['name']][[
'from_dist',
'to_dist'
]].apply(lambda x: x.str.strip()) # strip whitespaces
dist_to_rename = state_meta_df.set_index('from_dist').to_dict().get('to_dist')
# 3. structure the live_data, rename districts & sort
live_df = pd.DataFrame.from_dict(live_data)
live_df.rename(columns={
'confirmed': 'Confirmed',
'recovered': 'Recovered',
'deceased' : 'Deceased',
'migrated': 'Migrated_Other'
}, inplace=True)
live_df.replace(dist_to_rename, inplace=True)
live_df = live_df.set_index('districtName').sort_index(ascending=True)
# 4. calculate deltas, fill NA = 0, convert to int, structure it & return
delta_df = live_df - state_df
delta_df.fillna(0, inplace=True)
delta_df = delta_df.astype(int).reset_index()
delta_df.rename(columns={
'districtName': 'District',
'index': 'District'
}, inplace=True)
# 5. drop rows with no deltas
delta_df = delta_df.drop(delta_df[delta_df['District'].str.contains('Total')].index)
return {
'delta_totals': {
'confirmed': delta_df['Confirmed'].sum(),
'recovered': delta_df['Recovered'].sum(),
'deceased': delta_df['Deceased'].sum(),
'migrated': delta_df['Migrated_Other'].sum()
},
'deltas': delta_df,
'api_state_data': state_df.reset_index(),
'for_sheets': format_df(opt, delta_df)
}