-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathprocess.py
293 lines (265 loc) · 10.2 KB
/
process.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
"""Internal functions for creating Safegraph indicator."""
import datetime
import os
from typing import List
import numpy as np
import pandas as pd
import covidcast
from .constants import HOME_DWELL, COMPLETELY_HOME, FULL_TIME_WORK, PART_TIME_WORK
from .geo import FIPS_TO_STATE, VALID_GEO_RESOLUTIONS
# Magic number for modular arithmetic; CBG -> FIPS
MOD = 10000000
# Base file name for raw data CSVs.
CSV_NAME = 'social-distancing.csv.gz'
def validate(df):
"""Confirms that a data frame has only one date."""
timestamps = df['date_range_start'].apply(date_from_timestamp)
assert len(timestamps.unique()) == 1
def date_from_timestamp(timestamp) -> datetime.date:
"""Extracts the date from a timestamp beginning with {YYYY}-{MM}-{DD}T."""
return datetime.date.fromisoformat(timestamp.split('T')[0])
def files_in_past_week(current_filename) -> List[str]:
"""Constructs file paths from previous 6 days.
Parameters
----------
current_filename: str
name of CSV file. Must be of the form
{path}/{YYYY}/{MM}/{DD}/{YYYY}-{MM}-{DD}-{CSV_NAME}
Returns
-------
List of file names corresponding to the 6 days prior to YYYY-MM-DD.
"""
path, year, month, day, _ = current_filename.rsplit('/', 4)
current_date = datetime.date(int(year), int(month), int(day))
one_day = datetime.timedelta(days=1)
for _ in range(1, 7):
current_date = current_date - one_day
date_str = current_date.isoformat()
date_path = date_str.replace('-', '/')
new_filename = f'{path}/{date_path}/{date_str}-{CSV_NAME}'
yield new_filename
def add_suffix(signals, suffix):
"""Adds `suffix` to every element of `signals`."""
return [s + suffix for s in signals]
def add_prefix(signal_names, wip_signal, prefix: str):
"""Adds prefix to signal if there is a WIP signal
Parameters
----------
signal_names: List[str]
Names of signals to be exported
prefix : 'wip_'
prefix for new/non public signals
wip_signal : List[str] or bool
a list of wip signals: [], OR
all signals in the registry: True OR
only signals that have never been published: False
Returns
-------
List of signal names
wip/non wip signals for further computation
"""
if wip_signal is True:
return [prefix + signal for signal in signal_names]
if isinstance(wip_signal, list):
make_wip = set(wip_signal)
return [
(prefix if signal in make_wip else "") + signal
for signal in signal_names
]
if wip_signal in {False, ""}:
return [
signal if public_signal(signal)
else prefix + signal
for signal in signal_names
]
raise ValueError("Supply True | False or '' or [] | list()")
def public_signal(signal_):
"""Checks if the signal name is already public using COVIDcast
Parameters
----------
signal_ : str
Name of the signal
Returns
-------
bool
True if the signal is present
False if the signal is not present
"""
epidata_df = covidcast.metadata()
for index in range(len(epidata_df)):
if epidata_df['signal'][index] == signal_:
return True
return False
def construct_signals(cbg_df, signal_names):
"""Construct Census-block level signals.
In its current form, we prepare the following signals in addition to those
already available in raw form from Safegraph:
- completely_home_prop, defined as:
completely_home_device_count / device_count
- full_time_work_prop, defined as:
full_time_work_behavior_devices / device_count
- part_time_work_prop, defined as:
part_time_work_behavior_devices / device_count
Documentation for the social distancing metrics:
https://docs.safegraph.com/docs/social-distancing-metrics
Parameters
----------
cbg_df: pd.DataFrame
Census block group-level dataframe with raw social distancing
indicators from Safegraph.
signal_names: List[str]
Names of signals to be exported.
Returns
-------
pd.DataFrame
Dataframe with columns: timestamp, county_fips, and
{each signal described above}.
"""
# Preparation
cbg_df['county_fips'] = (cbg_df['origin_census_block_group'] // MOD).apply(
lambda x: f'{int(x):05d}')
# Transformation: create signal not available in raw data
for signal in signal_names:
if FULL_TIME_WORK in signal:
cbg_df[signal] = (cbg_df['full_time_work_behavior_devices']
/ cbg_df['device_count'])
elif COMPLETELY_HOME in signal:
cbg_df[signal] = (cbg_df['completely_home_device_count']
/ cbg_df['device_count'])
elif PART_TIME_WORK in signal:
cbg_df[signal] = (cbg_df['part_time_work_behavior_devices']
/ cbg_df['device_count'])
elif HOME_DWELL in signal:
cbg_df[signal] = (cbg_df['median_home_dwell_time'])
# Subsetting
return cbg_df[['county_fips'] + signal_names]
def aggregate(df, signal_names, geo_resolution='county'):
"""Aggregate signals to appropriate resolution and produce standard errors.
Parameters
----------
df: pd.DataFrame
County block group-level data with prepared signals (output of
construct_signals().
signal_names: List[str]
Names of signals to be exported.
geo_resolution: str
One of ('county', 'state')
Returns
-------
pd.DataFrame:
DataFrame with one row per geo_id, with columns for the individual
signals, standard errors, and sample sizes.
"""
# Prepare geo resolution
if geo_resolution == 'county':
df['geo_id'] = df['county_fips']
elif geo_resolution == 'state':
df['geo_id'] = df['county_fips'].apply(lambda x:
FIPS_TO_STATE[x[:2]])
else:
raise ValueError(
f'`geo_resolution` must be one of {VALID_GEO_RESOLUTIONS}.')
# Aggregation and signal creation
grouped_df = df.groupby(['geo_id'])[signal_names]
df_mean = grouped_df.mean()
df_sd = grouped_df.std()
df_n = grouped_df.count()
agg_df = pd.DataFrame.join(df_mean, df_sd,
lsuffix='_mean', rsuffix='_sd')
agg_df = pd.DataFrame.join(agg_df, df_n.rename({
signal: signal + '_n' for signal in signal_names
}, axis=1))
for signal in signal_names:
agg_df[f'{signal}_se'] = (agg_df[f'{signal}_sd']
/ np.sqrt(agg_df[f'{signal}_n']))
return agg_df.reset_index()
def process_window(df_list: List[pd.DataFrame],
signal_names: List[str],
geo_resolutions: List[str],
export_dir: str):
"""Processes a list of input census block group-level data frames as a
single data set and exports it. Assumes each data frame has _only_ one
date of data.
Parameters
----------
cbg_df: pd.DataFrame
list of census block group-level frames.
signal_names: List[str]
signal names to be processed
geo_resolutions: List[str]
List of geo resolutions to export the data.
export_dir
path where the output files are saved
Returns
-------
None. One file is written per (signal, resolution) pair containing the
aggregated data from `df`.
"""
for df in df_list:
validate(df)
date = date_from_timestamp(df_list[0].at[0, 'date_range_start'])
cbg_df = pd.concat(construct_signals(df, signal_names) for df in df_list)
for geo_res in geo_resolutions:
aggregated_df = aggregate(cbg_df, signal_names, geo_res)
for signal in signal_names:
df_export = aggregated_df[
['geo_id']
+ [f'{signal}_{x}' for x in ('mean', 'se', 'n')]
].rename({
f'{signal}_mean': 'val',
f'{signal}_se': 'se',
f'{signal}_n': 'sample_size',
}, axis=1)
df_export.to_csv(f'{export_dir}/{date}_{geo_res}_{signal}.csv',
na_rep='NA',
index=False, )
def process(current_filename: str,
previous_filenames: List[str],
signal_names: List[str],
wip_signal,
geo_resolutions: List[str],
export_dir: str):
"""Creates and exports signals corresponding both to a single day as well
as averaged over the previous week.
Parameters
----------
current_filename: str
path to file holding the target date's data.
previous_filenames: List[str]
paths to files holding data from each day in the week preceding the
target date.
signal_names: List[str]
signal names to be processed for a single date.
A second version of each such signal named {SIGNAL}_7d_avg will be
created averaging {SIGNAL} over the past 7 days.
wip_signal : List[str] or bool
a list of wip signals: [], OR
all signals in the registry: True OR
only signals that have never been published: False
geo_resolutions: List[str]
List of geo resolutions to export the data.
export_dir
path where the output files are saved.
Returns
-------
None. For each (signal, resolution) pair, one file is written for the
single date values to {export_dir}/{date}_{resolution}_{signal}.csv and
one for the data averaged over the previous week to
{export_dir}/{date}_{resolution}_{signal}_7d_avg.csv.
"""
past_week = [pd.read_csv(current_filename)]
for fname in previous_filenames:
if os.path.exists(fname):
past_week.append(pd.read_csv(fname))
# First process the current file alone...
process_window(past_week[:1],
add_prefix(signal_names, wip_signal, 'wip_'),
geo_resolutions,
export_dir)
# ...then as part of the whole window.
process_window(past_week,
add_prefix(add_suffix(signal_names, '_7d_avg'),
wip_signal,
'wip_'),
geo_resolutions,
export_dir)