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

adapt to changed worldbank data #841

Merged
merged 6 commits into from
Jan 18, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Code freeze date: YYYY-MM-DD
- Fix the dist_approx util function when used with method="geosphere" and log=True and points that are very close. [#792](https://github.com/CLIMADA-project/climada_python/pull/792)
- `climada.util.yearsets.sample_from_poisson`: fix a bug ([#819](https://github.com/CLIMADA-project/climada_python/issues/819)) and inconsistency that occurs when lambda events per year (`lam`) are set to 1. [[#823](https://github.com/CLIMADA-project/climada_python/pull/823)]
- In the TropCyclone class in the Holland model 2008 and 2010 implementation, a doublecounting of translational velocity is removed [#833](https://github.com/CLIMADA-project/climada_python/pull/833)
- `climada.util.test.test_finance` and `climada.test.test_engine` updated to recent input data from worldbank [#841](https://github.com/CLIMADA-project/climada_python/pull/841)

### Deprecated

Expand Down
2 changes: 1 addition & 1 deletion climada/test/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def test_scale_impact2refyear(self):
)
# 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])
self.assertListEqual(impact_scaled, [28, 137, 999, 163, 304])


class TestEmdatToImpact(unittest.TestCase):
Expand Down
104 changes: 54 additions & 50 deletions climada/util/test/test_finance.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,164 +45,168 @@ class TestWBData(unittest.TestCase):
def test_ne_income_grp_aia_pass(self):
"""Test nat_earth_adm0 function Anguilla."""
ref_year = 2012
res_year, res_val = nat_earth_adm0('AIA', 'INCOME_GRP',
shp_file=SHP_FILE)
ne_year, ne_val = nat_earth_adm0('AIA', 'INCOME_GRP',
shp_file=SHP_FILE)

ref_year = 0
ref_val = 3
self.assertEqual(res_year, ref_year)
self.assertEqual(res_val, ref_val)
self.assertEqual(ne_year, ref_year)
self.assertEqual(ne_val, ref_val)

def test_wb_income_grp_sxm_pass(self):
"""Test world_bank function Sint Maarten."""
ref_year = 2012
res_year, res_val = world_bank('SXM', ref_year, 'INC_GRP')
wb_year, wb_val = world_bank('SXM', ref_year, 'INC_GRP')

ref_year = 2012
ref_val = 4
self.assertEqual(res_year, ref_year)
self.assertEqual(res_val, ref_val)
self.assertEqual(wb_year, ref_year)
self.assertEqual(wb_val, ref_val)

def test_income_grp_sxm_1999_pass(self):
"""Test income_group function Sint Maarten."""
ref_year = 1999
with self.assertLogs('climada.util.finance', level='INFO') as cm:
res_year, res_val = income_group('SXM', ref_year, SHP_FILE)
ig_year, ig_val = income_group('SXM', ref_year, SHP_FILE)

ref_year = 2010
ref_val = 4
self.assertIn('Income group SXM 2010: 4.', cm.output[0])
self.assertEqual(res_year, ref_year)
self.assertEqual(res_val, ref_val)
self.assertEqual(ig_year, ref_year)
self.assertEqual(ig_val, ref_val)

def test_ne_gdp_aia_2012_pass(self):
"""Test nat_earth_adm0 function Anguilla."""
ref_year = 2012
res_year, res_val = nat_earth_adm0('AIA', 'GDP_MD',
'GDP_YEAR', SHP_FILE)
ne_year, ne_val = nat_earth_adm0('AIA', 'GDP_MD',
'GDP_YEAR', SHP_FILE)

ref_year = 2009
ref_val = 1.75e+08
self.assertEqual(res_year, ref_year)
self.assertEqual(res_val, ref_val)
self.assertEqual(ne_year, ref_year)
self.assertEqual(ne_val, ref_val)

def test_gdp_sxm_2010_pass(self):
"""Test gdp function Sint Maarten."""
# If World Bank input data changes, make sure to set ref_year to a year where
# no data is available so that the next available data point has to be selected.
ref_year = 2010
with self.assertLogs('climada.util.finance', level='INFO') as cm:
res_year, res_val = gdp('SXM', ref_year)
gdp_year, gdp_val = gdp('SXM', ref_year)

ref_val = 936089385.47486 # reference GDP value
ref_year = 2011 # nearest year with data available (might change)
# GDP and years with data available might change if worldbank input
# data changes, check magnitude and adjust ref_val and/or ref_year
# if test fails:
self.assertIn('GDP SXM %i: %1.3e' % (ref_year, ref_val), cm.output[0])
self.assertEqual(res_year, ref_year)
self.assertAlmostEqual(res_val, ref_val, places=0)
self.assertEqual(gdp_year, ref_year)
self.assertAlmostEqual(gdp_val, ref_val, places=0)

def test_gdp_twn_2012_pass(self):
"""Test gdp function TWN."""
ref_year = 2014
res_year, res_val = gdp('TWN', ref_year)
_, res_val_direct = _gdp_twn(ref_year)
gdp_year, gdp_val = gdp('TWN', ref_year)
_, gdp_val_direct = _gdp_twn(ref_year)
ref_val = 530515000000.0
ref_year = 2014
self.assertEqual(res_year, ref_year)
self.assertEqual(res_val, ref_val)
self.assertEqual(res_val_direct, ref_val)
self.assertEqual(gdp_year, ref_year)
self.assertEqual(gdp_val, ref_val)
self.assertEqual(gdp_val_direct, ref_val)


def test_wb_esp_1950_pass(self):
"""Test world_bank function Sint Maarten."""
ref_year = 1950
res_year, res_val = world_bank('ESP', ref_year, 'NY.GDP.MKTP.CD')
wb_year, wb_val = world_bank('ESP', ref_year, 'NY.GDP.MKTP.CD')

ref_year = 1960
ref_val = 12072126075.397
self.assertEqual(res_year, ref_year)
self.assertEqual(res_val, ref_val)
self.assertEqual(wb_year, ref_year)
self.assertEqual(wb_val, ref_val)

class TestWealth2GDP(unittest.TestCase):
"""Test Wealth to GDP factor extraction"""
def test_nfw_SUR_pass(self):
"""Test non-financial wealth-to-gdp factor with Suriname."""
res_year, res_val = wealth2gdp('SUR')
w2g_year, w2g_val = wealth2gdp('SUR')

ref_year = 2016
ref_val = 0.73656
self.assertEqual(res_year, ref_year)
self.assertEqual(res_val, ref_val)
self.assertEqual(w2g_year, ref_year)
self.assertEqual(w2g_val, ref_val)

def test_nfw_BEL_pass(self):
"""Test total wealth-to-gdp factor with Belgium."""
res_year, res_val = wealth2gdp('BEL', False)
w2g_year, w2g_val = wealth2gdp('BEL', False)

ref_year = 2016
ref_val = 4.88758
self.assertEqual(res_year, ref_year)
self.assertEqual(res_val, ref_val)
self.assertEqual(w2g_year, ref_year)
self.assertEqual(w2g_val, ref_val)

def test_nfw_LBY_pass(self):
"""Test missing factor with Libya."""
_, res_val = wealth2gdp('LBY')
_, w2g_val = wealth2gdp('LBY')

self.assertTrue(np.isnan(res_val))
self.assertTrue(np.isnan(w2g_val))

class TestWBWealthAccount(unittest.TestCase):
"""Test Wealth Indicator extraction from World Bank provided CSV"""
def test_pca_DEU_2010_pass(self):
"""Test Processed Capital value Germany 2010."""
ref_year = 2010
cntry_iso = 'DEU'
res_year, res_val, q = world_bank_wealth_account(cntry_iso, ref_year, no_land=0)
res_year_noland, res_val_noland, q = world_bank_wealth_account(cntry_iso, ref_year,
wb_year, wb_val, q = world_bank_wealth_account(cntry_iso, ref_year, no_land=0)
wb_year_noland, wb_val_noland, q = world_bank_wealth_account(cntry_iso, ref_year,
no_land=1)
ref_val = [17675048450284.9, 19767982562092.2] # second value as updated by worldbank on
# October 27 2021
ref_val_noland = [14254071330874.9, 15941921421042.1] # dito
self.assertEqual(res_year, ref_year)
self.assertEqual(wb_year, ref_year)
self.assertEqual(q, 1)
self.assertIn(res_val, ref_val)
self.assertEqual(res_year_noland, ref_year)
self.assertIn(res_val_noland, ref_val_noland)
self.assertIn(wb_val, ref_val)
self.assertEqual(wb_year_noland, ref_year)
self.assertIn(wb_val_noland, ref_val_noland)

def test_pca_CHE_2008_pass(self):
"""Test Prcoessed Capital per capita Switzerland 2008 (interp.)."""
ref_year = 2008
cntry_iso = 'CHE'
var_name = 'NW.PCA.PC'
res_year, res_val, _ = world_bank_wealth_account(cntry_iso, ref_year,
wb_year, wb_val, _ = world_bank_wealth_account(cntry_iso, ref_year,
variable_name=var_name, no_land=0)
ref_val = [328398.7, # values sporadically updated by worldbank
369081.0] # <- October 27 2021
self.assertEqual(res_year, ref_year)
self.assertIn(res_val, ref_val)
self.assertEqual(wb_year, ref_year)
self.assertIn(wb_val, ref_val)

def test_tow_IND_1985_pass(self):
"""Test Total Wealth value India 1985 (outside year range)."""
ref_year = 1985
cntry_iso = 'IND'
var_name = 'NW.TOW.TO'
res_year, res_val, _ = world_bank_wealth_account(cntry_iso, ref_year,
wb_year, wb_val, _ = world_bank_wealth_account(cntry_iso, ref_year,
variable_name=var_name)
ref_val = [5415188681934.5, # values sporadically updated by worldbank
5861193808779.6, # <- October 27 2021
5861186556152.8] # <- June 29 2023
self.assertEqual(res_year, ref_year)
self.assertIn(res_val, ref_val)
5861186556152.8, # <- June 29 2023
5861186367245.2, # <- December 20 2023
]
self.assertEqual(wb_year, ref_year)
self.assertIn(wb_val, ref_val)

def test_pca_CUB_2015_pass(self):
"""Test Processed Capital value Cuba 2015 (missing value)."""
ref_year = 2015
cntry_iso = 'CUB'
res_year, res_val, q = world_bank_wealth_account(cntry_iso, ref_year, no_land=1)
ref_val = 108675762920.0
wb_year, wb_val, q = world_bank_wealth_account(cntry_iso, ref_year, no_land=1)
ref_val = [108675762920.0, # values sporadically updated by worldbank
108675513472.0, # <- Dezember 20 2023
]
self.assertEqual(q, 0)
self.assertEqual(res_year, ref_year)
self.assertEqual(res_val, ref_val)
self.assertEqual(wb_year, ref_year)
self.assertIn(wb_val, ref_val)

# Execute Tests
if __name__ == "__main__":
Expand Down
Loading