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

Reenabling equity tests #84

Merged
merged 5 commits into from
Jul 6, 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
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from dscim.menu.simple_storage import Climate, EconVars
from dscim.menu.baseline import Baseline
from dscim.menu.risk_aversion import RiskAversionRecipe
from dscim.menu.equity import EquityRecipe
from pathlib import Path
from itertools import product
import numpy as np
Expand Down Expand Up @@ -61,7 +62,7 @@ def discount_types(request):
return request.param


all_menu_classes = [Baseline, RiskAversionRecipe]
all_menu_classes = [Baseline, RiskAversionRecipe, EquityRecipe]


@pytest.fixture(params=all_menu_classes, scope="module")
Expand Down
Binary file modified tests/data/menu_results.zip
Binary file not shown.
211 changes: 88 additions & 123 deletions tests/test_equity.py
Original file line number Diff line number Diff line change
@@ -1,123 +1,88 @@
# import os
# import pytest
# import pandas as pd
# import xarray as xr

# from pathlib import Path
# from pandas.testing import assert_frame_equal
# from xarray.testing import assert_equal, assert_allclose

# from dscim.tests import open_example_dataset, open_zipped_results
# from dscim.menu.equity import EquityRecipe


# @pytest.fixture(
# params=[
# "constant",
# # "constant_model_collapsed", # not used
# "naive_ramsey",
# "euler_ramsey",
# "naive_gwr",
# "gwr_gwr",
# "euler_gwr",
# ]
# )
# def discount_types(request):
# return request.param


# @pytest.fixture
# def equity(discount_types, econ, climate):
# datadir = os.path.join(os.path.dirname(__file__), "data")

# recipe = EquityRecipe(
# sector_path=[{"dummy_sector": os.path.join(datadir, "damages")}],
# save_path=None,
# econ_vars=econ,
# climate_vars=climate,
# fit_type="ols",
# variable=[{"dummy_sector": "damages"}],
# sector="dummy_sector",
# discounting_type=discount_types,
# ext_method="global_c_ratio",
# ce_path= os.path.join(datadir, "CEs"),

# subset_dict={
# "ssp": ["SSP2", "SSP3", "SSP4"],
# "region": [
# "IND.21.317.1249",
# "CAN.2.33.913",
# "USA.14.608",
# "EGY.11",
# "SDN.4.11.50.164",
# "NGA.25.510",
# "SAU.7",
# "RUS.16.430.430",
# "SOM.2.5",
# ],
# },
# fair_aggregation=["ce", "median_params", "mean"],
# extrap_formula=None,
# formula="damages ~ -1 + anomaly + np.power(anomaly, 2)",
# )

# yield recipe

# @pytest.mark.xfail
# def test_equity_points(equity, discount_types):
# path = f"equity_{discount_types}_eta{equity.eta}_rho{equity.rho}_damage_function_points.csv"
# expected = open_zipped_results(path)
# actual = equity.damage_function_points
# assert_frame_equal(expected, actual, rtol=1e-4, atol=1e-4)

# @pytest.mark.xfail
# def test_equity_coefficients(equity, discount_types):
# path = f"equity_{discount_types}_eta{equity.eta}_rho{equity.rho}_damage_function_coefficients.nc4"
# expected = open_zipped_results(path)
# actual = equity.damage_function_coefficients
# assert_allclose(
# expected.transpose(*sorted(expected.dims)).sortby(list(expected.dims)),
# actual.transpose(*sorted(actual.dims)).sortby(list(actual.dims)),
# )

# @pytest.mark.xfail
# def test_equity_fit(equity, discount_types):
# path = f"equity_{discount_types}_eta{equity.eta}_rho{equity.rho}_damage_function_fit.nc4"
# expected = open_zipped_results(path)
# actual = equity.damage_function_fit
# assert_allclose(
# expected.transpose(*sorted(expected.dims)).sortby(list(expected.dims)),
# actual.transpose(*sorted(actual.dims)).sortby(list(actual.dims)),
# )

# @pytest.mark.xfail
# def test_equity_global_consumption(equity, discount_types):
# path = f"equity_{discount_types}_eta{equity.eta}_rho{equity.rho}_global_consumption.nc4"
# expected = open_zipped_results(path)
# actual = equity.global_consumption.squeeze()
# # Small format hack from I/O
# if isinstance(expected, xr.Dataset):
# expected = expected.to_array().squeeze().drop("variable")

