Skip to content

Commit ca046a6

Browse files
authored
Merge pull request #313 from cmu-delphi/geo_refactor_safegraph
Refactor safegraph to use geo utils
2 parents 8c73df3 + a30dd99 commit ca046a6

File tree

3 files changed

+11
-72
lines changed

3 files changed

+11
-72
lines changed

safegraph/delphi_safegraph/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@
88

99
from __future__ import absolute_import
1010

11-
from . import geo
1211
from . import process

safegraph/delphi_safegraph/geo.py

-66
This file was deleted.

safegraph/delphi_safegraph/process.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
import pandas as pd
77
from delphi_utils.signal import add_prefix
88

9-
from .constants import HOME_DWELL, COMPLETELY_HOME, FULL_TIME_WORK, PART_TIME_WORK
10-
from .geo import FIPS_TO_STATE, VALID_GEO_RESOLUTIONS
9+
from delphi_utils import GeoMapper
10+
11+
from .constants import HOME_DWELL, COMPLETELY_HOME, FULL_TIME_WORK, PART_TIME_WORK, GEO_RESOLUTIONS
1112

1213
# Magic number for modular arithmetic; CBG -> FIPS
1314
MOD = 10000000
@@ -123,11 +124,16 @@ def aggregate(df, signal_names, geo_resolution='county'):
123124
if geo_resolution == 'county':
124125
df['geo_id'] = df['county_fips']
125126
elif geo_resolution == 'state':
126-
df['geo_id'] = df['county_fips'].apply(lambda x:
127-
FIPS_TO_STATE[x[:2]])
127+
gmpr = GeoMapper()
128+
df = gmpr.add_geocode(df,
129+
from_col='county_fips',
130+
from_code='fips',
131+
new_code='state_id',
132+
new_col='geo_id',
133+
dropna=False)
128134
else:
129135
raise ValueError(
130-
f'`geo_resolution` must be one of {VALID_GEO_RESOLUTIONS}.')
136+
f'`geo_resolution` must be one of {GEO_RESOLUTIONS}.')
131137

132138
# Aggregation and signal creation
133139
grouped_df = df.groupby(['geo_id'])[signal_names]

0 commit comments

Comments
 (0)