Skip to content

Commit 3634193

Browse files
authored
Merge pull request #1200 from cmu-delphi/krivard/update-template
Update Python template.
2 parents c6cb60b + 6beecb4 commit 3634193

File tree

5 files changed

+97
-21
lines changed

5 files changed

+97
-21
lines changed

_template_python/REVIEW.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ outputs should be given for non-trivial functions
1414

1515
**Structure**
1616

17-
- [ ] code should use 4 spaces for indentation; other style decisions are
18-
flexible, but be consistent within a module
17+
- [ ] code should pass lint checks (`make lint`)
1918
- [ ] any required metadata files are checked into the repository and placed
2019
within the directory `static`
2120
- [ ] any intermediate files that are created and stored by the module should
@@ -29,11 +28,11 @@ included in this template file
2928

3029
**Testing**
3130

32-
- [ ] module can be installed in a new virtual environment
33-
- [ ] pylint with the default `.pylint` settings run over the module produces
34-
minimal warnings; warnings that do exist have been confirmed as false positives
31+
- [ ] module can be installed in a new virtual environment (`make install`)
3532
- [ ] reasonably high level of unit test coverage covering all of the main logic
3633
of the code (e.g., missing coverage for raised errors that do not currently seem
3734
possible to reach are okay; missing coverage for options that will be needed are
3835
not)
39-
- [ ] all unit tests run without errors
36+
- [ ] all unit tests run without errors (`make test`)
37+
- [ ] indicator directory has been added to GitHub CI
38+
(`covidcast-indicators/.github/workflows/python-ci.yml`)

_template_python/delphi_NAME/constants.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
"""
2-
Registry for signal names
2+
Registry for variations
33
"""
44

5+
## example:
6+
#GEOS = [
7+
# "nation",
8+
# "hhs",
9+
# "state",
10+
# "hrr",
11+
# "msa",
12+
# "county"
13+
#]
14+
15+
GEOS = []
516

617
## example:
718
# FULL_TIME = "full_time_work_prop"
@@ -15,3 +26,11 @@
1526
# ]
1627

1728
SIGNALS = []
29+
30+
## example:
31+
#SMOOTHERS = [
32+
# (Smoother("identity", impute_method=None), ""),
33+
# (Smoother("moving_average", window_length=7), "_7dav"),
34+
#]
35+
36+
SMOOTHERS = []

_template_python/delphi_NAME/run.py

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@
1414
unpublished signals are. See `delphi_utils.add_prefix()`
1515
- Any other indicator-specific settings
1616
"""
17-
from delphi_utils import add_prefix
18-
from .constants import SIGNALS
17+
from datetime import timedelta
18+
import pandas as pd
19+
import time
20+
21+
from delphi_utils.export import create_export_csv
22+
from delphi_utils.geomap import GeoMapper
23+
from delphi_utils import get_structured_logger
24+
25+
from .constants import GEOS, SIGNALS, SMOOTHERS
1926

2027
def run_module(params):
2128
"""
@@ -25,11 +32,46 @@ def run_module(params):
2532
--------
2633
params: Dict[str, Any]
2734
Nested dictionary of parameters.
28-
29-
Returns
30-
-------
31-
prints the updated signal names
3235
"""
33-
wip_signal = params["indicator"]["wip_signal"]
34-
signal_names = add_prefix(SIGNALS, wip_signal, prefix="wip_")
35-
print(signal_names)
36+
start_time = time.time()
37+
logger = get_structured_logger(
38+
__name__, filename=params["common"].get("log_filename"),
39+
log_exceptions=params["common"].get("log_exceptions", True))
40+
mapper = GeoMapper()
41+
run_stats = []
42+
## build the base version of the signal at the most detailed geo level you can get.
43+
## compute stuff here or farm out to another function or file
44+
all_data = pd.DataFrame(columns=["timestamp", "val", "zip", "sample_size", "se"])
45+
## aggregate & smooth
46+
## TODO: add num/prop variations if needed
47+
for sensor, smoother, geo in product(SIGNALS, SMOOTHERS, GEOS):
48+
df = mapper.replace_geocode(
49+
all_data, "zip", geo,
50+
new_col="geo_id",
51+
date_col="timestamp")
52+
## TODO: recompute sample_size, se here if not NA
53+
df["val"] = df[["geo_id", "val"]].groupby("geo_id")["val"].transform(
54+
smoother[0].smooth
55+
)
56+
sensor_name = sensor + smoother[1] ## TODO: +num/prop variation if used
57+
# don't export first 6 days for smoothed signals since they'll be nan.
58+
start_date = min(df.timestamp) + timedelta(6) if smoother[1] else min(df.timestamp)
59+
dates = create_export_csv(
60+
df,
61+
params["common"]["export_dir"],
62+
geo,
63+
sensor_name,
64+
start_date=start_date)
65+
if len(dates) > 0:
66+
run_stats.append((max(dates), len(dates)))
67+
## log this indicator run
68+
elapsed_time_in_seconds = round(time.time() - start_time, 2)
69+
min_max_date = run_stats and min(s[0] for s in run_stats)
70+
csv_export_count = sum(s[-1] for s in run_stats)
71+
max_lag_in_days = min_max_date and (datetime.now() - min_max_date).days
72+
formatted_min_max_date = min_max_date and min_max_date.strftime("%Y-%m-%d")
73+
logger.info("Completed indicator run",
74+
elapsed_time_in_seconds = elapsed_time_in_seconds,
75+
csv_export_count = csv_export_count,
76+
max_lag_in_days = max_lag_in_days,
77+
oldest_final_export_date = formatted_min_max_date)

_template_python/params.json.template

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
{
2-
"static_file_dir": "./static",
3-
"export_dir": "./receiving",
4-
"cache_dir": "./cache",
5-
"wip_signal": ""
2+
"common": {
3+
"export_dir": "./receiving",
4+
"log_filename": "NAME.log"
5+
},
6+
"validation": {
7+
"common": {
8+
"data_source": "NAME",
9+
"span_length": 14,
10+
"min_expected_lag": {"all": "1"},
11+
"max_expected_lag": {"all": "3"},
12+
"dry_run": true,
13+
"suppressed_errors": []
14+
},
15+
"static": {
16+
"minimum_sample_size": 0,
17+
"missing_se_allowed": true,
18+
"missing_sample_size_allowed": true
19+
},
20+
"dynamic": {}
21+
}
622
}

_template_python/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
classifiers=[
2424
"Development Status :: 5 - Production/Stable",
2525
"Intended Audience :: Developers",
26-
"Programming Language :: Python :: 3.7",
26+
"Programming Language :: Python :: 3.8",
2727
],
2828
packages=find_packages(),
2929
)

0 commit comments

Comments
 (0)