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) 16from bioimageio.spec._internal.types import FormatVersionPlaceholder 17 18from ._resource_tests import test_description 19 20WeightFormatArgAll = Literal[SupportedWeightsFormat, "all"] 21WeightFormatArgAny = Literal[SupportedWeightsFormat, "any"] 22 23 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. 39 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 ) 53 54 _ = test_summary.log(summary) 55 return 0 if test_summary.status == "passed" else 1 56 57 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 64 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 70 71 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. 80 81 Note: If `path` does not have a `.zip` suffix this command will save the 82 package as an unzipped folder instead. 83 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}." 94 95 raise ValueError(msg) 96 97 if weight_format == "all": 98 weights_priority_order = None 99 else: 100 weights_priority_order = (weight_format,) 101 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
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:
25def test( 26 descr: Union[ResourceDescr, InvalidDescr], 27 *, 28 weight_format: WeightFormatArgAll = "all", 29 devices: Optional[Union[str, Sequence[str]]] = None, 30 summary: Union[ 31 Literal["display"], Path, Sequence[Union[Literal["display"], Path]] 32 ] = "display", 33 runtime_env: Union[ 34 Literal["currently-active", "as-described"], Path 35 ] = "currently-active", 36 determinism: Literal["seed_only", "full"] = "seed_only", 37 format_version: Union[FormatVersionPlaceholder, str] = "discover", 38) -> int: 39 """Test a bioimageio resource. 40 41 Arguments as described in `bioimageio.core.cli.TestCmd` 42 """ 43 if isinstance(descr, InvalidDescr): 44 test_summary = descr.validation_summary 45 else: 46 test_summary = test_description( 47 descr, 48 format_version=format_version, 49 weight_format=None if weight_format == "all" else weight_format, 50 devices=[devices] if isinstance(devices, str) else devices, 51 runtime_env=runtime_env, 52 determinism=determinism, 53 ) 54 55 _ = test_summary.log(summary) 56 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]] = ()):
59def validate_format( 60 descr: Union[ResourceDescr, InvalidDescr], 61 summary: Union[Path, Sequence[Path]] = (), 62): 63 """DEPRECATED; Access the existing `validation_summary` attribute instead. 64 validate the meta data format of a bioimageio resource 65 66 Args: 67 descr: a bioimageio resource description 68 """ 69 _ = descr.validation_summary.save(summary) 70 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'):
74def package( 75 descr: ResourceDescr, 76 path: Path, 77 *, 78 weight_format: WeightFormatArgAll = "all", 79): 80 """Save a resource's metadata with its associated files. 81 82 Note: If `path` does not have a `.zip` suffix this command will save the 83 package as an unzipped folder instead. 84 85 Args: 86 descr: a bioimageio resource description 87 path: output path 88 weight-format: include only this single weight-format (if not 'all'). 89 """ 90 if isinstance(descr, InvalidDescr): 91 logged = descr.validation_summary.save() 92 msg = f"Invalid {descr.type} description." 93 if logged: 94 msg += f" Details saved to {logged}." 95 96 raise ValueError(msg) 97 98 if weight_format == "all": 99 weights_priority_order = None 100 else: 101 weights_priority_order = (weight_format,) 102 103 if path.suffix == ".zip": 104 _ = save_bioimageio_package( 105 descr, 106 output_path=path, 107 weights_priority_order=weights_priority_order, 108 ) 109 else: 110 _ = save_bioimageio_package_as_folder( 111 descr, 112 output_path=path, 113 weights_priority_order=weights_priority_order, 114 ) 115 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').