Coverage for src/bioimageio/core/commands.py: 71%
32 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 09:46 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 09:46 +0000
1"""These functions are used in the bioimageio command line interface
2defined in `bioimageio.core.cli`."""
4from __future__ import annotations
6from collections.abc import Sequence
7from pathlib import Path
8from typing import Literal
10from bioimageio.spec import (
11 InvalidDescr,
12 ResourceDescr,
13 save_bioimageio_package,
14 save_bioimageio_package_as_folder,
15)
16from bioimageio.spec._internal.types import FormatVersionPlaceholder
18from ._resource_tests import test_description
20# unfortunately this does not work with py3.9 and pydantic 2.11
21# from bioimageio.core.common import SupportedWeightsFormat
22# WeightFormatArgAll = Literal[SupportedWeightsFormat, "all"]
23# WeightFormatArgAny = Literal[SupportedWeightsFormat, "any"]
24# so we write out the literal explicitly
25WeightFormatArgAll = Literal[
26 "keras_hdf5",
27 "onnx",
28 "pytorch_state_dict",
29 "tensorflow_saved_model_bundle",
30 "torchscript",
31 "all",
32]
33WeightFormatArgAny = Literal[
34 "keras_hdf5",
35 "onnx",
36 "pytorch_state_dict",
37 "tensorflow_saved_model_bundle",
38 "torchscript",
39 "any",
40]
43def test(
44 descr: ResourceDescr | InvalidDescr,
45 *,
46 weight_format: WeightFormatArgAll = "all",
47 devices: str | Sequence[str] | None = None,
48 summary: Literal["display"]
49 | Path
50 | Sequence[Literal["display"] | Path] = "display",
51 runtime_env: Literal["currently-active", "as-described"]
52 | Path = "currently-active",
53 determinism: Literal["seed_only", "full"] = "seed_only",
54 format_version: FormatVersionPlaceholder | str = "discover",
55 working_dir: Path | None = None,
56) -> int:
57 """Test a bioimageio resource.
59 Arguments as described in [bioimageio.core.cli.TestCmd][]
60 """
61 if isinstance(descr, InvalidDescr): 61 ↛ 62line 61 didn't jump to line 62 because the condition on line 61 was never true
62 test_summary = descr.validation_summary
63 else:
64 test_summary = test_description(
65 descr,
66 format_version=format_version,
67 weight_format=None if weight_format == "all" else weight_format,
68 devices=[devices] if isinstance(devices, str) else devices,
69 runtime_env=runtime_env,
70 determinism=determinism,
71 working_dir=working_dir,
72 )
74 _ = test_summary.log(summary)
75 return 0 if test_summary.status == "passed" else 1
78def validate_format(
79 descr: ResourceDescr | InvalidDescr,
80 summary: Path | Sequence[Path] = (),
81):
82 """DEPRECATED; Access the existing `validation_summary` attribute instead.
83 validate the meta data format of a bioimageio resource
85 Args:
86 descr: a bioimageio resource description
87 """
88 _ = descr.validation_summary.save(summary)
89 return 0 if descr.validation_summary.status in ("valid-format", "passed") else 1
92# TODO: absorb into `save_bioimageio_package`
93def package(
94 descr: ResourceDescr,
95 path: Path,
96 *,
97 weight_format: WeightFormatArgAll = "all",
98):
99 """Save a resource's metadata with its associated files.
101 Note: If `path` does not have a `.zip` suffix this command will save the
102 package as an unzipped folder instead.
104 Args:
105 descr: a bioimageio resource description
106 path: output path
107 weight_format: include only this single weight-format (if not 'all').
108 """
109 if isinstance(descr, InvalidDescr): 109 ↛ 110line 109 didn't jump to line 110 because the condition on line 109 was never true
110 logged = descr.validation_summary.save()
111 msg = f"Invalid {descr.type} description."
112 if logged:
113 msg += f" Details saved to {logged}."
115 raise TypeError(msg)
117 if weight_format == "all":
118 weights_priority_order = None
119 else:
120 weights_priority_order = (weight_format,)
122 if path.suffix == ".zip": 122 ↛ 129line 122 didn't jump to line 129 because the condition on line 122 was always true
123 _ = save_bioimageio_package(
124 descr,
125 output_path=path,
126 weights_priority_order=weights_priority_order,
127 )
128 else:
129 _ = save_bioimageio_package_as_folder(
130 descr,
131 output_path=path,
132 weights_priority_order=weights_priority_order,
133 )
134 return 0