Skip to content

Commit 2d377ec

Browse files
authored
Merge pull request #207 from cmu-delphi/handle_wip_signal_cdc
cdc_covidnet : standardizing signal names
2 parents 31d882b + fc1b1e3 commit 2d377ec

File tree

8 files changed

+33197
-6
lines changed

8 files changed

+33197
-6
lines changed

cdc_covidnet/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ should be raised and they should be manually checked (or better, fixed).
5454

5555
Unit tests are also included in the module. To execute these, run the following
5656
command from this directory:
57-
57+
(Note: the following command requires python 3.8, having any version less than 3.8 might
58+
fail some test cases. Please install it before running.)
5859
```
5960
(cd tests && ../env/bin/pytest --cov=delphi_cdc_covidnet --cov-report=term-missing)
6061
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""
2+
Registry for signal names
3+
"""
4+
COVIDNET = "covidnet"
5+
SIGNALS = [COVIDNET]

cdc_covidnet/delphi_cdc_covidnet/update_sensor.py

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
import numpy as np
1313
import pandas as pd
1414

15+
from delphi_utils import read_params
16+
import covidcast
1517
from .api_config import APIConfig
1618
from .covidnet import CovidNet
1719
from .geo_maps import GeoMaps
20+
from .constants import SIGNALS
1821

1922
def write_to_csv(data: pd.DataFrame, out_name: str, output_path: str):
2023
"""
@@ -93,7 +96,62 @@ def update_sensor(
9396
hosp_df["sample_size"] = np.nan
9497

9598
# Write results
96-
out_name = "wip_covidnet"
97-
write_to_csv(hosp_df, out_name, output_path)
99+
signals = add_prefix(SIGNALS, wip_signal=read_params()["wip_signal"], prefix="wip_")
100+
for signal in signals:
101+
write_to_csv(hosp_df, signal, output_path)
98102

99103
return hosp_df
104+
105+
106+
def add_prefix(signal_names, wip_signal, prefix):
107+
"""Adds prefix to signal if there is a WIP signal
108+
Parameters
109+
----------
110+
signal_names: List[str]
111+
Names of signals to be exported
112+
prefix : 'wip_'
113+
prefix for new/non public signals
114+
wip_signal : List[str] or bool
115+
a list of wip signals: [], OR
116+
all signals in the registry: True OR
117+
only signals that have never been published: False
118+
Returns
119+
-------
120+
List of signal names
121+
wip/non wip signals for further computation
122+
"""
123+
124+
if wip_signal is True:
125+
return [prefix + signal for signal in signal_names]
126+
if isinstance(wip_signal, list):
127+
make_wip = set(wip_signal)
128+
return [
129+
(prefix if signal in make_wip else "") + signal
130+
for signal in signal_names
131+
]
132+
if wip_signal in {False, ""}:
133+
return [
134+
signal if public_signal(signal)
135+
else prefix + signal
136+
for signal in signal_names
137+
]
138+
raise ValueError("Supply True | False or '' or [] | list()")
139+
140+
141+
def public_signal(signal_):
142+
"""Checks if the signal name is already public using COVIDcast
143+
Parameters
144+
----------
145+
signal_ : str
146+
Name of the signal
147+
Returns
148+
-------
149+
bool
150+
True if the signal is present
151+
False if the signal is not present
152+
"""
153+
epidata_df = covidcast.metadata()
154+
for index in range(len(epidata_df)):
155+
if epidata_df['signal'][index] == signal_:
156+
return True
157+
return False

cdc_covidnet/params.json.template

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"cache_dir": "./cache",
55
"start_date": "2020-03-07",
66
"end_date": "",
7-
"parallel": false
7+
"parallel": false,
8+
"wip_signal": ""
89
}

cdc_covidnet/setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"pytest-cov",
99
"pylint",
1010
"delphi-utils",
11-
"requests"
11+
"requests",
12+
"covidcast"
1213
]
1314

1415
setup(

cdc_covidnet/tests/params.json.template

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"cache_dir": "./cache",
55
"start_date": "2020-03-07",
66
"end_date": "",
7-
"parallel": true
7+
"parallel": true,
8+
"wip_signal": ""
89
}

cdc_covidnet/tests/static/02_20_uszips.csv

Lines changed: 33100 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import unittest
2+
from delphi_cdc_covidnet.update_sensor import add_prefix
3+
from delphi_cdc_covidnet.constants import *
4+
5+
def test_handle_wip_signal():
6+
# Test wip_signal = True, add prefix to all signals
7+
signal_names = add_prefix(SIGNALS, True, prefix="wip_")
8+
assert all(s.startswith("wip_") for s in signal_names)
9+
# Test wip_signal = list, add prefix to listed signals
10+
signal_names = add_prefix(SIGNALS, [SIGNALS[0]], prefix="wip_")
11+
assert signal_names[0].startswith("wip_")
12+
assert all(not s.startswith("wip_") for s in signal_names[1:])
13+
# Test wip_signal = False, add prefix to unpublished signals
14+
signal_names = add_prefix(["xyzzy", SIGNALS[0]], False, prefix="wip_")
15+
assert signal_names[0].startswith("wip_")
16+
assert all(s.startswith("wip_") for s in signal_names[1:])
17+
18+
19+
class MyTestCase(unittest.TestCase):
20+
pass
21+
22+
23+
if __name__ == '__main__':
24+
unittest.main()

0 commit comments

Comments
 (0)