Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/move unittest #732

Merged
merged 5 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 0 additions & 45 deletions climada/engine/test/test_impact_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,6 @@ def test_emdat_df_2020_load(self):
self.assertFalse(False in list(df['Disaster Subtype'] == 'Tropical cyclone'))
self.assertFalse('Flood' in list(df['Disaster Subtype']))

class TestGDPScaling(unittest.TestCase):
"""test scaling of impact values proportional to GDP"""
def test_scale_impact2refyear(self):
"""scale of impact values proportional to GDP"""
impact_scaled = im_d.scale_impact2refyear([10, 100, 1000, 100, 100],
[1999, 2005, 2015, 2000, 2000],
['CZE', 'CZE', 'MEX', 'MEX', 'CZE'],
reference_year=2015)
# scaled impact value might change if worldbank input data changes,
# check magnitude and adjust if test fails in the following line:
self.assertListEqual(impact_scaled, [28, 137, 1000, 165, 304])

class TestEmdatProcessing(unittest.TestCase):
def test_emdat_impact_event_2018(self):
"""test emdat_impact_event event impact data extraction, version 2018"""
Expand Down Expand Up @@ -183,20 +171,6 @@ def test_emdat_affected_yearlysum(self):
self.assertIn('USA', list(df['ISO']))
self.assertIn('BGD', list(df['ISO']))

def test_emdat_damage_yearlysum(self):
"""test emdat_impact_yearlysum yearly impact data extraction with scaling"""
df = im_d.emdat_impact_yearlysum(EMDAT_TEST_CSV, countries=['Bangladesh', 'USA'],
hazard='Flood', year_range=(2015, 2017),
reference_year=2000)

self.assertEqual(36, df.size)
self.assertAlmostEqual(df.impact.max(), 15150000000.0)
self.assertEqual(df.impact_scaled.min(), 10943000.0)
self.assertEqual(df["year"][5], 2017)
self.assertEqual(df["reference_year"].max(), 2000)
self.assertIn('USA', list(df['ISO']))
self.assertIn(50, list(df['region_id']))

def test_emdat_countries_by_hazard_2020_pass(self):
"""test to get list of countries impacted by tropical cyclones from 2000 to 2019"""
iso3_codes, country_names = im_d.emdat_countries_by_hazard(EMDAT_2020_CSV_DEMO,
Expand Down Expand Up @@ -241,24 +215,6 @@ def test_emdat_to_impact_all_countries_pass(self):
self.assertIn('SPI', countries2020)
self.assertNotIn('SPI', countries)

def test_emdat_to_impact_scale(self):
"""test import DR EM-DAT to Impact() for 1 country and ref.year (scaling)"""
impact_emdat = im_d.emdat_to_impact(EMDAT_TEST_CSV, 'DR',
year_range=[2010, 2016], countries=['USA'],
hazard_type_emdat='Drought',
reference_year=2016)[0]
self.assertEqual(5, impact_emdat.event_id.size)
self.assertEqual(4, impact_emdat.event_id[-1])
self.assertEqual(0, impact_emdat.event_id[0])
self.assertIn('2012-9235', impact_emdat.event_name)
self.assertEqual(1, len(impact_emdat.eai_exp))
self.assertAlmostEqual(impact_emdat.aai_agg, impact_emdat.eai_exp[0])
self.assertAlmostEqual(0.14285714, np.unique(impact_emdat.frequency)[0], places=3)
# scaled impact value might change if worldbank input data changes,
# check magnitude and adjust if test failes in the following 2 lines:
self.assertAlmostEqual(3.69, np.sum(impact_emdat.at_event * 1e-10), places=0)
self.assertAlmostEqual(5.28, impact_emdat.aai_agg * 1e-9, places=0)

def test_emdat_to_impact_fakedata(self):
"""test import TC EM-DAT to Impact() for all countries in CSV"""
impact_emdat, countries = im_d.emdat_to_impact(EMDAT_TEST_CSV_FAKE, 'FL',
Expand Down Expand Up @@ -301,7 +257,6 @@ def test_emdat_to_impact_2020format(self):
# Execute Tests
if __name__ == "__main__":
TESTS = unittest.TestLoader().loadTestsFromTestCase(TestEmdatImport)
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestGDPScaling))
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestEmdatProcessing))
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestEmdatToImpact))
unittest.TextTestRunner(verbosity=2).run(TESTS)
114 changes: 2 additions & 112 deletions climada/engine/unsequa/test/test_unsequa.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@
from climada.hazard import Hazard
from climada.engine.unsequa import InputVar, CalcImpact, UncOutput, CalcCostBenefit

from climada.util.constants import EXP_DEMO_H5, HAZ_DEMO_H5, ENT_DEMO_TODAY, ENT_DEMO_FUTURE
from climada.util.constants import TEST_UNC_OUTPUT_IMPACT, TEST_UNC_OUTPUT_COSTBEN
from climada.util.constants import (EXP_DEMO_H5, HAZ_DEMO_H5, ENT_DEMO_TODAY, ENT_DEMO_FUTURE,
TEST_UNC_OUTPUT_IMPACT, TEST_UNC_OUTPUT_COSTBEN)
from climada.util.api_client import Client


