def test_invalid_parameter(self): """Invalid techonology or variable input returns error:""" tech = 'XY' # does not exist ent = SpamAgrar() with self.assertRaises(ValueError) as cm: ent.init_spam_agrar(spam_technology=tech) self.assertIn('Invalid input parameter(s).', str(cm.exception))
def test_invalid_country(self): """invalid country name produces unchanged dataframe:""" ent = SpamAgrar() testdata = self.init_testdata() with self.assertLogs('climada.entity.exposures.spam_agrar', level='INFO') as cm: testdata1 = ent._spam_set_country(testdata, country='XXX')[0] self.assertIn('Country name not found in data: XXX', cm.output[0]) self.assert_pd_frame_equal(testdata1, testdata)
def test_invalid_parameter(self): """Invalid techonology or variable input returns error:""" tech = 'XY' # does not exist ent = SpamAgrar() with self.assertLogs('climada.entity.exposures.spam_agrar', level='ERROR') as cm: with self.assertRaises(ValueError): ent.init_spam_agrar(spam_technology=tech) self.assertIn('Invalid input parameter(s).', cm.output[0])
def test_invalid_adm2(self): """invalid admin 2 name produces unchanged dataframe:""" ent = SpamAgrar() testdata = self.init_testdata() with self.assertLogs('climada.entity.exposures.spam_agrar', level='INFO') as cm: testdata1, teststr1 = ent._spam_set_country(testdata, name_adm2='XXX') self.assertIn('Admin2 not found in data: XXX', cm.output[0]) self.assert_pd_frame_equal(testdata1, testdata) self.assertEqual(teststr1, 'global')
def test_iso3_country_name(self): """iso3 and country name produce same output:""" ent = SpamAgrar() testdata = self.init_testdata() testdata1, teststr1 = ent._spam_set_country(testdata, country='AAA') testdata2, teststr2 = ent._spam_set_country(testdata, country='countryA') self.assert_pd_frame_equal(testdata1, testdata2) self.assertEqual(teststr1, 'AAA') self.assertEqual(teststr2, 'countryA')
def test_suriname_pass(self): """Test country Suriname for default parameters:""" country_name = 'Suriname' ent = SpamAgrar() with self.assertLogs('climada.entity.exposures.spam_agrar', level='INFO') as cm: ent.init_spam_agrar(country=country_name) self.assertIn('Lat. range: +1.875 to +5.958.', cm.output[0]) self.assertIn('Lon. range: -58.042 to -54.042.', cm.output[1]) self.assertIn("Total V_agg TA Suriname: 78879225.2 USD.", cm.output[2]) self.assertTrue(ent.region_id.min() == 740) self.assertTrue(ent.region_id.max() == 740)
def test_invalid_country(self): """Invalid country or admin input returns global entity:""" country_name = 'Utopia' ent = SpamAgrar() with self.assertLogs('climada.entity.exposures.spam_agrar', level='INFO') as cm: ent.init_spam_agrar(country=country_name) self.assertIn('Country name not found in data: Utopia', cm.output[0]) self.assertIn('Lat. range: -55.375 to +71.125.', cm.output[1]) self.assertIn('Lon. range: -179.125 to +179.958.', cm.output[2]) self.assertIn("Total V_agg TA global: 1301919384722.2 USD.", cm.output[3])
def test_all_names_given(self): """all names given and returned as string:""" ent = SpamAgrar() testdata = self.init_testdata() testdata1, teststr1 = ent._spam_set_country(testdata, name_adm2='municB') testdata2, teststr2 = ent._spam_set_country(testdata, country='AAA', name_adm1='kantonB', name_adm2='municB') self.assert_pd_frame_equal(testdata1, testdata2) self.assertEqual(teststr1, ' municB') self.assertEqual(teststr2, 'AAA kantonB municB')
def test_ucayali_pass(self): """Test admin 2 region Ucayali for non-default parameters:""" adm2 = 'Ucayali' tech = 'TA' # all var = 'Y' # yield ent = SpamAgrar() with self.assertLogs('climada.entity.exposures.spam_agrar', level='INFO') as cm: ent.init_spam_agrar(name_adm2=adm2, spam_variable=var,\ spam_technology=tech) self.assertIn('Lat. range: -8.625 to -6.042.', cm.output[0]) self.assertIn('Lon. range: -76.125 to -74.208.', cm.output[1]) self.assertIn("Total Y TA Ucayali: 12298441.3 kg/Ha.", cm.output[2])
def test_zurich_pass(self): """Test admin 1 Zurich for default parameters:""" country_name = 'CHE' adm1 = 'Zurich' ent = SpamAgrar() with self.assertLogs('climada.entity.exposures.spam_agrar', level='INFO') as cm: ent.init_spam_agrar(country=country_name, name_adm1=adm1) self.assertIn('Lat. range: +47.208 to +47.625.', cm.output[0]) self.assertIn('Lon. range: +8.375 to +8.875.', cm.output[1]) self.assertIn("Total V_agg TA CHE Zurich: 56644555.1 USD.", cm.output[2]) self.assertTrue(ent.region_id.min() == 756) self.assertTrue(ent.region_id.max() == 756)
def test_switzerland_pass(self): """Test country CHE for non-default parameters:""" country_name = 'CHE' tech = 'TI' # irrigated var = 'H' # harvest area ent = SpamAgrar() with self.assertLogs('climada.entity.exposures.spam_agrar', level='INFO') as cm: ent.init_spam_agrar(country=country_name, spam_variable=var,\ spam_technology=tech) self.assertIn('Lat. range: +45.875 to +47.792.', cm.output[0]) self.assertIn('Lon. range: +6.042 to +10.375.', cm.output[1]) self.assertIn("Total H TI CHE: 28427.1 Ha.", cm.output[2]) self.assertTrue(ent.region_id.min() == 756) self.assertTrue(ent.region_id.max() == 756)
def test_global_pass(self): """Test global entity for default parameters and ent.select:""" ent = SpamAgrar() with self.assertLogs('climada.entity.exposures.spam_agrar', level='INFO') as cm: ent.init_spam_agrar() ent_select = ent[ent['region_id'] == 208] # select Denmark only self.assertIn('Lat. range: -55.375 to +71.125.', cm.output[0]) self.assertIn('Lon. range: -179.125 to +179.958.', cm.output[1]) self.assertIn("Total V_agg TA global: 1301919384722.2 USD.", cm.output[2]) self.assertTrue(ent.region_id.min() == 4) self.assertTrue(ent.region_id[10000] == 246) self.assertTrue(ent.region_id.max() == 894) self.assertTrue(ent_select.value.sum() == 1878858118.0) self.assertTrue(ent_select.region_id.min() == 208) self.assertTrue(ent_select.region_id.max() == 208)
def test_switzerland(self): drought = Drought() drought.set_area(44.5, 5, 50, 12) hazard_set = drought.setup() imp_drought = Impact() dr_if = ImpactFuncSet() if_def = IFDrought() if_def.set_default() dr_if.append(if_def) exposure_agrar = SpamAgrar() exposure_agrar.init_spam_agrar(country='CHE') exposure_agrar.assign_centroids(hazard_set) imp_drought.calc(exposure_agrar, dr_if, hazard_set) index_event_start = imp_drought.event_name.index('2003') damages_drought = imp_drought.at_event[index_event_start] self.assertEqual(hazard_set.tag.haz_type, 'DR') self.assertEqual(hazard_set.size, 114) self.assertEqual(hazard_set.centroids.size, 130) self.assertEqual(exposure_agrar.latitude.values.size, 766 / 2) self.assertEqual(exposure_agrar.value[3], 1720024.4) self.assertEqual(damages_drought, 61995472.555223145)
new_haz = d.setup() """Plot intensity of one year event""" # new_haz.plot_intensity_drought(event='2003') """Initialize Impact function""" dr_if = ImpactFuncSet() if_def = IFDrought() """set impact function: for min: set_default; for sum-thr: set_default_sumthr; for sum: set_default_sum""" #if_def.set_default() #if_def.set_default_sumthr() if_def.set_default_sum() dr_if.append(if_def) """Initialize Exposure""" exposure_agrar = SpamAgrar() exposure_agrar.init_spam_agrar(country='CHE') """If intensity def is not default, exposure has to be adapted""" """In case of sum-thr: 'if_DR_sumthr', in case of sum:'if_DR_sum' """ #exposure_agrar['if_DR_sumthr'] = np.ones(exposure_agrar.shape[0]) exposure_agrar['if_DR_sum'] = np.ones(exposure_agrar.shape[0]) """Initialize impact of the drought""" imp_drought = Impact() """Calculate Damage for a specific event""" imp_drought.calc(exposure_agrar, dr_if, new_haz) index_event_start = imp_drought.event_name.index('2003') damages_drought = np.asarray([imp_drought.at_event[index_event_start]]) print(damages_drought)