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

Hierarchy Filter (first pull request) #226

Merged
merged 23 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c9a8bc4
First Commit with Hierarchy Filter
GretchenSchowalter Feb 28, 2023
65b284e
updated codelist.py to include hierarchy filter
GretchenSchowalter Feb 28, 2023
5876b2b
Commiting the hierarchy filter with docstrings
GretchenSchowalter Mar 1, 2023
8a5d962
Hierarchy filter now returns RegionCodeList instance
GretchenSchowalter Mar 6, 2023
792c8a8
Update codelist.py
GretchenSchowalter Mar 6, 2023
36d16af
Fixing extra whitespaces and lengthy ValueError message.
GretchenSchowalter Mar 6, 2023
92633f9
Editing to add whitespaces
GretchenSchowalter Mar 6, 2023
209a5f0
Update nomenclature/codelist.py
GretchenSchowalter Mar 6, 2023
e0d6b34
Update nomenclature/codelist.py
GretchenSchowalter Mar 6, 2023
d6734c8
changed call to just "filter"
GretchenSchowalter Mar 6, 2023
4839eea
Removing extra whitespaces (stickler)
GretchenSchowalter Mar 6, 2023
dd52a98
Removed period at the end of match string in the filter value error test
GretchenSchowalter Mar 6, 2023
36031e3
Update nomenclature/codelist.py
GretchenSchowalter Mar 6, 2023
ec15039
Update nomenclature/codelist.py
GretchenSchowalter Mar 6, 2023
c1fcb01
adding list of available filter hierarchy options
GretchenSchowalter Mar 6, 2023
8c56c6b
Updating to reflect the name change in codelist.py
GretchenSchowalter Mar 6, 2023
234928e
Updated ValueError to include available options
GretchenSchowalter Mar 7, 2023
da7eb0f
fixing conflicts updating test and codelist
GretchenSchowalter Mar 7, 2023
8fee65b
update files to match flake8/black standards
GretchenSchowalter Mar 7, 2023
8d0c2c9
Adding RegionCodeList and VariableCodeList to codelist.rst
GretchenSchowalter Mar 9, 2023
4dd2d1a
Added line of space in codelist.py
GretchenSchowalter Mar 10, 2023
1006b3d
Made error message simpler, and added def hierarchy(self) placeholder
GretchenSchowalter Mar 15, 2023
614e30b
Made hierarchy method a property and added a test
GretchenSchowalter Mar 15, 2023
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
30 changes: 15 additions & 15 deletions doc/source/api.rst
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
.. _api:

.. currentmodule:: nomenclature

API documentation
=================

.. toctree::
:maxdepth: 2

api/nomenclature
api/datastructuredefinition
api/codelist
api/regionprocessor
api/testing
.. _api:
.. currentmodule:: nomenclature
API documentation
=================
.. toctree::
:maxdepth: 2
api/nomenclature
api/datastructuredefinition
api/codelist
api/regionprocessor
api/testing
22 changes: 15 additions & 7 deletions doc/source/api/codelist.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
.. currentmodule:: nomenclature.codelist

**CodeList**
============

.. autoclass:: CodeList
:members:
.. currentmodule:: nomenclature.codelist

**CodeList**
============

.. autoclass:: CodeList
:members:


.. autoclass:: VariableCodeList
:members:


.. autoclass:: RegionCodeList
:members:
47 changes: 45 additions & 2 deletions nomenclature/codelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ def validate_items(self, items: List[str]) -> List[str]:
def replace_tags(
cls, code_list: List[Code], tag_name: str, tags: List[Code]
) -> List[Code]:

_code_list: List[Code] = []

for code in code_list:
Expand Down Expand Up @@ -524,7 +523,7 @@ def from_directory(cls, name: str, path: Path, file_glob_pattern: str = "**/*"):
with open(yaml_file, "r", encoding="utf-8") as stream:
_code_list = yaml.safe_load(stream)

# a "region" codelist assumes a top-level key to be used as attribute
# a "region" codelist assumes a top-level category to be used as attribute
for top_level_cat in _code_list:
for top_key, _codes in top_level_cat.items():
for item in _codes:
Expand All @@ -542,3 +541,47 @@ def from_directory(cls, name: str, path: Path, file_glob_pattern: str = "**/*"):
mapping[code.name] = code

return cls(name=name, mapping=mapping)

GretchenSchowalter marked this conversation as resolved.
Show resolved Hide resolved
def filter(self, hierarchy: str) -> "RegionCodeList":
GretchenSchowalter marked this conversation as resolved.
Show resolved Hide resolved
"""A filter that returns the components within a provided hierarchy.

Parameters
----------
hierarchy : str
The name of the hierarchy whose components you are looking for.
Raises
GretchenSchowalter marked this conversation as resolved.
Show resolved Hide resolved
------
ValueError
If User inputs a typo or a hierarchy not compatible with the given model.
GretchenSchowalter marked this conversation as resolved.
Show resolved Hide resolved
Returns
GretchenSchowalter marked this conversation as resolved.
Show resolved Hide resolved
-------
RegionCodeList
Returns a list of the component regions of the inputted hierarchy.
"""

mapping = {k: v for k, v in self.mapping.items() if v.hierarchy == hierarchy}
if mapping:
return RegionCodeList(name=self.name, mapping=mapping)
else:
GretchenSchowalter marked this conversation as resolved.
Show resolved Hide resolved
# creating list of possible hierarchies user can enter
avail_ops = []
for v in self.mapping.values():
# making sure there are no duplicates in avail_ops
if v.hierarchy not in avail_ops:
avail_ops.append(v.hierarchy)
# creating the error message that will be displayed
n = len(avail_ops)
msg_1: str = f"No hierarchy found for {hierarchy}. Options available: "
if n - 1 >= 2:
# when there are three or more options
msg_3: str = (
f"{'{} and {}'.format(', '.join(avail_ops[:-1]), avail_ops[-1])}"
)
raise ValueError(msg_1 + msg_3)
elif n - 1 == 1:
# when there are only two options
msg_2: str = f"{'{} and {}'.format(avail_ops[0], avail_ops[1])}"
raise ValueError(msg_1 + msg_2)
else:
# when there is only one option
raise ValueError(msg_1 + f"{avail_ops[0]}")
22 changes: 22 additions & 0 deletions tests/data/region_codelist_four/four_region_hierarchies.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
- First Region:
GretchenSchowalter marked this conversation as resolved.
Show resolved Hide resolved
- World:
description: The entire world
- Second Region:
- Some Country:
iso2: XY
iso3: XYZ
- Third Region:
- ComponentA:
part1: windy
part2: rainy
- ComponentB:
part1: peatbogs
part2: forest
- Fourth Region:
- North America:
country1: Canada
country2: USA
country3: Mexico
- South America:
country1: Brazil
country2: Argentina
3 changes: 3 additions & 0 deletions tests/data/region_codelist_one/one_region_hierarchy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- common:
- World:
definition: The entire world
Loading