Skip to content

Commit

Permalink
Raise error for unknown region (#284)
Browse files Browse the repository at this point in the history
* Add error for unknown regions in RegionProcessor

* Add test for unknown regions in RegionProcessor
  • Loading branch information
phackstock authored Sep 26, 2023
1 parent c276955 commit 7196480
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
5 changes: 4 additions & 1 deletion nomenclature/processor/region.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
)
from nomenclature.processor import Processor
from nomenclature.processor.utils import get_relative_path
from nomenclature.validation import log_error

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -450,7 +451,9 @@ def apply(self, df: IamDataFrame) -> IamDataFrame:
processed_dfs.append(self._apply_region_processing(model_df)[0])

res = pyam.concat(processed_dfs)
self.region_codelist.validate_items(res.region)
if not_defined_regions := self.region_codelist.validate_items(res.region):
log_error("region", not_defined_regions)
raise ValueError("The validation failed. Please check the log for details.")
return res

def check_region_aggregation(
Expand Down
28 changes: 28 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,3 +455,31 @@ def test_aggregation_differences_export(input_data, expected_difference):
exp = pd.DataFrame(expected_difference, columns=index + columns).set_index(index)

assert_frame_equal(exp, obs)


def test_region_aggregation_unknown_region(simple_df, simple_definition, caplog):
# add an unknown region
df_with_unknown_region = simple_df.append(
pd.DataFrame(
[
[
"model_a",
"scen_a",
"unknown region",
"Primary Energy",
"EJ/yr",
1,
6.0,
],
],
columns=IAMC_IDX + [2005, 2010],
)
)
with pytest.raises(ValueError):
RegionProcessor.from_directory(
TEST_DATA_DIR / "region_processing" / "no_mapping", simple_definition
).apply(df_with_unknown_region)
assert all(
text in caplog.text
for text in ["not defined in the 'region' codelist", "unknown region"]
)

0 comments on commit 7196480

Please sign in to comment.