Skip to content

Commit 7d6007a

Browse files
add test of centroid assignment to cost benefit unit tests
1 parent 18d6849 commit 7d6007a

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

climada/engine/test/test_cost_benefit.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
)
3636
from climada.entity.disc_rates import DiscRates
3737
from climada.entity.entity_def import Entity
38+
from climada.entity.measures import Measure
39+
from climada.entity.measures.base import LOGGER as ILOG
3840
from climada.hazard.base import Hazard
3941
from climada.test import get_test_file
4042
from climada.util.api_client import Client
@@ -1119,6 +1121,58 @@ def test_remove_measure(self):
11191121
self.assertEqual(len(cost_ben.cost_ben_ratio), 3)
11201122
self.assertEqual(len(cost_ben.benefit), 3)
11211123

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+
11221176

11231177
class TestCalc(unittest.TestCase):
11241178
"""Test calc"""

0 commit comments

Comments
 (0)