-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb8d5e5
commit c130ccf
Showing
1 changed file
with
103 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,221 +1,103 @@ | ||
# # Test projection of adjusted investment costs | ||
# def test_project_adjusted_inv_costs(): | ||
# df_weo = get_weo_data() | ||
# df_nam_orig_message = get_cost_assumption_data() | ||
# df_tech_cost_ratios = calculate_region_cost_ratios(df_weo) | ||
|
||
# df_region_diff = get_region_differentiated_costs( | ||
# df_weo, df_nam_orig_message, df_tech_cost_ratios | ||
# ) | ||
|
||
# df_learning_rates = get_cost_reduction_data() | ||
# df_technology_first_year = get_technology_first_year_data() | ||
|
||
# df_gdp = get_gdp_data() | ||
# df_linreg = linearly_regress_tech_cost_vs_gdp_ratios(df_gdp, df_tech_cost_ratios) | ||
|
||
# df_adj_cost_ratios = calculate_adjusted_region_cost_ratios(df_gdp, df_linreg) | ||
# df_nam_learning = project_NAM_inv_costs_using_learning_rates( | ||
# df_region_diff, df_learning_rates, df_technology_first_year | ||
# ) | ||
|
||
# res = project_adjusted_inv_costs( | ||
# df_nam_learning, | ||
# df_adj_cost_ratios, | ||
# df_region_diff, | ||
# convergence_year_flag=2060, | ||
# ) | ||
|
||
# # Check that the appropriate columns are present | ||
# assert ( | ||
# bool( | ||
# res.columns.isin( | ||
# [ | ||
# "scenario", | ||
# "message_technology", | ||
# "weo_technology", | ||
# "r11_region", | ||
# "year", | ||
# "inv_cost_learning_only", | ||
# "inv_cost_gdp_adj", | ||
# "inv_cost_converge", | ||
# ] | ||
# ).any() | ||
# ) | ||
# is True | ||
# ) | ||
|
||
# # Check that the maximum year is 2100 | ||
# assert res.year.max() == 2100 | ||
|
||
|
||
# # Test application of polynomial regression | ||
# def test_apply_polynominal_regression(): | ||
# df_weo = get_weo_data() | ||
# df_nam_orig_message = get_cost_assumption_data() | ||
# df_tech_cost_ratios = calculate_region_cost_ratios(df_weo) | ||
|
||
# df_region_diff = get_region_differentiated_costs( | ||
# df_weo, df_nam_orig_message, df_tech_cost_ratios | ||
# ) | ||
|
||
# df_learning_rates = get_cost_reduction_data() | ||
# df_technology_first_year = get_technology_first_year_data() | ||
|
||
# df_gdp = get_gdp_data() | ||
# df_linreg = linearly_regress_tech_cost_vs_gdp_ratios(df_gdp, df_tech_cost_ratios) | ||
|
||
# df_adj_cost_ratios = calculate_adjusted_region_cost_ratios(df_gdp, df_linreg) | ||
# df_nam_learning = project_NAM_inv_costs_using_learning_rates( | ||
# df_region_diff, df_learning_rates, df_technology_first_year | ||
# ) | ||
|
||
# df_adj_inv = project_adjusted_inv_costs( | ||
# df_nam_learning, | ||
# df_adj_cost_ratios, | ||
# df_region_diff, | ||
# convergence_year_flag=2060, | ||
# ) | ||
|
||
# res = apply_polynominal_regression(df_adj_inv, convergence_year_flag=2060) | ||
|
||
# # Check that the appropriate columns are present | ||
# assert ( | ||
# bool( | ||
# res.columns.isin( | ||
# [ | ||
# "scenario", | ||
# "message_technology", | ||
# "r11_region", | ||
# "beta_1", | ||
# "beta_2", | ||
# "beta_3", | ||
# "intercept", | ||
# ] | ||
# ).any() | ||
# ) | ||
# is True | ||
# ) | ||
|
||
|
||
# # Test projections using spline regression results | ||
# def test_apply_splines_projection(): | ||
# df_weo = get_weo_data() | ||
# df_nam_orig_message = get_cost_assumption_data() | ||
# df_tech_cost_ratios = calculate_region_cost_ratios(df_weo) | ||
|
||
# df_region_diff = get_region_differentiated_costs( | ||
# df_weo, df_nam_orig_message, df_tech_cost_ratios | ||
# ) | ||
|
||
# df_learning_rates = get_cost_reduction_data() | ||
# df_technology_first_year = get_technology_first_year_data() | ||
|
||
# df_gdp = get_gdp_data() | ||
# df_linreg = linearly_regress_tech_cost_vs_gdp_ratios(df_gdp, df_tech_cost_ratios) | ||
|
||
# df_adj_cost_ratios = calculate_adjusted_region_cost_ratios(df_gdp, df_linreg) | ||
# df_nam_learning = project_NAM_inv_costs_using_learning_rates( | ||
# df_region_diff, df_learning_rates, df_technology_first_year | ||
# ) | ||
|
||
# df_adj_inv = project_adjusted_inv_costs( | ||
# df_nam_learning, | ||
# df_adj_cost_ratios, | ||
# df_region_diff, | ||
# convergence_year_flag=2060, | ||
# ) | ||
|
||
# df_poly_reg = apply_polynominal_regression(df_adj_inv, convergence_year_flag=2060) | ||
|
||
# res = apply_splines_projection( | ||
# df_region_diff, df_technology_first_year, df_poly_reg, df_adj_inv | ||
# ) | ||
|
||
# # Check that the appropriate columns are present | ||
# assert ( | ||
# bool( | ||
# res.columns.isin( | ||
# [ | ||
# "scenario", | ||
# "message_technology", | ||
# "r11_region", | ||
# "year", | ||
# "inv_cost_learning_only", | ||
# "inv_cost_gdp_adj", | ||
# "inv_cost_converge", | ||
# "inv_cost_splines", | ||
# ] | ||
# ).any() | ||
# ) | ||
# is True | ||
# ) | ||
|
||
# # Check that the maximum year is 2100 | ||
# assert res.year.max() == 2100 | ||
|
||
|
||
# # Test function to get final investment and fixed costs | ||
# def test_project_final_inv_and_fom_costs(): | ||
# df_weo = get_weo_data() | ||
# df_nam_orig_message = get_cost_assumption_data() | ||
# df_tech_cost_ratios = calculate_region_cost_ratios(df_weo) | ||
# df_fom_inv_ratios = calculate_fom_to_inv_cost_ratios(df_weo) | ||
|
||
# df_region_diff = get_region_differentiated_costs( | ||
# df_weo, df_nam_orig_message, df_tech_cost_ratios | ||
# ) | ||
|
||
# df_learning_rates = get_cost_reduction_data() | ||
# df_technology_first_year = get_technology_first_year_data() | ||
|
||
# df_gdp = get_gdp_data() | ||
# df_linreg = linearly_regress_tech_cost_vs_gdp_ratios(df_gdp, df_tech_cost_ratios) | ||
|
||
# df_adj_cost_ratios = calculate_adjusted_region_cost_ratios(df_gdp, df_linreg) | ||
# df_nam_learning = project_NAM_inv_costs_using_learning_rates( | ||
# df_region_diff, df_learning_rates, df_technology_first_year | ||
# ) | ||
|
||
# df_adj_inv = project_adjusted_inv_costs( | ||
# df_nam_learning, | ||
# df_adj_cost_ratios, | ||
# df_region_diff, | ||
# convergence_year_flag=2060, | ||
# ) | ||
|
||
# df_poly_reg = apply_polynominal_regression(df_adj_inv, convergence_year_flag=2060) | ||
|
||
# df_spline_projections = apply_splines_projection( | ||
# df_region_diff, df_technology_first_year, df_poly_reg, df_adj_inv | ||
# ) | ||
|
||
# res = project_final_inv_and_fom_costs( | ||
# df_spline_projections, | ||
# df_fom_inv_ratios, | ||
# use_gdp_flag=False, | ||
# converge_costs_flag=True, | ||
# ) | ||
|
||
# # Check that the appropriate columns are present | ||
# assert ( | ||
# bool( | ||
# res.columns.isin( | ||
# [ | ||
# "scenario", | ||
# "message_technology", | ||
# "r11_region", | ||
# "year", | ||
# "inv_cost", | ||
# "fix_cost", | ||
# ] | ||
# ).any() | ||
# ) | ||
# is True | ||
# ) | ||
|
||
# # Check that the maximum year is 2100 | ||
# assert res.year.max() == 2100 | ||
|
||
# # Check that all fix costs are less than investment costs | ||
# assert bool((res.fix_cost / res.inv_cost).max() < 1) | ||
import numpy as np | ||
|
||
from message_ix_models.tools.costs.config import FIRST_MODEL_YEAR | ||
from message_ix_models.tools.costs.learning import ( | ||
project_ref_region_inv_costs_using_learning_rates, | ||
) | ||
from message_ix_models.tools.costs.splines import apply_splines_to_convergence | ||
from message_ix_models.tools.costs.weo import get_weo_region_differentiated_costs | ||
|
||
|
||
def test_apply_splines_to_convergence(): | ||
in_node = "r12" | ||
in_ref_region = "R12_NAM" | ||
in_base_year = 2021 | ||
in_module = "materials" | ||
in_convergence_year = 2060 | ||
in_scenario = "SSP2" | ||
|
||
df_region_diff = get_weo_region_differentiated_costs( | ||
input_node=in_node, | ||
input_ref_region=in_ref_region, | ||
input_base_year=in_base_year, | ||
input_module=in_module, | ||
) | ||
|
||
df_ref_reg_learning = project_ref_region_inv_costs_using_learning_rates( | ||
df_region_diff, | ||
input_node=in_node, | ||
input_ref_region=in_ref_region, | ||
input_base_year=in_base_year, | ||
input_module=in_module, | ||
) | ||
|
||
if in_scenario is not None: | ||
df_ref_reg_learning = df_ref_reg_learning.query("scenario == @sel_scen") | ||
|
||
df_pre_costs = df_region_diff.merge( | ||
df_ref_reg_learning, on="message_technology" | ||
).assign( | ||
inv_cost_converge=lambda x: np.where( | ||
x.year <= FIRST_MODEL_YEAR, | ||
x.reg_cost_base_year, | ||
np.where( | ||
x.year < in_convergence_year, | ||
x.inv_cost_ref_region_learning * x.reg_cost_ratio, | ||
x.inv_cost_ref_region_learning, | ||
), | ||
), | ||
) | ||
|
||
df_splines = apply_splines_to_convergence( | ||
df_pre_costs, | ||
column_name="inv_cost_converge", | ||
input_convergence_year=in_convergence_year, | ||
) | ||
|
||
# Assert that all regions are present | ||
regions = [ | ||
"R12_AFR", | ||
"R12_CHN", | ||
"R12_EEU", | ||
"R12_FSU", | ||
"R12_LAM", | ||
"R12_MEA", | ||
"R12_NAM", | ||
"R12_PAO", | ||
"R12_PAS", | ||
"R12_SAS", | ||
"R12_WEU", | ||
] | ||
assert bool(all(i in df_splines.region.unique() for i in regions)) is True | ||
|
||
# Assert that materials and base technologies are present | ||
tech = [ | ||
"coal_ppl", | ||
"gas_ppl", | ||
"gas_cc", | ||
"biomass_NH3", | ||
"biomass_NH3", | ||
"furnace_foil_steel", | ||
] | ||
assert bool(all(i in df_splines.message_technology.unique() for i in tech)) is True | ||
|
||
# For each region, using coal_ppl as an example, assert that the costs converge | ||
# to approximately the reference region costs | ||
# in the convergence year | ||
for i in regions: | ||
assert ( | ||
np.allclose( | ||
df_splines.query( | ||
"region == @in_ref_region \ | ||
and message_technology == 'coal_ppl' \ | ||
and year >= @in_convergence_year" | ||
).inv_cost_splines, | ||
df_splines.query( | ||
"region == @i \ | ||
and message_technology == 'coal_ppl' \ | ||
and year >= @in_convergence_year" | ||
).inv_cost_splines, | ||
rtol=3, | ||
) | ||
is True | ||
) |