Skip to content

Commit 3134dc7

Browse files
committed
improve attachment handling
1 parent 75bea45 commit 3134dc7

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

bioimageio/core/build_spec/build_model.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from bioimageio.core import export_resource_package, load_raw_resource_description
1515
from bioimageio.core.resource_io.nodes import URI
1616
from bioimageio.core.resource_io.utils import resolve_local_source, resolve_source
17+
from bioimageio.spec.shared import fields
1718
from bioimageio.spec.shared.raw_nodes import ImportableSourceFile, ImportableModule
1819

1920
try:
@@ -363,7 +364,7 @@ def get_size(path):
363364
"allow_tiling": True,
364365
"model_keys": None,
365366
}
366-
return {"deepimagej": config}, attachments
367+
return {"deepimagej": config}, [Path(a) for a in attachments]
367368

368369

369370
def _write_sample_data(input_paths, output_paths, input_axes, output_axes, export_folder: Path):
@@ -657,6 +658,20 @@ def build_model(
657658
root = "."
658659
root = Path(root)
659660

661+
if attachments is not None:
662+
assert isinstance(attachments, dict)
663+
if "files" in attachments:
664+
afiles = attachments["files"]
665+
if isinstance(afiles, str):
666+
afiles = [afiles]
667+
668+
if isinstance(afiles, list):
669+
afiles = _ensure_local_or_url(afiles, root)
670+
else:
671+
raise TypeError(attachments)
672+
673+
attachments["files"] = afiles
674+
660675
#
661676
# generate the model specific fields
662677
#
@@ -783,7 +798,7 @@ def build_model(
783798
elif "files" not in attachments:
784799
attachments["files"] = ij_attachments
785800
else:
786-
attachments["files"].extend(ij_attachments)
801+
attachments["files"] = list(set(attachments["files"]) | set(ij_attachments))
787802

788803
if links is None:
789804
links = ["deepimagej/deepimagej"]
@@ -814,15 +829,7 @@ def build_model(
814829
kwargs = {k: v for k, v in optional_kwargs.items() if v is not None}
815830

816831
if attachments is not None:
817-
file_attachments = attachments.pop("files", None)
818-
# this is my attempt at creating the correct attachments, but this is still not working
819-
# (the attachments field is empty after serialization and the content of attachments:files is not copied)
820-
# I also tried this and it doesn't work either:
821-
# spec.model.schema.Attachments().load({"files": file_attachments})
822-
if file_attachments is None:
823-
kwargs["attachments"] = model_spec.raw_nodes.Attachments(**attachments)
824-
else:
825-
kwargs["attachments"] = model_spec.raw_nodes.Attachments(files=file_attachments, **attachments)
832+
kwargs["attachments"] = model_spec.raw_nodes.Attachments(**attachments)
826833
if dependencies is not None:
827834
kwargs["dependencies"] = _get_dependencies(dependencies, root)
828835
if maintainers is not None:

0 commit comments

Comments
 (0)