Skip to content

Commit

Permalink
(partial) tests update
Browse files Browse the repository at this point in the history
  • Loading branch information
aleeciu committed Oct 19, 2023
1 parent 3c55332 commit cc49358
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 42 deletions.
68 changes: 38 additions & 30 deletions climada/hazard/test/test_tc_cc.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,45 @@
class TestKnutson(unittest.TestCase):
"""Test loading funcions from the TropCyclone class"""

def test_get_pass(self):
def test_get_knutson_scaling_pass(self):
"""Test get_knutson_criterion function."""
criterion = tc_cc.get_knutson_criterion()
self.assertEqual(len(criterion), 24)
for crit_val in criterion:
self.assertTrue('year' in crit_val)
self.assertTrue('change' in crit_val)

self.assertEqual(criterion[0]['change'], 1 - 0.094)
self.assertEqual(criterion[4]['change'], 1.163)
self.assertEqual(criterion[-10]['basin'], "SP")
self.assertEqual(criterion[-10]['change'], 1 - 0.506)

def test_scale_pass(self):
"""Test calc_scale_knutson function."""
self.assertAlmostEqual(tc_cc.calc_scale_knutson(ref_year=2050, rcp_scenario=45),
0.759630751756698)
self.assertAlmostEqual(tc_cc.calc_scale_knutson(ref_year=2070, rcp_scenario=45),
0.958978483788876)
self.assertAlmostEqual(tc_cc.calc_scale_knutson(ref_year=2060, rcp_scenario=60),
0.825572149523299)
self.assertAlmostEqual(tc_cc.calc_scale_knutson(ref_year=2080, rcp_scenario=60),
1.309882943406079)
self.assertAlmostEqual(tc_cc.calc_scale_knutson(ref_year=2090, rcp_scenario=85),
2.635069196605717)
self.assertAlmostEqual(tc_cc.calc_scale_knutson(ref_year=2100, rcp_scenario=85),
2.940055236533517)
self.assertAlmostEqual(tc_cc.calc_scale_knutson(ref_year=2066, rcp_scenario=26),
0.341930203294547)
self.assertAlmostEqual(tc_cc.calc_scale_knutson(ref_year=2078, rcp_scenario=26),
0.312383928930456)
criterion = tc_cc.get_knutson_scaling_factor()
self.assertEqual(criterion.shape, (21, 4))

self.assertEqual(criterion.columns[0], '2.6')
self.assertEqual(criterion.columns[1], '4.5')
self.assertEqual(criterion.columns[2], '6.0')
self.assertEqual(criterion.columns[3], '8.5')

self.assertAlmostEqual(criterion.loc[2030, '2.6'], -16.13547, 4)
self.assertAlmostEqual(criterion.loc[2050, '4.5'], -25.19448, 4)
self.assertAlmostEqual(criterion.loc[2070, '6.0'], -31.06633, 4)
self.assertAlmostEqual(criterion.loc[2100, '8.5'], -58.98637, 4)

def test_get_gmst_pass(self):
"""Test get_gmst_info function."""
gmst_data, gmst_start_year, gmst_end_year, rcps = tc_cc.get_gmst_info()

self.assertAlmostEqual(gmst_data.shape,
(len(rcps),
gmst_end_year-gmst_start_year+1))
self.assertAlmostEqual(gmst_data[0,0], -0.16)
self.assertAlmostEqual(gmst_data[0,-1], 1.27641, 4)
self.assertAlmostEqual(gmst_data[-1,0], -0.16)
self.assertAlmostEqual(gmst_data[-1,-1], 4.477764, 4)

def test_get_knutson_data_pass(self):
"""Test get_knutson_data function."""

data_knutson = tc_cc.get_knutson_data()

self.assertAlmostEqual(data_knutson.shape, (4,6,5))
self.assertAlmostEqual(data_knutson[0,0,0], -34.49)
self.assertAlmostEqual(data_knutson[-1,-1,-1], 15.419)
self.assertAlmostEqual(data_knutson[0,-1,-1], 4.689)
self.assertAlmostEqual(data_knutson[-1,0,0], 5.848)
self.assertAlmostEqual(data_knutson[-1,0,-1], 22.803)
self.assertAlmostEqual(data_knutson[2,3,2], 4.324)

if __name__ == "__main__":
TESTS = unittest.TestLoader().loadTestsFromTestCase(TestKnutson)
Expand Down
21 changes: 9 additions & 12 deletions climada/hazard/test/test_trop_cyclone.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,13 +429,9 @@ def test_apply_climate_scenario_track(self):
self.assertFalse(
np.allclose(tc.frequency[2], tc_cc.frequency[2])
)
self.assertTrue(
self.assertFalse(
np.allclose(tc.frequency[3], tc_cc.frequency[3])
)
self.assertEqual(
tc_cc.tag.description,
['climate change scenario for year 2050 and RCP 45 from Knutson et al 2015.']
)

def test_apply_criterion_track(self):
"""Test _apply_criterion function."""
Expand All @@ -445,7 +441,8 @@ def test_apply_criterion_track(self):
]
scale = 0.75

# artificially increase the size of the hazard by repeating (tiling) the data:
# artificially increase the size of
# the hazard by repeating (tiling) the data:
ntiles = 8

intensity = np.zeros((4, 10))
Expand All @@ -462,7 +459,7 @@ def test_apply_criterion_track(self):
event_id=np.arange(intensity.shape[0]),
)

tc_cc = tc._apply_knutson_criterion(criterion, scale)
tc_cc = tc.apply_climate_scenario_knu()
for i_tile in range(ntiles):
offset = i_tile * 4
# no factor applied because of category 0
Expand All @@ -484,7 +481,7 @@ def test_apply_criterion_track(self):
)

def test_two_criterion_track(self):
"""Test _apply_criterion function with two criteria"""
"""Test apply_climate_scenario_knu function with two criteria"""
criterion = [
{'basin': 'WP', 'category': [1, 2, 3, 4, 5],
'year': 2100, 'change': 1.025},
Expand Down Expand Up @@ -512,7 +509,7 @@ def test_two_criterion_track(self):
event_id=np.arange(4),
)

tc_cc = tc._apply_knutson_criterion(criterion, scale)
tc_cc = tc.apply_climate_scenario_knu(criterion, scale)

res_frequency = np.ones(4) * 0.5
res_frequency[1] = 0.5 * (1 + (0.7 - 1) * scale)
Expand All @@ -521,7 +518,7 @@ def test_two_criterion_track(self):
self.assertTrue(np.allclose(tc_cc.frequency, res_frequency))

def test_no_negative_freq(self):
"""Test _apply_knutson_criterion with too high changes and check
"""Test apply_climate_scenario_knu with too high changes and check
that no negative frequencies are returned."""
criterion = [{'basin': 'SP', 'category': [0, 1],
'year': 2100, 'change': 0.5}
Expand All @@ -533,8 +530,8 @@ def test_no_negative_freq(self):
category=np.array([0, 1]),
)

tc_cc = tc._apply_knutson_criterion(criterion, 3)
self.assertEqual(tc_cc.frequency.min(), 0.)
tc_cc = tc.apply_climate_scenario_knu(criterion, 3)
self.assertTrue(tc_cc.frequency.min() >= 0.)

class TestDumpReloadCycle(unittest.TestCase):
def setUp(self):
Expand Down

0 comments on commit cc49358

Please sign in to comment.