Skip to content

Commit 5b56357

Browse files
committed
switch doctor visits to structured logger
1 parent c7272fc commit 5b56357

File tree

5 files changed

+46
-40
lines changed

5 files changed

+46
-40
lines changed

doctor_visits/delphi_doctor_visits/run.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
"""
77

88
# standard packages
9-
import logging
109
from datetime import datetime, timedelta
1110
from pathlib import Path
1211

12+
from delphi_utils import get_structured_logger
13+
1314
# first party
1415
from .update_sensor import update_sensor, write_to_csv
1516

@@ -37,7 +38,9 @@ def run_module(params):
3738
- "obfuscated_prefix": str, prefix for signal name if write_se is True.
3839
- "parallel": bool, whether to update sensor in parallel.
3940
"""
40-
logging.basicConfig(level=logging.DEBUG)
41+
logger = get_structured_logger(
42+
__name__, filename=params["common"].get("log_filename"),
43+
log_exceptions=params["common"].get("log_exceptions", True))
4144

4245
## get end date from input file
4346
# the filename is expected to be in the format:
@@ -61,30 +64,30 @@ def run_module(params):
6164
startdate_dt = enddate_dt - timedelta(days=n_backfill_days)
6265
enddate = str(enddate_dt.date())
6366
startdate = str(startdate_dt.date())
64-
logging.info("drop date:\t\t%s", dropdate)
65-
logging.info("first sensor date:\t%s", startdate)
66-
logging.info("last sensor date:\t%s", enddate)
67-
logging.info("n_backfill_days:\t%s", n_backfill_days)
68-
logging.info("n_waiting_days:\t%s", n_waiting_days)
67+
logger.info("drop date:\t\t%s", dropdate)
68+
logger.info("first sensor date:\t%s", startdate)
69+
logger.info("last sensor date:\t%s", enddate)
70+
logger.info("n_backfill_days:\t%s", n_backfill_days)
71+
logger.info("n_waiting_days:\t%s", n_waiting_days)
6972

7073
## geographies
7174
geos = ["state", "msa", "hrr", "county", "hhs", "nation"]
7275

7376

7477
## print out other vars
75-
logging.info("outpath:\t\t%s", export_dir)
76-
logging.info("parallel:\t\t%s", params["indicator"]["parallel"])
77-
logging.info("weekday:\t\t%s", params["indicator"]["weekday"])
78-
logging.info("write se:\t\t%s", se)
79-
logging.info("obfuscated prefix:\t%s", prefix)
78+
logger.info("outpath:\t\t%s", export_dir)
79+
logger.info("parallel:\t\t%s", params["indicator"]["parallel"])
80+
logger.info("weekday:\t\t%s", params["indicator"]["weekday"])
81+
logger.info("write se:\t\t%s", se)
82+
logger.info("obfuscated prefix:\t%s", prefix)
8083

8184
## start generating
8285
for geo in geos:
8386
for weekday in params["indicator"]["weekday"]:
8487
if weekday:
85-
logging.info("starting %s, weekday adj", geo)
88+
logger.info("starting %s, weekday adj", geo)
8689
else:
87-
logging.info("starting %s, no adj", geo)
90+
logger.info("starting %s, no adj", geo)
8891
sensor = update_sensor(
8992
filepath=params["indicator"]["input_file"],
9093
startdate=startdate,
@@ -93,19 +96,20 @@ def run_module(params):
9396
geo=geo,
9497
parallel=params["indicator"]["parallel"],
9598
weekday=weekday,
96-
se=params["indicator"]["se"]
99+
se=params["indicator"]["se"],
100+
logger=logger,
97101
)
98102
if sensor is None:
99-
logging.error("No sensors calculated, no output will be produced")
103+
logger.error("No sensors calculated, no output will be produced")
100104
continue
101105
# write out results
102106
out_name = "smoothed_adj_cli" if weekday else "smoothed_cli"
103107
if params["indicator"]["se"]:
104108
assert prefix is not None, "template has no obfuscated prefix"
105109
out_name = prefix + "_" + out_name
106110

107-
write_to_csv(sensor, geo, se, out_name, export_dir)
108-
logging.debug(f"wrote files to {export_dir}")
109-
logging.info("finished %s", geo)
111+
write_to_csv(sensor, geo, se, out_name, logger, export_dir)
112+
logger.debug(f"wrote files to {export_dir}")
113+
logger.info("finished %s", geo)
110114

111-
logging.info("finished all")
115+
logger.info("finished all")

