bioimageio.core.commands

These functions are used in the bioimageio command line interface defined in bioimageio.core.cli.

  1"""These functions are used in 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.core.common import SupportedWeightsFormat
 10from bioimageio.spec import (
 11    InvalidDescr,
 12    ResourceDescr,
 13    save_bioimageio_package,
 14    save_bioimageio_package_as_folder,
 15)
 16
 17from ._resource_tests import test_description
 18
 19WeightFormatArgAll = Literal[SupportedWeightsFormat, "all"]
 20WeightFormatArgAny = Literal[SupportedWeightsFormat, "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    summary: Union[
 29        Literal["display"], Path, Sequence[Union[Literal["display"], Path]]
 30    ] = "display",
 31    runtime_env: Union[
 32        Literal["currently-active", "as-described"], Path
 33    ] = "currently-active",
 34    determinism: Literal["seed_only", "full"] = "seed_only",
 35) -> int:
 36    """Test a bioimageio resource.
 37
 38    Arguments as described in `bioimageio.core.cli.TestCmd`
 39    """
 40    if isinstance(descr, InvalidDescr):
 41        test_summary = descr.validation_summary
 42    else:
 43        test_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            runtime_env=runtime_env,
 48            determinism=determinism,
 49        )
 50
 51    _ = test_summary.log(summary)
 52    return 0 if test_summary.status == "passed" else 1
 53
 54
 55def validate_format(
 56    descr: Union[ResourceDescr, InvalidDescr],
 57    summary: Union[Path, Sequence[Path]] = (),
 58):
 59    """DEPRECATED; Access the existing `validation_summary` attribute instead.
 60    validate the meta data format of a bioimageio resource
 61
 62    Args:
 63        descr: a bioimageio resource description
 64    """
 65    _ = descr.validation_summary.save(summary)
 66    return 0 if descr.validation_summary.status in ("valid-format", "passed") else 1
 67
 68
 69# TODO: absorb into `save_bioimageio_package`
 70def package(
 71    descr: ResourceDescr,
 72    path: Path,
 73    *,
 74    weight_format: WeightFormatArgAll = "all",
 75):
 76    """Save a resource's metadata with its associated files.
 77
 78    Note: If `path` does not have a `.zip` suffix this command will save the
 79          package as an unzipped folder instead.
 80
 81    Args:
 82        descr: a bioimageio resource description
 83        path: output path
 84        weight-format: include only this single weight-format (if not 'all').
 85    """
 86    if isinstance(descr, InvalidDescr):
 87        logged = descr.validation_summary.save()
 88        msg = f"Invalid {descr.type} description."
 89        if logged:
 90            msg += f" Details saved to {logged}."
 91
 92        raise ValueError(msg)
 93
 94    if weight_format == "all":
 95        weights_priority_order = None
 96    else:
 97        weights_priority_order = (weight_format,)
 98
 99    if path.suffix == ".zip":
100        _ = save_bioimageio_package(
101            descr,
102            output_path=path,
103            weights_priority_order=weights_priority_order,
104        )
105    else:
106        _ = save_bioimageio_package_as_folder(
107            descr,
108            output_path=path,
109            weights_priority_order=weights_priority_order,
110        )
111    return 0
WeightFormatArgAll = typing.Literal['keras_hdf5', 'onnx', 'pytorch_state_dict', 'tensorflow_saved_model_bundle', 'torchscript', 'all']
WeightFormatArgAny = typing.Literal['keras_hdf5', 'onnx', 'pytorch_state_dict', 'tensorflow_saved_model_bundle', 'torchscript', 'any']
def test( descr: Union[Annotated[Union[Annotated[Union[Annotated[bioimageio.spec.application.v0_2.ApplicationDescr, FieldInfo(annotation=NoneType, required=True, title='application 0.2')], Annotated[bioimageio.spec.ApplicationDescr, FieldInfo(annotation=NoneType, required=True, title='application 0.3')]], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None), FieldInfo(annotation=NoneType, required=True, title='application')], Annotated[Union[Annotated[bioimageio.spec.dataset.v0_2.DatasetDescr, FieldInfo(annotation=NoneType, required=True, title='dataset 0.2')], Annotated[bioimageio.spec.DatasetDescr, FieldInfo(annotation=NoneType, required=True, title='dataset 0.3')]], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None), FieldInfo(annotation=NoneType, required=True, title='dataset')], Annotated[Union[Annotated[bioimageio.spec.model.v0_4.ModelDescr, FieldInfo(annotation=NoneType, required=True, title='model 0.4')], Annotated[bioimageio.spec.ModelDescr, FieldInfo(annotation=NoneType, required=True, title='model 0.5')]], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None), FieldInfo(annotation=NoneType, required=True, title='model')], Annotated[Union[Annotated[bioimageio.spec.NotebookDescr, FieldInfo(annotation=NoneType, required=True, title='notebook 0.2')], Annotated[bioimageio.spec.NotebookDescr, FieldInfo(annotation=NoneType, required=True, title='notebook 0.3')]], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None), FieldInfo(annotation=NoneType, required=True, title='notebook')]], Discriminator(discriminator='type', custom_error_type=None, custom_error_message=None, custom_error_context=None)], Annotated[Union[Annotated[bioimageio.spec.generic.v0_2.GenericDescr, FieldInfo(annotation=NoneType, required=True, title='generic 0.2')], Annotated[bioimageio.spec.GenericDescr, FieldInfo(annotation=NoneType, required=True, title='generic 0.3')]], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None), FieldInfo(annotation=NoneType, required=True, title='generic')], bioimageio.spec.InvalidDescr], *, weight_format: Literal['keras_hdf5', 'onnx', 'pytorch_state_dict', 'tensorflow_saved_model_bundle', 'torchscript', 'all'] = 'all', devices: Union[str, Sequence[str], NoneType] = None, summary: Union[Literal['display'], pathlib.Path, Sequence[Union[Literal['display'], pathlib.Path]]] = 'display', runtime_env: Union[Literal['currently-active', 'as-described'], pathlib.Path] = 'currently-active', determinism: Literal['seed_only', 'full'] = 'seed_only') -> int:
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) -> int:
37    """Test a bioimageio resource.
38
39    Arguments as described in `bioimageio.core.cli.TestCmd`
40    """
41    if isinstance(descr, InvalidDescr):
42        test_summary = descr.validation_summary
43    else:
44        test_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            runtime_env=runtime_env,
49            determinism=determinism,
50        )
51
52    _ = test_summary.log(summary)
53    return 0 if test_summary.status == "passed" else 1

Test a bioimageio resource.

Arguments as described in bioimageio.core.cli.TestCmd

def validate_format( descr: Union[Annotated[Union[Annotated[Union[Annotated[bioimageio.spec.application.v0_2.ApplicationDescr, FieldInfo(annotation=NoneType, required=True, title='application 0.2')], Annotated[bioimageio.spec.ApplicationDescr, FieldInfo(annotation=NoneType, required=True, title='application 0.3')]], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None), FieldInfo(annotation=NoneType, required=True, title='application')], Annotated[Union[Annotated[bioimageio.spec.dataset.v0_2.DatasetDescr, FieldInfo(annotation=NoneType, required=True, title='dataset 0.2')], Annotated[bioimageio.spec.DatasetDescr, FieldInfo(annotation=NoneType, required=True, title='dataset 0.3')]], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None), FieldInfo(annotation=NoneType, required=True, title='dataset')], Annotated[Union[Annotated[bioimageio.spec.model.v0_4.ModelDescr, FieldInfo(annotation=NoneType, required=True, title='model 0.4')], Annotated[bioimageio.spec.ModelDescr, FieldInfo(annotation=NoneType, required=True, title='model 0.5')]], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None), FieldInfo(annotation=NoneType, required=True, title='model')], Annotated[Union[Annotated[bioimageio.spec.NotebookDescr, FieldInfo(annotation=NoneType, required=True, title='notebook 0.2')], Annotated[bioimageio.spec.NotebookDescr, FieldInfo(annotation=NoneType, required=True, title='notebook 0.3')]], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None), FieldInfo(annotation=NoneType, required=True, title='notebook')]], Discriminator(discriminator='type', custom_error_type=None, custom_error_message=None, custom_error_context=None)], Annotated[Union[Annotated[bioimageio.spec.generic.v0_2.GenericDescr, FieldInfo(annotation=NoneType, required=True, title='generic 0.2')], Annotated[bioimageio.spec.GenericDescr, FieldInfo(annotation=NoneType, required=True, title='generic 0.3')]], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None), FieldInfo(annotation=NoneType, required=True, title='generic')], bioimageio.spec.InvalidDescr], summary: Union[pathlib.Path, Sequence[pathlib.Path]] = ()):
56def validate_format(
57    descr: Union[ResourceDescr, InvalidDescr],
58    summary: Union[Path, Sequence[Path]] = (),
59):
60    """DEPRECATED; Access the existing `validation_summary` attribute instead.
61    validate the meta data format of a bioimageio resource
62
63    Args:
64        descr: a bioimageio resource description
65    """
66    _ = descr.validation_summary.save(summary)
67    return 0 if descr.validation_summary.status in ("valid-format", "passed") else 1

DEPRECATED; Access the existing validation_summary attribute instead. validate the meta data format of a bioimageio resource

Arguments:
  • descr: a bioimageio resource description
def package( descr: Union[Annotated[Union[Annotated[Union[Annotated[bioimageio.spec.application.v0_2.ApplicationDescr, FieldInfo(annotation=NoneType, required=True, title='application 0.2')], Annotated[bioimageio.spec.ApplicationDescr, FieldInfo(annotation=NoneType, required=True, title='application 0.3')]], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None), FieldInfo(annotation=NoneType, required=True, title='application')], Annotated[Union[Annotated[bioimageio.spec.dataset.v0_2.DatasetDescr, FieldInfo(annotation=NoneType, required=True, title='dataset 0.2')], Annotated[bioimageio.spec.DatasetDescr, FieldInfo(annotation=NoneType, required=True, title='dataset 0.3')]], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None), FieldInfo(annotation=NoneType, required=True, title='dataset')], Annotated[Union[Annotated[bioimageio.spec.model.v0_4.ModelDescr, FieldInfo(annotation=NoneType, required=True, title='model 0.4')], Annotated[bioimageio.spec.ModelDescr, FieldInfo(annotation=NoneType, required=True, title='model 0.5')]], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None), FieldInfo(annotation=NoneType, required=True, title='model')], Annotated[Union[Annotated[bioimageio.spec.NotebookDescr, FieldInfo(annotation=NoneType, required=True, title='notebook 0.2')], Annotated[bioimageio.spec.NotebookDescr, FieldInfo(annotation=NoneType, required=True, title='notebook 0.3')]], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None), FieldInfo(annotation=NoneType, required=True, title='notebook')]], Discriminator(discriminator='type', custom_error_type=None, custom_error_message=None, custom_error_context=None)], Annotated[Union[Annotated[bioimageio.spec.generic.v0_2.GenericDescr, FieldInfo(annotation=NoneType, required=True, title='generic 0.2')], Annotated[bioimageio.spec.GenericDescr, FieldInfo(annotation=NoneType, required=True, title='generic 0.3')]], Discriminator(discriminator='format_version', custom_error_type=None, custom_error_message=None, custom_error_context=None), FieldInfo(annotation=NoneType, required=True, title='generic')]], path: pathlib.Path, *, weight_format: Literal['keras_hdf5', 'onnx', 'pytorch_state_dict', 'tensorflow_saved_model_bundle', 'torchscript', 'all'] = 'all'):
 71def package(
 72    descr: ResourceDescr,
 73    path: Path,
 74    *,
 75    weight_format: WeightFormatArgAll = "all",
 76):
 77    """Save a resource's metadata with its associated files.
 78
 79    Note: If `path` does not have a `.zip` suffix this command will save the
 80          package as an unzipped folder instead.
 81
 82    Args:
 83        descr: a bioimageio resource description
 84        path: output path
 85        weight-format: include only this single weight-format (if not 'all').
 86    """
 87    if isinstance(descr, InvalidDescr):
 88        logged = descr.validation_summary.save()
 89        msg = f"Invalid {descr.type} description."
 90        if logged:
 91            msg += f" Details saved to {logged}."
 92
 93        raise ValueError(msg)
 94
 95    if weight_format == "all":
 96        weights_priority_order = None
 97    else:
 98        weights_priority_order = (weight_format,)
 99
100    if path.suffix == ".zip":
101        _ = save_bioimageio_package(
102            descr,
103            output_path=path,
104            weights_priority_order=weights_priority_order,
105        )
106    else:
107        _ = save_bioimageio_package_as_folder(
108            descr,
109            output_path=path,
110            weights_priority_order=weights_priority_order,
111        )
112    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').