Skip to content

Commit

Permalink
Update error message for empty dataframe (#107)
Browse files Browse the repository at this point in the history
* Update error message for multiple models

* Add test for multiple models

* Update nomenclature/processor/region.py

Co-authored-by: Daniel Huppmann <dh@dergelbesalon.at>

* Update to use region-processing

* Shortened error match

Co-authored-by: Daniel Huppmann <dh@dergelbesalon.at>
  • Loading branch information
phackstock and danielhuppmann committed Mar 17, 2022
1 parent b812fb7 commit a9f64c4
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
7 changes: 4 additions & 3 deletions nomenclature/processor/region.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,15 +323,15 @@ def apply(self, df: IamDataFrame, dsd: DataStructureDefinition) -> IamDataFrame:

# If no mapping is defined the data frame is returned unchanged
if model not in self.mappings:
logger.info(f"No region aggregation mapping found for model {model}")
logger.info(f"No model mapping found for model {model}")
processed_dfs.append(model_df)

# Otherwise we first rename, then aggregate
else:
# before aggregating, check that all regions are valid
self.mappings[model].validate_regions(dsd)
logger.info(
f"Applying region aggregation for model {model} from file "
f"Applying region-processing for model {model} from file "
f"{self.mappings[model].file}"
)

Expand Down Expand Up @@ -408,7 +408,8 @@ def apply(self, df: IamDataFrame, dsd: DataStructureDefinition) -> IamDataFrame:

if not processed_dfs:
raise ValueError(
f"The region aggregation for model {model} resulted in an empty dataset"
f"The region-processing for model(s) {df.model} returned an empty "
"dataset"
)

return pyam.concat(processed_dfs)
Expand Down
4 changes: 4 additions & 0 deletions tests/data/region_processing/rename_only/model_b.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
model: model_b
native_regions:
- region_a: region_A
- region_B
4 changes: 2 additions & 2 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ def test_region_processing_empty_raises():
pd.DataFrame(
[
["model_a", "scen_a", "region_foo", "Primary Energy", "EJ/yr", 1, 2],
["model_b", "scen_a", "region_foo", "Primary Energy", "EJ/yr", 1, 2],
],
columns=IAMC_IDX + [2005, 2010],
)
)

with pytest.raises(ValueError, match="The region aggregation for model model_a"):
with pytest.raises(ValueError, match=("'model_a', 'model_b'.*empty dataset")):
process(
test_df,
DataStructureDefinition(TEST_DATA_DIR / "region_processing/dsd"),
Expand Down

0 comments on commit a9f64c4

Please sign in to comment.