Skip to content

Commit 47bfd62

Browse files
authored
Merge pull request #722 from cmu-delphi/changehc-fix-geo-constants
Changehc: get region count from GeoMapper instead of config.Constants
2 parents 127c976 + 5d83734 commit 47bfd62

File tree

5 files changed

+18
-31
lines changed

5 files changed

+18
-31
lines changed

_delphi_utils_python/delphi_utils/geomap.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,17 @@ def fips_to_megacounty(
528528
data = data.reset_index().groupby([date_col, mega_col]).sum()
529529
return data.reset_index()
530530

531+
def as_mapper_name(self, geo_type, state="state_id"):
532+
"""
533+
Return the mapper equivalent of a region type.
534+
535+
Human-readable names like 'county' will return their mapper equivalents ('fips').
536+
"""
537+
if geo_type == "state":
538+
return state
539+
if geo_type == "county":
540+
return "fips"
541+
return geo_type
531542
def get_geo_values(self, geo_type):
532543
"""
533544
Return a set of all values for a given geography type.

changehc/delphi_changehc/config.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,3 @@ class Config:
5454
7 # maximum number of days used to average a backfill correction
5555
)
5656
MIN_CUM_VISITS = 500 # need to observe at least 500 counts before averaging
57-
58-
59-
class Constants:
60-
"""
61-
Contains the maximum number of geo units for each geo type.
62-
63-
Used for sanity checks
64-
"""
65-
66-
# number of counties in usa, including megacounties
67-
NUM_COUNTIES = 3141 + 52
68-
NUM_HRRS = 308
69-
NUM_MSAS = 392 + 52 # MSA + States
70-
NUM_STATES = 52 # including DC and PR
71-
NUM_NATIONS = 1
72-
NUM_HHSS = 10
73-
74-
MAX_GEO = {"county": NUM_COUNTIES,
75-
"hrr": NUM_HRRS,
76-
"msa": NUM_MSAS,
77-
"state": NUM_STATES,
78-
"nation": NUM_NATIONS,
79-
"hhs": NUM_HHSS}

changehc/delphi_changehc/update_sensor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from delphi_utils import GeoMapper, read_params, add_prefix
1515

1616
# first party
17-
from .config import Config, Constants
17+
from .config import Config
1818
from .constants import SMOOTHED, SMOOTHED_ADJ, SMOOTHED_CLI, SMOOTHED_ADJ_CLI, NA
1919
from .sensor import CHCSensor
2020
from .weekday import Weekday
@@ -164,7 +164,7 @@ def geo_reindex(self, data):
164164
# for each location, fill in all missing dates with 0 values
165165
multiindex = pd.MultiIndex.from_product((unique_geo_ids, self.fit_dates),
166166
names=[geo, Config.DATE_COL])
167-
assert (len(multiindex) <= (Constants.MAX_GEO[geo] * len(self.fit_dates))
167+
assert (len(multiindex) <= (len(gmpr.get_geo_values(gmpr.as_mapper_name(geo))) * len(self.fit_dates))
168168
), "more loc-date pairs than maximum number of geographies x number of dates"
169169
# fill dataframe with missing dates using 0
170170
data_frame = data_frame.reindex(multiindex, fill_value=0)

changehc/tests/test_load_data.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
import pytest
33

44
# third party
5-
from delphi_utils import read_params
5+
from delphi_utils import read_params, GeoMapper
66
import pandas as pd
77

88
# first party
9-
from delphi_changehc.config import Config, Constants
9+
from delphi_changehc.config import Config
1010
from delphi_changehc.load_data import *
1111

1212
CONFIG = Config()
13-
CONSTANTS = Constants()
1413
PARAMS = read_params()
1514
COVID_FILEPATH = PARAMS["input_covid_file"]
1615
DENOM_FILEPATH = PARAMS["input_denom_file"]
@@ -24,6 +23,7 @@ class TestLoadData:
2423
Config.COVID_COLS, Config.COVID_DTYPES, Config.COVID_COL)
2524
combined_data = load_combined_data(DENOM_FILEPATH, COVID_FILEPATH, DROP_DATE,
2625
"fips")
26+
gmpr = GeoMapper()
2727

2828
def test_base_unit(self):
2929
with pytest.raises(AssertionError):
@@ -78,7 +78,7 @@ def test_fips_values(self):
7878
self.combined_data]:
7979
assert (
8080
len(data.index.get_level_values(
81-
'fips').unique()) <= CONSTANTS.NUM_COUNTIES
81+
'fips').unique()) <= len(self.gmpr.get_geo_values("fips"))
8282
)
8383

8484
def test_combined_fips_values(self):

changehc/tests/test_update_sensor.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
from delphi_utils import read_params
1616

1717
# first party
18-
from delphi_changehc.config import Config, Constants
18+
from delphi_changehc.config import Config
1919
from delphi_changehc.update_sensor import write_to_csv, CHCSensorUpdator
2020

2121
CONFIG = Config()
22-
CONSTANTS = Constants()
2322
PARAMS = read_params()
2423
COVID_FILEPATH = PARAMS["input_covid_file"]
2524
DENOM_FILEPATH = PARAMS["input_denom_file"]

0 commit comments

Comments
 (0)