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

More metrics #112

Merged
merged 6 commits into from
Jul 17, 2020
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 CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Changelog
master
------

- (`#111 <https://github.com/GranthamImperial/silicone/pull/111>`_) Minor improvements to error messages and documentation.
- (`#112 <https://github.com/GranthamImperial/silicone/pull/112>`_) Enabled more general unit conversion, bug fix and improvement for infill_composite_values.
- (`#111 <https://github.com/GranthamImperial/silicone/pull/111>`_) Minor improvements to error messages and documentation.
- (`#110 <https://github.com/GranthamImperial/silicone/pull/110>`_) Gave an option to time_dep_ratio and decompose_collection to ignore model/scenario combinations missing values at some required times.
- (`#108 <https://github.com/GranthamImperial/silicone/pull/108>`_) Added a multiple infiller to split up an aggregate with a remainder. Disabled test for downloading database.
- (`#103 <https://github.com/GranthamImperial/silicone/pull/103>`_) Update github address to GranthamImperial.
Expand Down
238 changes: 117 additions & 121 deletions notebooks/05_Multiple_infillers.ipynb

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def infill_components(
aggregate,
components,
to_infill_df,
use_ar4_data=False,
metric_name="AR5GWP100",
only_consistent_cases=True,
):
"""
Expand All @@ -115,9 +115,9 @@ def infill_components(
The dataframe that already contains the ``aggregate`` variable, but needs
the ``components`` to be infilled.

use_ar4_data : bool
If true, we convert all values to Mt CO2 equivalent using the IPCC AR4
GWP100 data, otherwise (by default) we use the GWP100 data from AR5.
metric_name : str
The name of the conversion metric to use. This will usually be
AR<4/5/6>GWP100.

only_consistent_cases : bool
Do we want to only use model/scenario combinations where all aggregate and
Expand Down Expand Up @@ -184,7 +184,7 @@ def infill_components(
)
if len(self._set_of_units_without_equiv(self._filtered_db)) > 1:
db_to_generate = convert_units_to_MtCO2_equiv(
self._filtered_db, use_ar4_data=use_ar4_data
self._filtered_db, metric_name=metric_name
)
else:
db_to_generate = self._filtered_db
Expand Down
3 changes: 2 additions & 1 deletion src/silicone/multiple_infillers/infill_composite_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ def infill_composite_values(df, composite_dic=None):
composite_df = _construct_consistent_values(
composite, composite_variables, df
)
df.append(composite_df, inplace=True)
try:
to_return.append(composite_df)
to_return.append(composite_df, inplace=True)
except NameError:
to_return = composite_df

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def infill_components(
remainder,
to_infill_df,
cruncher_class=QuantileRollingWindows,
use_ar4_data=False,
metric_name="AR5GWP100",
**kwargs
):
"""
Expand Down Expand Up @@ -131,10 +131,9 @@ def infill_components(
The cruncher used to perform the infilling. By default this will be
QuantileRollingWindows.

use_ar4_data : bool
Only used if the variables have different units. If true, we convert all
values to Mt CO2 equivalent using the IPCC AR4
GWP100 data, otherwise (by default) we use the GWP100 data from AR5.
metric_name : str
The name of the conversion metric to use. This will usually be
AR<4/5/6>GWP100.

**kwargs :
Handed to the cruncher.
Expand Down Expand Up @@ -192,7 +191,7 @@ def infill_components(
calculate_remainder_df = df_to_append.append(to_infill_df)
if _remove_equivs(aggregate_unit) == "Mt CO2/yr":
calculate_remainder_df = convert_units_to_MtCO2_equiv(
calculate_remainder_df, use_ar4_data
calculate_remainder_df, metric_name
)

df_to_append.append(
Expand Down
19 changes: 8 additions & 11 deletions src/silicone/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def _get_unit_of_variable(df, variable, multiple_units="raise"):


def return_cases_which_consistently_split(
df, aggregate, components, how_close=None, use_ar4_data=False
df, aggregate, components, how_close=None, metric_name="AR5GWP100"
):
"""
Returns model-scenario tuples which correctly split up the to_split into the various
Expand All @@ -290,9 +290,8 @@ def return_cases_which_consistently_split(
tolerance of 1% ('rtol': 1e-2). The syntax for this can be found in the numpy
documentation.

use_ar4_data : bool
Determines whether the unit conversion takes place using GWP100 values from
the UNFCCC AR5 (if false, default) or AR4 (if true).
metric_name : str
The name of the conversion metric to use. This will usually be AR<4/5/6>GWP100.

Returns
-------
Expand All @@ -303,7 +302,7 @@ def return_cases_which_consistently_split(
how_close = {"equal_nan": True, "rtol": 1e-02}
valid_model_scenario = []
df = convert_units_to_MtCO2_equiv(
df.filter(variable=[aggregate] + components), use_ar4_data
df.filter(variable=[aggregate] + components), metric_name
)
combinations = df.data[["model", "scenario", "region"]].drop_duplicates()
for ind in range(len(combinations)):
Expand Down Expand Up @@ -331,7 +330,7 @@ def return_cases_which_consistently_split(
return valid_model_scenario


def convert_units_to_MtCO2_equiv(df, use_ar4_data=False):
def convert_units_to_MtCO2_equiv(df, metric_name="AR5GWP100"):
"""
Converts the units of gases reported in kt into Mt CO2 equivalent per year

Expand All @@ -342,8 +341,8 @@ def convert_units_to_MtCO2_equiv(df, use_ar4_data=False):
df : :obj:`pyam.IamDataFrame`
The input dataframe whose units need to be converted.

use_ar4_data : bool
If true, use the AR4 GWP100 conversion figures, else use the AR5.
metric_name : str
The name of the conversion metric to use. This will usually be AR<4/5/6>GWP100.

Return
------
Expand All @@ -356,8 +355,6 @@ def convert_units_to_MtCO2_equiv(df, use_ar4_data=False):
if df["unit"].isin([convert_to_str, convert_to_str_clean]).all():
return df

context = "AR4GWP100" if use_ar4_data else "AR5GWP100"

to_convert_df = df.copy()
to_convert_var = to_convert_df.filter(
unit=[convert_to_str, convert_to_str_clean], keep=False
Expand All @@ -370,7 +367,7 @@ def convert_units_to_MtCO2_equiv(df, use_ar4_data=False):

conversion_factors = {}
not_found = []
with _ur.context(context):
with _ur.context(metric_name):
for unit in to_convert_units:
if unit in conversion_factors:
continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,31 @@ def test_infill_composite_values_works(self, larger_df, caplog):
)

def test_infill_with_factors(self, larger_df):
# Ensure that the multiplier works correctly
# Ensure that the multiplier works correctly. Also ensure that calculated values
# can be used in subsequent calculations.
larger_df_copy = larger_df.copy()
half_industry_df = infill_composite_values(
larger_df_copy,
composite_dic={
"Emissions|CO2": {
"Emissions|CO2|AFOLU": 1,
"Emissions|CO2|Industry": 0.5,
}
},
"Emissions|CO2|Energy": {
"Emissions|CO2": 1,
"Emissions|CO2|AFOLU": -1,
"Emissions|CO2|Industry": -0.5,
},
},
)
assert np.allclose(
half_industry_df.filter(variable="Emissions|CO2")["value"].values,
[2, 2, 2, 1, 1, 1, 2, 2, 2],
)
assert np.allclose(
half_industry_df.filter(variable="Emissions|CO2|Energy")["value"].values,
[0] * 9,
)

def test_infill_composite_values_subtraction(self, larger_df, caplog):
# Ensure that the code performs correctly when we subtract emissions too
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ def test_convert_units_to_mtco2_equiv_fails_with_bad_units(check_aggregate_df):


@pytest.mark.parametrize(
"ARoption,expected", [(False, [28, 6.63]), (True, [25, 7.390])]
"ARoption,expected", [("AR5GWP100", [28, 6.63]), ("AR4GWP100", [25, 7.390])]
)
def test_convert_units_to_MtCO2_equiv_works(check_aggregate_df, ARoption, expected):
# ARoption turns the use of AR4 on, rather than AR5 (the default)
Expand Down