Skip to content

Commit 1e6bba5

Browse files
added unit tests for method
1 parent a566722 commit 1e6bba5

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

climada/engine/test/test_impact.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,120 @@ def test_local_exceedance_impact_methods(self):
642642
rtol=0.8,
643643
)
644644

645+
def test_local_return_period(self):
646+
"""Test local return periods with lin lin interpolation"""
647+
648+
impact = dummy_impact()
649+
impact.coord_exp = np.array([np.arange(4), np.arange(4)]).T
650+
impact.imp_mat = sparse.csr_matrix(
651+
np.array([[0, 0, 1, 2], [0, 0, 4, 4], [0, 0, 1, 1], [0, 1, 1, 3]])
652+
)
653+
impact.frequency = np.ones(4)
654+
# first centroid has impacts None with cum frequencies None
655+
# second centroid has impacts 1 with cum frequencies 1
656+
# third centroid has impacts 1, 4 with cum frequencies 4, 1
657+
# fourth centroid has impacts 1,2,3,4 with cum frequencies 4,3,2,1
658+
# testing at threshold impacts (0.5, 2.5, 5)
659+
return_stats, _, _ = impact.local_return_period(
660+
threshold_impact=(0.5, 2.5, 5),
661+
method="extrapolate",
662+
log_frequency=False,
663+
log_impact=False,
664+
)
665+
666+
np.testing.assert_allclose(
667+
return_stats[return_stats.columns[1:]].values,
668+
np.array(
669+
[
670+
[np.nan, np.nan, np.nan],
671+
[1.0, np.nan, np.nan],
672+
[1 / 4.5, 1 / 2.5, np.nan],
673+
[1 / 4.5, 1 / 2.5, np.nan],
674+
]
675+
),
676+
)
677+
678+
def test_local_return_period_methods(self):
679+
"""Test local return periods different methods"""
680+
impact = dummy_impact()
681+
impact.coord_exp = np.array([np.arange(4), np.arange(4)]).T
682+
impact.imp_mat = sparse.csr_matrix(
683+
np.array([[0, 0, 0, 1e1], [0, 0, 1e1, 1e2], [0, 1e3, 1e3, 1e3]])
684+
)
685+
impact.frequency = np.array([1.0, 0.1, 0.01])
686+
# first centroid has impacts None with cum frequencies None
687+
# second centroid has impacts 1e3 with frequencies .01, cum freq .01
688+
# third centroid has impacts 1e1, 1e3 with cum frequencies .1, .01, cum freq .11, .01
689+
# fourth centroid has impacts 1e1, 1e2, 1e3 with cum frequencies 1., .1, .01, cum freq 1.11, .11, .01
690+
# testing at threshold impacts .1, 300, 1e5
691+
692+
# test stepfunction
693+
return_stats, _, _ = impact.local_return_period(
694+
threshold_impact=(0.1, 300, 1e5), method="stepfunction"
695+
)
696+
np.testing.assert_allclose(
697+
return_stats.values[:, 1:].astype(float),
698+
np.array(
699+
[
700+
[np.nan, np.nan, np.nan],
701+
[100, 100, np.nan],
702+
[1 / 0.11, 100, np.nan],
703+
[1 / 1.11, 100, np.nan],
704+
]
705+
),
706+
)
707+
708+
# test log log extrapolation
709+
return_stats, _, _ = impact.local_return_period(
710+
threshold_impact=(0.1, 300, 1e5), method="extrapolate"
711+
)
712+
np.testing.assert_allclose(
713+
return_stats.values[:, 1:].astype(float),
714+
np.array(
715+
[
716+
[np.nan, np.nan, np.nan],
717+
[100, 100, np.nan],
718+
[1.0, 30, 1e3],
719+
[0.01, 30, 1e4],
720+
]
721+
),
722+
rtol=0.8,
723+
)
724+
725+
# test log log interpolation and extrapolation with constant
726+
return_stats, _, _ = impact.local_return_period(
727+
threshold_impact=(0.1, 300, 1e5), method="extrapolate_constant"
728+
)
729+
np.testing.assert_allclose(
730+
return_stats.values[:, 1:].astype(float),
731+
np.array(
732+
[
733+
[np.nan, np.nan, np.nan],
734+
[100, 100, np.nan],
735+
[1 / 0.11, 30, np.nan],
736+
[1 / 1.11, 30, np.nan],
737+
]
738+
),
739+
rtol=0.8,
740+
)
741+
742+
# test log log interpolation and no extrapolation
743+
return_stats, _, _ = impact.local_return_period(
744+
threshold_impact=(0.1, 300, 1e5)
745+
)
746+
np.testing.assert_allclose(
747+
return_stats.values[:, 1:].astype(float),
748+
np.array(
749+
[
750+
[np.nan, np.nan, np.nan],
751+
[np.nan, np.nan, np.nan],
752+
[np.nan, 30, np.nan],
753+
[np.nan, 30, np.nan],
754+
]
755+
),
756+
rtol=0.8,
757+
)
758+
645759

646760
class TestImpactReg(unittest.TestCase):
647761
"""Test impact aggregation per aggregation region or admin 0"""

0 commit comments

Comments
 (0)