Skip to content

Commit 9f343cb

Browse files
restructured unit and integration tests
1 parent e44b9f0 commit 9f343cb

File tree

4 files changed

+234
-225
lines changed

4 files changed

+234
-225
lines changed

climada/engine/test/test_impact.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,90 @@ def test_local_exceedance_impact(self):
528528
])
529529
)
530530

531+
def test_local_exceedance_impact_methods(self):
532+
"""Test local exceedance impacts per return period with different methods"""
533+
impact = dummy_impact()
534+
impact.coord_exp = np.array([np.arange(4), np.arange(4)]).T
535+
impact.imp_mat = sparse.csr_matrix(
536+
np.array([
537+
[0, 0, 0, 1e1],
538+
[0, 0, 1e1, 1e2],
539+
[0, 1e3, 1e3, 1e3]
540+
])
541+
)
542+
impact.frequency = np.array([1., .1, .01])
543+
# first centroid has impacts None with cum frequencies None
544+
# second centroid has impacts 1e3 with frequencies .01, cum freq .01
545+
# third centroid has impacts 1e1, 1e3 with cum frequencies .1, .01, cum freq .11, .01
546+
# fourth centroid has impacts 1e1, 1e2, 1e3 with cum frequencies 1., .1, .01, cum freq 1.11, .11, .01
547+
# testing at frequencies .001, .033, 10.
548+
549+
# test stepfunction
550+
impact_stats, _, _ = impact.local_exceedance_impact(
551+
return_periods=(1000, 30, .1), method='stepfunction')
552+
np.testing.assert_allclose(
553+
impact_stats.values[:,1:].astype(float),
554+
np.array([
555+
[0, 0, 0],
556+
[1e3, 0, 0],
557+
[1e3, 1e1, 0],
558+
[1e3, 1e2, 0]
559+
])
560+
)
561+
562+
# test log log extrapolation
563+
impact_stats, _, _ = impact.local_exceedance_impact(
564+
return_periods=(1000, 30, .1), method = "extrapolate")
565+
np.testing.assert_allclose(
566+
impact_stats.values[:,1:].astype(float),
567+
np.array([
568+
[0, 0, 0],
569+
[1e3, 0, 0],
570+
[1e5, 1e2, 1e-3],
571+
[1e4, 300, 1]
572+
]),
573+
rtol=0.8)
574+
575+
# test log log interpolation and extrapolation with constant
576+
impact_stats, _, _ = impact.local_exceedance_impact(
577+
return_periods=(1000, 30, .1), method = "extrapolate_constant")
578+
np.testing.assert_allclose(
579+
impact_stats.values[:,1:].astype(float),
580+
np.array([
581+
[0, 0, 0],
582+
[1e3, 0, 0],
583+
[1e3, 1e2, 0],
584+
[1e3, 300, 0]
585+
]),
586+
rtol=0.8)
587+
588+
# test log log interpolation and no extrapolation
589+
impact_stats, _, _ = impact.local_exceedance_impact(
590+
return_periods=(1000, 30, .1))
591+
np.testing.assert_allclose(
592+
impact_stats.values[:,1:].astype(float),
593+
np.array([
594+
[np.nan, np.nan, np.nan],
595+
[np.nan, np.nan, np.nan],
596+
[np.nan, 1e2, np.nan],
597+
[np.nan, 300, np.nan]
598+
]),
599+
rtol=0.8)
600+
601+
# test lin lin interpolation with no extrapolation
602+
impact_stats, _, _ = impact.local_exceedance_impact(
603+
return_periods=(1000, 30, .1), method = "extrapolate_constant",
604+
log_frequency=False, log_impact=False)
605+
np.testing.assert_allclose(
606+
impact_stats.values[:,1:].astype(float),
607+
np.array([
608+
[0, 0, 0],
609+
[1e3, 0, 0],
610+
[1e3, 750, 0],
611+
[1e3, 750, 0]
612+
]),
613+
rtol=0.8)
614+
531615
class TestImpactReg(unittest.TestCase):
532616
"""Test impact aggregation per aggregation region or admin 0"""
533617

