Skip to content

Commit

Permalink
Extend tests to make sure that method works with same/different regio…
Browse files Browse the repository at this point in the history
…n index inconsistency
  • Loading branch information
danielhuppmann committed Oct 17, 2023
1 parent bacab9d commit 1042938
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
5 changes: 4 additions & 1 deletion pyam/aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ def _aggregate_region(
raise ValueError("Using weights and components in one operation not supported.")

# default subregions to all regions other than `region`
subregions = subregions or df._all_other_regions(region, variable)
if weight is None:
subregions = subregions or df._all_other_regions(region, variable)
else:
subregions = subregions or df._all_other_regions(region, [variable, weight])

if not len(subregions):
logger.info(
Expand Down
34 changes: 27 additions & 7 deletions tests/test_feature_aggregate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import re

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -314,6 +315,7 @@ def test_aggregate_region_with_weights(simple_df, caplog):
exp = simple_df.filter(variable=v, region="World")
assert_iamframe_equal(simple_df.aggregate_region(v, weight=w), exp)


def test_aggregate_region_with_negative_weights(simple_df, caplog):
# carbon price shouldn't be summed but be weighted by emissions
v = "Price|Carbon"
Expand Down Expand Up @@ -341,28 +343,46 @@ def test_aggregate_region_with_negative_weights(simple_df, caplog):
)


def test_aggregate_region_with_weights_inconsistent_index(simple_df, caplog):
@pytest.mark.parametrize(
"filter_arg,log_message",
(
(dict(year=2010), ""),
(dict(), "model_a scen_a reg_b 2005\n1 "),
),
)
def test_aggregate_region_with_weights_inconsistent_index(
simple_df, caplog, filter_arg, log_message
):
# carbon price shouldn't be summed but be weighted by emissions
v = "Price|Carbon"
w = "Emissions|CO2"

log_message = "\n0 " + log_message + "model_a scen_a reg_b 2010"
if simple_df.time_domain == "datetime":
time_col = " time"
log_message = log_message.replace("2005", "2005-6-17").replace(" 2010", "2010-07-21")
else:
time_col = "year"

# missing weight row raises an error
_df = simple_df.filter(variable=w, region="reg_b", keep=False)
match = r"Missing weights for the following data.*\n.*\n.*\n.*scen_a reg_b 2010"
_df = simple_df.filter(variable=w, region="reg_b", keep=False, **filter_arg)
match = r"Missing weights for the following data.*\n.*" + re.escape(log_message)
with pytest.raises(ValueError, match=match):
_df.aggregate_region(v, weight=w)

# missing data row prints a warning (data-index is a subset of weight-index)
exp = simple_df.filter(variable=v, region="World")
exp._data[1] = 30.
_df = simple_df.filter(variable=v, region="reg_b", year=2010, keep=False)
if not filter_arg:
exp._data[0] = 1.
exp._data[1] = 30.0
_df = simple_df.filter(variable=v, region="reg_b", keep=False, **filter_arg)
assert_iamframe_equal(_df.aggregate_region(v, weight=w), exp)

msg = (
"Ignoring weights for the following missing data rows:\n"
" model scenario region year\n"
"0 model_a scen_a reg_b 2010"
f" model scenario region {time_col}" + log_message
)

idx = caplog.messages.index(msg)
assert caplog.records[idx].levelname == "WARNING"

Expand Down

0 comments on commit 1042938

Please sign in to comment.