-
Notifications
You must be signed in to change notification settings - Fork 16
Switch to geo utils for combined Quidel pipeline #308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fb5dc98
switch the link for historical flu data from local to midas
5ad17c1
update code for geo mapping using utils
cdab936
change the order of the loops to speed up
3c06160
fixed linter errors
4230e39
updated unit tests
5986293
fixed linter errors
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,49 @@ | ||
"""Contains geographic mapping tools.""" | ||
def geo_map(geo_res, data, map_df): | ||
if geo_res == "county": | ||
return zip_to_county(data, map_df) | ||
if geo_res == "msa": | ||
return zip_to_msa(data, map_df) | ||
if geo_res == "hrr": | ||
return zip_to_hrr(data, map_df) | ||
return zip_to_state(data, map_df) | ||
|
||
def zip_to_msa(data, map_df): | ||
"""Map from zipcode to MSA (along with parent state). | ||
Args: | ||
data: dataframe at the day-zip resolution. | ||
Returns: | ||
tuple, a dataframe at day-msa, with parent state column, and their string keys | ||
from delphi_utils import GeoMapper | ||
|
||
date_col = "timestamp" | ||
data_cols = ['totalTest', 'numUniqueDevices', 'positiveTest', "population"] | ||
gmpr = GeoMapper() # Use geo utils | ||
GEO_KEY_DICT = { | ||
"county": "fips", | ||
"msa": "msa", | ||
"hrr": "hrr", | ||
"state": "state_id" | ||
} | ||
def geo_map(geo_res, df): | ||
data = df.copy() | ||
geo_key = GEO_KEY_DICT[geo_res] | ||
# Add population for each zipcode | ||
data = gmpr.add_population_column(data, "zip") | ||
# zip -> geo_res | ||
data = gmpr.replace_geocode(data, "zip", geo_key, | ||
date_col=date_col, data_cols=data_cols) | ||
if geo_res == "state": | ||
return data | ||
# Add parent state | ||
data = add_parent_state(data, geo_res, geo_key) | ||
return data, geo_key | ||
|
||
def add_parent_state(data, geo_res, geo_key): | ||
""" | ||
# zip -> msa | ||
zip_map = map_df[["zip", "cbsa_id"]].dropna().drop_duplicates() | ||
# forget about the rest of the zips that aren't in MSA | ||
data = data.merge(zip_map, how="left", on="zip").dropna().drop(columns=["zip"], axis=1) | ||
|
||
# msa + parent state | ||
# msa_map has mapping from msa to state, going by the state with the largest | ||
# population (since a msa may span multiple states) | ||
msa_map = map_df[["cbsa_id", "state_id", "population"]] | ||
msa_map = msa_map.groupby(["cbsa_id"]).max().reset_index() | ||
data = data.merge(msa_map, how="left", on="cbsa_id").drop( | ||
columns=["population"]).dropna() | ||
data = data.groupby(["timestamp", "cbsa_id", "state_id"]).sum().reset_index() | ||
data["cbsa_id"] = data["cbsa_id"].apply(lambda x: str(int(x)).zfill(5)) | ||
|
||
return data, "cbsa_id" | ||
|
||
def zip_to_hrr(data, map_df): | ||
"""Map from zipcode to HRR (along with parent state). | ||
Args: | ||
data: dataframe at the day-zip resolution. | ||
Returns: | ||
tuple, a dataframe at day-msa, with parent state column, and their string keys | ||
- map from msa/hrr to state, going by the state with the largest | ||
population (since a msa/hrr may span multiple states) | ||
- map from county to the corresponding state | ||
""" | ||
# zip -> msa | ||
zip_map = map_df[["zip", "hrrnum"]].dropna().drop_duplicates() | ||
# forget about the rest of the zips that aren't in MSA | ||
data = data.merge(zip_map, how="left", on="zip").dropna().drop(columns=["zip"], axis=1) | ||
|
||
# msa + parent state | ||
# msa_map has mapping from msa to state, going by the state with the largest | ||
# population (since a msa may span multiple states) | ||
msa_map = map_df[["hrrnum", "state_id", "population"]] | ||
msa_map = msa_map.groupby(["hrrnum"]).max().reset_index() | ||
data = data.merge(msa_map, how="left", on="hrrnum").drop( | ||
fips_to_state = gmpr._load_crosswalk(from_code="fips", to_code="state") | ||
if geo_res == "county": | ||
mix_map = fips_to_state[["fips", "state_id"]] | ||
else: | ||
fips_to_geo_res = gmpr._load_crosswalk(from_code="fips", to_code=geo_res) | ||
mix_map = fips_to_geo_res[["fips", geo_res]].merge( | ||
fips_to_state[["fips", "state_id"]], | ||
on="fips", | ||
how="inner") | ||
mix_map = gmpr.add_population_column(mix_map, "fips").groupby( | ||
geo_res).max().reset_index().drop( | ||
["fips", "population"], axis = 1) | ||
# Merge the info of parent state to the data | ||
data = data.merge(mix_map, how="left", on=geo_key).drop( | ||
columns=["population"]).dropna() | ||
data = data.groupby(["timestamp", "hrrnum", "state_id"]).sum().reset_index() | ||
data["hrrnum"] = data["hrrnum"].astype(int) | ||
|
||
return data, "hrrnum" | ||
|
||
def zip_to_county(data, map_df): | ||
"""Aggregate zip codes to the county resolution, along with its parent state. | ||
Args: | ||
data: dataframe aggregated to the day-zip resolution | ||
Returns: | ||
dataframe at the day-county resolution and parent state, with their string keys | ||
""" | ||
# zip -> county + parent state (county has unique state) | ||
zip_map = map_df[["fips", "zip", "state_id"]].dropna().drop_duplicates() | ||
data = data.merge(zip_map, how="left", on="zip").drop(columns=["zip"]).dropna() | ||
data = data.groupby(["timestamp", "fips", "state_id"]).sum().reset_index() | ||
data["fips"] = data["fips"].apply(lambda x: str(int(x)).zfill(5)) | ||
|
||
return data, "fips" | ||
|
||
def zip_to_state(data, map_df): | ||
"""Aggregate zip codes to the state resolution. | ||
Args: | ||
data: dataframe aggregated to the day-zip resolution | ||
Returns: | ||
dataframe at the day-state resolution, with the state key | ||
""" | ||
zip_map = map_df[["zip", "state_id"]].dropna().drop_duplicates() | ||
data = data.merge(zip_map, how="left", on="zip").drop( | ||
columns=["zip"]).dropna() | ||
data = data.groupby(["timestamp", "state_id"]).sum().reset_index() | ||
data = data.groupby(["timestamp", geo_key, "state_id"]).sum().reset_index() | ||
return data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from os import listdir, remove | ||
from os import listdir | ||
from os.path import join | ||
|
||
import pandas as pd | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fill in MODULE_NAME