climada/hazard/test/test_base.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,84 @@ def test_local_exceedance_intensity(self):
10581058
[0., 0., 1.]
10591059
])
10601060
)
1061+
1062+
def test_local_exceedance_intensity_methods(self):
1063+
"""Test local exceedance frequencies with different methods"""
1064+
haz = dummy_hazard()
1065+
haz.intensity = sparse.csr_matrix([
1066+
[0, 0, 1e1],
1067+
[0.2, 1e1, 1e2],
1068+
[1e3, 1e3, 1e3]
1069+
])
1070+
haz.intensity_thres = .5
1071+
haz.frequency = np.array([1., .1, .01])
1072+
return_period = (1000, 30, .1)
1073+
# first centroid has intensities 1e3 with frequencies .01, cum freq .01
1074+
# second centroid has intensities 1e1, 1e3 with cum frequencies .1, .01, cum freq .11, .01
1075+
# third centroid has intensities 1e1, 1e2, 1e3 with cum frequencies 1., .1, .01, cum freq 1.11, .11, .01
1076+
# testing at frequencies .001, .033, 10.
1077+
1078+
# test stepfunction
1079+
inten_stats, _, _ = haz.local_exceedance_intensity(
1080+
return_periods=(1000, 30, .1), method='stepfunction')
1081+
np.testing.assert_allclose(
1082+
inten_stats.values[:,1:].astype(float),
1083+
np.array([
1084+
[1e3, 0, 0],
1085+
[1e3, 1e1, 0],
1086+
[1e3, 1e2, 0]
1087+
])
1088+
)
1089+
1090+
# test log log extrapolation
1091+
inten_stats, _, _ = haz.local_exceedance_intensity(
1092+
return_periods=(1000, 30, .1), method = "extrapolate")
1093+
np.testing.assert_allclose(
1094+
inten_stats.values[:,1:].astype(float),
1095+
np.array([
1096+
[1e3, 0, 0],
1097+
[1e5, 1e2, 1e-3],
1098+
[1e4, 300, 1]
1099+
]),
1100+
rtol=0.8)
1101+
1102+
# test log log interpolation and extrapolation with constant
1103+
inten_stats, _, _ = haz.local_exceedance_intensity(
1104+
return_periods=(1000, 30, .1), method='extrapolate_constant')
1105+
np.testing.assert_allclose(
1106+
inten_stats.values[:,1:].astype(float),
1107+
np.array([
1108+
[1e3, 0, 0],
1109+
[1e3, 1e2, 0],
1110+
[1e3, 300, 0]
1111+
]),
1112+
rtol=0.8)
1113+
1114+
# test log log interpolation and no extrapolation
1115+
inten_stats, _, _ = haz.local_exceedance_intensity(
1116+
return_periods=(1000, 30, .1))
1117+
np.testing.assert_allclose(
1118+
inten_stats.values[:,1:].astype(float),
1119+
np.array([
1120+
[np.nan, np.nan, np.nan],
1121+
[np.nan, 1e2, np.nan],
1122+
[np.nan, 300, np.nan]
1123+
]),
1124+
rtol=0.8)
1125+
1126+
# test lin lin interpolation without extrapolation
1127+
inten_stats, _, _ = haz.local_exceedance_intensity(
1128+
return_periods=(1000, 30, .1), log_frequency=False, log_intensity=False,
1129+
method='extrapolate_constant')
1130+
np.testing.assert_allclose(
1131+
inten_stats.values[:,1:].astype(float),
1132+
np.array([
1133+
[1e3, 0, 0],
1134+
[1e3, 750, 0],
1135+
[1e3, 750, 0]
1136+
]),
1137+
rtol=0.8)
1138+
10611139

10621140
def test_local_return_period(self):
10631141
"""Test local return periods with lin lin interpolation"""
@@ -1084,6 +1162,68 @@ def test_local_return_period(self):
10841162
])
10851163
)
10861164

1165+
def test_local_return_period_methods(self):
1166+
"""Test local return periods different methods"""
1167+
haz = dummy_hazard()
1168+
haz.intensity = sparse.csr_matrix([
1169+
[0, 0, 1e1],
1170+
[0.0, 1e1, 1e2],
1171+
[1e3, 1e3, 1e3]
1172+
])
1173+
haz.intensity_thres = .5
1174+
haz.frequency = np.array([1., .1, .01])
1175+
# first centroid has intensities 1e3 with frequencies .01, cum freq .01
1176+
# second centroid has intensities 1e1, 1e3 with cum frequencies .1, .01, cum freq .11, .01
1177+
# third centroid has intensities 1e1, 1e2, 1e3 with cum frequencies 1., .1, .01, cum freq 1.11, .11, .01
1178+
# testing at intensities .1, 300, 1e4
1179+
1180+
# test stepfunction
1181+
return_stats, _, _ = haz.local_return_period(
1182+
threshold_intensities=(.1, 300, 1e5), method='stepfunction')
1183+
np.testing.assert_allclose(
1184+
return_stats.values[:,1:].astype(float),
1185+
np.array([
1186+
[100, 100, np.nan],
1187+
[1/.11, 100, np.nan],
1188+
[1/1.11, 100, np.nan]
1189+
])
1190+
)
1191+
1192+
# test log log extrapolation
1193+
return_stats, _, _ = haz.local_return_period(
1194+
threshold_intensities=(.1, 300, 1e5), method="extrapolate")
1195+
np.testing.assert_allclose(
1196+
return_stats.values[:,1:].astype(float),
1197+
np.array([
1198+
[100, 100, np.nan],
1199+
[1., 30, 1e3],
1200+
[.01, 30, 1e4]
1201+
]),
1202+
rtol=0.8)
1203+
1204+
# test log log interpolation and extrapolation with constant
1205+
return_stats, _, _ = haz.local_return_period(
1206+
threshold_intensities=(.1, 300, 1e5), method='extrapolate_constant')
1207+
np.testing.assert_allclose(
1208+
return_stats.values[:,1:].astype(float),
1209+
np.array([
1210+
[100, 100, np.nan],
1211+
[1/.11, 30, np.nan],
1212+
[1/1.11, 30, np.nan]
1213+
]),
1214+
rtol=0.8)
1215+
1216+
# test log log interpolation and no extrapolation
1217+
return_stats, _, _ = haz.local_return_period(
1218+
threshold_intensities=(.1, 300, 1e5))
1219+
np.testing.assert_allclose(
1220+
return_stats.values[:,1:].astype(float),
1221+
np.array([
1222+
[np.nan, np.nan, np.nan],
1223+
[np.nan, 30, np.nan],
1224+
[np.nan, 30, np.nan]
1225+
]),
1226+
rtol=0.8)
10871227

