Coverage for src / bioimageio / core / commands.py: 77%
31 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-02-13 09:46 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-02-13 09:46 +0000
1"""These functions are used in the bioimageio command line interface
2defined in `bioimageio.core.cli`."""
4from pathlib import Path
5from typing import Optional, Sequence, Union
7from typing_extensions import Literal
9from bioimageio.spec import (
10 InvalidDescr,
11 ResourceDescr,
12 save_bioimageio_package,
13 save_bioimageio_package_as_folder,
14)
15from bioimageio.spec._internal.types import FormatVersionPlaceholder
17from ._resource_tests import test_description
19# unfortunately this does not work with py3.9 and pydantic 2.11
20# from bioimageio.core.common import SupportedWeightsFormat
21# WeightFormatArgAll = Literal[SupportedWeightsFormat, "all"]
22# WeightFormatArgAny = Literal[SupportedWeightsFormat, "any"]
23# so we write out the literal explicitly
24WeightFormatArgAll = Literal[
25 "keras_hdf5",
26 "onnx",
27 "pytorch_state_dict",
28 "tensorflow_saved_model_bundle",
29 "torchscript",
30 "all",
31]
32WeightFormatArgAny = Literal[
33 "keras_hdf5",
34 "onnx",
35 "pytorch_state_dict",
36 "tensorflow_saved_model_bundle",
37 "torchscript",
38 "any",
39]
42def test(
43 descr: Union[ResourceDescr, InvalidDescr],
44 *,
45 weight_format: WeightFormatArgAll = "all",
46 devices: Optional[Union[str, Sequence[str]]] = None,
47 summary: Union[
48 Literal["display"], Path, Sequence[Union[Literal["display"], Path]]
49 ] = "display",
50 runtime_env: Union[
51 Literal["currently-active", "as-described"], Path
52 ] = "currently-active",
53 determinism: Literal["seed_only", "full"] = "seed_only",
54 format_version: Union[FormatVersionPlaceholder, str] = "discover",
55 working_dir: Optional[Path] = None,
56) -> int:
57 """Test a bioimageio resource.
59 Arguments as described in [bioimageio.core.cli.TestCmd][]
60 """
61 if isinstance(descr, InvalidDescr):
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: Union[ResourceDescr, InvalidDescr],
80 summary: Union[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):
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 ValueError(msg)
117 if weight_format == "all":
118 weights_priority_order = None
119 else:
120 weights_priority_order = (weight_format,)
122 if path.suffix == ".zip":
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