|
1 | 1 | """Tests for calibration module""" |
| 2 | + |
| 3 | +import unittest |
| 4 | +from unittest.mock import create_autospec |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +import numpy.testing as npt |
| 8 | +import pandas as pd |
| 9 | +from scipy.sparse import csr_matrix |
| 10 | +from shapely.geometry import Point |
| 11 | + |
| 12 | +from climada.entity import Exposures, ImpactFuncSet |
| 13 | +from climada.hazard import Hazard, Centroids |
| 14 | + |
| 15 | +from ..impact_func import Input, ScipyMinimizeOptimizer |
| 16 | + |
| 17 | + |
| 18 | +def hazard(): |
| 19 | + """Create a dummy hazard instance""" |
| 20 | + lat = [1, 2] |
| 21 | + lon = [0, 1] |
| 22 | + centroids = Centroids.from_lat_lon(lat=lat, lon=lon) |
| 23 | + event_id = np.array([1, 3, 10]) |
| 24 | + intensity = csr_matrix([[1, 1], [2, 2], [3, 3]]) |
| 25 | + return Hazard(event_id=event_id, centroids=centroids, intensity=intensity) |
| 26 | + |
| 27 | + |
| 28 | +def exposure(): |
| 29 | + """Create a dummy exposure instance""" |
| 30 | + return Exposures(data=dict(longitude=[0, 1, 100], latitude=[1, 2, 50])) |
| 31 | + |
| 32 | + |
| 33 | +class TestInputPostInit(unittest.TestCase): |
| 34 | + """Test the post_init dunder method of Input""" |
| 35 | + |
| 36 | + def setUp(self): |
| 37 | + """Create default input instance""" |
| 38 | + # Create the hazard instance |
| 39 | + self.hazard = hazard() |
| 40 | + |
| 41 | + # Create the exposure instance |
| 42 | + self.exposure = exposure() |
| 43 | + |
| 44 | + # Create some data |
| 45 | + self.data_events = [10, 3] |
| 46 | + self.data = pd.DataFrame(data={"a": [1, 2]}, index=self.data_events) |
| 47 | + |
| 48 | + # Create dummy funcs |
| 49 | + self.cost_func = lambda impact, data: 1.0 |
| 50 | + self.impact_func_gen = lambda **kwargs: ImpactFuncSet() |
| 51 | + |
| 52 | + def test_post_init_calls(self): |
| 53 | + """Test if post_init calls stuff correctly using mocks""" |
| 54 | + # Create mocks |
| 55 | + hazard_mock_1 = create_autospec(Hazard, instance=True) |
| 56 | + hazard_mock_2 = create_autospec(Hazard, instance=True) |
| 57 | + exposure_mock = create_autospec(Exposures, instance=True) |
| 58 | + |
| 59 | + # Make first hazard mock return another instance |
| 60 | + hazard_mock_1.select.return_value = hazard_mock_2 |
| 61 | + |
| 62 | + # Create input |
| 63 | + input = Input( |
| 64 | + hazard=hazard_mock_1, |
| 65 | + exposure=exposure_mock, |
| 66 | + data=self.data, |
| 67 | + cost_func=self.cost_func, |
| 68 | + impact_func_gen=self.impact_func_gen, |
| 69 | + ) |
| 70 | + |
| 71 | + # Query checks |
| 72 | + hazard_mock_1.select.assert_called_once_with(event_id=self.data_events) |
| 73 | + self.assertNotEqual(input.hazard, hazard_mock_1) |
| 74 | + self.assertEqual(input.hazard, hazard_mock_2) |
| 75 | + exposure_mock.assign_centroids.assert_called_once_with(hazard_mock_2) |
| 76 | + |
| 77 | + def test_post_init(self): |
| 78 | + """Test if post_init results in a sensible hazard and exposure""" |
| 79 | + # Create input |
| 80 | + input = Input( |
| 81 | + hazard=self.hazard, |
| 82 | + exposure=self.exposure, |
| 83 | + data=self.data, |
| 84 | + cost_func=self.cost_func, |
| 85 | + impact_func_gen=self.impact_func_gen, |
| 86 | + ) |
| 87 | + |
| 88 | + # Check hazard and exposure |
| 89 | + npt.assert_array_equal(input.hazard.event_id, self.data.index) |
| 90 | + self.assertIn("centr_", input.exposure.gdf) |
| 91 | + npt.assert_array_equal(input.exposure.gdf["centr_"], [0, 1, -1]) |
| 92 | + |
| 93 | + |
| 94 | +class TestScipyMinimizeOptimizer(unittest.TestCase): |
| 95 | + """Tests for the optimizer based on scipy.optimize.minimize""" |
| 96 | + |
| 97 | + def setUp(self): |
| 98 | + """Mock the input and create the optimizer""" |
| 99 | + self.input = create_autospec(Input, instance=True) |
| 100 | + self.optimizer = ScipyMinimizeOptimizer(self.input) |
| 101 | + |
| 102 | + def test_kwargs_to_impact_func_gen(self): |
| 103 | + """Test the _kwargs_to_impact_func_gen method""" |
| 104 | + # _param_names is empty in the beginning |
| 105 | + x = np.array([1, 2, 3]) |
| 106 | + self.assertDictEqual(self.optimizer._kwargs_to_impact_func_gen(x), {}) |
| 107 | + |
| 108 | + # Now populate it and try again |
| 109 | + self.optimizer._param_names = ["x_2", "x_1", "x_3"] |
| 110 | + result = {"x_2": 1, "x_1": 2, "x_3": 3} |
| 111 | + self.assertDictEqual(self.optimizer._kwargs_to_impact_func_gen(x), result) |
| 112 | + |
| 113 | + # Other arguments are ignored |
| 114 | + self.assertDictEqual( |
| 115 | + self.optimizer._kwargs_to_impact_func_gen(x, x + 3), result |
| 116 | + ) |
| 117 | + |
| 118 | + # Array is flattened, iterator stops |
| 119 | + self.assertDictEqual( |
| 120 | + self.optimizer._kwargs_to_impact_func_gen(np.array([[1, 2], [3, 4]])), |
| 121 | + result, |
| 122 | + ) |
| 123 | + |
| 124 | + def test_select_by_keys(self): |
| 125 | + """Test the _select_by_keys method""" |
| 126 | + param_names = ["a", "b", "c", "d"] |
| 127 | + mapping = dict(zip(param_names, [1, "2", (1, 2)])) |
| 128 | + |
| 129 | + # _param_names is empty in the beginning |
| 130 | + self.assertListEqual(self.optimizer._select_by_param_names(mapping), []) |
| 131 | + |
| 132 | + # Set _param_names |
| 133 | + self.optimizer._param_names = param_names |
| 134 | + |
| 135 | + # Check result |
| 136 | + self.assertListEqual( |
| 137 | + self.optimizer._select_by_param_names(mapping), [1, "2", (1, 2), None] |
| 138 | + ) |
0 commit comments