apiclient = Client()
ds = apiclient.get_dataset_info(name=TEST_UNC_OUTPUT_IMPACT, status='test_dataset')
_target_dir, [test_unc_output_impact] = apiclient.download_dataset(ds)
Expand Down Expand Up @@ -283,24 +282,6 @@ def test_plot_unc_imp(self):
self.assertIsNotNone(plt_map)
plt.close()

def test_plot_unc_cb(self):
"""Test all cost benefit plots"""
unc_output = UncOutput.from_hdf5(test_unc_output_costben)
plt_s = unc_output.plot_sample()
self.assertIsNotNone(plt_s)
plt.close()
plt_u = unc_output.plot_uncertainty()
self.assertIsNotNone(plt_u)
plt.close()
with self.assertRaises(ValueError):
unc_output.plot_rp_uncertainty()
plt_sens = unc_output.plot_sensitivity()
self.assertIsNotNone(plt_sens)
plt.close()
plt_sens_2 = unc_output.plot_sensitivity_second_order(salib_si='S1')
self.assertIsNotNone(plt_sens_2)
plt.close()

def test_save_load_pass(self):
"""Test save and load output data"""

Expand Down Expand Up @@ -680,58 +661,6 @@ def test_make_sample_pass(self):
np.array(['x_haz', 'EN', 'IFi', 'CO', 'EG', 'PAA', 'MDD'])
)


def test_calc_uncertainty_pass(self):
"""Test compute the uncertainty distribution for an impact"""

ent_iv, ent_fut_iv = make_costben_iv()
_, _, haz_iv = make_input_vars()
unc_calc = CalcCostBenefit(haz_iv, ent_iv)
unc_data = unc_calc.make_sample( N=2)
unc_data = unc_calc.uncertainty(unc_data)

self.assertEqual(unc_data.unit, ent_dem().exposures.value_unit)

self.assertEqual(
unc_data.tot_climate_risk_unc_df.size,
unc_data.n_samples
)
self.assertEqual(
unc_data.cost_ben_ratio_unc_df.size,
unc_data.n_samples * 4 #number of measures
)
self.assertEqual(
unc_data.imp_meas_present_unc_df.size,
0
)
self.assertEqual(
unc_data.imp_meas_future_unc_df.size,
unc_data.n_samples * 4 * 5 #All measures 4 and risks/benefits 5
)

unc_calc = CalcCostBenefit(haz_iv, ent_iv, haz_iv, ent_fut_iv)
unc_data = unc_calc.make_sample( N=2)
unc_data = unc_calc.uncertainty(unc_data)

self.assertEqual(unc_data.unit, ent_dem().exposures.value_unit)

self.assertEqual(
unc_data.tot_climate_risk_unc_df.size,
unc_data.n_samples
)
self.assertEqual(
unc_data.cost_ben_ratio_unc_df.size,
unc_data.n_samples * 4 #number of measures
)
self.assertEqual(
unc_data.imp_meas_present_unc_df.size,
unc_data.n_samples * 4 * 5 #All measures 4 and risks/benefits 5
)
self.assertEqual(
unc_data.imp_meas_future_unc_df.size,
unc_data.n_samples * 4 * 5 #All measures 4 and risks/benefits 5
)

def test_calc_uncertainty_pool_pass(self):
"""Test compute the uncertainty distribution for an impact"""

Expand Down Expand Up @@ -767,45 +696,6 @@ def test_calc_uncertainty_pool_pass(self):
unc_data.n_samples * 4 * 5 #All measures 4 and risks/benefits 5
)

def test_calc_sensitivity_pass(self):
"""Test compute sensitivity default"""

ent_iv, _ = make_costben_iv()
_, _, haz_iv = make_input_vars()
unc_calc = CalcCostBenefit(haz_iv, ent_iv)
unc_data = unc_calc.make_sample(N=4, sampling_kwargs={'calc_second_order': True})
unc_data = unc_calc.uncertainty(unc_data)

unc_data = unc_calc.sensitivity(
unc_data,
sensitivity_kwargs = {'calc_second_order': True}
)

self.assertEqual(unc_data.sensitivity_method, 'sobol')
self.assertTupleEqual(unc_data.sensitivity_kwargs,
tuple({'calc_second_order': 'True'}.items())
)

for name, attr in unc_data.__dict__.items():
if 'sens_df' in name:
if 'imp_meas_present' in name:
self.assertTrue(attr.empty)
else:
np.testing.assert_array_equal(
attr.param.unique(),
np.array(['x_haz', 'EN', 'IFi', 'CO'])
)

np.testing.assert_array_equal(
attr.si.unique(),
np.array(['S1', 'S1_conf', 'ST', 'ST_conf', 'S2', 'S2_conf'])
)

self.assertEqual(len(attr),
len(unc_data.param_labels) * (4 + 4 + 4)
)


if __name__ == "__main__":
TESTS = unittest.TestLoader().loadTestsFromTestCase(TestInputVar)
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestOutput))
Expand Down
Loading