bioimageio.core.commands

These functions implement the logic of the bioimageio command line interface defined in bioimageio.core.cli.

 1"""These functions implement the logic of the bioimageio command line interface
 2defined in `bioimageio.core.cli`."""
 3
 4from pathlib import Path
 5from typing import Optional, Sequence, Union
 6
 7from typing_extensions import Literal
 8
 9from bioimageio.spec import (
10    InvalidDescr,
11    ResourceDescr,
12    save_bioimageio_package,
13    save_bioimageio_package_as_folder,
14)
15from bioimageio.spec.model.v0_5 import WeightsFormat
16
17from ._resource_tests import test_description
18
19WeightFormatArgAll = Literal[WeightsFormat, "all"]
20WeightFormatArgAny = Literal[WeightsFormat, "any"]
21
22
23def test(
24    descr: Union[ResourceDescr, InvalidDescr],
25    *,
26    weight_format: WeightFormatArgAll = "all",
27    devices: Optional[Union[str, Sequence[str]]] = None,
28    decimal: int = 4,
29) -> int:
30    """test a bioimageio resource
31
32    Args:
33        source: Path or URL to the bioimageio resource description file
34                (bioimageio.yaml or rdf.yaml) or to a zipped resource
35        weight_format: (model only) The weight format to use
36        devices: Device(s) to use for testing
37        decimal: Precision for numerical comparisons
38    """
39    if isinstance(descr, InvalidDescr):
40        descr.validation_summary.display()
41        return 1
42
43    summary = test_description(
44        descr,
45        weight_format=None if weight_format == "all" else weight_format,
46        devices=[devices] if isinstance(devices, str) else devices,
47        decimal=decimal,
48    )
49    summary.display()
50    return 0 if summary.status == "passed" else 1
51
52
53def validate_format(
54    descr: Union[ResourceDescr, InvalidDescr],
55):
56    """validate the meta data format of a bioimageio resource
57
58    Args:
59        descr: a bioimageio resource description
60    """
61    descr.validation_summary.display()
62    return 0 if descr.validation_summary.status == "passed" else 1
63
64
65def package(
66    descr: ResourceDescr, path: Path, *, weight_format: WeightFormatArgAll = "all"
67):
68    """Save a resource's metadata with its associated files.
69
70    Note: If `path` does not have a `.zip` suffix this command will save the
71          package as an unzipped folder instead.
72
73    Args:
74        descr: a bioimageio resource description
75        path: output path
76        weight-format: include only this single weight-format (if not 'all').
77    """
78    if isinstance(descr, InvalidDescr):
79        descr.validation_summary.display()
80        raise ValueError("resource description is invalid")
81
82    if weight_format == "all":
83        weights_priority_order = None
84    else:
85        weights_priority_order = (weight_format,)
86
87    if path.suffix == ".zip":
88        _ = save_bioimageio_package(
89            descr,
90            output_path=path,
91            weights_priority_order=weights_priority_order,
92        )
93    else:
94        _ = save_bioimageio_package_as_folder(
95            descr,
96            output_path=path,
97            weights_priority_order=weights_priority_order,
98        )
99    return 0
WeightFormatArgAll = typing.Literal['keras_hdf5', 'onnx', 'pytorch_state_dict', 'tensorflow_js', 'tensorflow_saved_model_bundle', 'torchscript', 'all']
WeightFormatArgAny = typing.Literal['keras_hdf5', 'onnx', 'pytorch_state_dict', 'tensorflow_js', 'tensorflow_saved_model_bundle', 'torchscript', 'any']
def test( descr: Union[Annotated[Union[Annotated[Union[bioimageio.spec.application.v0_2.ApplicationDescr, bioimageio.spec.ApplicationDescr], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None)], Annotated[Union[bioimageio.spec.dataset.v0_2.DatasetDescr, bioimageio.spec.DatasetDescr], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None)], Annotated[Union[bioimageio.spec.model.v0_4.ModelDescr, bioimageio.spec.ModelDescr], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None)], Annotated[Union[bioimageio.spec.NotebookDescr, bioimageio.spec.NotebookDescr], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None)]], Discriminator(discriminator='type', custom_error_type=None, custom_error_message=None, custom_error_context=None)], Annotated[Union[bioimageio.spec.generic.v0_2.GenericDescr, bioimageio.spec.GenericDescr], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None)], bioimageio.spec.InvalidDescr], *, weight_format: Literal['keras_hdf5', 'onnx', 'pytorch_state_dict', 'tensorflow_js', 'tensorflow_saved_model_bundle', 'torchscript', 'all'] = 'all', devices: Union[str, Sequence[str], NoneType] = None, decimal: int = 4) -> int:
24def test(
25    descr: Union[ResourceDescr, InvalidDescr],
26    *,
27    weight_format: WeightFormatArgAll = "all",
28    devices: Optional[Union[str, Sequence[str]]] = None,
29    decimal: int = 4,
30) -> int:
31    """test a bioimageio resource
32
33    Args:
34        source: Path or URL to the bioimageio resource description file
35                (bioimageio.yaml or rdf.yaml) or to a zipped resource
36        weight_format: (model only) The weight format to use
37        devices: Device(s) to use for testing
38        decimal: Precision for numerical comparisons
39    """
40    if isinstance(descr, InvalidDescr):
41        descr.validation_summary.display()
42        return 1
43
44    summary = test_description(
45        descr,
46        weight_format=None if weight_format == "all" else weight_format,
47        devices=[devices] if isinstance(devices, str) else devices,
48        decimal=decimal,
49    )
50    summary.display()
51    return 0 if summary.status == "passed" else 1

