-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfunctions_case2_html.py
More file actions
427 lines (325 loc) · 17.5 KB
/
functions_case2_html.py
File metadata and controls
427 lines (325 loc) · 17.5 KB
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
## Misc utilities
import pandas as pd
import os
from datetime import datetime, timedelta
#import wget
import numpy as np
import datetime
import re
## Read webpage
from bs4 import BeautifulSoup
from bs4.dammit import EncodingDetector
import requests
## Display pandas dataframe
from IPython.display import display, HTML
from misc_helper_functions import find_all_links, download_file, unzip, url_to_soup, get_json, get_metadata_date
# Import packages needed to run GA code
#import datetime
import email.utils as eut
from io import BytesIO
import re
import zipfile
import urllib.request
import requests
import ssl
import shutil
ssl._create_default_https_context = ssl._create_unverified_context
## Status code and error code
success_code = 'Success!'
error_code = 'An error occured.'
#import datetime fix
def data_extract_michigan(validation=False, home_dir=None):
try:
# # Michigan
MI = {
'Location': 'Michigan'
}
MI_url = "https://www.michigan.gov/coronavirus/0,9753,7-406-98163_98173---,00.html"
MI_soup = url_to_soup(MI_url)
tables = MI_soup.find_all('table')
for table in tables:
caption = table.find('caption')
if caption.string.find('Confirmed COVID-19 Case') >= 0:
m = re.search('updated (\d+)/(\d+)/(\d+)', caption.string)
mon, day, year = tuple(map(int, m.groups()))
MI['Date Published'] = str(datetime.date(year, mon, day).strftime('%-m/%-d/%Y'))
trs = table.find('tbody').find_all('tr')
tds = trs[-1].find_all('td')
total_cases = int(tds[1].string)
total_deaths = int(tds[2].string)
elif caption.string == 'Cases by Race':
for tr in table.find('tbody').find_all('tr'):
tds = tr.find_all('td')
if tds[0].string == 'Black or African American':
aa_cases_pct = int(tds[1].string.strip('% '))
aa_deaths_pct = int(tds[2].string.strip('% '))
aa_cases = int(np.round(total_cases * (aa_cases_pct / 100)))
aa_deaths = int(np.round(total_deaths * (aa_deaths_pct / 100)))
MI['Total Cases'] = total_cases
MI['Total Deaths'] = total_deaths
MI['Count Cases Black/AA'] = aa_cases
MI['Count Deaths Black/AA'] = aa_deaths
MI['Pct Cases Black/AA'] = aa_cases_pct
MI['Pct Deaths Black/AA'] = aa_deaths_pct
print(success_code)
MI['Status code'] = success_code
return MI
except Exception as inst:
print(error_code)
print(inst)
return {
'Location': 'Michigan',
'Date Published': '',
'Total Cases': np.nan,
'Total Deaths': np.nan,
'Count Cases Black/AA': np.nan,
'Count Deaths Black/AA': np.nan,
'Pct Cases Black/AA': np.nan,
'Pct Deaths Black/AA': np.nan,
'Status code': "{} ... {}".format(error_code, repr(inst))
}
def data_extract_minnesota(validation=False, home_dir=None):
try:
MN_url = "https://www.health.state.mn.us/diseases/coronavirus/situation.html#raceeth1"
MN_soup = url_to_soup(MN_url)
# find date
strong = MN_soup.find('strong', string=re.compile('Updated '))
date_text = re.search(r'[A-Z][a-z][a-z] \d\d, 20\d\d',
strong.text).group()
# find total number of confirmed cases
strong = MN_soup.find('strong', string=re.compile('Total positive:'))
num_cases = int(str(strong.next_sibling).strip().replace(',', ''))
# find total number of deaths
strong = MN_soup.find('strong', string=re.compile('Deaths:'))
num_deaths = int(strong.next_sibling.strip().replace(',', ''))
date_time_obj = datetime.datetime.strptime(date_text, "%B %d, %Y")
date_formatted = date_time_obj.strftime("%-m/%-d/%Y")
print('Date:', date_formatted)
print('Number Cases:', num_cases)
print('Number Deaths:', num_deaths)
# find number of Black/AA cases and deaths
table = MN_soup.find("div", attrs={"id": "raceeth"})
th = table.find('th', string="Black")
if not th:
raise ValueError('Unable to locate Black/AA data row')
tds = th.find_next_siblings('td')
cnt_aa_cases = int(tds[0].text.strip().replace(',', ''))
cnt_aa_deaths = int(tds[1].text.strip().replace(',', ''))
pct_aa_cases = round(100 * cnt_aa_cases / num_cases, 2)
pct_aa_deaths = round(100 * cnt_aa_deaths / num_deaths, 2)
print('Pct Cases Black/AA:', pct_cases)
print('Pct Deaths Black/AA:', pct_deaths)
print(success_code)
return {
'Location': 'Minnesota',
'Date Published': date_formatted,
'Total Cases': num_cases,
'Total Deaths': num_deaths,
'Count Cases Black/AA': cnt_aa_cases,
'Count Deaths Black/AA': cnt_aa_deaths,
'Pct Cases Black/AA': pct_aa_cases,
'Pct Deaths Black/AA': pct_aa_deaths,
'Status code': success_code
}
except Exception as inst:
print(error_code)
print(inst)
return {
'Location': 'Minnesota',
'Date Published': '',
'Total Cases': np.nan,
'Total Deaths': np.nan,
'Count Cases Black/AA': np.nan,
'Count Deaths Black/AA': np.nan,
'Pct Cases Black/AA': np.nan,
'Pct Deaths Black/AA': np.nan,
'Status code': "{} ... {}".format(error_code, repr(inst))
}
def data_extract_north_carolina(validation=False, home_dir=None):
try:
NC_url = "https://www.ncdhhs.gov/divisions/public-health/covid19/covid-19-nc-case-count#by-race-ethnicity"
NC_soup = url_to_soup(NC_url)
# find date and total number of cases and deaths
date_match = re.search(r'([A-Za-z]+\s[0-9]+,\s[0-9]+)', NC_soup.find("div", attrs={"class":"field-item"}).p.text)
if date_match:
date_text = ' '.join(date_match.group(1).split())
else:
raise ValueError('Unable to extract date from table header.')
date_time_obj = datetime.datetime.strptime(date_text, "%B %d, %Y")
date_formatted = date_time_obj.strftime("%-m/%-d/%Y")
field_item = NC_soup.find("div", attrs={"class":"field-item"})
# num_cases = field_item.findAll("tr")[1].td.text
thead = field_item.find("thead")
for idx, th in enumerate(thead.tr.find_all('th')):
if th.text.find('Cases') >= 0:
cases_idx = idx
elif th.text.find('Deaths') >= 0:
deaths_idx = idx
tbody = field_item.find("tbody")
tds = tbody.find_all('td')
num_cases = int(tds[cases_idx].text.replace(',', ''))
num_deaths = int(tds[deaths_idx].text.replace(',', ''))
print('Date:', date_formatted)
print('Number Cases:', num_cases)
print('Number Deaths:', num_deaths)
# find number of Black/AA cases and deaths
print('Processing Race/Ethnicity table')
h2 = NC_soup.find('h2', string=re.compile('Race/Ethnicity'))
race_data = h2.find_next_sibling("table")
thead = race_data.find("thead")
ths = thead.find_all("th")
print(f'Found {len(ths)} column headers')
for idx, th in enumerate(ths):
# Search for percentages first to avoid false matches
text = th.text.strip()
# TODO: these equality comparisons may be fragile. If the
# state changes the text, we will see an error occur that
# eg 'aa_cases_idx' is not defined when processin the
# table body.
if text == 'Laboratory-Confirmed Cases':
aa_cases_idx = idx
elif text == '% Laboratory-Confirmed Cases':
pct_aa_cases_idx = idx
elif text == 'Deaths from COVID-19':
aa_deaths_idx = idx
elif text == '% Deaths from COVID-19':
pct_aa_deaths_idx = idx
print('Processing race/eth table body')
tbody = race_data.find('tbody')
for tr in tbody.find_all('tr'):
tds = tr.find_all('td')
if tds[0].text == 'Black or African American':
cnt_aa_cases = int(tds[aa_cases_idx].text.replace(',', ''))
cnt_aa_deaths = int(tds[aa_deaths_idx].text.replace(',', ''))
pct_aa_cases = int(tds[pct_aa_cases_idx].text.strip('%'))
pct_aa_deaths = int(tds[pct_aa_deaths_idx].text.strip('%'))
print('Pct Cases Black/AA:', pct_aa_cases)
print('Pct Deaths Black/AA:', pct_aa_deaths)
print(success_code)
return {
'Location': 'North Carolina',
'Date Published': date_formatted,
'Total Cases': num_cases,
'Total Deaths': num_deaths,
'Count Cases Black/AA': cnt_aa_cases,
'Count Deaths Black/AA': cnt_aa_deaths,
'Pct Cases Black/AA': int(pct_aa_cases),
'Pct Deaths Black/AA': int(pct_aa_deaths),
'Status code': success_code
}
except Exception as inst:
print(error_code)
print(inst)
return {
'Location': 'North Carolina',
'Date Published': '',
'Total Cases': np.nan,
'Total Deaths': np.nan,
'Count Cases Black/AA': np.nan,
'Count Deaths Black/AA': np.nan,
'Pct Cases Black/AA': np.nan,
'Pct Deaths Black/AA': np.nan,
'Status code': "{} ... {}".format(error_code, repr(inst))
}
def data_extract_texas_bexar_county(validation=False, home_dir=None):
location_name = 'Texas -- Bexar County'
TX_Bexar = {
'Location': location_name,
}
placeholder_output = {
'Location': location_name,
'Date Published': '',
'Total Cases': np.nan,
'Total Deaths': np.nan,
'Count Cases Black/AA': np.nan,
'Count Deaths Black/AA': np.nan,
'Pct Cases Black/AA': np.nan,
'Pct Deaths Black/AA': np.nan
}
try:
# Start by fetching the metadata to get the likey timestamp
md_date = get_metadata_date('https://services.arcgis.com/g1fRTDLeMgspWrYp/arcgis/rest/services/vRaceEthnicity/FeatureServer/0?f=json')
TX_Bexar['Date Published'] = str(md_date.strftime('%-m/%-d/%Y'))
# Next get the cumulative case and death counts
total = get_json('https://services.arcgis.com/g1fRTDLeMgspWrYp/arcgis/rest/services/vDateCOVID19_Tracker_Public/FeatureServer/0/query?f=json&where=Date%20BETWEEN%20timestamp%20%272020-05-07%2005%3A00%3A00%27%20AND%20timestamp%20%272020-05-08%2004%3A59%3A59%27&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&resultOffset=0&resultRecordCount=50&resultType=standard&cacheHint=true')
TX_Bexar['Total Cases'] = total['features'][0]['attributes']['ReportedCum']
TX_Bexar['Total Deaths'] = total['features'][0]['attributes']['DeathsCum']
# And finally the race/ethnicity breakdowns
data = get_json('https://services.arcgis.com/g1fRTDLeMgspWrYp/arcgis/rest/services/vRaceEthnicity/FeatureServer/0/query?f=json&where=1%3D1&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&resultOffset=0&resultRecordCount=20&resultType=standard&cacheHint=true')
for feature in data.get('features', []):
if feature['attributes']['RaceEthnicity'] == 'Black':
TX_Bexar['Count Cases Black/AA'] = feature['attributes']['CasesConfirmed']
TX_Bexar['Count Deaths Black/AA'] = feature['attributes']['Deaths']
TX_Bexar['Pct Cases Black/AA'] = round(100 * feature['attributes']['CasesConfirmed'] / TX_Bexar['Total Cases'], 2)
TX_Bexar['Pct Deaths Black/AA'] = round(100 * feature['attributes']['Deaths'] / TX_Bexar['Total Deaths'], 2)
break
if 'Pct Cases Black/AA' not in TX_Bexar:
raise ValueError('No data found for Black RaceEthnicity category')
print(success_code)
TX_Bexar['Status code'] = success_code
return TX_Bexar
except OverflowError as e:
print("Error processing last update timstamp for TX_Bexar")
placeholder_output['Status code'] = "{} ... {}".format(error_code, repr(e))
return placeholder_output
except ValueError as e:
placeholder_output['Status code'] = "{} ... {}".format(error_code, repr(e))
print("Error processing data for TX_Bexar", e)
return placeholder_output
except requests.RequestException as e:
print("Error retrieving URL for TX_Bexar:", e.request.url)
placeholder_output['Status code'] = "{} ... {}".format(error_code, repr(e))
return placeholder_output
def data_extract_wisconsin_milwaukee(validation=False, home_dir=None):
location_name = 'Wisconsin -- Milwaukee'
WI_Milwaukee = {
'Location': location_name,
}
placeholder_output = {
'Location': location_name,
'Date Published': '',
'Total Cases': np.nan,
'Total Deaths': np.nan,
'Count Cases Black/AA': np.nan,
'Count Deaths Black/AA': np.nan,
'Pct Cases Black/AA': np.nan,
'Pct Deaths Black/AA': np.nan
}
try:
# Get the timestamp
cases_date = get_metadata_date('https://services5.arcgis.com/8Q02ELWlq5TYUASS/arcgis/rest/services/Cases_View/FeatureServer/0?f=json')
deaths_date = get_metadata_date('https://services5.arcgis.com/8Q02ELWlq5TYUASS/arcgis/rest/services/Deaths_View1/FeatureServer/0?f=json')
if cases_date != deaths_date:
print('Unexpected mismath between cases and deaths metadata dates:', cases_date, '!=', deaths_date)
WI_Milwaukee['Date Published'] = str(cases_date.strftime('%-m/%-d/%Y'))
cases_total = get_json('https://services5.arcgis.com/8Q02ELWlq5TYUASS/arcgis/rest/services/Cases_View/FeatureServer/0/query?f=json&where=1%3D1&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&outStatistics=%5B%7B%22statisticType%22%3A%22count%22%2C%22onStatisticField%22%3A%22ObjectId%22%2C%22outStatisticFieldName%22%3A%22value%22%7D%5D&resultType=standard&cacheHint=true')
WI_Milwaukee['Total Cases'] = cases_total['features'][0]['attributes']['value']
deaths_total = get_json('https://services5.arcgis.com/8Q02ELWlq5TYUASS/arcgis/rest/services/Deaths_View1/FeatureServer/0/query?f=json&where=1%3D1&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&outStatistics=%5B%7B%22statisticType%22%3A%22count%22%2C%22onStatisticField%22%3A%22ObjectId%22%2C%22outStatisticFieldName%22%3A%22value%22%7D%5D&resultType=standard&cacheHint=true')
WI_Milwaukee['Total Deaths'] = deaths_total['features'][0]['attributes']['value']
cases_by_race = get_json('https://services5.arcgis.com/8Q02ELWlq5TYUASS/arcgis/rest/services/Cases_View/FeatureServer/0/query?f=json&where=Race_Eth%20NOT%20LIKE%20%27%25%23N%2FA%27&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&groupByFieldsForStatistics=Race_Eth&orderByFields=value%20desc&outStatistics=%5B%7B%22statisticType%22%3A%22count%22%2C%22onStatisticField%22%3A%22ObjectId%22%2C%22outStatisticFieldName%22%3A%22value%22%7D%5D&resultType=standard&cacheHint=true')
for feature in cases_by_race['features']:
if feature['attributes']['Race_Eth'] == 'Black Alone':
WI_Milwaukee['Count Cases Black/AA'] = feature['attributes']['value']
WI_Milwaukee['Pct Cases Black/AA'] = round(100 * feature['attributes']['value'] / WI_Milwaukee['Total Cases'], 2)
break
deaths_by_race = get_json('https://services5.arcgis.com/8Q02ELWlq5TYUASS/arcgis/rest/services/Deaths_View1/FeatureServer/0/query?f=json&where=1%3D1&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&groupByFieldsForStatistics=Race_Eth&orderByFields=value%20desc&outStatistics=%5B%7B%22statisticType%22%3A%22count%22%2C%22onStatisticField%22%3A%22ObjectId%22%2C%22outStatisticFieldName%22%3A%22value%22%7D%5D&resultType=standard&cacheHint=true')
for feature in deaths_by_race['features']:
if feature['attributes']['Race_Eth'] == 'Black Alone':
WI_Milwaukee['Count Deaths Black/AA'] = feature['attributes']['value']
WI_Milwaukee['Pct Deaths Black/AA'] = round(100 * feature['attributes']['value'] / WI_Milwaukee['Total Deaths'], 2)
break
print(success_code)
WI_Milwaukee['Status code'] = success_code
return WI_Milwaukee
except OverflowError as e:
print("Error processing last update timstamp for WI_Milwaukee")
placeholder_output['Status code'] = "{} ... {}".format(error_code, repr(e))
return placeholder_output
except ValueError as e:
print("Error processing data for WI_Milwaukee", e)
placeholder_output['Status code'] = "{} ... {}".format(error_code, repr(e))
return placeholder_output
except requests.RequestException as e:
print("Error retrieving URL for WI_Milwaukee:", e.request.url)
placeholder_output['Status code'] = "{} ... {}".format(error_code, repr(e))
return placeholder_output