|
35 | 35 | ) |
36 | 36 | from climada.entity.disc_rates import DiscRates |
37 | 37 | from climada.entity.entity_def import Entity |
| 38 | +from climada.entity.measures import Measure |
| 39 | +from climada.entity.measures.base import LOGGER as ILOG |
38 | 40 | from climada.hazard.base import Hazard |
39 | 41 | from climada.test import get_test_file |
40 | 42 | from climada.util.api_client import Client |
@@ -1119,6 +1121,58 @@ def test_remove_measure(self): |
1119 | 1121 | self.assertEqual(len(cost_ben.cost_ben_ratio), 3) |
1120 | 1122 | self.assertEqual(len(cost_ben.benefit), 3) |
1121 | 1123 |
|
| 1124 | + def test_measure_exposure_no_centroids(self): |
| 1125 | + """Test centroids assigned if no centroids were assigned in Measure Exposures object""" |
| 1126 | + hazard = Hazard.from_hdf5(HAZ_TEST_TC) |
| 1127 | + entity = Entity.from_excel(ENT_DEMO_TODAY) |
| 1128 | + entity.check() |
| 1129 | + entity.exposures.ref_year = 2018 |
| 1130 | + |
| 1131 | + # test that warning is not raised when centroid are assigned |
| 1132 | + with self.assertLogs(ILOG, level="WARNING") as logs: |
| 1133 | + cost_ben = CostBenefit() |
| 1134 | + cost_ben.calc( |
| 1135 | + hazard, |
| 1136 | + entity, |
| 1137 | + future_year=2040, |
| 1138 | + risk_func=risk_aai_agg, |
| 1139 | + imp_time_depen=None, |
| 1140 | + save_imp=True, |
| 1141 | + ) |
| 1142 | + ILOG.warning("Dummy warning") |
| 1143 | + self.assertEqual( |
| 1144 | + ["WARNING:climada.entity.measures.base:Dummy warning"], |
| 1145 | + logs.output, |
| 1146 | + ) |
| 1147 | + |
| 1148 | + # add measure with exposure without assigned centroids |
| 1149 | + exp_no_assigned_centroids = entity.exposures.copy() |
| 1150 | + exp_no_assigned_centroids.data = entity.exposures.data.drop( |
| 1151 | + columns=["centr_TC"] |
| 1152 | + ) |
| 1153 | + entity.measures.append( |
| 1154 | + Measure( |
| 1155 | + name="no centroids", |
| 1156 | + haz_type="TC", |
| 1157 | + exposures_set=exp_no_assigned_centroids, |
| 1158 | + ) |
| 1159 | + ) |
| 1160 | + |
| 1161 | + # test if warning is raised |
| 1162 | + with self.assertLogs(ILOG, level="WARNING") as logs: |
| 1163 | + cost_ben = CostBenefit() |
| 1164 | + cost_ben.calc( |
| 1165 | + hazard, |
| 1166 | + entity, |
| 1167 | + future_year=2040, |
| 1168 | + risk_func=risk_aai_agg, |
| 1169 | + imp_time_depen=None, |
| 1170 | + save_imp=True, |
| 1171 | + ) |
| 1172 | + self.assertIn( |
| 1173 | + "No assigned hazard centroids in exposure object after", logs.output[0] |
| 1174 | + ) |
| 1175 | + |
1122 | 1176 |
|
1123 | 1177 | class TestCalc(unittest.TestCase): |
1124 | 1178 | """Test calc""" |
|
0 commit comments