Skip to content

Commit eed8564

Browse files
authored
Merge pull request #195 from cmu-delphi/handle_wip_signals
handling wip signal in python template
2 parents 24e8f6d + c2643e5 commit eed8564

File tree

7 files changed

+123
-7
lines changed

7 files changed

+123
-7
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
Registry for signal names
3+
"""
4+
5+
6+
## example:
7+
# FULL_TIME = "full_time_work_prop"
8+
# PART_TIME = "part_time_work_prop"
9+
# COVIDNET = "covidnet"
10+
#
11+
# SIGNALS = [
12+
# FULL_TIME,
13+
# PART_TIME,
14+
# COVIDNET
15+
# ]
16+
17+
SIGNALS = []
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
Handle signal names.
3+
4+
Author: Vishakha
5+
Created: 2020-08-07
6+
"""
7+
8+
import covidcast
9+
10+
11+
def add_prefix(signal_names, wip_signal, prefix="wip_"):
12+
"""Adds prefix to signal if there is a WIP signal
13+
Parameters
14+
----------
15+
signal_names: List[str]
16+
Names of signals to be exported
17+
prefix : 'wip_'
18+
prefix for new/non public signals
19+
wip_signal : List[str] or bool
20+
a list of wip signals: [], OR
21+
all signals in the registry: True OR
22+
only signals that have never been published: False
23+
Returns
24+
-------
25+
List of signal names
26+
wip/non wip signals for further computation
27+
"""
28+
29+
if wip_signal is True:
30+
return [prefix + signal for signal in signal_names]
31+
if isinstance(wip_signal, list):
32+
make_wip = set(wip_signal)
33+
return[
34+
prefix + signal if signal in make_wip else signal
35+
for signal in signal_names
36+
]
37+
if wip_signal in {False, ""}:
38+
return [
39+
signal if public_signal(signal)
40+
else prefix + signal
41+
for signal in signal_names
42+
]
43+
raise ValueError("Supply True | False or '' or [] | list()")
44+
45+
46+
def public_signal(signal_):
47+
"""Checks if the signal name is already public using COVIDcast
48+
Parameters
49+
----------
50+
signal_ : str
51+
Name of the signal
52+
Returns
53+
-------
54+
bool
55+
True if the signal is present
56+
False if the signal is not present
57+
"""
58+
epidata_df = covidcast.metadata()
59+
for index in range(len(epidata_df)):
60+
if epidata_df['signal'][index] == signal_:
61+
return True
62+
return False

_template_python/delphi_NAME/run.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@
44
This module should contain a function called `run_module`, that is executed
55
when the module is run with `python -m MODULE_NAME`.
66
"""
7-
import numpy as np
8-
import pandas as pd
97
from delphi_utils import read_params
10-
8+
from .handle_wip_signal import add_prefix
9+
from .constants import SIGNALS
1110

1211
def run_module():
13-
12+
"""
13+
Calls the method for handling the wip signals
14+
Returns
15+
-------
16+
prints the updated signal names
17+
"""
1418
params = read_params()
19+
wip_signal = params["wip_signal"]
20+
signal_names = add_prefix(SIGNALS, wip_signal, prefix="wip_")
21+
print(signal_names)

_template_python/params.json.template

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"static_file_dir": "./static",
33
"export_dir": "./receiving",
4-
"cache_dir": "./cache"
4+
"cache_dir": "./cache",
5+
"wip_signal": ""
56
}

_template_python/setup.py

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

1314
setup(
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"static_file_dir": "../static",
33
"export_dir": "./receiving",
4-
"cache_dir": "./cache"
4+
"cache_dir": "./cache",
5+
"wip_signal": ""
56
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import unittest
2+
from delphi_NAME.handle_wip_signal import add_prefix
3+
from delphi_NAME.run import SIGNALS
4+
from delphi_utils import read_params
5+
6+
7+
8+
def test_handle_wip_signal():
9+
# Test wip_signal = True (all signals should receive prefix)
10+
signal_names = add_prefix(SIGNALS, True, prefix="wip_")
11+
assert all(s.startswith("wip_") for s in signal_names)
12+
# Test wip_signal = list (only listed signals should receive prefix)
13+
signal_names = add_prefix(SIGNALS, [SIGNALS[0]], prefix="wip_")
14+
assert signal_names[0].startswith("wip_")
15+
assert all(not s.startswith("wip_") for s in signal_names[1:])
16+
# Test wip_signal = False (only unpublished signals should receive prefix)
17+
signal_names = add_prefix(["xyzzy", SIGNALS[0]], False, prefix="wip_")
18+
assert signal_names[0].startswith("wip_")
19+
assert all(not s.startswith("wip_") for s in signal_names[1:])
20+
21+
22+
class MyTestCase(unittest.TestCase):
23+
pass
24+
25+
26+
if __name__ == '__main__':
27+
unittest.main()

0 commit comments

Comments
 (0)