Skip to content

Commit

Permalink
Merge pull request #132 from EMMC-ASBL/mappings-cleanup
Browse files Browse the repository at this point in the history
Cleaned up mappings module
  • Loading branch information
jesper-friis committed Oct 17, 2023
2 parents dba3971 + 94700ac commit 3212e48
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion tripper/mappings/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""Sub-package for working with mappings."""
from .mappings import MappingStep, Value, mapping_routes
from .mappings import MappingStep, StepType, Value, mapping_routes
33 changes: 18 additions & 15 deletions tripper/mappings/mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import subprocess # nosec: B404
from collections import defaultdict
from enum import Enum
from typing import TYPE_CHECKING, Sequence
from typing import TYPE_CHECKING, Mapping, Sequence

import numpy as np
from pint import Quantity # remove
Expand Down Expand Up @@ -112,13 +112,13 @@ def __init__(
def __repr__(self):
args = []
if self.unit:
args.append(", unit={self.unit}")
args.append(f", unit={self.unit}")
if self.output_iri:
args.append(", iri={self.output_iri}")
args.append(f", iri={self.output_iri}")
if self.property_iri:
args.append(", property_iri={self.property_iri}")
args.append(f", property_iri={self.property_iri}")
if self.cost:
args.append(", cost={self.cost}")
args.append(f", cost={self.cost}")
return f"Value({self._value}{''.join(args)})"

def get_value(self, unit=None, magnitude=False, quantity=None) -> "Any":
Expand Down Expand Up @@ -219,8 +219,8 @@ def __init__(
self.joined_input: "Inputs" = {}

def add_inputs(self, inputs: "Inputs") -> None:
"""Add input dict for an input route."""
assert isinstance(inputs, dict) # nosec B101
"""Add input Mapping (e.g., dict) for an input route."""
assert isinstance(inputs, Mapping) # nosec B101
self.input_routes.append(inputs)

def add_input(self, input: "Input", name: "Optional[str]" = None) -> None:
Expand Down Expand Up @@ -274,18 +274,23 @@ def eval(
Returns:
Evaluation result.
"""
if not self.number_of_routes():
raise MissingRelationError(
f"no route to evaluate '{self.output_iri}'"
)
if quantity is None:
quantity = Quantity
if routeno is None:
((_, routeno),) = self.lowest_costs(nresults=1)
inputs, idx = self.get_inputs(routeno)
values = get_values(inputs, idx, quantity=quantity)

if len(inputs) == 1 and all(
isinstance(v, Value) for v in inputs.values()
):
(value,) = tuple(inputs.values())
elif self.function:
# if len(inputs) == 1 and all(
# isinstance(v, Value) for v in inputs.values()
# ):
# (value,) = tuple(inputs.values())
# elif self.function:
if self.function:
value = self.function(**values)
elif len(values) == 1:
(value,) = values.values()
Expand Down Expand Up @@ -648,9 +653,7 @@ def get_values(
else value
)
elif isinstance(v, Value):
values[k] = (
v.value if v.unit is None else quantity(v.value, v.unit)
)
values[k] = v.value if not v.unit else quantity(v.value, v.unit)
else:
raise TypeError(
"Expected values in inputs to be either `MappingStep` or "
Expand Down

0 comments on commit 3212e48

Please sign in to comment.