test a bioimageio resource

Arguments:
  • source: Path or URL to the bioimageio resource description file (bioimageio.yaml or rdf.yaml) or to a zipped resource
  • weight_format: (model only) The weight format to use
  • devices: Device(s) to use for testing
  • decimal: Precision for numerical comparisons
def validate_format( descr: Union[Annotated[Union[Annotated[Union[bioimageio.spec.application.v0_2.ApplicationDescr, bioimageio.spec.ApplicationDescr], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None)], Annotated[Union[bioimageio.spec.dataset.v0_2.DatasetDescr, bioimageio.spec.DatasetDescr], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None)], Annotated[Union[bioimageio.spec.model.v0_4.ModelDescr, bioimageio.spec.ModelDescr], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None)], Annotated[Union[bioimageio.spec.NotebookDescr, bioimageio.spec.NotebookDescr], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None)]], Discriminator(discriminator='type', custom_error_type=None, custom_error_message=None, custom_error_context=None)], Annotated[Union[bioimageio.spec.generic.v0_2.GenericDescr, bioimageio.spec.GenericDescr], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None)], bioimageio.spec.InvalidDescr]):
54def validate_format(
55    descr: Union[ResourceDescr, InvalidDescr],
56):
57    """validate the meta data format of a bioimageio resource
58
59    Args:
60        descr: a bioimageio resource description
61    """
62    descr.validation_summary.display()
63    return 0 if descr.validation_summary.status == "passed" else 1

validate the meta data format of a bioimageio resource

Arguments:
  • descr: a bioimageio resource description
def package( descr: Union[Annotated[Union[Annotated[Union[bioimageio.spec.application.v0_2.ApplicationDescr, bioimageio.spec.ApplicationDescr], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None)], Annotated[Union[bioimageio.spec.dataset.v0_2.DatasetDescr, bioimageio.spec.DatasetDescr], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None)], Annotated[Union[bioimageio.spec.model.v0_4.ModelDescr, bioimageio.spec.ModelDescr], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None)], Annotated[Union[bioimageio.spec.NotebookDescr, bioimageio.spec.NotebookDescr], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None)]], Discriminator(discriminator='type', custom_error_type=None, custom_error_message=None, custom_error_context=None)], Annotated[Union[bioimageio.spec.generic.v0_2.GenericDescr, bioimageio.spec.GenericDescr], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None)]], path: pathlib.Path, *, weight_format: Literal['keras_hdf5', 'onnx', 'pytorch_state_dict', 'tensorflow_js', 'tensorflow_saved_model_bundle', 'torchscript', 'all'] = 'all'):
 66def package(
 67    descr: ResourceDescr, path: Path, *, weight_format: WeightFormatArgAll = "all"
 68):
 69    """Save a resource's metadata with its associated files.
 70
 71    Note: If `path` does not have a `.zip` suffix this command will save the
 72          package as an unzipped folder instead.
 73
 74    Args:
 75        descr: a bioimageio resource description
 76        path: output path
 77        weight-format: include only this single weight-format (if not 'all').
 78    """
 79    if isinstance(descr, InvalidDescr):
 80        descr.validation_summary.display()
 81        raise ValueError("resource description is invalid")
 82
 83    if weight_format == "all":
 84        weights_priority_order = None
 85    else:
 86        weights_priority_order = (weight_format,)
 87
 88    if path.suffix == ".zip":
 89        _ = save_bioimageio_package(
 90            descr,
 91            output_path=path,
 92            weights_priority_order=weights_priority_order,
 93        )
 94    else:
 95        _ = save_bioimageio_package_as_folder(
 96            descr,
 97            output_path=path,
 98            weights_priority_order=weights_priority_order,
 99        )
100    return 0

Save a resource's metadata with its associated files.

Note: If path does not have a .zip suffix this command will save the package as an unzipped folder instead.

Arguments:
  • descr: a bioimageio resource description
  • path: output path
  • weight-format: include only this single weight-format (if not 'all').