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 VariableCodeList.units attribute and tests #289

Merged
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
19 changes: 19 additions & 0 deletions nomenclature/codelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
VariableRenameArgError,
VariableRenameTargetError,
)
from pyam.utils import is_list_like

here = Path(__file__).parent.absolute()

Expand Down Expand Up @@ -450,6 +451,24 @@ class VariableCodeList(CodeList):
code_basis: ClassVar = VariableCode
validation_schema: ClassVar[str] = "variable"

@property
def units(self):
"""Get the list of all units"""
units = set()

# replace "dimensionless" variables (unit: `None`) with empty string
# for consistency with the yaml file format
def to_dimensionless(u):
return u or ""

for variable in self.mapping.values():
if is_list_like(variable.unit):
units.update([to_dimensionless(u) for u in variable.unit])
else:
units.add(to_dimensionless(variable.unit))

return sorted(list(units))

@validator("mapping")
def check_variable_region_aggregation_args(cls, v):
"""Check that any variable "region-aggregation" mappings are valid"""
Expand Down
17 changes: 13 additions & 4 deletions tests/test_codelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

def test_simple_codelist():
"""Import a simple codelist"""
code = VariableCodeList.from_directory(
codelist = VariableCodeList.from_directory(
"variable", TEST_DATA_DIR / "simple_codelist"
)

assert "Some Variable" in code
assert code["Some Variable"].unit is None # this is a dimensionless variable
assert type(code["Some Variable"].bool) == bool # this is a boolean
assert "Some Variable" in codelist
assert codelist["Some Variable"].unit is None # this is a dimensionless variable
assert type(codelist["Some Variable"].bool) == bool # this is a boolean


def test_codelist_to_yaml():
Expand Down Expand Up @@ -155,12 +155,21 @@ def test_end_whitespace_fails():
)


def test_variable_codelist_units():
"""Check that the units-attribute works as expected"""
codelist = VariableCodeList.from_directory(
"variable", TEST_DATA_DIR / "validation_nc" / "variable"
)
assert codelist.units == ["", "EJ/yr"]


def test_variable_codelist_multiple_units():
"""Check that multiple units work in a VariableCodeList"""
codelist = VariableCodeList.from_directory(
"variable", TEST_DATA_DIR / "multiple_unit_codelist"
)
assert codelist["Var1"].unit == ["unit1", "unit2"]
assert codelist.units == ["unit1", "unit2"]


def test_to_excel_read_excel_roundtrip(tmpdir):
Expand Down
Loading