-
Notifications
You must be signed in to change notification settings - Fork 62
/
csv_importer.py
306 lines (240 loc) · 9.54 KB
/
csv_importer.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
"""Collects and reads covidcast data from a set of local CSV files."""
# standard library
from datetime import date
import glob
import math
import os
import re
# third party
import pandas
import epiweeks as epi
# first party
from delphi.utils.epiweek import delta_epiweeks
class CsvImporter:
"""Finds and parses covidcast CSV files."""
# .../source/yyyymmdd_geo_signal.csv
PATTERN_DAILY = re.compile(r'^.*/([^/]*)/(\d{8})_(\w+?)_(\w+)\.csv$')
# .../source/weekly_yyyyww_geo_signal.csv
PATTERN_WEEKLY = re.compile(r'^.*/([^/]*)/weekly_(\d{6})_(\w+?)_(\w+)\.csv$')
# .../issue_yyyymmdd
PATTERN_ISSUE_DIR = re.compile(r'^.*/([^/]*)/issue_(\d{8})$')
# set of allowed resolutions (aka "geo_type")
GEOGRAPHIC_RESOLUTIONS = {'county', 'hrr', 'msa', 'dma', 'state', 'nation'}
# set of required CSV columns
REQUIRED_COLUMNS = {'geo_id', 'val', 'se', 'sample_size'}
# reasonable time bounds for sanity checking time values
MIN_YEAR = 2019
MAX_YEAR = 2030
# NOTE: this should be a Python 3.7+ `dataclass`, but the server is on 3.4
# See https://docs.python.org/3/library/dataclasses.html
class RowValues:
"""A container for the values of a single covidcast row."""
def __init__(self, geo_value, value, stderr, sample_size):
self.geo_value = geo_value
self.value = value
self.stderr = stderr
self.sample_size = sample_size
@staticmethod
def is_sane_day(value):
"""Return whether `value` is a sane (maybe not valid) YYYYMMDD date.
Truthy return is is a datetime.date object representing `value`."""
year, month, day = value // 10000, (value % 10000) // 100, value % 100
nearby_year = CsvImporter.MIN_YEAR <= year <= CsvImporter.MAX_YEAR
valid_month = 1 <= month <= 12
sensible_day = 1 <= day <= 31
if not (nearby_year and valid_month and sensible_day):
return False
return date(year=year,month=month,day=day)
@staticmethod
def is_sane_week(value):
"""Return whether `value` is a sane (maybe not valid) YYYYWW epiweek.
Truthy return is `value`."""
year, week = value // 100, value % 100
nearby_year = CsvImporter.MIN_YEAR <= year <= CsvImporter.MAX_YEAR
sensible_week = 1 <= week <= 53
if not (nearby_year and sensible_week):
return False
return value
@staticmethod
def find_issue_specific_csv_files(scan_dir, glob=glob):
for path in sorted(glob.glob(os.path.join(scan_dir, '*'))):
issuedir_match = CsvImporter.PATTERN_ISSUE_DIR.match(path.lower())
if issuedir_match and os.path.isdir(path):
issue_date_value = int(issuedir_match.group(2))
issue_date = CsvImporter.is_sane_day(issue_date_value)
if issue_date:
print(' processing csv files from issue date: "' + str(issue_date) + '", directory', path)
yield from CsvImporter.find_csv_files(path, issue=(issue_date, epi.Week.fromdate(issue_date)), glob=glob)
else:
print(' invalid issue directory day', issue_date_value)
@staticmethod
def find_csv_files(scan_dir, issue=(date.today(), epi.Week.fromdate(date.today())), glob=glob):
"""Recursively search for and yield covidcast-format CSV files.
scan_dir: the directory to scan (recursively)
The return value is a tuple of (path, details), where, if the path was
valid, details is a tuple of (source, signal, time_type, geo_type,
time_value, issue, lag) (otherwise None).
"""
issue_day,issue_epiweek=issue
issue_day_value=int(issue_day.strftime("%Y%m%d"))
issue_epiweek_value=int(str(issue_epiweek))
issue_value=-1
lag_value=-1
for path in sorted(glob.glob(os.path.join(scan_dir, '*', '*'))):
if not path.lower().endswith('.csv'):
# safe to ignore this file
continue
print('file:', path)
# match a daily or weekly naming pattern
daily_match = CsvImporter.PATTERN_DAILY.match(path.lower())
weekly_match = CsvImporter.PATTERN_WEEKLY.match(path.lower())
if not daily_match and not weekly_match:
print(' invalid csv path/filename', path)
yield (path, None)
continue
# extract and validate time resolution
if daily_match:
time_type = 'day'
time_value = int(daily_match.group(2))
match = daily_match
time_value_day = CsvImporter.is_sane_day(time_value)
if not time_value_day:
print(' invalid filename day', time_value)
yield (path, None)
continue
issue_value=issue_day_value
lag_value=(issue_day-time_value_day).days
else:
time_type = 'week'
time_value = int(weekly_match.group(2))
match = weekly_match
time_value_week=CsvImporter.is_sane_week(time_value)
if not time_value_week:
print(' invalid filename week', time_value)
yield (path, None)
continue
issue_value=issue_epiweek_value
lag_value=delta_epiweeks(time_value_week, issue_epiweek_value)
# # extract and validate geographic resolution
geo_type = match.group(3).lower()
if geo_type not in CsvImporter.GEOGRAPHIC_RESOLUTIONS:
print(' invalid geo_type', geo_type)
yield (path, None)
continue
# extract additional values, lowercased for consistency
source = match.group(1).lower()
signal = match.group(4).lower()
if len(signal) > 64:
print(' invalid signal name (64 char limit)',signal)
yield (path, None)
continue
yield (path, (source, signal, time_type, geo_type, time_value, issue_value, lag_value))
@staticmethod
def is_header_valid(columns):
"""Return whether the given pandas columns contains the required fields."""
return set(columns) >= CsvImporter.REQUIRED_COLUMNS
@staticmethod
def floaty_int(value):
"""Cast a string to an int, even if it looks like a float.
For example, "-1" and "-1.0" should both result in -1. Non-integer floats
will cause `ValueError` to be reaised.
"""
float_value = float(value)
int_value = round(float_value)
if float_value != int_value:
raise ValueError('not an int: "%s"' % str(value))
return int_value
@staticmethod
def maybe_apply(func, value):
"""Apply the given function to the given value if not null-ish."""
if str(value).lower() not in ('', 'na', 'nan', 'inf', '-inf', 'none'):
return func(value)
@staticmethod
def extract_and_check_row(row, geo_type):
"""Extract and return `RowValues` from a CSV row, with sanity checks.
Also returns the name of the field which failed sanity check, or None.
row: the pandas table row to extract
geo_type: the geographic resolution of the file
"""
# use consistent capitalization (e.g. for states)
try:
geo_id = row.geo_id.lower()
except AttributeError as e:
# geo_id was `None`
return (None, 'geo_id')
if geo_type in ('hrr', 'msa', 'dma'):
# these particular ids are prone to be written as ints -- and floats
try:
geo_id = str(CsvImporter.floaty_int(geo_id))
except ValueError:
# expected a number, but got a string
return (None, 'geo_id')
# sanity check geo_id with respect to geo_type
if geo_type == 'county':
if len(geo_id) != 5 or not '01000' <= geo_id <= '80000':
return (None, 'geo_id')
elif geo_type == 'hrr':
if not 1 <= int(geo_id) <= 500:
return (None, 'geo_id')
elif geo_type == 'msa':
if len(geo_id) != 5 or not '10000' <= geo_id <= '99999':
return (None, 'geo_id')
elif geo_type == 'dma':
if not 450 <= int(geo_id) <= 950:
return (None, 'geo_id')
elif geo_type == 'state':
# note that geo_id is lowercase
if len(geo_id) != 2 or not 'aa' <= geo_id <= 'zz':
return (None, 'geo_id')
elif geo_type == 'nation':
# geo_id is lowercase
if len(geo_id) != 2 or not 'aa' <= geo_id <= 'zz':
return (None, 'geo_id')
else:
return (None, 'geo_type')
# required float
try:
value = float(row.val)
if math.isnan(value):
raise ValueError('nan not valid for `value`')
except (TypeError, ValueError) as e:
# val was either `None` or not a float
return (None, 'val')
# optional nonnegative float
try:
stderr = CsvImporter.maybe_apply(float, row.se)
except ValueError:
# expected a number, but got a string
return (None, 'se')
if stderr is not None and stderr < 0:
return (None, 'se')
# optional not-too-small float
try:
sample_size = CsvImporter.maybe_apply(float, row.sample_size)
except ValueError:
# expected a number, but got a string
return (None, 'sample_size')
# return extracted and validated row values
row_values = CsvImporter.RowValues(geo_id, value, stderr, sample_size)
return (row_values, None)
@staticmethod
def load_csv(filepath, geo_type, pandas=pandas):
"""Load, validate, and yield data as `RowValues` from a CSV file.
filepath: the CSV file to be loaded
geo_type: the geographic resolution (e.g. county)
In case of a validation error, `None` is yielded for the offending row,
including the header.
"""
# don't use type inference, just get strings
table = pandas.read_csv(filepath, dtype='str')
if not CsvImporter.is_header_valid(table.columns):
print(' invalid header')
yield None
return
for row in table.itertuples(index=False):
row_values, error = CsvImporter.extract_and_check_row(row, geo_type)
if error:
print(' invalid value for %s (%s)' % (str(row), error))
yield None
continue
yield row_values