Skip to content

Commit 0e5a557

Browse files
committed
Remove hazard event selection from calibrate.Input
1 parent ea0eb47 commit 0e5a557

File tree

3 files changed

+25
-63
lines changed

3 files changed

+25
-63
lines changed

climada/test/test_util_calibrate.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def setUp(self) -> None:
2626
self.hazard.event_name = ["event"] * len(self.hazard.event_id)
2727
self.exposure = exposure()
2828
self.events = [10, 1]
29+
self.hazard = self.hazard.select(event_id=self.events)
2930
self.data = pd.DataFrame(
3031
data={"a": [3, 1], "b": [0.2, 0.01]}, index=self.events
3132
)

climada/util/calibrate/base.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,10 @@ class Input:
4949
impact_calc_kwds : Mapping (str, Any), optional
5050
Keyword arguments to :py:meth:`climada.engine.impact_calc.ImpactCalc.impact`.
5151
Defaults to ``{"assign_centroids": False}`` (by default, centroids are assigned
52-
here via the ``align`` parameter, to avoid assigning them each time the impact is
53-
calculated).
54-
align : bool, optional
55-
Match event IDs from ``hazard`` and ``data``, and assign the centroids from
56-
``hazard`` to ``exposure``. Defaults to ``True``.
52+
here via the ``assign_centroids`` parameter, to avoid assigning them each time
53+
the impact is calculated).
54+
assign_centroids : bool, optional
55+
If ``True`` (default), assign the hazard centroids to the exposure.
5756
"""
5857

5958
hazard: Hazard
@@ -66,18 +65,11 @@ class Input:
6665
impact_calc_kwds: Mapping[str, Any] = field(
6766
default_factory=lambda: {"assign_centroids": False}
6867
)
69-
align: InitVar[bool] = True
68+
assign_centroids: InitVar[bool] = True
7069

71-
def __post_init__(self, align):
70+
def __post_init__(self, assign_centroids):
7271
"""Prepare input data"""
73-
if align:
74-
event_diff = np.setdiff1d(self.data.index, self.hazard.event_id)
75-
if event_diff.size > 0:
76-
raise RuntimeError(
77-
"Event IDs in 'data' do not match event IDs in 'hazard': \n"
78-
f"{event_diff}"
79-
)
80-
self.hazard = self.hazard.select(event_id=self.data.index.tolist())
72+
if assign_centroids:
8173
self.exposure.assign_centroids(self.hazard)
8274

8375

climada/util/calibrate/test/test_calibrate.py

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -57,36 +57,33 @@ def setUp(self):
5757
self.cost_func = lambda impact, data: 1.0
5858
self.impact_func_gen = lambda **kwargs: ImpactFuncSet()
5959

60-
@patch("climada.util.calibrate.impact_func.np.setdiff1d")
61-
def test_post_init_calls(self, setdiff1d_mock):
60+
def test_post_init_calls(self):
6261
"""Test if post_init calls stuff correctly using mocks"""
6362
# Create mocks
64-
hazard_mock_1 = create_autospec(Hazard())
65-
event_id = [10]
66-
hazard_mock_1.event_id = event_id
67-
hazard_mock_2 = create_autospec(Hazard())
6863
exposure_mock = create_autospec(Exposures())
69-
setdiff1d_mock.return_value = np.array([])
7064

71-
# Make first hazard mock return another instance
72-
hazard_mock_1.select.return_value = hazard_mock_2
73-
74-
# Create input
65+
# Default
7566
input = Input(
76-
hazard=hazard_mock_1,
67+
hazard=self.hazard,
7768
exposure=exposure_mock,
7869
data=self.data,
7970
cost_func=self.cost_func,
8071
impact_func_gen=self.impact_func_gen,
8172
)
73+
exposure_mock.assign_centroids.assert_called_once_with(self.hazard)
74+
exposure_mock.reset_mock()
8275

83-
# Query checks
84-
npt.assert_array_equal(setdiff1d_mock.call_args.args[0], self.data_events)
85-
npt.assert_array_equal(setdiff1d_mock.call_args.args[1], event_id)
86-
hazard_mock_1.select.assert_called_once_with(event_id=self.data_events)
87-
self.assertNotEqual(input.hazard, hazard_mock_1)
88-
self.assertEqual(input.hazard, hazard_mock_2)
89-
exposure_mock.assign_centroids.assert_called_once_with(hazard_mock_2)
76+
# Default
77+
input = Input(
78+
hazard=self.hazard,
79+
exposure=exposure_mock,
80+
data=self.data,
81+
cost_func=self.cost_func,
82+
impact_func_gen=self.impact_func_gen,
83+
assign_centroids=False
84+
)
85+
exposure_mock.assign_centroids.assert_not_called()
86+
9087

9188
def test_post_init(self):
9289
"""Test if post_init results in a sensible hazard and exposure"""
@@ -100,37 +97,9 @@ def test_post_init(self):
10097
)
10198

10299
# Check hazard and exposure
103-
npt.assert_array_equal(input.hazard.event_id, self.data.index)
104100
self.assertIn("centr_", input.exposure.gdf)
105101
npt.assert_array_equal(input.exposure.gdf["centr_"], [0, 1, -1])
106102

107-
def test_non_matching_events(self):
108-
"""Test if non-matching events result in errors"""
109-
data = pd.DataFrame(data={"a": [1, 2, 3]}, index=[9, 3, 12])
110-
input_kwargs = {
111-
"hazard": self.hazard,
112-
"exposure": self.exposure,
113-
"data": data,
114-
"cost_func": self.cost_func,
115-
"impact_func_gen": self.impact_func_gen,
116-
"align": False,
117-
}
118-
119-
# No error without alignment
120-
Input(**input_kwargs)
121-
122-
# Error with alignment
123-
input_kwargs.update(align=True)
124-
with self.assertRaises(RuntimeError) as cm:
125-
Input(**input_kwargs)
126-
127-
self.assertIn(
128-
"Event IDs in 'data' do not match event IDs in 'hazard'", str(cm.exception)
129-
)
130-
self.assertIn("9", str(cm.exception))
131-
self.assertIn("12", str(cm.exception))
132-
self.assertNotIn("3", str(cm.exception))
133-
134103

135104
class TestOptimizer(unittest.TestCase):
136105
"""Base class for testing optimizers. Creates an input mock"""
@@ -144,7 +113,7 @@ def setUp(self):
144113
data=create_autospec(pd.DataFrame, instance=True),
145114
cost_func=MagicMock(),
146115
impact_func_gen=MagicMock(),
147-
align=False,
116+
assign_centroids=False,
148117
)
149118
)
150119

0 commit comments

Comments
 (0)