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.spec import ( 10 InvalidDescr, 11 ResourceDescr, 12 save_bioimageio_package, 13 save_bioimageio_package_as_folder, 14) 15from bioimageio.spec._internal.types import FormatVersionPlaceholder 16 17from ._resource_tests import test_description 18 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] 40 41 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. 57 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 ) 71 72 _ = test_summary.log(summary) 73 return 0 if test_summary.status == "passed" else 1 74 75 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 82 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 88 89 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. 98 99 Note: If `path` does not have a `.zip` suffix this command will save the 100 package as an unzipped folder instead. 101 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}." 112 113 raise ValueError(msg) 114 115 if weight_format == "all": 116 weights_priority_order = None 117 else: 118 weights_priority_order = (weight_format,) 119 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
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', format_version: Union[Literal['latest', 'discover'], str] = 'discover') -> int:
43def test( 44 descr: Union[ResourceDescr, InvalidDescr], 45 *, 46 weight_format: WeightFormatArgAll = "all", 47 devices: Optional[Union[str, Sequence[str]]] = None, 48 summary: Union[ 49 Literal["display"], Path, Sequence[Union[Literal["display"], Path]] 50 ] = "display", 51 runtime_env: Union[ 52 Literal["currently-active", "as-described"], Path 53 ] = "currently-active", 54 determinism: Literal["seed_only", "full"] = "seed_only", 55 format_version: Union[FormatVersionPlaceholder, str] = "discover", 56) -> int: 57 """Test a bioimageio resource. 58 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 ) 72 73 _ = test_summary.log(summary) 74 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]] = ()):
77def validate_format( 78 descr: Union[ResourceDescr, InvalidDescr], 79 summary: Union[Path, Sequence[Path]] = (), 80): 81 """DEPRECATED; Access the existing `validation_summary` attribute instead. 82 validate the meta data format of a bioimageio resource 83 84 Args: 85 descr: a bioimageio resource description 86 """ 87 _ = descr.validation_summary.save(summary) 88 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'):
92def package( 93 descr: ResourceDescr, 94 path: Path, 95 *, 96 weight_format: WeightFormatArgAll = "all", 97): 98 """Save a resource's metadata with its associated files. 99 100 Note: If `path` does not have a `.zip` suffix this command will save the 101 package as an unzipped folder instead. 102 103 Args: 104 descr: a bioimageio resource description 105 path: output path 106 weight-format: include only this single weight-format (if not 'all'). 107 """ 108 if isinstance(descr, InvalidDescr): 109 logged = descr.validation_summary.save() 110 msg = f"Invalid {descr.type} description." 111 if logged: 112 msg += f" Details saved to {logged}." 113 114 raise ValueError(msg) 115 116 if weight_format == "all": 117 weights_priority_order = None 118 else: 119 weights_priority_order = (weight_format,) 120 121 if path.suffix == ".zip": 122 _ = save_bioimageio_package( 123 descr, 124 output_path=path, 125 weights_priority_order=weights_priority_order, 126 ) 127 else: 128 _ = save_bioimageio_package_as_folder( 129 descr, 130 output_path=path, 131 weights_priority_order=weights_priority_order, 132 ) 133 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').