Skip to content

Commit 95cc7a8

Browse files
authored
Merge pull request #813 from cmu-delphi/nowcast-nodata
Add better no data handling to nowcast
2 parents 48b4357 + 5925b01 commit 95cc7a8

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

nowcast/delphi_nowcast/data_containers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,15 @@ def add_data(self,
4242
@property
4343
def dates(self) -> list:
4444
"""Check if there is no stored data in the class."""
45+
if not self.data:
46+
raise ValueError("No data")
4547
return list(self.data.keys())
4648

4749
@property
4850
def values(self) -> list:
4951
"""Check if there is no stored data in the class."""
52+
if not self.data:
53+
raise ValueError("No data")
5054
return list(self.data.values())
5155

5256
def get_data_range(self,

nowcast/delphi_nowcast/sensorization/regression_model.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ def compute_regression_sensor(day: date,
3939
Float value of sensor on `date`
4040
"""
4141
previous_day = day - timedelta(1)
42-
first_day = max(min(covariate.dates), min(response.dates))
4342
try:
43+
first_day = max(min(covariate.dates), min(response.dates))
4444
train_Y = response.get_data_range(first_day, previous_day)
4545
train_covariates = covariate.get_data_range(first_day, previous_day)
4646
except ValueError:
4747
return np.nan
4848
if not train_Y:
4949
return np.nan
50-
train_Y, train_covariates = zip( # only get pairs where both are not nan
51-
*[(i, j) for i, j in zip(train_Y, train_covariates) if not (np.isnan(i) or np.isnan(j))]
52-
)
50+
non_nan_values = [(i, j) for i, j in zip(train_Y, train_covariates)
51+
if not (np.isnan(i) or np.isnan(j))]
52+
train_Y, train_covariates = zip(*non_nan_values) if non_nan_values else ([], [])
5353
if len(train_Y) < MIN_SAMPLE_SIZE:
5454
print("insufficient observations")
5555
return np.nan

nowcast/tests/sensorization/test_regression_model.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,8 @@ def test_compute_regression_sensor_out_of_range(self):
6868
date(2020, 1, 7): 42}
6969
)
7070
assert np.isnan(compute_regression_sensor(date(2020, 1, 16), test_covariate, test_response, False))
71+
72+
def test_compute_regression_sensor_no_data(self):
73+
test_covariate = LocationSeries()
74+
test_response = LocationSeries()
75+
assert np.isnan(compute_regression_sensor(date(2020, 1, 16), test_covariate, test_response, False))

nowcast/tests/test_data_containers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,10 @@ def test_get_data_range_invalid_impute(self):
3737
test_ls = LocationSeries(data={date(2020, 1, 1): 7, date(2020, 1, 2): np.nan, date(2020, 1, 3): 9})
3838
with pytest.raises(ValueError, match="Invalid imputation method. Must be None or 'mean'"):
3939
test_ls.get_data_range(date(2020, 1, 1), date(2020, 1, 3), "fakeimpute")
40+
41+
def test_no_data(self):
42+
test_ls = LocationSeries()
43+
with pytest.raises(ValueError, match="No data"):
44+
test_ls.dates
45+
with pytest.raises(ValueError, match="No data"):
46+
test_ls.values

0 commit comments

Comments
 (0)