Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Cleaned up mappings module #132

Merged
merged 7 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
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
31 changes: 17 additions & 14 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 @@ -220,7 +220,7 @@ def __init__(

def add_inputs(self, inputs: "Inputs") -> None:
"""Add input dict for an input route."""
jesper-friis marked this conversation as resolved.
Show resolved Hide resolved
assert isinstance(inputs, dict) # nosec B101
assert isinstance(inputs, Mapping) # nosec B101
ajeklund marked this conversation as resolved.
Show resolved Hide resolved
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:
francescalb marked this conversation as resolved.
Show resolved Hide resolved
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