Coverage for src/bioimageio/core/commands.py: 77%
31 statements
« prev ^ index » next coverage.py v7.10.7, created at 2025-09-22 09:21 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2025-09-22 09:21 +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) -> int:
56 """Test a bioimageio resource.
58 Arguments as described in `bioimageio.core.cli.TestCmd`
59 """
60 if isinstance(descr, InvalidDescr):
61 test_summary = descr.validation_summary
62 else:
63 test_summary = test_description(
64 descr,
65 format_version=format_version,
66 weight_format=None if weight_format == "all" else weight_format,
67 devices=[devices] if isinstance(devices, str) else devices,
68 runtime_env=runtime_env,
69 determinism=determinism,
70 )
72 _ = test_summary.log(summary)
73 return 0 if test_summary.status == "passed" else 1
76def validate_format(
77 descr: Union[ResourceDescr, InvalidDescr],
78 summary: Union[Path, Sequence[Path]] = (),
79):
80 """DEPRECATED; Access the existing `validation_summary` attribute instead.
81 validate the meta data format of a bioimageio resource
83 Args:
84 descr: a bioimageio resource description
85 """
86 _ = descr.validation_summary.save(summary)
87 return 0 if descr.validation_summary.status in ("valid-format", "passed") else 1
90# TODO: absorb into `save_bioimageio_package`
91def package(
92 descr: ResourceDescr,
93 path: Path,
94 *,
95 weight_format: WeightFormatArgAll = "all",
96):
97 """Save a resource's metadata with its associated files.
99 Note: If `path` does not have a `.zip` suffix this command will save the
100 package as an unzipped folder instead.
102 Args:
103 descr: a bioimageio resource description
104 path: output path
105 weight-format: include only this single weight-format (if not 'all').
106 """
107 if isinstance(descr, InvalidDescr):
108 logged = descr.validation_summary.save()
109 msg = f"Invalid {descr.type} description."
110 if logged:
111 msg += f" Details saved to {logged}."
113 raise ValueError(msg)
115 if weight_format == "all":
116 weights_priority_order = None
117 else:
118 weights_priority_order = (weight_format,)
120 if path.suffix == ".zip":
121 _ = save_bioimageio_package(
122 descr,
123 output_path=path,
124 weights_priority_order=weights_priority_order,
125 )
126 else:
127 _ = save_bioimageio_package_as_folder(
128 descr,
129 output_path=path,
130 weights_priority_order=weights_priority_order,
131 )
132 return 0