10881228
class TestYearset(unittest.TestCase):
10891229
"""Test return period statistics"""

climada/test/test_engine.py

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -288,98 +288,10 @@ def test_calc_sensitivity_pass(self):
288288
len(attr), len(unc_data.param_labels) * (4 + 4 + 4)
289289
)
290290

291-
class TestImpactRPCals(unittest.TestCase):
292-
"""Test the return periods and exceedance impact calculation of Impact objects"""
293-
294-
def test_local_exceedance_impact_methods(self):
295-
"""Test local exceedance impacts per return period with different methods"""
296-
impact = dummy_impact()
297-
impact.coord_exp = np.array([np.arange(4), np.arange(4)]).T
298-
impact.imp_mat = sp.sparse.csr_matrix(
299-
np.array([
300-
[0, 0, 0, 1e1],
301-
[0, 0, 1e1, 1e2],
302-
[0, 1e3, 1e3, 1e3]
303-
])
304-
)
305-
impact.frequency = np.array([1., .1, .01])
306-
# first centroid has impacts None with cum frequencies None
307-
# second centroid has impacts 1e3 with frequencies .01, cum freq .01
308-
# third centroid has impacts 1e1, 1e3 with cum frequencies .1, .01, cum freq .11, .01
309-
# fourth centroid has impacts 1e1, 1e2, 1e3 with cum frequencies 1., .1, .01, cum freq 1.11, .11, .01
310-
# testing at frequencies .001, .033, 10.
311-
312-
# test stepfunction
313-
impact_stats, _, _ = impact.local_exceedance_impact(
314-
return_periods=(1000, 30, .1), method='stepfunction')
315-
np.testing.assert_allclose(
316-
impact_stats.values[:,1:].astype(float),
317-
np.array([
318-
[0, 0, 0],
319-
[1e3, 0, 0],
320-
[1e3, 1e1, 0],
321-
[1e3, 1e2, 0]
322-
])
323-
)
324-
325-
# test log log extrapolation
326-
impact_stats, _, _ = impact.local_exceedance_impact(
327-
return_periods=(1000, 30, .1), method = "extrapolate")
328-
np.testing.assert_allclose(
329-
impact_stats.values[:,1:].astype(float),
330-
np.array([
331-
[0, 0, 0],
332-
[1e3, 0, 0],
333-
[1e5, 1e2, 1e-3],
334-
[1e4, 300, 1]
335-
]),
336-
rtol=0.8)
337-
338-
# test log log interpolation and extrapolation with constant
339-
impact_stats, _, _ = impact.local_exceedance_impact(
340-
return_periods=(1000, 30, .1), method = "extrapolate_constant")
341-
np.testing.assert_allclose(
342-
impact_stats.values[:,1:].astype(float),
343-
np.array([
344-
[0, 0, 0],
345-
[1e3, 0, 0],
346-
[1e3, 1e2, 0],
347-
[1e3, 300, 0]
348-
]),
349-
rtol=0.8)
350-
351-
# test log log interpolation and no extrapolation
352-
impact_stats, _, _ = impact.local_exceedance_impact(
353-
return_periods=(1000, 30, .1))
354-
np.testing.assert_allclose(
355-
impact_stats.values[:,1:].astype(float),
356-
np.array([
357-
[np.nan, np.nan, np.nan],
358-
[np.nan, np.nan, np.nan],
359-
[np.nan, 1e2, np.nan],
360-
[np.nan, 300, np.nan]
361-
]),
362-
rtol=0.8)
363-
364-
# test lin lin interpolation with no extrapolation
365-
impact_stats, _, _ = impact.local_exceedance_impact(
366-
return_periods=(1000, 30, .1), method = "extrapolate_constant",
367-
log_frequency=False, log_impact=False)
368-
np.testing.assert_allclose(
369-
impact_stats.values[:,1:].astype(float),
370-
np.array([
371-
[0, 0, 0],
372-
[1e3, 0, 0],
373-
[1e3, 750, 0],
374-
[1e3, 750, 0]
375-
]),
376-
rtol=0.8)
377-
378291
# Execute Tests
379292
if __name__ == "__main__":
380293
TESTS = unittest.TestLoader().loadTestsFromTestCase(TestEmdatProcessing)
381294
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestGDPScaling))
382295
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestEmdatToImpact))
383296
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestCalcCostBenefit))
384-
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestImpactRPCals))
385297
unittest.TextTestRunner(verbosity=2).run(TESTS)

0 commit comments

Comments
 (0)