Skip to content

Commit a1e8ce5

Browse files
committed
add signal module to delphi_utils
1 parent 2904c1d commit a1e8ce5

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

_delphi_utils_python/delphi_utils/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
from .export import create_export_csv
99
from .utils import read_params
1010
from .geomap import GeoMapper
11+
from .signal import add_prefix, public_signal
1112

1213
__version__ = "0.1.0"
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""Functions for understanding and creating signal names."""
2+
import covidcast
3+
4+
def add_prefix(signal_names, wip_signal, prefix: str):
5+
"""Adds prefix to signal if there is a WIP signal
6+
Parameters
7+
----------
8+
signal_names: List[str]
9+
Names of signals to be exported
10+
prefix : 'wip_'
11+
prefix for new/non public signals
12+
wip_signal : List[str] or bool
13+
a list of wip signals: [], OR
14+
all signals in the registry: True OR
15+
only signals that have never been published: False
16+
Returns
17+
-------
18+
List of signal names
19+
wip/non wip signals for further computation
20+
"""
21+
22+
if wip_signal is True:
23+
return [prefix + signal for signal in signal_names]
24+
if isinstance(wip_signal, list):
25+
make_wip = set(wip_signal)
26+
return [
27+
(prefix if signal in make_wip else "") + signal
28+
for signal in signal_names
29+
]
30+
if wip_signal in {False, ""}:
31+
return [
32+
signal if public_signal(signal)
33+
else prefix + signal
34+
for signal in signal_names
35+
]
36+
raise ValueError("Supply True | False or '' or [] | list()")
37+
38+
39+
def public_signal(signal):
40+
"""Checks if the signal name is already public using COVIDcast
41+
Parameters
42+
----------
43+
signal : str
44+
Name of the signal
45+
Returns
46+
-------
47+
bool
48+
True if the signal is present
49+
False if the signal is not present
50+
"""
51+
epidata_df = covidcast.metadata()
52+
for index in range(len(epidata_df)):
53+
if epidata_df['signal'][index] == signal:
54+
return True
55+
return False
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""Tests for delphi_utils.signal."""
2+
3+
from delphi_utils.signal import add_prefix, public_signal
4+
5+
SIGNALS = ["median_home_dwell_time", "completely_home_prop", "full_time_work_prop"]
6+
7+
class TestSignal:
8+
"""Tests for signal.py."""
9+
def test_handle_wip_signal(self):
10+
"""Tests that `add_prefix()` derives work-in-progress signals."""
11+
# Test wip_signal = True
12+
signal_names = SIGNALS
13+
signal_names = add_prefix(SIGNALS, True, prefix="wip_")
14+
assert all(s.startswith("wip_") for s in signal_names)
15+
# Test wip_signal = list
16+
signal_names = add_prefix(SIGNALS, [SIGNALS[0]], prefix="wip_")
17+
assert signal_names[0].startswith("wip_")
18+
assert all(not s.startswith("wip_") for s in signal_names[1:])
19+
# Test wip_signal = False
20+
signal_names = add_prefix(["xyzzy", SIGNALS[0]], False, prefix="wip_")
21+
assert signal_names[0].startswith("wip_")
22+
assert all(not s.startswith("wip_") for s in signal_names[1:])
23+
24+
def test_public_signal(self):
25+
"""Tests that `public_signal()` identifies public vs. private signals."""
26+
assert not public_signal("junk")
27+
assert public_signal("covid_ag_smoothed_pct_positive")

0 commit comments

Comments
 (0)