doctor_visits/delphi_doctor_visits/sensor.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
77
"""
88

9-
# standard packages
10-
import logging
11-
129
# third party
1310
import numpy as np
1411
import pandas as pd
@@ -162,7 +159,8 @@ def fit(y_data,
162159
geo_id,
163160
recent_min_visits,
164161
min_recent_obs,
165-
jeffreys):
162+
jeffreys,
163+
logger):
166164
"""Fitting routine.
167165
168166
Args:
@@ -217,7 +215,7 @@ def fit(y_data,
217215
# if all rates are zero, don't bother
218216
if code_vals.sum() == 0:
219217
if jeffreys:
220-
logging.error("p is 0 even though we used Jefferys estimate")
218+
logger.error("p is 0 even though we used Jefferys estimate")
221219
new_rates.append(np.zeros((n_dates,)))
222220
continue
223221

@@ -240,7 +238,7 @@ def fit(y_data,
240238
se[include] = np.sqrt(
241239
np.divide((new_rates[include] * (1 - new_rates[include])), den[include]))
242240

243-
logging.debug(f"{geo_id}: {new_rates[-1]:.3f},[{se[-1]:.3f}]")
241+
logger.debug(f"{geo_id}: {new_rates[-1]:.3f},[{se[-1]:.3f}]")
244242

245243
included_indices = [x for x in final_sensor_idxs if include[x]]
246244

doctor_visits/delphi_doctor_visits/update_sensor.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"""
1010

1111
# standard packages
12-
import logging
1312
from datetime import timedelta
1413
from multiprocessing import Pool, cpu_count
1514

@@ -24,7 +23,7 @@
2423
from .weekday import Weekday
2524

2625

27-
def write_to_csv(output_df: pd.DataFrame, geo_level, se, out_name, output_path="."):
26+
def write_to_csv(output_df: pd.DataFrame, geo_level, se, out_name, logger, output_path="."):
2827
"""Write sensor values to csv.
2928
3029
Args:
@@ -34,7 +33,7 @@ def write_to_csv(output_df: pd.DataFrame, geo_level, se, out_name, output_path="
3433
output_path: outfile path to write the csv (default is current directory)
3534
"""
3635
if se:
37-
logging.info(f"========= WARNING: WRITING SEs TO {out_name} =========")
36+
logger.info(f"========= WARNING: WRITING SEs TO {out_name} =========")
3837

3938
out_n = 0
4039
for d in set(output_df["date"]):
@@ -64,12 +63,12 @@ def write_to_csv(output_df: pd.DataFrame, geo_level, se, out_name, output_path="
6463
outfile.write(
6564
"%s,%f,%s,%s,%s\n" % (geo_id, sensor, "NA", "NA", "NA"))
6665
out_n += 1
67-
logging.debug(f"wrote {out_n} rows for {geo_level}")
66+
logger.debug(f"wrote {out_n} rows for {geo_level}")
6867

6968

7069
def update_sensor(
7170
filepath, startdate, enddate, dropdate, geo, parallel,
72-
weekday, se
71+
weekday, se, logger
7372
):
7473
"""Generate sensor values.
7574
@@ -82,6 +81,7 @@ def update_sensor(
8281
parallel: boolean to run the sensor update in parallel
8382
weekday: boolean to adjust for weekday effects
8483
se: boolean to write out standard errors, if true, use an obfuscated name
84+
logger: the structured logger
8585
"""
8686
# as of 2020-05-11, input file expected to have 10 columns
8787
# id cols: ServiceDate, PatCountyFIPS, PatAgeGroup, Pat HRR ID/Pat HRR Name
@@ -125,7 +125,7 @@ def update_sensor(
125125
(burn_in_dates >= startdate) & (burn_in_dates <= enddate))[0][:len(sensor_dates)]
126126

127127
# handle if we need to adjust by weekday
128-
params = Weekday.get_params(data) if weekday else None
128+
params = Weekday.get_params(data, logger) if weekday else None
129129
if weekday and np.any(np.all(params == 0,axis=1)):
130130
# Weekday correction failed for at least one count type
131131
return None
@@ -155,13 +155,14 @@ def update_sensor(
155155
geo_id,
156156
Config.MIN_RECENT_VISITS,
157157
Config.MIN_RECENT_OBS,
158-
jeffreys
158+
jeffreys,
159+
logger
159160
)
160161
out.append(res)
161162

162163
else:
163164
n_cpu = min(10, cpu_count())
164-
logging.debug(f"starting pool with {n_cpu} workers")
165+
logger.debug(f"starting pool with {n_cpu} workers")
165166

166167
with Pool(n_cpu) as pool:
167168
pool_results = []
@@ -182,6 +183,7 @@ def update_sensor(
182183
Config.MIN_RECENT_VISITS,
183184
Config.MIN_RECENT_OBS,
184185
jeffreys,
186+
logger
185187
),
186188
)
187189
)

doctor_visits/delphi_doctor_visits/weekday.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
Created: 2020-05-06
55
"""
66

7-
# standard packages
8-
import logging
7+
98

109
# third party
1110
import cvxpy as cp
@@ -19,7 +18,7 @@ class Weekday:
1918
"""Class to handle weekday effects."""
2019

2120
@staticmethod
22-
def get_params(data):
21+
def get_params(data, logger):
2322
r"""Correct a signal estimated as numerator/denominator for weekday effects.
2423
2524
The ordinary estimate would be numerator_t/denominator_t for each time point
@@ -98,7 +97,7 @@ def get_params(data):
9897
pass
9998
else:
10099
# Leaving params[i,:] = 0 is equivalent to not performing weekday correction
101-
logging.error("Unable to calculate weekday correction")
100+
logger.error("Unable to calculate weekday correction")
102101

103102
return params
104103

doctor_visits/tests/test_update_sensor.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
"""Tests for update_sensor.py."""
2-
2+
import logging
33
import pandas as pd
44

55
from delphi_doctor_visits.update_sensor import update_sensor
66

7+
TEST_LOGGER = logging.getLogger()
8+
79
class TestUpdateSensor:
810
def test_update_sensor(self):
911
actual = update_sensor(
@@ -14,7 +16,8 @@ def test_update_sensor(self):
1416
geo="state",
1517
parallel=False,
1618
weekday=False,
17-
se=False
19+
se=False,
20+
logger=TEST_LOGGER,
1821
)
1922

2023
comparison = pd.read_csv("./comparison/update_sensor/all.csv", parse_dates=["date"])

0 commit comments

Comments
 (0)