Skip to content

Commit d3a5642

Browse files
committed
Returns nans for eai_exp and aai_agg when exposures is empty
1 parent c10a4b3 commit d3a5642

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

climada/engine/impact_calc.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,13 @@ def _return_empty(self, save_mat):
273273
Impact or ImpactForecast
274274
Empty impact object with correct array sizes.
275275
"""
276+
if isinstance(self.hazard, HazardForecast):
277+
eai_exp = np.nan * np.ones(self.n_exp_pnt)
278+
aai_agg = np.nan
279+
else:
280+
eai_exp = np.zeros(self.n_exp_pnt)
281+
aai_agg = 0.0
276282
at_event = np.zeros(self.n_events)
277-
eai_exp = np.zeros(self.n_exp_pnt)
278-
aai_agg = 0.0
279283
if save_mat:
280284
imp_mat = sparse.csr_matrix(
281285
(self.n_events, self.n_exp_pnt), dtype=np.float64

climada/engine/test/test_impact_calc.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@
5151

5252

5353
@pytest.fixture(autouse=True)
54-
def exposure_fixture():
55-
n_exp = 50
54+
def exposure_fixture(n_exp=50):
5655
lats = np.linspace(-10, 10, n_exp)
5756
lons = np.linspace(-10, 10, n_exp)
5857
data = gpd.GeoDataFrame(
@@ -749,6 +748,15 @@ def test_impact_forecast_blocked_nonsense_attrs(
749748
assert np.all(np.isnan(impact.eai_exp))
750749
assert impact.eai_exp.shape == (len(exposure_fixture.gdf),)
751750

751+
# test that aai_agg and eai_exp are also nan when 0-size exp
752+
empty_exp = exposure_fixture(n_exp=0)
753+
impact_empty = ImpactCalc(
754+
exposure_fixture, impact_func_set_fixture, hazard_forecast_fixture
755+
).impact(assign_centroids=True, save_mat=True)
756+
assert np.isnan(impact_empty.aai_agg)
757+
assert np.all(np.isnan(impact_empty.eai_exp))
758+
assert impact_empty.eai_exp.shape == (len(empty_exp.gdf),)
759+
752760

753761
class TestImpactMatrixCalc(unittest.TestCase):
754762
"""Verify the computation of the impact matrix"""

0 commit comments

Comments
 (0)