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

Add CLI command to validate scenario data file from definitions #419

Merged
merged 3 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 27 additions & 0 deletions nomenclature/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,30 @@ def cli_run_workflow(
df = getattr(workflow, workflow_function)(IamDataFrame(input_file))
if output_file is not None:
df.to_excel(output_file)


@cli.command("validate-scenarios")
@click.argument("input_file", type=click.Path(exists=True, path_type=Path))
@click.option(
"--definitions",
help="Optional name for definitions folder",
type=click.Path(exists=True, path_type=Path),
default="definitions",
)
def cli_validate_scenarios(input_file: Path, definitions: Path):
"""Run a given input file through a DataStructureDefinition validation,
checking it against defined codelist(s)
dc-almeida marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
input_file : Path
Input data file, must be IAMC format, .xlsx or .csv
definitions : Path
Definitions folder with codelists, by default "definitions"

Raises
------
ValueError
If input_file validation fails against specified codelist(s).
"""
DataStructureDefinition(definitions).validate(IamDataFrame(input_file))
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- common:
dc-almeida marked this conversation as resolved.
Show resolved Hide resolved
- World
- country:
- Austria
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Primary Energy:
definition: Total primary energy consumption
unit: EJ/yr
25 changes: 25 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,28 @@ def test_cli_run_workflow(tmp_path, simple_df):
)

assert_iamframe_equal(simple_df, IamDataFrame(tmp_path / "output.xlsx"))


@pytest.mark.parametrize(
"status, unit, exit_code", [("valid", "EJ/yr", 0), ("invalid", "EJ", 1)]
)
def test_cli_valid_scenarios(status, unit, exit_code, tmp_path):
"""Check that CLI validates an IAMC dataset according to defined codelist."""
IamDataFrame(
pd.DataFrame(
[
["m_a", "s_a", "World", "Primary Energy", unit, 1, 2],
],
columns=IAMC_IDX + [2005, 2010],
)
).to_excel(tmp_path / f"{status}_data.xlsx")
result_valid = runner.invoke(
cli,
[
"validate-scenarios",
str(tmp_path / f"{status}_data.xlsx"),
"--definitions",
str(MODULE_TEST_DATA_DIR / "validate_scenarios" / "definitions"),
],
)
assert result_valid.exit_code == exit_code