Skip to content

Commit

Permalink
Merge pull request #165 from primap-community/conversion-from-csv-mp
Browse files Browse the repository at this point in the history
  • Loading branch information
crdanielbusch authored Oct 23, 2024
2 parents dea46dc + c545090 commit 926c589
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 34 deletions.
35 changes: 10 additions & 25 deletions climate_categories/_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ def hydrate(
auxiliary_categories_hydrated = {}
for aux_categorization_name, categories in self.auxiliary_categories.items():
aux_categorization = cats[aux_categorization_name]
auxiliary_categories_hydrated[
aux_categorization
] = self._hydrate_handle_errors(categories, aux_categorization)
auxiliary_categories_hydrated[aux_categorization] = (
self._hydrate_handle_errors(categories, aux_categorization)
)

return ConversionRule(
factors_categories_a=self._hydrate_handle_errors(
Expand All @@ -87,14 +87,12 @@ def hydrate(
@typing.overload
def _hydrate_handle_errors(
self, to_hydrate: dict[str, int], categorization: "Categorization"
) -> dict["Category", int]:
...
) -> dict["Category", int]: ...

@typing.overload
def _hydrate_handle_errors(
self, to_hydrate: set[str], categorization: "Categorization"
) -> set["Category"]:
...
) -> set["Category"]: ...

def _hydrate_handle_errors(
self,
Expand Down Expand Up @@ -901,29 +899,16 @@ def __init__(

@staticmethod
def from_csv(
filepath: typing.Union[str, pathlib.Path, typing.TextIO],
cats : typing.Union[dict[str, "Categorization"], None] = None,
filepath: typing.Union[str, pathlib.Path, typing.TextIO],
cats: typing.Union[dict[str, "Categorization"], None] = None,
) -> "Conversion":
"""Read conversion from comma-separated-values file and add categorizations."""

conv = ConversionSpec.from_csv(filepath)
def get_cats(cat_names):
import climate_categories

return {
cat_name: climate_categories.cats[cat_name] for cat_name in cat_names
}

if not cats:
cat_names = [
conv.categorization_a_name,
conv.categorization_b_name,
]

if conv.auxiliary_categorizations_names:
cat_names += conv.auxiliary_categorizations_names
if cats is None:
import climate_categories

cats = get_cats(cat_names)
cats = climate_categories.cats

return conv.hydrate(cats=cats)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
# last_update: 2021-07-09
IPCC1996,gas,IPCC2006,comment
0,,0,
1,,1,
1,,1,
2 changes: 1 addition & 1 deletion climate_categories/tests/data/simple_categorisation_a.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ categories:
- C
- CatC
unnumbered:
title: The unnumbered category
title: The unnumbered category
2 changes: 1 addition & 1 deletion climate_categories/tests/data/simple_categorisation_b.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ categories:
comment: The second category
alternative_codes:
- B
- CatB
- CatB
16 changes: 10 additions & 6 deletions climate_categories/tests/test_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,7 @@ def get_test_data_filepath(fname: str):


def test_read_csv_in_conversion_class():
fd = get_test_data_filepath(
"example_conversion.IPCC1996.IPCC2006.csv"
)
fd = get_test_data_filepath("example_conversion.IPCC1996.IPCC2006.csv")

conv_from_conversion_spec = conversions.ConversionSpec.from_csv(fd)
conv_from_conversion_spec = conv_from_conversion_spec.hydrate(
Expand All @@ -448,9 +446,13 @@ def test_read_csv_in_conversion_class():


def test_read_conversion_from_csv_with_custom_categorizations():
categorisation_a = climate_categories.from_yaml(get_test_data_filepath("simple_categorisation_a.yaml"))
categorisation_a = climate_categories.from_yaml(
get_test_data_filepath("simple_categorisation_a.yaml")
)

categorisation_b = climate_categories.from_yaml(get_test_data_filepath("simple_categorisation_b.yaml"))
categorisation_b = climate_categories.from_yaml(
get_test_data_filepath("simple_categorisation_b.yaml")
)

cats = {"A": categorisation_a, "B": categorisation_b}

Expand All @@ -473,7 +475,9 @@ def test_read_conversion_from_csv_with_existing_categorizations():

def test_read_conversion_from_csv_with_existing_categorizations_aux_dims():
conv = climate_categories.Conversion.from_csv(
get_test_data_filepath("test_conversion_with_existing_categorizations_aux_dims.csv")
get_test_data_filepath(
"test_conversion_with_existing_categorizations_aux_dims.csv"
)
)

assert conv.categorization_a_name == "BURDI"
Expand Down

0 comments on commit 926c589

Please sign in to comment.