Skip to content

Commit 4b7b5e0

Browse files
authored
Merge pull request #1199 from cmu-delphi/pop_prop_signal
HHS data: population-proportionate variants
2 parents 3634193 + b4fc197 commit 4b7b5e0

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

hhs_hosp/delphi_hhs/constants.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33

44
CONFIRMED = "confirmed_admissions_covid_1d"
55
SUM_CONF_SUSP = "sum_confirmed_suspected_admissions_covid_1d"
6-
6+
CONFIRMED_PROP = "confirmed_admissions_covid_1d_prop"
7+
SUM_CONF_SUSP_PROP = "sum_confirmed_suspected_admissions_covid_1d_prop"
78

89
SIGNALS = [
910
CONFIRMED,
10-
SUM_CONF_SUSP
11+
SUM_CONF_SUSP,
12+
CONFIRMED_PROP,
13+
SUM_CONF_SUSP_PROP
1114
]
1215

1316
GEOS = [

hhs_hosp/delphi_hhs/run.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
from .constants import SIGNALS, GEOS, SMOOTHERS, CONFIRMED, SUM_CONF_SUSP
1919

20-
2120
def _date_to_int(d):
2221
"""Return a date object as a yyyymmdd int."""
2322
return int(d.strftime("%Y%m%d"))
@@ -98,12 +97,13 @@ def run_module(params):
9897
all_columns = pd.concat(dfs)
9998
geo_mapper = GeoMapper()
10099
stats = []
101-
102100
for sensor, smoother, geo in product(SIGNALS, SMOOTHERS, GEOS):
103101
df = geo_mapper.add_geocode(make_signal(all_columns, sensor),
104102
"state_id",
105103
"state_code",
106104
from_col="state")
105+
if sensor.endswith("_prop"):
106+
df=pop_proportion(df, geo_mapper)
107107
df = make_geo(df, geo, geo_mapper)
108108
df = smooth_values(df, smoother[0])
109109
if df.empty:
@@ -139,6 +139,12 @@ def smooth_values(df, smoother):
139139
)
140140
return df
141141

142+
def pop_proportion(df,geo_mapper):
143+
"""Get the population-proportionate variants as the dataframe val."""
144+
pop_val=geo_mapper.add_population_column(df, "state_code")
145+
df["val"]=round(df["val"]/pop_val["population"]*100000, 7)
146+
pop_val.drop("population", axis=1, inplace=True)
147+
return df
142148

143149
def make_geo(state, geo, geo_mapper):
144150
"""Transform incoming geo (state) to another geo."""
@@ -158,15 +164,15 @@ def make_signal(all_columns, sig):
158164
"""Generate column sums according to signal name."""
159165
assert sig in SIGNALS, f"Unexpected signal name '{sig}';" + \
160166
" familiar names are '{', '.join(SIGNALS)}'"
161-
if sig == CONFIRMED:
167+
if sig.startswith(CONFIRMED):
162168
df = pd.DataFrame({
163169
"state": all_columns.state.apply(str.lower),
164170
"timestamp":int_date_to_previous_day_datetime(all_columns.date),
165171
"val": \
166172
all_columns.previous_day_admission_adult_covid_confirmed + \
167173
all_columns.previous_day_admission_pediatric_covid_confirmed
168174
})
169-
elif sig == SUM_CONF_SUSP:
175+
elif sig.startswith(SUM_CONF_SUSP):
170176
df = pd.DataFrame({
171177
"state": all_columns.state.apply(str.lower),
172178
"timestamp":int_date_to_previous_day_datetime(all_columns.date),

hhs_hosp/tests/test_run.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import os
66

77
from delphi_hhs.run import _date_to_int, int_date_to_previous_day_datetime, generate_date_ranges, \
8-
make_signal, make_geo, run_module
9-
from delphi_hhs.constants import CONFIRMED, SUM_CONF_SUSP, SMOOTHERS, GEOS, SIGNALS
8+
make_signal, make_geo, run_module, pop_proportion
9+
from delphi_hhs.constants import CONFIRMED, SUM_CONF_SUSP, SMOOTHERS, GEOS, SIGNALS, CONFIRMED_PROP, SUM_CONF_SUSP_PROP
1010
from delphi_utils.geomap import GeoMapper
1111
from freezegun import freeze_time
1212
import numpy as np
@@ -66,17 +66,49 @@ def test_make_signal():
6666
'val': [5.],
6767
})
6868
pd.testing.assert_frame_equal(expected_confirmed, make_signal(data, CONFIRMED))
69+
pd.testing.assert_frame_equal(expected_confirmed, make_signal(data, CONFIRMED_PROP))
6970

7071
expected_sum = pd.DataFrame({
7172
'state': ['na'],
7273
'timestamp': [datetime(year=2020, month=1, day=1)],
7374
'val': [15.],
7475
})
7576
pd.testing.assert_frame_equal(expected_sum, make_signal(data, SUM_CONF_SUSP))
77+
pd.testing.assert_frame_equal(expected_sum, make_signal(data, SUM_CONF_SUSP_PROP))
7678

7779
with pytest.raises(Exception):
7880
make_signal(data, "zig")
7981

82+
def test_pop_proportion():
83+
geo_mapper = GeoMapper()
84+
test_df = pd.DataFrame({
85+
'state': ['PA'],
86+
'state_code': [42],
87+
'timestamp': [datetime(year=2020, month=1, day=1)],
88+
'val': [15.],})
89+
pd.testing.assert_frame_equal(
90+
pop_proportion(test_df, geo_mapper),
91+
pd.DataFrame({
92+
'state': ['PA'],
93+
'state_code': [42],
94+
'timestamp': [datetime(year=2020, month=1, day=1)],
95+
'val': [0.1171693],})
96+
)
97+
98+
test_df= pd.DataFrame({
99+
'state': ['WV'],
100+
'state_code': [54],
101+
'timestamp': [datetime(year=2020, month=1, day=1)],
102+
'val': [150.],})
103+
104+
pd.testing.assert_frame_equal(
105+
pop_proportion(test_df, geo_mapper),
106+
pd.DataFrame({
107+
'state': ['WV'],
108+
'state_code': [54],
109+
'timestamp': [datetime(year=2020, month=1, day=1)],
110+
'val': [8.3698491],})
111+
)
80112

81113
def test_make_geo():
82114
"""Check that geographies transform correctly."""

0 commit comments

Comments
 (0)