Skip to content

Commit cc92195

Browse files
committed
handling wip signal in python template
1 parent 3fffd54 commit cc92195

File tree

4 files changed

+104
-4
lines changed

4 files changed

+104
-4
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""
2+
Handle signal names.
3+
4+
Author: Vishakha
5+
Created: 2020-08-07
6+
"""
7+
8+
from delphi_epidata import Epidata
9+
10+
11+
def add_prefix(signal_names, wip_signal, prefix: str):
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+
Either takes a list of wip signals: [], OR
21+
incorporated all signals in the registry: True OR
22+
no signals: False
23+
Returns
24+
-------
25+
List of signal names
26+
wip/non wip signals for further computation
27+
"""
28+
if wip_signal in ("", False):
29+
return signal_names
30+
elif wip_signal and isinstance(wip_signal, bool):
31+
return [
32+
(prefix + signal) if public_signal(signal)
33+
else signal
34+
for signal in signal_names
35+
]
36+
elif isinstance(wip_signal, list):
37+
for signal in wip_signal:
38+
if public_signal(signal):
39+
signal_names.append(prefix + signal)
40+
signal_names.remove(signal)
41+
return signal_names
42+
else:
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 Epidata
48+
Parameters
49+
----------
50+
signal_ : str
51+
Name of the signal
52+
Returns
53+
-------
54+
bool
55+
True if the signal is not present
56+
False if the signal is present
57+
"""
58+
epidata_df = Epidata.covidcast_meta()
59+
for index in range(len(epidata_df['epidata'])):
60+
if 'signal' in epidata_df['epidata'][index]:
61+
if epidata_df['epidata'][index]['signal'] == signal_:
62+
return False
63+
return True

_template_python/delphi_NAME/run.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,25 @@
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
8+
from .handle_wip_signal import add_prefix
109

10+
# Sample signals
11+
SIGNALS = ["full_time_work_prop",
12+
"covidnet",
13+
"part_time_work_prop",
14+
"completely_home_prop"
15+
]
1116

12-
def run_module():
1317

18+
def run_module():
19+
"""
20+
Calls the method for handling the wip signals
21+
Returns
22+
-------
23+
prints the updated signal names
24+
"""
1425
params = read_params()
26+
wip_signal = params["wip_signal"]
27+
signal_names = add_prefix(SIGNALS, wip_signal, prefix="wip_")
28+
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
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
wip_signal = read_params()["wip_signal"]
7+
8+
9+
def test_handle_wip_signal():
10+
assert isinstance(wip_signal, (list, bool)) or wip_signal == "", "Supply True | False or "" or [] | list()"
11+
if isinstance(wip_signal, list):
12+
assert set(wip_signal).issubset(set(SIGNALS)), "signal in params don't belong in the registry"
13+
updated_signal_names = add_prefix(SIGNALS, wip_signal, prefix='wip_')
14+
assert (len(updated_signal_names) >= len(SIGNALS))
15+
16+
17+
class MyTestCase(unittest.TestCase):
18+
pass
19+
20+
21+
if __name__ == '__main__':
22+
unittest.main()

0 commit comments

Comments
 (0)