Skip to content

Commit 331c82d

Browse files
authored
Merge pull request #1527 from cmu-delphi/ndefries/cpr-generate-full-history-new-signals
Generate full CPR history for new signals
2 parents 672892c + 8e63fdc commit 331c82d

File tree

2 files changed

+46
-3
lines changed
  • dsew_community_profile/delphi_dsew_community_profile

2 files changed

+46
-3
lines changed

dsew_community_profile/delphi_dsew_community_profile/pull.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,12 @@ def fetch_listing(params):
312312
el for el in listing
313313
if start_date <= el['publish_date'] <= end_date
314314
]
315-
# reference date is guaranteed to be before publish date, so we can trim
315+
# reference date is guaranteed to be on or before publish date, so we can trim
316316
# reports that are too early
317317
if 'export_start_date' in params['indicator']:
318318
listing = [
319319
el for el in listing
320-
if params['indicator']['export_start_date'] < el['publish_date']
320+
if params['indicator']['export_start_date'] <= el['publish_date']
321321
]
322322
# can't do the same for export_end_date
323323
return listing

dsew_community_profile/delphi_dsew_community_profile/run.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
from delphi_utils import get_structured_logger
2020
from delphi_utils.export import create_export_csv
2121
import pandas as pd
22+
import covidcast
2223

23-
from .constants import make_signal_name
24+
from .constants import make_signal_name, SIGNALS
2425
from .pull import fetch_new_reports
2526

2627

@@ -71,6 +72,48 @@ def replace_date_param(p):
7172
if len(dates)>0:
7273
run_stats.append((max(dates), len(dates)))
7374

75+
## If any requested signal is not in metadata, generate it for all dates.
76+
#
77+
# Only do so if params.reports is set to "new". If set to "all", the
78+
# previous fetch_new_reports + CSV loop will already have generated the full
79+
# history for new signals. If params.reports is set to a specific date
80+
# range, that request overrides automated backfill.
81+
if params['indicator']['reports'] == 'new':
82+
# Fetch metadata to check how recent signals are
83+
metadata = covidcast.metadata()
84+
sensor_names = {
85+
SIGNALS[key][name_field]: key
86+
for key in params["indicator"]["export_signals"]
87+
for name_field in ["api_name", "api_prop_name"]
88+
if name_field in SIGNALS[key].keys()
89+
}
90+
91+
# Filter to only those we currently want to produce
92+
cpr_metadata = metadata[(metadata.data_source == "dsew-cpr") &
93+
(metadata.signal.isin(sensor_names.keys()))]
94+
95+
new_signals = set(sensor_names.keys()).difference(set(cpr_metadata.signal))
96+
if new_signals:
97+
# If any signal not in metadata yet, we need to backfill its full
98+
# history.
99+
params['indicator']['reports'] = 'all'
100+
params['indicator']['export_signals'] = {sensor_names[key] for key in new_signals}
101+
102+
dfs = fetch_new_reports(params, logger)
103+
for key, df in dfs.items():
104+
(geo, sig, is_prop) = key
105+
if sig not in params["indicator"]["export_signals"]:
106+
continue
107+
dates = create_export_csv(
108+
df,
109+
params['common']['export_dir'],
110+
geo,
111+
make_signal_name(sig, is_prop),
112+
**export_params
113+
)
114+
if len(dates)>0:
115+
run_stats.append((max(dates), len(dates)))
116+
74117
## log this indicator run
75118
elapsed_time_in_seconds = round(time.time() - start_time, 2)
76119
min_max_date = run_stats and min(s[0] for s in run_stats)

0 commit comments

Comments
 (0)