diff --git a/doctor_visits/delphi_doctor_visits/run.py b/doctor_visits/delphi_doctor_visits/run.py index f3417d283..65f30ed69 100644 --- a/doctor_visits/delphi_doctor_visits/run.py +++ b/doctor_visits/delphi_doctor_visits/run.py @@ -95,6 +95,9 @@ def run_module(params): weekday=weekday, se=params["indicator"]["se"] ) + if sensor is None: + logging.error("No sensors calculated, no output will be produced") + continue # write out results out_name = "smoothed_adj_cli" if weekday else "smoothed_cli" if params["indicator"]["se"]: diff --git a/doctor_visits/delphi_doctor_visits/update_sensor.py b/doctor_visits/delphi_doctor_visits/update_sensor.py index 01e1647fe..931ec3afa 100644 --- a/doctor_visits/delphi_doctor_visits/update_sensor.py +++ b/doctor_visits/delphi_doctor_visits/update_sensor.py @@ -126,6 +126,9 @@ def update_sensor( # handle if we need to adjust by weekday params = Weekday.get_params(data) if weekday else None + if weekday and np.any(np.all(params == 0,axis=1)): + # Weekday correction failed for at least one count type + return None # handle explicitly if we need to use Jeffreys estimate for binomial proportions jeffreys = bool(se) diff --git a/doctor_visits/delphi_doctor_visits/weekday.py b/doctor_visits/delphi_doctor_visits/weekday.py index c0085f6e0..86e5278b2 100644 --- a/doctor_visits/delphi_doctor_visits/weekday.py +++ b/doctor_visits/delphi_doctor_visits/weekday.py @@ -4,6 +4,9 @@ Created: 2020-05-06 """ +# standard packages +import logging + # third party import cvxpy as cp import numpy as np @@ -82,15 +85,20 @@ def get_params(data): penalty = ( lmbda * cp.norm(cp.diff(b[6:], 3), 1) / (X.shape[0] - 2) ) # L-1 Norm of third differences, rewards smoothness - try: - prob = cp.Problem(cp.Minimize(-ll + lmbda * penalty)) - _ = prob.solve() - except: - # If the magnitude of the objective function is too large, an error is - # thrown; Rescale the objective function - prob = cp.Problem(cp.Minimize((-ll + lmbda * penalty) / 1e5)) - _ = prob.solve() - params[i, :] = b.value + scales = [1, 1e5, 1e10, 1e15] + for scale in scales: + try: + prob = cp.Problem(cp.Minimize((-ll + lmbda * penalty) / scale)) + _ = prob.solve() + params[i,:] = b.value + break + except: + # If the magnitude of the objective function is too large, an error is + # thrown; Rescale the objective function by going through loop + pass + else: + # Leaving params[i,:] = 0 is equivalent to not performing weekday correction + logging.error("Unable to calculate weekday correction") return params