Skip to content

Commit

Permalink
Fix issue in mappings.instantiate_all() (#820)
Browse files Browse the repository at this point in the history
* Fix issue in mappings.instantiate_all()

Do not raise InsufficientMappingError if a target property is mapped
to a concept with not input mappings to when `allow_incomplete=True`
or `default` is given.

* Avoid duplicating calculation of properties and correctly assign default values.
  • Loading branch information
jesper-friis authored Apr 22, 2024
1 parent 1d4c4e1 commit c274db8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
1 change: 1 addition & 0 deletions bindings/python/dlite-collection-python.i
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ class Collection(Instance):
**kwargs
):
yield inst

elif property_mappings:
raise _dlite.DLiteError(
'`metaid` is required when `property_mappings` is true')
Expand Down
30 changes: 17 additions & 13 deletions bindings/python/mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def instance_routes(
def instantiate_from_routes(
meta: str | dlite.Metadata | Namespace,
routes: dict[str, MappingStep],
routedict: dict[str, int] = None,
routedict: "Optional[dict[str, int]]" = None,
id: "Optional[str]" = None,
default: "Optional[dlite.Instance]" = None,
quantity: "Type[Quantity]" = Quantity,
Expand Down Expand Up @@ -146,7 +146,8 @@ def instantiate_from_routes(
if default and default.meta.uri != meta.uri:
raise f"`default` must be an instance of {meta.uri}"

routedict = routedict or {}
if routedict is None:
routedict = {}

values = {}
for prop in meta["properties"]:
Expand All @@ -170,14 +171,8 @@ def instantiate_from_routes(
dimensions = infer_dimensions(meta, values)
inst = meta(dimensions=dimensions, id=id)

for key, value in routes.items():
try:
val = value.eval(magnitude=True, unit=meta.getprop(key).unit)
except MissingRelationError:
if not default:
raise
val = default[key]
inst[key] = val
for key, value in values.items():
inst[key] = value

return inst

Expand Down Expand Up @@ -283,6 +278,9 @@ def instantiate_all(
elif isinstance(meta, Namespace):
meta = dlite.get_instance(str(meta).rstrip("/#"))

if default:
allow_incomplete = True

routes = instance_routes(
meta=meta,
instances=instances,
Expand All @@ -305,11 +303,17 @@ def routedicts(n: int) -> "Generator[dict[str, int], None, None]":
if routedict and name in routedict:
outer[name] = routedict[name]
yield outer
else:
elif name in routes:
step = routes[name]
for inner in range(step.number_of_routes()):
outer[name] = inner
nroutes = step.number_of_routes()
if nroutes:
for inner in range(nroutes):
outer[name] = inner
yield outer
elif allow_incomplete:
yield outer
elif allow_incomplete:
yield outer

for route_dict in routedicts(len(property_names) - 1):
yield instantiate_from_routes(
Expand Down
1 change: 1 addition & 0 deletions examples/read-csv/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
try:
import pandas # noqa: F401
import pytables # noqa: F401
except ImportError:
import sys
sys.exit(44) # skip this test if pandas is not available
Expand Down

0 comments on commit c274db8

Please sign in to comment.