|
1 | | -# import os |
2 | | -# import pytest |
3 | | -# import pandas as pd |
4 | | -# import xarray as xr |
5 | | - |
6 | | -# from pathlib import Path |
7 | | -# from pandas.testing import assert_frame_equal |
8 | | -# from xarray.testing import assert_equal, assert_allclose |
9 | | - |
10 | | -# from dscim.tests import open_example_dataset, open_zipped_results |
11 | | -# from dscim.menu.equity import EquityRecipe |
12 | | - |
13 | | - |
14 | | -# @pytest.fixture( |
15 | | -# params=[ |
16 | | -# "constant", |
17 | | -# # "constant_model_collapsed", # not used |
18 | | -# "naive_ramsey", |
19 | | -# "euler_ramsey", |
20 | | -# "naive_gwr", |
21 | | -# "gwr_gwr", |
22 | | -# "euler_gwr", |
23 | | -# ] |
24 | | -# ) |
25 | | -# def discount_types(request): |
26 | | -# return request.param |
27 | | - |
28 | | - |
29 | | -# @pytest.fixture |
30 | | -# def equity(discount_types, econ, climate): |
31 | | -# datadir = os.path.join(os.path.dirname(__file__), "data") |
32 | | - |
33 | | -# recipe = EquityRecipe( |
34 | | -# sector_path=[{"dummy_sector": os.path.join(datadir, "damages")}], |
35 | | -# save_path=None, |
36 | | -# econ_vars=econ, |
37 | | -# climate_vars=climate, |
38 | | -# fit_type="ols", |
39 | | -# variable=[{"dummy_sector": "damages"}], |
40 | | -# sector="dummy_sector", |
41 | | -# discounting_type=discount_types, |
42 | | -# ext_method="global_c_ratio", |
43 | | -# ce_path= os.path.join(datadir, "CEs"), |
44 | | - |
45 | | -# subset_dict={ |
46 | | -# "ssp": ["SSP2", "SSP3", "SSP4"], |
47 | | -# "region": [ |
48 | | -# "IND.21.317.1249", |
49 | | -# "CAN.2.33.913", |
50 | | -# "USA.14.608", |
51 | | -# "EGY.11", |
52 | | -# "SDN.4.11.50.164", |
53 | | -# "NGA.25.510", |
54 | | -# "SAU.7", |
55 | | -# "RUS.16.430.430", |
56 | | -# "SOM.2.5", |
57 | | -# ], |
58 | | -# }, |
59 | | -# fair_aggregation=["ce", "median_params", "mean"], |
60 | | -# extrap_formula=None, |
61 | | -# formula="damages ~ -1 + anomaly + np.power(anomaly, 2)", |
62 | | -# ) |
63 | | - |
64 | | -# yield recipe |
65 | | - |
66 | | -# @pytest.mark.xfail |
67 | | -# def test_equity_points(equity, discount_types): |
68 | | -# path = f"equity_{discount_types}_eta{equity.eta}_rho{equity.rho}_damage_function_points.csv" |
69 | | -# expected = open_zipped_results(path) |
70 | | -# actual = equity.damage_function_points |
71 | | -# assert_frame_equal(expected, actual, rtol=1e-4, atol=1e-4) |
72 | | - |
73 | | -# @pytest.mark.xfail |
74 | | -# def test_equity_coefficients(equity, discount_types): |
75 | | -# path = f"equity_{discount_types}_eta{equity.eta}_rho{equity.rho}_damage_function_coefficients.nc4" |
76 | | -# expected = open_zipped_results(path) |
77 | | -# actual = equity.damage_function_coefficients |
78 | | -# assert_allclose( |
79 | | -# expected.transpose(*sorted(expected.dims)).sortby(list(expected.dims)), |
80 | | -# actual.transpose(*sorted(actual.dims)).sortby(list(actual.dims)), |
81 | | -# ) |
82 | | - |
83 | | -# @pytest.mark.xfail |
84 | | -# def test_equity_fit(equity, discount_types): |
85 | | -# path = f"equity_{discount_types}_eta{equity.eta}_rho{equity.rho}_damage_function_fit.nc4" |
86 | | -# expected = open_zipped_results(path) |
87 | | -# actual = equity.damage_function_fit |
88 | | -# assert_allclose( |
89 | | -# expected.transpose(*sorted(expected.dims)).sortby(list(expected.dims)), |
90 | | -# actual.transpose(*sorted(actual.dims)).sortby(list(actual.dims)), |
91 | | -# ) |
92 | | - |
93 | | -# @pytest.mark.xfail |
94 | | -# def test_equity_global_consumption(equity, discount_types): |
95 | | -# path = f"equity_{discount_types}_eta{equity.eta}_rho{equity.rho}_global_consumption.nc4" |
96 | | -# expected = open_zipped_results(path) |
97 | | -# actual = equity.global_consumption.squeeze() |
98 | | -# # Small format hack from I/O |
99 | | -# if isinstance(expected, xr.Dataset): |
100 | | -# expected = expected.to_array().squeeze().drop("variable") |
101 | | - |
102 | | -# assert_allclose( |
103 | | -# expected.transpose(*sorted(expected.dims)).sortby(list(expected.dims)), |
104 | | -# actual.transpose(*sorted(actual.dims)).sortby(list(actual.dims)), |
105 | | -# ) |
106 | | - |
107 | | - |
108 | | -# @pytest.mark.xfail |
109 | | -# def test_equity_scc(equity, discount_types): |
110 | | -# path = f"equity_{discount_types}_eta{equity.eta}_rho{equity.rho}_scc.nc4" |
111 | | -# expected = open_zipped_results(path) |
112 | | -# actual = equity.calculate_scc.squeeze() |
113 | | - |
114 | | -# # Small format hack from I/O |
115 | | -# if isinstance(expected, xr.Dataset): |
116 | | -# expected = expected.to_array().squeeze().drop("variable") |
117 | | - |
118 | | -# assert_allclose( |
119 | | -# expected.transpose(*sorted(expected.dims)).sortby(list(expected.dims)), |
120 | | -# actual.transpose(*sorted(actual.dims)).sortby(list(actual.dims)), |
121 | | -# rtol=1.5e-4, |
122 | | -# atol=1e-4, |
123 | | -# ) |
| 1 | +import pandas |
| 2 | +import xarray as xr |
| 3 | +from pandas.testing import assert_frame_equal |
| 4 | +from xarray.testing import assert_allclose |
| 5 | +import pytest |
| 6 | + |
| 7 | +from . import open_zipped_results |
| 8 | +from dscim.menu.equity import EquityRecipe |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.parametrize("menu_class", [EquityRecipe], indirect=True) |
| 12 | +def test_equity_points(menu_instance, discount_types): |
| 13 | + path = f"equity_{discount_types}_eta{menu_instance.eta}_rho{menu_instance.rho}_damage_function_points.csv" |
| 14 | + expected = open_zipped_results(path) |
| 15 | + actual = menu_instance.damage_function_points |
| 16 | + assert_frame_equal( |
| 17 | + expected, |
| 18 | + actual, |
| 19 | + rtol=1e-4, |
| 20 | + atol=1e-4, |
| 21 | + ) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.parametrize("menu_class", [EquityRecipe], indirect=True) |
| 25 | +def test_equity_coefficients(menu_instance, discount_types): |
| 26 | + path = f"equity_{discount_types}_eta{menu_instance.eta}_rho{menu_instance.rho}_damage_function_coefficients.nc4" |
| 27 | + expected = open_zipped_results(path) |
| 28 | + actual = menu_instance.damage_function_coefficients |
| 29 | + assert_allclose( |
| 30 | + expected.transpose(*sorted(expected.dims)).sortby(list(expected.dims)), |
| 31 | + actual.transpose(*sorted(actual.dims)).sortby(list(actual.dims)), |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.parametrize("menu_class", [EquityRecipe], indirect=True) |
| 36 | +def test_equity_fit(menu_instance, discount_types): |
| 37 | + path = f"equity_{discount_types}_eta{menu_instance.eta}_rho{menu_instance.rho}_damage_function_fit.nc4" |
| 38 | + expected = open_zipped_results(path) |
| 39 | + actual = menu_instance.damage_function_fit |
| 40 | + assert_allclose( |
| 41 | + expected.transpose(*sorted(expected.dims)).sortby(list(expected.dims)), |
| 42 | + actual.transpose(*sorted(actual.dims)).sortby(list(actual.dims)), |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.parametrize("menu_class", [EquityRecipe], indirect=True) |
| 47 | +def test_equity_global_consumption(menu_instance, discount_types): |
| 48 | + path = f"equity_{discount_types}_eta{menu_instance.eta}_rho{menu_instance.rho}_global_consumption.nc4" |
| 49 | + expected = open_zipped_results(path) |
| 50 | + actual = menu_instance.global_consumption.squeeze() |
| 51 | + # Small format hack from I/O |
| 52 | + if isinstance(expected, xr.Dataset): |
| 53 | + expected = expected.to_array().squeeze().drop("variable") |
| 54 | + |
| 55 | + assert_allclose( |
| 56 | + expected.transpose(*sorted(expected.dims)).sortby(list(expected.dims)), |
| 57 | + actual.transpose(*sorted(actual.dims)).sortby(list(actual.dims)), |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.parametrize("menu_class", [EquityRecipe], indirect=True) |
| 62 | +def test_equity_scc(menu_instance, discount_types): |
| 63 | + path = ( |
| 64 | + f"equity_{discount_types}_eta{menu_instance.eta}_rho{menu_instance.rho}_scc.nc4" |
| 65 | + ) |
| 66 | + expected = open_zipped_results(path) |
| 67 | + actual = menu_instance.calculate_scc.squeeze() |
| 68 | + |
| 69 | + # Small format hack from I/O |
| 70 | + if isinstance(expected, xr.Dataset): |
| 71 | + expected = expected.to_array().squeeze().drop("variable") |
| 72 | + |
| 73 | + assert_allclose( |
| 74 | + expected.transpose(*sorted(expected.dims)).sortby(list(expected.dims)), |
| 75 | + actual.transpose(*sorted(actual.dims)).sortby(list(actual.dims)), |
| 76 | + rtol=1e-4, |
| 77 | + atol=1e-4, |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.parametrize("discount_types", ["euler_ramsey"], indirect=True) |
| 82 | +@pytest.mark.parametrize("menu_class", [EquityRecipe], indirect=True) |
| 83 | +def test_global_damages_calculation(menu_instance): |
| 84 | + global_damages = menu_instance.global_damages_calculation() |
| 85 | + assert ( |
| 86 | + isinstance(global_damages, pandas.DataFrame) |
| 87 | + and "region" not in global_damages.columns |
| 88 | + ) |
0 commit comments