# assert_allclose(
# expected.transpose(*sorted(expected.dims)).sortby(list(expected.dims)),
# actual.transpose(*sorted(actual.dims)).sortby(list(actual.dims)),
# )


# @pytest.mark.xfail
# def test_equity_scc(equity, discount_types):
# path = f"equity_{discount_types}_eta{equity.eta}_rho{equity.rho}_scc.nc4"
# expected = open_zipped_results(path)
# actual = equity.calculate_scc.squeeze()

# # Small format hack from I/O
# if isinstance(expected, xr.Dataset):
# expected = expected.to_array().squeeze().drop("variable")

# assert_allclose(
# expected.transpose(*sorted(expected.dims)).sortby(list(expected.dims)),
# actual.transpose(*sorted(actual.dims)).sortby(list(actual.dims)),
# rtol=1.5e-4,
# atol=1e-4,
# )
import pandas
import xarray as xr
from pandas.testing import assert_frame_equal
from xarray.testing import assert_allclose
import pytest

from . import open_zipped_results
from dscim.menu.equity import EquityRecipe


@pytest.mark.parametrize("menu_class", [EquityRecipe], indirect=True)
def test_equity_points(menu_instance, discount_types):
path = f"equity_{discount_types}_eta{menu_instance.eta}_rho{menu_instance.rho}_damage_function_points.csv"
expected = open_zipped_results(path)
actual = menu_instance.damage_function_points
assert_frame_equal(
expected,
actual,
rtol=1e-4,
atol=1e-4,
)


@pytest.mark.parametrize("menu_class", [EquityRecipe], indirect=True)
def test_equity_coefficients(menu_instance, discount_types):
path = f"equity_{discount_types}_eta{menu_instance.eta}_rho{menu_instance.rho}_damage_function_coefficients.nc4"
expected = open_zipped_results(path)
actual = menu_instance.damage_function_coefficients
assert_allclose(
expected.transpose(*sorted(expected.dims)).sortby(list(expected.dims)),
actual.transpose(*sorted(actual.dims)).sortby(list(actual.dims)),
)


@pytest.mark.parametrize("menu_class", [EquityRecipe], indirect=True)
def test_equity_fit(menu_instance, discount_types):
path = f"equity_{discount_types}_eta{menu_instance.eta}_rho{menu_instance.rho}_damage_function_fit.nc4"
expected = open_zipped_results(path)
actual = menu_instance.damage_function_fit
assert_allclose(
expected.transpose(*sorted(expected.dims)).sortby(list(expected.dims)),
actual.transpose(*sorted(actual.dims)).sortby(list(actual.dims)),
)


@pytest.mark.parametrize("menu_class", [EquityRecipe], indirect=True)
def test_equity_global_consumption(menu_instance, discount_types):
path = f"equity_{discount_types}_eta{menu_instance.eta}_rho{menu_instance.rho}_global_consumption.nc4"
expected = open_zipped_results(path)
actual = menu_instance.global_consumption.squeeze()
# Small format hack from I/O
if isinstance(expected, xr.Dataset):
expected = expected.to_array().squeeze().drop("variable")

assert_allclose(
expected.transpose(*sorted(expected.dims)).sortby(list(expected.dims)),
actual.transpose(*sorted(actual.dims)).sortby(list(actual.dims)),
)


@pytest.mark.parametrize("menu_class", [EquityRecipe], indirect=True)
def test_equity_scc(menu_instance, discount_types):
path = (
f"equity_{discount_types}_eta{menu_instance.eta}_rho{menu_instance.rho}_scc.nc4"
)
expected = open_zipped_results(path)
actual = menu_instance.calculate_scc.squeeze()

# Small format hack from I/O
if isinstance(expected, xr.Dataset):
expected = expected.to_array().squeeze().drop("variable")

assert_allclose(
expected.transpose(*sorted(expected.dims)).sortby(list(expected.dims)),
actual.transpose(*sorted(actual.dims)).sortby(list(actual.dims)),
rtol=1e-4,
atol=1e-4,
)


@pytest.mark.parametrize("discount_types", ["euler_ramsey"], indirect=True)
@pytest.mark.parametrize("menu_class", [EquityRecipe], indirect=True)
def test_global_damages_calculation(menu_instance):
global_damages = menu_instance.global_damages_calculation()
assert (
isinstance(global_damages, pandas.DataFrame)
and "region" not in global_damages.columns
)