Coverage for bioimageio/core/commands.py: 78%
32 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-07-01 13:47 +0000
« prev ^ index » next coverage.py v7.9.1, created at 2025-07-01 13:47 +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.core.common import SupportedWeightsFormat
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
20WeightFormatArgAll = Literal[SupportedWeightsFormat, "all"]
21WeightFormatArgAny = Literal[SupportedWeightsFormat, "any"]
24def test(
25 descr: Union[ResourceDescr, InvalidDescr],
26 *,
27 weight_format: WeightFormatArgAll = "all",
28 devices: Optional[Union[str, Sequence[str]]] = None,
29 summary: Union[
30 Literal["display"], Path, Sequence[Union[Literal["display"], Path]]
31 ] = "display",
32 runtime_env: Union[
33 Literal["currently-active", "as-described"], Path
34 ] = "currently-active",
35 determinism: Literal["seed_only", "full"] = "seed_only",
36 format_version: Union[FormatVersionPlaceholder, str] = "discover",
37) -> int:
38 """Test a bioimageio resource.
40 Arguments as described in `bioimageio.core.cli.TestCmd`
41 """
42 if isinstance(descr, InvalidDescr):
43 test_summary = descr.validation_summary
44 else:
45 test_summary = test_description(
46 descr,
47 format_version=format_version,
48 weight_format=None if weight_format == "all" else weight_format,
49 devices=[devices] if isinstance(devices, str) else devices,
50 runtime_env=runtime_env,
51 determinism=determinism,
52 )
54 _ = test_summary.log(summary)
55 return 0 if test_summary.status == "passed" else 1
58def validate_format(
59 descr: Union[ResourceDescr, InvalidDescr],
60 summary: Union[Path, Sequence[Path]] = (),
61):
62 """DEPRECATED; Access the existing `validation_summary` attribute instead.
63 validate the meta data format of a bioimageio resource
65 Args:
66 descr: a bioimageio resource description
67 """
68 _ = descr.validation_summary.save(summary)
69 return 0 if descr.validation_summary.status in ("valid-format", "passed") else 1
72# TODO: absorb into `save_bioimageio_package`
73def package(
74 descr: ResourceDescr,
75 path: Path,
76 *,
77 weight_format: WeightFormatArgAll = "all",
78):
79 """Save a resource's metadata with its associated files.
81 Note: If `path` does not have a `.zip` suffix this command will save the
82 package as an unzipped folder instead.
84 Args:
85 descr: a bioimageio resource description
86 path: output path
87 weight-format: include only this single weight-format (if not 'all').
88 """
89 if isinstance(descr, InvalidDescr):
90 logged = descr.validation_summary.save()
91 msg = f"Invalid {descr.type} description."
92 if logged:
93 msg += f" Details saved to {logged}."
95 raise ValueError(msg)
97 if weight_format == "all":
98 weights_priority_order = None
99 else:
100 weights_priority_order = (weight_format,)
102 if path.suffix == ".zip":
103 _ = save_bioimageio_package(
104 descr,
105 output_path=path,
106 weights_priority_order=weights_priority_order,
107 )
108 else:
109 _ = save_bioimageio_package_as_folder(
110 descr,
111 output_path=path,
112 weights_priority_order=weights_priority_order,
113 )
114 return 0