bioimageio.spec

License PyPI conda-version downloads conda-forge downloads code style coverage

Specifications for bioimage.io

This repository contains the specifications of the standard format defined by the bioimage.io community for the content (i.e., models, datasets and applications) in the bioimage.io website. Each item in the content is always described using a YAML 1.2 file named rdf.yaml or bioimageio.yaml. This rdf.yaml \ bioimageio.yaml--- along with the files referenced in it --- can be downloaded from or uploaded to the bioimage.io website and may be produced or consumed by bioimage.io-compatible consumers (e.g., image analysis software like ilastik).

These are the rules and format that bioimage.io-compatible resources must fulfill.

Note that the Python package PyYAML does not support YAML 1.2 . We therefore use and recommend ruyaml. For differences see https://ruamelyaml.readthedocs.io/en/latest/pyyaml.

Please also note that the best way to check whether your rdf.yaml file is bioimage.io-compliant is to call bioimageio.core.validate from the bioimageio.core Python package. The bioimageio.core Python package also provides the bioimageio command line interface (CLI) with the validate command:

bioimageio validate path/to/your/rdf.yaml

Format version overview

All bioimage.io description formats are defined as Pydantic models.

Type Format Version Documentation1 Developer Documentation2
model 0.5
0.4
model 0.5
model 0.4
ModelDescr_v0_5
ModelDescr_v0_4
dataset 0.3
0.2
dataset 0.3
dataset 0.2
DatasetDescr_v0_3
DatasetDescr_v0_2
notebook 0.3
0.2
notebook 0.3
notebook 0.2
NotebookDescr_v0_3
NotebookDescr_v0_2
application 0.3
0.2
application 0.3
application 0.2
ApplicationDescr_v0_3
ApplicationDescr_v0_2
generic 0.3
0.2
- GenericDescr_v0_3
GenericDescr_v0_2

JSON Schema

Simplified descriptions are available as JSON Schema (generated with Pydantic):

bioimageio.spec version JSON Schema documentation3
latest bioimageio_schema_latest.json latest documentation
0.5 bioimageio_schema_v0-5.json 0.5 documentation

Note: bioimageio_schema_v0-5.json and bioimageio_schema_latest.json are identical, but bioimageio_schema_latest.json will eventually refer to the future bioimageio_schema_v0-6.json.

Flattened, interactive docs

A flattened view of the types used by the spec that also shows values constraints.

rendered

You can also generate these docs locally by running PYTHONPATH=./scripts python -m interactive_docs

Examples

We provide some bioimageio.yaml/rdf.yaml example files to describe models, applications, notebooks and datasets; more examples are available at bioimage.io. There is also an example notebook demonstrating how to programmatically access the models, applications, notebooks and datasets descriptions in Python. For integration of bioimageio resources we recommend the bioimageio.core Python package.

💁 Recommendations

  • Use the bioimageio.core Python package to not only validate the format of your bioimageio.yaml/rdf.yaml file, but also test and deploy it (e.g. model inference).
  • bioimageio.spec keeps evolving. Try to use and upgrade to the most current format version! Note: The command line interface bioimageio (part of bioimageio.core) has the update-format command to help you with that.

⌨ bioimageio command-line interface (CLI)

The bioimageio CLI has moved to bioimageio.core.

🖥 Installation

bioimageio.spec can be installed with either conda or pip. We recommend installing bioimageio.core instead to get access to the Python programmatic features available in the BioImage.IO community:

conda install -c conda-forge bioimageio.core

or

pip install -U bioimageio.core

Still, for a lighter package or just testing, you can install the bioimageio.spec package solely:

conda install -c conda-forge bioimageio.spec

or

pip install -U bioimageio.spec

🏞 Environment variables

TODO: link to settings in dev docs

🤝 How to contribute

♥ Contributors

<a href=bioimageio.spec contributors" src="https://contrib.rocks/image?repo=bioimage-io/spec-bioimage-io" />

Made with contrib.rocks.

🛈 Versioining scheme

To keep the bioimageio.spec Python package version in sync with the (model) description format version, bioimageio.spec is versioned as MAJOR.MINRO.PATCH.LIB, where MAJOR.MINRO.PATCH correspond to the latest model description format version implemented and LIB may be bumpbed for library changes that do not affect the format version. This change was introduced with bioimageio.spec 0.5.3.1.

Δ Changelog

The changelog of the bioimageio.spec Python package and the changes to the resource description format it implements can be found here.


  1. JSON Schema based documentation generated with json-schema-for-humans

  2. JSON Schema based documentation generated with json-schema-for-humans

  3. Part of the bioimageio.spec package documentation generated with pdoc

  1"""
  2.. include:: ../../README.md
  3"""
  4
  5from . import (
  6    application,
  7    common,
  8    conda_env,
  9    dataset,
 10    generic,
 11    model,
 12    pretty_validation_errors,
 13    summary,
 14    utils,
 15)
 16from ._description import (
 17    LatestResourceDescr,
 18    ResourceDescr,
 19    SpecificResourceDescr,
 20    build_description,
 21    dump_description,
 22    validate_format,
 23)
 24from ._get_conda_env import BioimageioCondaEnv, get_conda_env
 25from ._internal import settings
 26from ._internal.common_nodes import InvalidDescr
 27from ._internal.constants import VERSION
 28from ._internal.validation_context import ValidationContext, get_validation_context
 29from ._io import (
 30    load_dataset_description,
 31    load_description,
 32    load_description_and_validate_format_only,
 33    load_model_description,
 34    save_bioimageio_yaml_only,
 35    update_format,
 36    update_hashes,
 37)
 38from ._package import (
 39    get_resource_package_content,
 40    save_bioimageio_package,
 41    save_bioimageio_package_as_folder,
 42    save_bioimageio_package_to_stream,
 43)
 44from .application import AnyApplicationDescr, ApplicationDescr
 45from .dataset import AnyDatasetDescr, DatasetDescr
 46from .generic import AnyGenericDescr, GenericDescr
 47from .model import AnyModelDescr, ModelDescr
 48from .notebook import AnyNotebookDescr, NotebookDescr
 49from .pretty_validation_errors import enable_pretty_validation_errors_in_ipynb
 50from .summary import ValidationSummary
 51
 52__version__ = VERSION
 53
 54__all__ = [
 55    "__version__",
 56    "AnyApplicationDescr",
 57    "AnyDatasetDescr",
 58    "AnyGenericDescr",
 59    "AnyModelDescr",
 60    "AnyNotebookDescr",
 61    "application",
 62    "ApplicationDescr",
 63    "BioimageioCondaEnv",
 64    "build_description",
 65    "common",
 66    "conda_env",
 67    "dataset",
 68    "DatasetDescr",
 69    "dump_description",
 70    "enable_pretty_validation_errors_in_ipynb",
 71    "generic",
 72    "GenericDescr",
 73    "get_conda_env",
 74    "get_resource_package_content",
 75    "get_validation_context",
 76    "InvalidDescr",
 77    "LatestResourceDescr",
 78    "load_dataset_description",
 79    "load_description_and_validate_format_only",
 80    "load_description",
 81    "load_model_description",
 82    "model",
 83    "ModelDescr",
 84    "NotebookDescr",
 85    "pretty_validation_errors",
 86    "ResourceDescr",
 87    "save_bioimageio_package_as_folder",
 88    "save_bioimageio_package_to_stream",
 89    "save_bioimageio_package",
 90    "save_bioimageio_yaml_only",
 91    "settings",
 92    "SpecificResourceDescr",
 93    "summary",
 94    "update_format",
 95    "update_hashes",
 96    "utils",
 97    "validate_format",
 98    "ValidationContext",
 99    "ValidationSummary",
100]
__version__ = '0.5.4.4'
AnyApplicationDescr = typing.Annotated[typing.Union[typing.Annotated[bioimageio.spec.application.v0_2.ApplicationDescr, FieldInfo(annotation=NoneType, required=True, title='application 0.2')], typing.Annotated[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')]
AnyDatasetDescr = typing.Annotated[typing.Union[typing.Annotated[bioimageio.spec.dataset.v0_2.DatasetDescr, FieldInfo(annotation=NoneType, required=True, title='dataset 0.2')], typing.Annotated[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')]
AnyGenericDescr = typing.Annotated[typing.Union[typing.Annotated[bioimageio.spec.generic.v0_2.GenericDescr, FieldInfo(annotation=NoneType, required=True, title='generic 0.2')], typing.Annotated[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')]
AnyModelDescr = typing.Annotated[typing.Union[typing.Annotated[bioimageio.spec.model.v0_4.ModelDescr, FieldInfo(annotation=NoneType, required=True, title='model 0.4')], typing.Annotated[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')]
AnyNotebookDescr = typing.Annotated[typing.Union[typing.Annotated[NotebookDescr, FieldInfo(annotation=NoneType, required=True, title='notebook 0.2')], typing.Annotated[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')]
class ApplicationDescr(bioimageio.spec.generic.v0_3.GenericDescrBase):
33class ApplicationDescr(GenericDescrBase):
34    """Bioimage.io description of an application."""
35
36    implemented_type: ClassVar[Literal["application"]] = "application"
37    if TYPE_CHECKING:
38        type: Literal["application"] = "application"
39    else:
40        type: Literal["application"]
41
42    id: Optional[ApplicationId] = None
43    """bioimage.io-wide unique resource identifier
44    assigned by bioimage.io; version **un**specific."""
45
46    parent: Optional[ApplicationId] = None
47    """The description from which this one is derived"""
48
49    source: Annotated[
50        Optional[FileSource_],
51        Field(description="URL or path to the source of the application"),
52    ] = None
53    """The primary source of the application"""

Bioimage.io description of an application.

implemented_type: ClassVar[Literal['application']] = 'application'

bioimage.io-wide unique resource identifier assigned by bioimage.io; version unspecific.

The description from which this one is derived

source: Annotated[Optional[Annotated[Union[bioimageio.spec._internal.url.HttpUrl, bioimageio.spec._internal.io.RelativeFilePath, Annotated[pathlib.Path, PathType(path_type='file'), FieldInfo(annotation=NoneType, required=True, title='FilePath')]], FieldInfo(annotation=NoneType, required=True, metadata=[_PydanticGeneralMetadata(union_mode='left_to_right')]), AfterValidator(func=<function wo_special_file_name at 0x7f5fe201bd80>), PlainSerializer(func=<function _package_serializer at 0x7f5fe20b6d40>, return_type=PydanticUndefined, when_used='unless-none')]], FieldInfo(annotation=NoneType, required=True, description='URL or path to the source of the application')]

The primary source of the application

implemented_format_version_tuple: ClassVar[Tuple[int, int, int]] = (0, 3, 0)
model_config: ClassVar[pydantic.config.ConfigDict] = {'allow_inf_nan': False, 'extra': 'forbid', 'frozen': False, 'model_title_generator': <function _node_title_generator>, 'populate_by_name': True, 'revalidate_instances': 'never', 'use_attribute_docstrings': True, 'validate_assignment': True, 'validate_default': False, 'validate_return': True, 'validate_by_alias': True, 'validate_by_name': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

def model_post_init(self: pydantic.main.BaseModel, context: Any, /) -> None:
337def init_private_attributes(self: BaseModel, context: Any, /) -> None:
338    """This function is meant to behave like a BaseModel method to initialise private attributes.
339
340    It takes context as an argument since that's what pydantic-core passes when calling it.
341
342    Args:
343        self: The BaseModel instance.
344        context: The context.
345    """
346    if getattr(self, '__pydantic_private__', None) is None:
347        pydantic_private = {}
348        for name, private_attr in self.__private_attributes__.items():
349            default = private_attr.get_default()
350            if default is not PydanticUndefined:
351                pydantic_private[name] = default
352        object_setattr(self, '__pydantic_private__', pydantic_private)

This function is meant to behave like a BaseModel method to initialise private attributes.

It takes context as an argument since that's what pydantic-core passes when calling it.

Arguments:
  • self: The BaseModel instance.
  • context: The context.
class BioimageioCondaEnv(bioimageio.spec.conda_env.CondaEnv):
 80class BioimageioCondaEnv(CondaEnv):
 81    """A special `CondaEnv` that
 82    - automatically adds bioimageio specific dependencies
 83    - sorts dependencies
 84    """
 85
 86    @model_validator(mode="after")
 87    def _normalize_bioimageio_conda_env(self):
 88        """update a conda env such that we have bioimageio.core and sorted dependencies"""
 89        for req_channel in ("conda-forge", "nodefaults"):
 90            if req_channel not in self.channels:
 91                self.channels.append(req_channel)
 92
 93        if "defaults" in self.channels:
 94            warnings.warn("removing 'defaults' from conda-channels")
 95            self.channels.remove("defaults")
 96
 97        if "pip" not in self.dependencies:
 98            self.dependencies.append("pip")
 99
100        for dep in self.dependencies:
101            if isinstance(dep, PipDeps):
102                pip_section = dep
103                pip_section.pip.sort()
104                break
105        else:
106            pip_section = None
107
108        if (
109            pip_section is None
110            or not any(pd.startswith("bioimageio.core") for pd in pip_section.pip)
111        ) and not any(
112            d.startswith("bioimageio.core")
113            or d.startswith("conda-forge::bioimageio.core")
114            for d in self.dependencies
115            if not isinstance(d, PipDeps)
116        ):
117            self.dependencies.append("conda-forge::bioimageio.core")
118
119        self.dependencies.sort()
120        return self

A special CondaEnv that

  • automatically adds bioimageio specific dependencies
  • sorts dependencies
model_config: ClassVar[pydantic.config.ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

def build_description( content: Mapping[str, YamlValueView], /, *, context: Optional[ValidationContext] = None, format_version: Union[Literal['latest', 'discover'], str] = 'discover') -> Union[Annotated[Union[Annotated[Union[Annotated[bioimageio.spec.application.v0_2.ApplicationDescr, FieldInfo(annotation=NoneType, required=True, title='application 0.2')], Annotated[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[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[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[NotebookDescr, FieldInfo(annotation=NoneType, required=True, title='notebook 0.2')], Annotated[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[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')], InvalidDescr]:
173def build_description(
174    content: BioimageioYamlContentView,
175    /,
176    *,
177    context: Optional[ValidationContext] = None,
178    format_version: Union[FormatVersionPlaceholder, str] = DISCOVER,
179) -> Union[ResourceDescr, InvalidDescr]:
180    """build a bioimage.io resource description from an RDF's content.
181
182    Use `load_description` if you want to build a resource description from an rdf.yaml
183    or bioimage.io zip-package.
184
185    Args:
186        content: loaded rdf.yaml file (loaded with YAML, not bioimageio.spec)
187        context: validation context to use during validation
188        format_version: (optional) use this argument to load the resource and
189                        convert its metadata to a higher format_version
190
191    Returns:
192        An object holding all metadata of the bioimage.io resource
193
194    """
195
196    return build_description_impl(
197        content,
198        context=context,
199        format_version=format_version,
200        get_rd_class=_get_rd_class,
201    )

build a bioimage.io resource description from an RDF's content.

Use load_description if you want to build a resource description from an rdf.yaml or bioimage.io zip-package.

Arguments:
  • content: loaded rdf.yaml file (loaded with YAML, not bioimageio.spec)
  • context: validation context to use during validation
  • format_version: (optional) use this argument to load the resource and convert its metadata to a higher format_version
Returns:

An object holding all metadata of the bioimage.io resource

class DatasetDescr(bioimageio.spec.generic.v0_3.GenericDescrBase):
 39class DatasetDescr(GenericDescrBase):
 40    """A bioimage.io dataset resource description file (dataset RDF) describes a dataset relevant to bioimage
 41    processing.
 42    """
 43
 44    implemented_type: ClassVar[Literal["dataset"]] = "dataset"
 45    if TYPE_CHECKING:
 46        type: Literal["dataset"] = "dataset"
 47    else:
 48        type: Literal["dataset"]
 49
 50    id: Optional[DatasetId] = None
 51    """bioimage.io-wide unique resource identifier
 52    assigned by bioimage.io; version **un**specific."""
 53
 54    parent: Optional[DatasetId] = None
 55    """The description from which this one is derived"""
 56
 57    source: Optional[HttpUrl] = None
 58    """"URL to the source of the dataset."""
 59
 60    @model_validator(mode="before")
 61    @classmethod
 62    def _convert(cls, data: Dict[str, Any], /) -> Dict[str, Any]:
 63        if (
 64            data.get("type") == "dataset"
 65            and isinstance(fv := data.get("format_version"), str)
 66            and fv.startswith("0.2.")
 67        ):
 68            old = DatasetDescr02.load(data)
 69            if isinstance(old, InvalidDescr):
 70                return data
 71
 72            return cast(
 73                Dict[str, Any],
 74                (cls if TYPE_CHECKING else dict)(
 75                    attachments=(
 76                        []
 77                        if old.attachments is None
 78                        else [FileDescr(source=f) for f in old.attachments.files]
 79                    ),
 80                    authors=[
 81                        _author_conv.convert_as_dict(a) for a in old.authors
 82                    ],  # pyright: ignore[reportArgumentType]
 83                    badges=old.badges,
 84                    cite=[
 85                        {"text": c.text, "doi": c.doi, "url": c.url} for c in old.cite
 86                    ],  # pyright: ignore[reportArgumentType]
 87                    config=old.config,  # pyright: ignore[reportArgumentType]
 88                    covers=old.covers,
 89                    description=old.description,
 90                    documentation=old.documentation,
 91                    format_version="0.3.0",
 92                    git_repo=old.git_repo,  # pyright: ignore[reportArgumentType]
 93                    icon=old.icon,
 94                    id=None if old.id is None else DatasetId(old.id),
 95                    license=old.license,  # type: ignore
 96                    links=old.links,
 97                    maintainers=[
 98                        _maintainer_conv.convert_as_dict(m) for m in old.maintainers
 99                    ],  # pyright: ignore[reportArgumentType]
100                    name=old.name,
101                    source=old.source,
102                    tags=old.tags,
103                    type=old.type,
104                    uploader=old.uploader,
105                    version=old.version,
106                    **(old.model_extra or {}),
107                ),
108            )
109
110        return data

A bioimage.io dataset resource description file (dataset RDF) describes a dataset relevant to bioimage processing.

implemented_type: ClassVar[Literal['dataset']] = 'dataset'

bioimage.io-wide unique resource identifier assigned by bioimage.io; version unspecific.

The description from which this one is derived

"URL to the source of the dataset.

implemented_format_version_tuple: ClassVar[Tuple[int, int, int]] = (0, 3, 0)
model_config: ClassVar[pydantic.config.ConfigDict] = {'allow_inf_nan': False, 'extra': 'forbid', 'frozen': False, 'model_title_generator': <function _node_title_generator>, 'populate_by_name': True, 'revalidate_instances': 'never', 'use_attribute_docstrings': True, 'validate_assignment': True, 'validate_default': False, 'validate_return': True, 'validate_by_alias': True, 'validate_by_name': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

def model_post_init(self: pydantic.main.BaseModel, context: Any, /) -> None:
337def init_private_attributes(self: BaseModel, context: Any, /) -> None:
338    """This function is meant to behave like a BaseModel method to initialise private attributes.
339
340    It takes context as an argument since that's what pydantic-core passes when calling it.
341
342    Args:
343        self: The BaseModel instance.
344        context: The context.
345    """
346    if getattr(self, '__pydantic_private__', None) is None:
347        pydantic_private = {}
348        for name, private_attr in self.__private_attributes__.items():
349            default = private_attr.get_default()
350            if default is not PydanticUndefined:
351                pydantic_private[name] = default
352        object_setattr(self, '__pydantic_private__', pydantic_private)

This function is meant to behave like a BaseModel method to initialise private attributes.

It takes context as an argument since that's what pydantic-core passes when calling it.

Arguments:
  • self: The BaseModel instance.
  • context: The context.
def dump_description( rd: Union[Annotated[Union[Annotated[Union[Annotated[bioimageio.spec.application.v0_2.ApplicationDescr, FieldInfo(annotation=NoneType, required=True, title='application 0.2')], Annotated[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[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[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[NotebookDescr, FieldInfo(annotation=NoneType, required=True, title='notebook 0.2')], Annotated[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[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')], InvalidDescr], /, *, exclude_unset: bool = True, exclude_defaults: bool = False) -> Dict[str, YamlValue]:
66def dump_description(
67    rd: Union[ResourceDescr, InvalidDescr],
68    /,
69    *,
70    exclude_unset: bool = True,
71    exclude_defaults: bool = False,
72) -> BioimageioYamlContent:
73    """Converts a resource to a dictionary containing only simple types that can directly be serialzed to YAML.
74
75    Args:
76        rd: bioimageio resource description
77        exclude_unset: Exclude fields that have not explicitly be set.
78        exclude_defaults: Exclude fields that have the default value (even if set explicitly).
79    """
80    return rd.model_dump(
81        mode="json", exclude_unset=exclude_unset, exclude_defaults=exclude_defaults
82    )

Converts a resource to a dictionary containing only simple types that can directly be serialzed to YAML.

Arguments:
  • rd: bioimageio resource description
  • exclude_unset: Exclude fields that have not explicitly be set.
  • exclude_defaults: Exclude fields that have the default value (even if set explicitly).
def enable_pretty_validation_errors_in_ipynb():
92def enable_pretty_validation_errors_in_ipynb():
93    """DEPRECATED; this is enabled by default at import time."""
94    warnings.warn(
95        "deprecated, this is enabled by default at import time.",
96        DeprecationWarning,
97        stacklevel=2,
98    )

DEPRECATED; this is enabled by default at import time.

class GenericDescr(bioimageio.spec.generic.v0_3.GenericDescrBase):
478class GenericDescr(GenericDescrBase, extra="ignore"):
479    """Specification of the fields used in a generic bioimage.io-compliant resource description file (RDF).
480
481    An RDF is a YAML file that describes a resource such as a model, a dataset, or a notebook.
482    Note that those resources are described with a type-specific RDF.
483    Use this generic resource description, if none of the known specific types matches your resource.
484    """
485
486    implemented_type: ClassVar[Literal["generic"]] = "generic"
487    if TYPE_CHECKING:
488        type: Annotated[str, LowerCase] = "generic"
489        """The resource type assigns a broad category to the resource."""
490    else:
491        type: Annotated[str, LowerCase]
492        """The resource type assigns a broad category to the resource."""
493
494    id: Optional[
495        Annotated[ResourceId, Field(examples=["affable-shark", "ambitious-sloth"])]
496    ] = None
497    """bioimage.io-wide unique resource identifier
498    assigned by bioimage.io; version **un**specific."""
499
500    parent: Optional[ResourceId] = None
501    """The description from which this one is derived"""
502
503    source: Optional[HttpUrl] = None
504    """The primary source of the resource"""
505
506    @field_validator("type", mode="after")
507    @classmethod
508    def check_specific_types(cls, value: str) -> str:
509        if value in KNOWN_SPECIFIC_RESOURCE_TYPES:
510            raise ValueError(
511                f"Use the {value} description instead of this generic description for"
512                + f" your '{value}' resource."
513            )
514
515        return value

Specification of the fields used in a generic bioimage.io-compliant resource description file (RDF).

An RDF is a YAML file that describes a resource such as a model, a dataset, or a notebook. Note that those resources are described with a type-specific RDF. Use this generic resource description, if none of the known specific types matches your resource.

implemented_type: ClassVar[Literal['generic']] = 'generic'
id: Optional[Annotated[bioimageio.spec.generic.v0_3.ResourceId, FieldInfo(annotation=NoneType, required=True, examples=['affable-shark', 'ambitious-sloth'])]]

bioimage.io-wide unique resource identifier assigned by bioimage.io; version unspecific.

The description from which this one is derived

The primary source of the resource

@field_validator('type', mode='after')
@classmethod
def check_specific_types(cls, value: str) -> str:
506    @field_validator("type", mode="after")
507    @classmethod
508    def check_specific_types(cls, value: str) -> str:
509        if value in KNOWN_SPECIFIC_RESOURCE_TYPES:
510            raise ValueError(
511                f"Use the {value} description instead of this generic description for"
512                + f" your '{value}' resource."
513            )
514
515        return value
implemented_format_version_tuple: ClassVar[Tuple[int, int, int]] = (0, 3, 0)
model_config: ClassVar[pydantic.config.ConfigDict] = {'allow_inf_nan': False, 'extra': 'ignore', 'frozen': False, 'model_title_generator': <function _node_title_generator>, 'populate_by_name': True, 'revalidate_instances': 'never', 'use_attribute_docstrings': True, 'validate_assignment': True, 'validate_default': False, 'validate_return': True, 'validate_by_alias': True, 'validate_by_name': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

def model_post_init(self: pydantic.main.BaseModel, context: Any, /) -> None:
337def init_private_attributes(self: BaseModel, context: Any, /) -> None:
338    """This function is meant to behave like a BaseModel method to initialise private attributes.
339
340    It takes context as an argument since that's what pydantic-core passes when calling it.
341
342    Args:
343        self: The BaseModel instance.
344        context: The context.
345    """
346    if getattr(self, '__pydantic_private__', None) is None:
347        pydantic_private = {}
348        for name, private_attr in self.__private_attributes__.items():
349            default = private_attr.get_default()
350            if default is not PydanticUndefined:
351                pydantic_private[name] = default
352        object_setattr(self, '__pydantic_private__', pydantic_private)

This function is meant to behave like a BaseModel method to initialise private attributes.

It takes context as an argument since that's what pydantic-core passes when calling it.

Arguments:
  • self: The BaseModel instance.
  • context: The context.
27def get_conda_env(
28    *,
29    entry: SupportedWeightsEntry,
30    env_name: Optional[Union[Literal["DROP"], str]] = None,
31) -> BioimageioCondaEnv:
32    """get the recommended Conda environment for a given weights entry description"""
33    if isinstance(entry, (v0_4.OnnxWeightsDescr, v0_5.OnnxWeightsDescr)):
34        conda_env = _get_default_onnx_env(opset_version=entry.opset_version)
35    elif isinstance(
36        entry,
37        (
38            v0_4.PytorchStateDictWeightsDescr,
39            v0_5.PytorchStateDictWeightsDescr,
40            v0_4.TorchscriptWeightsDescr,
41            v0_5.TorchscriptWeightsDescr,
42        ),
43    ):
44        if (
45            isinstance(entry, v0_5.TorchscriptWeightsDescr)
46            or entry.dependencies is None
47        ):
48            conda_env = _get_default_pytorch_env(pytorch_version=entry.pytorch_version)
49        else:
50            conda_env = _get_env_from_deps(entry.dependencies)
51
52    elif isinstance(
53        entry,
54        (
55            v0_4.TensorflowSavedModelBundleWeightsDescr,
56            v0_5.TensorflowSavedModelBundleWeightsDescr,
57        ),
58    ):
59        if entry.dependencies is None:
60            conda_env = _get_default_tf_env(tensorflow_version=entry.tensorflow_version)
61        else:
62            conda_env = _get_env_from_deps(entry.dependencies)
63    elif isinstance(
64        entry,
65        (v0_4.KerasHdf5WeightsDescr, v0_5.KerasHdf5WeightsDescr),
66    ):
67        conda_env = _get_default_tf_env(tensorflow_version=entry.tensorflow_version)
68    else:
69        assert_never(entry)
70
71    if env_name == "DROP":
72        conda_env.name = None
73    elif env_name is not None:
74        conda_env.name = env_name
75
76    return conda_env

get the recommended Conda environment for a given weights entry description

def get_resource_package_content( rd: Union[Annotated[Union[Annotated[Union[Annotated[bioimageio.spec.application.v0_2.ApplicationDescr, FieldInfo(annotation=NoneType, required=True, title='application 0.2')], Annotated[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[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[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[NotebookDescr, FieldInfo(annotation=NoneType, required=True, title='notebook 0.2')], Annotated[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[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_yaml_file_name: str = 'rdf.yaml', weights_priority_order: Optional[Sequence[Literal['keras_hdf5', 'onnx', 'pytorch_state_dict', 'tensorflow_js', 'tensorflow_saved_model_bundle', 'torchscript']]] = None) -> Dict[str, Union[bioimageio.spec._internal.url.HttpUrl, Annotated[pathlib.Path, PathType(path_type='file'), Predicate(is_absolute), FieldInfo(annotation=NoneType, required=True, title='AbsoluteFilePath')], Dict[str, YamlValue], zipp.Path]]:
40def get_resource_package_content(
41    rd: ResourceDescr,
42    /,
43    *,
44    bioimageio_yaml_file_name: FileName = BIOIMAGEIO_YAML,
45    weights_priority_order: Optional[Sequence[WeightsFormat]] = None,  # model only
46) -> Dict[FileName, Union[HttpUrl, AbsoluteFilePath, BioimageioYamlContent, ZipPath]]:
47    ret: Dict[
48        FileName, Union[HttpUrl, AbsoluteFilePath, BioimageioYamlContent, ZipPath]
49    ] = {}
50    for k, v in get_package_content(
51        rd,
52        bioimageio_yaml_file_name=bioimageio_yaml_file_name,
53        weights_priority_order=weights_priority_order,
54    ).items():
55        if isinstance(v, FileDescr):
56            if isinstance(v.source, (Path, RelativeFilePath)):
57                ret[k] = v.source.absolute()
58            else:
59                ret[k] = v.source
60
61        else:
62            ret[k] = v
63
64    return ret
def get_validation_context( default: Optional[ValidationContext] = None) -> ValidationContext:
192def get_validation_context(
193    default: Optional[ValidationContext] = None,
194) -> ValidationContext:
195    """Get the currently active validation context (or a default)"""
196    return _validation_context_var.get() or default or ValidationContext()

Get the currently active validation context (or a default)

392class InvalidDescr(
393    ResourceDescrBase,
394    extra="allow",
395    title="An invalid resource description",
396):
397    """A representation of an invalid resource description"""
398
399    implemented_type: ClassVar[Literal["unknown"]] = "unknown"
400    if TYPE_CHECKING:  # see NodeWithExplicitlySetFields
401        type: Any = "unknown"
402    else:
403        type: Any
404
405    implemented_format_version: ClassVar[Literal["unknown"]] = "unknown"
406    if TYPE_CHECKING:  # see NodeWithExplicitlySetFields
407        format_version: Any = "unknown"
408    else:
409        format_version: Any

A representation of an invalid resource description

implemented_type: ClassVar[Literal['unknown']] = 'unknown'
implemented_format_version: ClassVar[Literal['unknown']] = 'unknown'
implemented_format_version_tuple: ClassVar[Tuple[int, int, int]] = (0, 0, 0)
model_config: ClassVar[pydantic.config.ConfigDict] = {'allow_inf_nan': False, 'extra': 'allow', 'frozen': False, 'model_title_generator': <function _node_title_generator>, 'populate_by_name': True, 'revalidate_instances': 'never', 'use_attribute_docstrings': True, 'validate_assignment': True, 'validate_default': False, 'validate_return': True, 'validate_by_alias': True, 'validate_by_name': True, 'title': 'An invalid resource description'}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

def model_post_init(self: pydantic.main.BaseModel, context: Any, /) -> None:
337def init_private_attributes(self: BaseModel, context: Any, /) -> None:
338    """This function is meant to behave like a BaseModel method to initialise private attributes.
339
340    It takes context as an argument since that's what pydantic-core passes when calling it.
341
342    Args:
343        self: The BaseModel instance.
344        context: The context.
345    """
346    if getattr(self, '__pydantic_private__', None) is None:
347        pydantic_private = {}
348        for name, private_attr in self.__private_attributes__.items():
349            default = private_attr.get_default()
350            if default is not PydanticUndefined:
351                pydantic_private[name] = default
352        object_setattr(self, '__pydantic_private__', pydantic_private)

This function is meant to behave like a BaseModel method to initialise private attributes.

It takes context as an argument since that's what pydantic-core passes when calling it.

Arguments:
  • self: The BaseModel instance.
  • context: The context.
LatestResourceDescr = typing.Union[typing.Annotated[typing.Union[ApplicationDescr, DatasetDescr, ModelDescr, NotebookDescr], Discriminator(discriminator='type', custom_error_type=None, custom_error_message=None, custom_error_context=None)], GenericDescr]
def load_dataset_description( source: Union[Annotated[Union[bioimageio.spec._internal.url.HttpUrl, bioimageio.spec._internal.io.RelativeFilePath, Annotated[pathlib.Path, PathType(path_type='file'), FieldInfo(annotation=NoneType, required=True, title='FilePath')]], FieldInfo(annotation=NoneType, required=True, metadata=[_PydanticGeneralMetadata(union_mode='left_to_right')])], str, pydantic.networks.HttpUrl, zipfile.ZipFile], /, *, format_version: Union[Literal['latest', 'discover'], str] = 'discover', perform_io_checks: Optional[bool] = None, known_files: Optional[Dict[str, Optional[bioimageio.spec._internal.io_basics.Sha256]]] = None, sha256: Optional[bioimageio.spec._internal.io_basics.Sha256] = None) -> Annotated[Union[Annotated[bioimageio.spec.dataset.v0_2.DatasetDescr, FieldInfo(annotation=NoneType, required=True, title='dataset 0.2')], Annotated[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')]:
180def load_dataset_description(
181    source: Union[PermissiveFileSource, ZipFile],
182    /,
183    *,
184    format_version: Union[FormatVersionPlaceholder, str] = DISCOVER,
185    perform_io_checks: Optional[bool] = None,
186    known_files: Optional[Dict[str, Optional[Sha256]]] = None,
187    sha256: Optional[Sha256] = None,
188) -> AnyDatasetDescr:
189    """same as `load_description`, but addtionally ensures that the loaded
190    description is valid and of type 'dataset'.
191    """
192    rd = load_description(
193        source,
194        format_version=format_version,
195        perform_io_checks=perform_io_checks,
196        known_files=known_files,
197        sha256=sha256,
198    )
199    return ensure_description_is_dataset(rd)

same as load_description, but addtionally ensures that the loaded description is valid and of type 'dataset'.

def load_description_and_validate_format_only( source: Union[Annotated[Union[bioimageio.spec._internal.url.HttpUrl, bioimageio.spec._internal.io.RelativeFilePath, Annotated[pathlib.Path, PathType(path_type='file'), FieldInfo(annotation=NoneType, required=True, title='FilePath')]], FieldInfo(annotation=NoneType, required=True, metadata=[_PydanticGeneralMetadata(union_mode='left_to_right')])], str, pydantic.networks.HttpUrl, zipfile.ZipFile], /, *, format_version: Union[Literal['latest', 'discover'], str] = 'discover', perform_io_checks: Optional[bool] = None, known_files: Optional[Dict[str, Optional[bioimageio.spec._internal.io_basics.Sha256]]] = None, sha256: Optional[bioimageio.spec._internal.io_basics.Sha256] = None) -> ValidationSummary:
232def load_description_and_validate_format_only(
233    source: Union[PermissiveFileSource, ZipFile],
234    /,
235    *,
236    format_version: Union[FormatVersionPlaceholder, str] = DISCOVER,
237    perform_io_checks: Optional[bool] = None,
238    known_files: Optional[Dict[str, Optional[Sha256]]] = None,
239    sha256: Optional[Sha256] = None,
240) -> ValidationSummary:
241    """same as `load_description`, but only return the validation summary.
242
243    Returns:
244        Validation summary of the bioimage.io resource found at `source`.
245
246    """
247    rd = load_description(
248        source,
249        format_version=format_version,
250        perform_io_checks=perform_io_checks,
251        known_files=known_files,
252        sha256=sha256,
253    )
254    assert rd.validation_summary is not None
255    return rd.validation_summary

same as load_description, but only return the validation summary.

Returns:

Validation summary of the bioimage.io resource found at source.

def load_description( source: Union[Annotated[Union[bioimageio.spec._internal.url.HttpUrl, bioimageio.spec._internal.io.RelativeFilePath, Annotated[pathlib.Path, PathType(path_type='file'), FieldInfo(annotation=NoneType, required=True, title='FilePath')]], FieldInfo(annotation=NoneType, required=True, metadata=[_PydanticGeneralMetadata(union_mode='left_to_right')])], str, pydantic.networks.HttpUrl, zipfile.ZipFile], /, *, format_version: Union[Literal['latest', 'discover'], str] = 'discover', perform_io_checks: Optional[bool] = None, known_files: Optional[Dict[str, Optional[bioimageio.spec._internal.io_basics.Sha256]]] = None, sha256: Optional[bioimageio.spec._internal.io_basics.Sha256] = None) -> Union[Annotated[Union[Annotated[Union[Annotated[bioimageio.spec.application.v0_2.ApplicationDescr, FieldInfo(annotation=NoneType, required=True, title='application 0.2')], Annotated[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[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[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[NotebookDescr, FieldInfo(annotation=NoneType, required=True, title='notebook 0.2')], Annotated[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[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')], InvalidDescr]:
 57def load_description(
 58    source: Union[PermissiveFileSource, ZipFile],
 59    /,
 60    *,
 61    format_version: Union[FormatVersionPlaceholder, str] = DISCOVER,
 62    perform_io_checks: Optional[bool] = None,
 63    known_files: Optional[Dict[str, Optional[Sha256]]] = None,
 64    sha256: Optional[Sha256] = None,
 65) -> Union[ResourceDescr, InvalidDescr]:
 66    """load a bioimage.io resource description
 67
 68    Args:
 69        source: Path or URL to an rdf.yaml or a bioimage.io package
 70                (zip-file with rdf.yaml in it).
 71        format_version: (optional) Use this argument to load the resource and
 72                        convert its metadata to a higher format_version.
 73        perform_io_checks: Wether or not to perform validation that requires file io,
 74                           e.g. downloading a remote files. The existence of local
 75                           absolute file paths is still being checked.
 76        known_files: Allows to bypass download and hashing of referenced files
 77                     (even if perform_io_checks is True).
 78                     Checked files will be added to this dictionary
 79                     with their SHA-256 value.
 80        sha256: Optional SHA-256 value of **source**
 81
 82    Returns:
 83        An object holding all metadata of the bioimage.io resource
 84
 85    """
 86    if isinstance(source, ResourceDescrBase):
 87        name = getattr(source, "name", f"{str(source)[:10]}...")
 88        logger.warning("returning already loaded description '{}' as is", name)
 89        return source  # pyright: ignore[reportReturnType]
 90
 91    opened = open_bioimageio_yaml(source, sha256=sha256)
 92
 93    context = get_validation_context().replace(
 94        root=opened.original_root,
 95        file_name=opened.original_file_name,
 96        perform_io_checks=perform_io_checks,
 97        known_files=known_files,
 98    )
 99
100    return build_description(
101        opened.content,
102        context=context,
103        format_version=format_version,
104    )

load a bioimage.io resource description

Arguments:
  • source: Path or URL to an rdf.yaml or a bioimage.io package (zip-file with rdf.yaml in it).
  • format_version: (optional) Use this argument to load the resource and convert its metadata to a higher format_version.
  • perform_io_checks: Wether or not to perform validation that requires file io, e.g. downloading a remote files. The existence of local absolute file paths is still being checked.
  • known_files: Allows to bypass download and hashing of referenced files (even if perform_io_checks is True). Checked files will be added to this dictionary with their SHA-256 value.
  • sha256: Optional SHA-256 value of source
Returns:

An object holding all metadata of the bioimage.io resource

def load_model_description( source: Union[Annotated[Union[bioimageio.spec._internal.url.HttpUrl, bioimageio.spec._internal.io.RelativeFilePath, Annotated[pathlib.Path, PathType(path_type='file'), FieldInfo(annotation=NoneType, required=True, title='FilePath')]], FieldInfo(annotation=NoneType, required=True, metadata=[_PydanticGeneralMetadata(union_mode='left_to_right')])], str, pydantic.networks.HttpUrl, zipfile.ZipFile], /, *, format_version: Union[Literal['latest', 'discover'], str] = 'discover', perform_io_checks: Optional[bool] = None, known_files: Optional[Dict[str, Optional[bioimageio.spec._internal.io_basics.Sha256]]] = None, sha256: Optional[bioimageio.spec._internal.io_basics.Sha256] = None) -> Annotated[Union[Annotated[bioimageio.spec.model.v0_4.ModelDescr, FieldInfo(annotation=NoneType, required=True, title='model 0.4')], Annotated[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')]:
131def load_model_description(
132    source: Union[PermissiveFileSource, ZipFile],
133    /,
134    *,
135    format_version: Union[FormatVersionPlaceholder, str] = DISCOVER,
136    perform_io_checks: Optional[bool] = None,
137    known_files: Optional[Dict[str, Optional[Sha256]]] = None,
138    sha256: Optional[Sha256] = None,
139) -> AnyModelDescr:
140    """same as `load_description`, but addtionally ensures that the loaded
141    description is valid and of type 'model'.
142
143    Raises:
144        ValueError: for invalid or non-model resources
145    """
146    rd = load_description(
147        source,
148        format_version=format_version,
149        perform_io_checks=perform_io_checks,
150        known_files=known_files,
151        sha256=sha256,
152    )
153    return ensure_description_is_model(rd)

same as load_description, but addtionally ensures that the loaded description is valid and of type 'model'.

Raises:
  • ValueError: for invalid or non-model resources
2554class ModelDescr(GenericModelDescrBase):
2555    """Specification of the fields used in a bioimage.io-compliant RDF to describe AI models with pretrained weights.
2556    These fields are typically stored in a YAML file which we call a model resource description file (model RDF).
2557    """
2558
2559    implemented_format_version: ClassVar[Literal["0.5.4"]] = "0.5.4"
2560    if TYPE_CHECKING:
2561        format_version: Literal["0.5.4"] = "0.5.4"
2562    else:
2563        format_version: Literal["0.5.4"]
2564        """Version of the bioimage.io model description specification used.
2565        When creating a new model always use the latest micro/patch version described here.
2566        The `format_version` is important for any consumer software to understand how to parse the fields.
2567        """
2568
2569    implemented_type: ClassVar[Literal["model"]] = "model"
2570    if TYPE_CHECKING:
2571        type: Literal["model"] = "model"
2572    else:
2573        type: Literal["model"]
2574        """Specialized resource type 'model'"""
2575
2576    id: Optional[ModelId] = None
2577    """bioimage.io-wide unique resource identifier
2578    assigned by bioimage.io; version **un**specific."""
2579
2580    authors: NotEmpty[List[Author]]
2581    """The authors are the creators of the model RDF and the primary points of contact."""
2582
2583    documentation: FileSource_documentation
2584    """URL or relative path to a markdown file with additional documentation.
2585    The recommended documentation file name is `README.md`. An `.md` suffix is mandatory.
2586    The documentation should include a '#[#] Validation' (sub)section
2587    with details on how to quantitatively validate the model on unseen data."""
2588
2589    @field_validator("documentation", mode="after")
2590    @classmethod
2591    def _validate_documentation(
2592        cls, value: FileSource_documentation
2593    ) -> FileSource_documentation:
2594        if not get_validation_context().perform_io_checks:
2595            return value
2596
2597        doc_reader = get_reader(value)
2598        doc_content = doc_reader.read().decode(encoding="utf-8")
2599        if not re.search("#.*[vV]alidation", doc_content):
2600            issue_warning(
2601                "No '# Validation' (sub)section found in {value}.",
2602                value=value,
2603                field="documentation",
2604            )
2605
2606        return value
2607
2608    inputs: NotEmpty[Sequence[InputTensorDescr]]
2609    """Describes the input tensors expected by this model."""
2610
2611    @field_validator("inputs", mode="after")
2612    @classmethod
2613    def _validate_input_axes(
2614        cls, inputs: Sequence[InputTensorDescr]
2615    ) -> Sequence[InputTensorDescr]:
2616        input_size_refs = cls._get_axes_with_independent_size(inputs)
2617
2618        for i, ipt in enumerate(inputs):
2619            valid_independent_refs: Dict[
2620                Tuple[TensorId, AxisId],
2621                Tuple[TensorDescr, AnyAxis, Union[int, ParameterizedSize]],
2622            ] = {
2623                **{
2624                    (ipt.id, a.id): (ipt, a, a.size)
2625                    for a in ipt.axes
2626                    if not isinstance(a, BatchAxis)
2627                    and isinstance(a.size, (int, ParameterizedSize))
2628                },
2629                **input_size_refs,
2630            }
2631            for a, ax in enumerate(ipt.axes):
2632                cls._validate_axis(
2633                    "inputs",
2634                    i=i,
2635                    tensor_id=ipt.id,
2636                    a=a,
2637                    axis=ax,
2638                    valid_independent_refs=valid_independent_refs,
2639                )
2640        return inputs
2641
2642    @staticmethod
2643    def _validate_axis(
2644        field_name: str,
2645        i: int,
2646        tensor_id: TensorId,
2647        a: int,
2648        axis: AnyAxis,
2649        valid_independent_refs: Dict[
2650            Tuple[TensorId, AxisId],
2651            Tuple[TensorDescr, AnyAxis, Union[int, ParameterizedSize]],
2652        ],
2653    ):
2654        if isinstance(axis, BatchAxis) or isinstance(
2655            axis.size, (int, ParameterizedSize, DataDependentSize)
2656        ):
2657            return
2658        elif not isinstance(axis.size, SizeReference):
2659            assert_never(axis.size)
2660
2661        # validate axis.size SizeReference
2662        ref = (axis.size.tensor_id, axis.size.axis_id)
2663        if ref not in valid_independent_refs:
2664            raise ValueError(
2665                "Invalid tensor axis reference at"
2666                + f" {field_name}[{i}].axes[{a}].size: {axis.size}."
2667            )
2668        if ref == (tensor_id, axis.id):
2669            raise ValueError(
2670                "Self-referencing not allowed for"
2671                + f" {field_name}[{i}].axes[{a}].size: {axis.size}"
2672            )
2673        if axis.type == "channel":
2674            if valid_independent_refs[ref][1].type != "channel":
2675                raise ValueError(
2676                    "A channel axis' size may only reference another fixed size"
2677                    + " channel axis."
2678                )
2679            if isinstance(axis.channel_names, str) and "{i}" in axis.channel_names:
2680                ref_size = valid_independent_refs[ref][2]
2681                assert isinstance(ref_size, int), (
2682                    "channel axis ref (another channel axis) has to specify fixed"
2683                    + " size"
2684                )
2685                generated_channel_names = [
2686                    Identifier(axis.channel_names.format(i=i))
2687                    for i in range(1, ref_size + 1)
2688                ]
2689                axis.channel_names = generated_channel_names
2690
2691        if (ax_unit := getattr(axis, "unit", None)) != (
2692            ref_unit := getattr(valid_independent_refs[ref][1], "unit", None)
2693        ):
2694            raise ValueError(
2695                "The units of an axis and its reference axis need to match, but"
2696                + f" '{ax_unit}' != '{ref_unit}'."
2697            )
2698        ref_axis = valid_independent_refs[ref][1]
2699        if isinstance(ref_axis, BatchAxis):
2700            raise ValueError(
2701                f"Invalid reference axis '{ref_axis.id}' for {tensor_id}.{axis.id}"
2702                + " (a batch axis is not allowed as reference)."
2703            )
2704
2705        if isinstance(axis, WithHalo):
2706            min_size = axis.size.get_size(axis, ref_axis, n=0)
2707            if (min_size - 2 * axis.halo) < 1:
2708                raise ValueError(
2709                    f"axis {axis.id} with minimum size {min_size} is too small for halo"
2710                    + f" {axis.halo}."
2711                )
2712
2713            input_halo = axis.halo * axis.scale / ref_axis.scale
2714            if input_halo != int(input_halo) or input_halo % 2 == 1:
2715                raise ValueError(
2716                    f"input_halo {input_halo} (output_halo {axis.halo} *"
2717                    + f" output_scale {axis.scale} / input_scale {ref_axis.scale})"
2718                    + f"     {tensor_id}.{axis.id}."
2719                )
2720
2721    @model_validator(mode="after")
2722    def _validate_test_tensors(self) -> Self:
2723        if not get_validation_context().perform_io_checks:
2724            return self
2725
2726        test_output_arrays = [load_array(descr.test_tensor) for descr in self.outputs]
2727        test_input_arrays = [load_array(descr.test_tensor) for descr in self.inputs]
2728
2729        tensors = {
2730            descr.id: (descr, array)
2731            for descr, array in zip(
2732                chain(self.inputs, self.outputs), test_input_arrays + test_output_arrays
2733            )
2734        }
2735        validate_tensors(tensors, tensor_origin="test_tensor")
2736
2737        output_arrays = {
2738            descr.id: array for descr, array in zip(self.outputs, test_output_arrays)
2739        }
2740        for rep_tol in self.config.bioimageio.reproducibility_tolerance:
2741            if not rep_tol.absolute_tolerance:
2742                continue
2743
2744            if rep_tol.output_ids:
2745                out_arrays = {
2746                    oid: a
2747                    for oid, a in output_arrays.items()
2748                    if oid in rep_tol.output_ids
2749                }
2750            else:
2751                out_arrays = output_arrays
2752
2753            for out_id, array in out_arrays.items():
2754                if rep_tol.absolute_tolerance > (max_test_value := array.max()) * 0.01:
2755                    raise ValueError(
2756                        "config.bioimageio.reproducibility_tolerance.absolute_tolerance="
2757                        + f"{rep_tol.absolute_tolerance} > 0.01*{max_test_value}"
2758                        + f" (1% of the maximum value of the test tensor '{out_id}')"
2759                    )
2760
2761        return self
2762
2763    @model_validator(mode="after")
2764    def _validate_tensor_references_in_proc_kwargs(self, info: ValidationInfo) -> Self:
2765        ipt_refs = {t.id for t in self.inputs}
2766        out_refs = {t.id for t in self.outputs}
2767        for ipt in self.inputs:
2768            for p in ipt.preprocessing:
2769                ref = p.kwargs.get("reference_tensor")
2770                if ref is None:
2771                    continue
2772                if ref not in ipt_refs:
2773                    raise ValueError(
2774                        f"`reference_tensor` '{ref}' not found. Valid input tensor"
2775                        + f" references are: {ipt_refs}."
2776                    )
2777
2778        for out in self.outputs:
2779            for p in out.postprocessing:
2780                ref = p.kwargs.get("reference_tensor")
2781                if ref is None:
2782                    continue
2783
2784                if ref not in ipt_refs and ref not in out_refs:
2785                    raise ValueError(
2786                        f"`reference_tensor` '{ref}' not found. Valid tensor references"
2787                        + f" are: {ipt_refs | out_refs}."
2788                    )
2789
2790        return self
2791
2792    # TODO: use validate funcs in validate_test_tensors
2793    # def validate_inputs(self, input_tensors: Mapping[TensorId, NDArray[Any]]) -> Mapping[TensorId, NDArray[Any]]:
2794
2795    name: Annotated[
2796        Annotated[
2797            str, RestrictCharacters(string.ascii_letters + string.digits + "_+- ()")
2798        ],
2799        MinLen(5),
2800        MaxLen(128),
2801        warn(MaxLen(64), "Name longer than 64 characters.", INFO),
2802    ]
2803    """A human-readable name of this model.
2804    It should be no longer than 64 characters
2805    and may only contain letter, number, underscore, minus, parentheses and spaces.
2806    We recommend to chose a name that refers to the model's task and image modality.
2807    """
2808
2809    outputs: NotEmpty[Sequence[OutputTensorDescr]]
2810    """Describes the output tensors."""
2811
2812    @field_validator("outputs", mode="after")
2813    @classmethod
2814    def _validate_tensor_ids(
2815        cls, outputs: Sequence[OutputTensorDescr], info: ValidationInfo
2816    ) -> Sequence[OutputTensorDescr]:
2817        tensor_ids = [
2818            t.id for t in info.data.get("inputs", []) + info.data.get("outputs", [])
2819        ]
2820        duplicate_tensor_ids: List[str] = []
2821        seen: Set[str] = set()
2822        for t in tensor_ids:
2823            if t in seen:
2824                duplicate_tensor_ids.append(t)
2825
2826            seen.add(t)
2827
2828        if duplicate_tensor_ids:
2829            raise ValueError(f"Duplicate tensor ids: {duplicate_tensor_ids}")
2830
2831        return outputs
2832
2833    @staticmethod
2834    def _get_axes_with_parameterized_size(
2835        io: Union[Sequence[InputTensorDescr], Sequence[OutputTensorDescr]],
2836    ):
2837        return {
2838            f"{t.id}.{a.id}": (t, a, a.size)
2839            for t in io
2840            for a in t.axes
2841            if not isinstance(a, BatchAxis) and isinstance(a.size, ParameterizedSize)
2842        }
2843
2844    @staticmethod
2845    def _get_axes_with_independent_size(
2846        io: Union[Sequence[InputTensorDescr], Sequence[OutputTensorDescr]],
2847    ):
2848        return {
2849            (t.id, a.id): (t, a, a.size)
2850            for t in io
2851            for a in t.axes
2852            if not isinstance(a, BatchAxis)
2853            and isinstance(a.size, (int, ParameterizedSize))
2854        }
2855
2856    @field_validator("outputs", mode="after")
2857    @classmethod
2858    def _validate_output_axes(
2859        cls, outputs: List[OutputTensorDescr], info: ValidationInfo
2860    ) -> List[OutputTensorDescr]:
2861        input_size_refs = cls._get_axes_with_independent_size(
2862            info.data.get("inputs", [])
2863        )
2864        output_size_refs = cls._get_axes_with_independent_size(outputs)
2865
2866        for i, out in enumerate(outputs):
2867            valid_independent_refs: Dict[
2868                Tuple[TensorId, AxisId],
2869                Tuple[TensorDescr, AnyAxis, Union[int, ParameterizedSize]],
2870            ] = {
2871                **{
2872                    (out.id, a.id): (out, a, a.size)
2873                    for a in out.axes
2874                    if not isinstance(a, BatchAxis)
2875                    and isinstance(a.size, (int, ParameterizedSize))
2876                },
2877                **input_size_refs,
2878                **output_size_refs,
2879            }
2880            for a, ax in enumerate(out.axes):
2881                cls._validate_axis(
2882                    "outputs",
2883                    i,
2884                    out.id,
2885                    a,
2886                    ax,
2887                    valid_independent_refs=valid_independent_refs,
2888                )
2889
2890        return outputs
2891
2892    packaged_by: List[Author] = Field(
2893        default_factory=cast(Callable[[], List[Author]], list)
2894    )
2895    """The persons that have packaged and uploaded this model.
2896    Only required if those persons differ from the `authors`."""
2897
2898    parent: Optional[LinkedModel] = None
2899    """The model from which this model is derived, e.g. by fine-tuning the weights."""
2900
2901    @model_validator(mode="after")
2902    def _validate_parent_is_not_self(self) -> Self:
2903        if self.parent is not None and self.parent.id == self.id:
2904            raise ValueError("A model description may not reference itself as parent.")
2905
2906        return self
2907
2908    run_mode: Annotated[
2909        Optional[RunMode],
2910        warn(None, "Run mode '{value}' has limited support across consumer softwares."),
2911    ] = None
2912    """Custom run mode for this model: for more complex prediction procedures like test time
2913    data augmentation that currently cannot be expressed in the specification.
2914    No standard run modes are defined yet."""
2915
2916    timestamp: Datetime = Field(default_factory=Datetime.now)
2917    """Timestamp in [ISO 8601](#https://en.wikipedia.org/wiki/ISO_8601) format
2918    with a few restrictions listed [here](https://docs.python.org/3/library/datetime.html#datetime.datetime.fromisoformat).
2919    (In Python a datetime object is valid, too)."""
2920
2921    training_data: Annotated[
2922        Union[None, LinkedDataset, DatasetDescr, DatasetDescr02],
2923        Field(union_mode="left_to_right"),
2924    ] = None
2925    """The dataset used to train this model"""
2926
2927    weights: Annotated[WeightsDescr, WrapSerializer(package_weights)]
2928    """The weights for this model.
2929    Weights can be given for different formats, but should otherwise be equivalent.
2930    The available weight formats determine which consumers can use this model."""
2931
2932    config: Config = Field(default_factory=Config)
2933
2934    @model_validator(mode="after")
2935    def _add_default_cover(self) -> Self:
2936        if not get_validation_context().perform_io_checks or self.covers:
2937            return self
2938
2939        try:
2940            generated_covers = generate_covers(
2941                [(t, load_array(t.test_tensor)) for t in self.inputs],
2942                [(t, load_array(t.test_tensor)) for t in self.outputs],
2943            )
2944        except Exception as e:
2945            issue_warning(
2946                "Failed to generate cover image(s): {e}",
2947                value=self.covers,
2948                msg_context=dict(e=e),
2949                field="covers",
2950            )
2951        else:
2952            self.covers.extend(generated_covers)
2953
2954        return self
2955
2956    def get_input_test_arrays(self) -> List[NDArray[Any]]:
2957        data = [load_array(ipt.test_tensor) for ipt in self.inputs]
2958        assert all(isinstance(d, np.ndarray) for d in data)
2959        return data
2960
2961    def get_output_test_arrays(self) -> List[NDArray[Any]]:
2962        data = [load_array(out.test_tensor) for out in self.outputs]
2963        assert all(isinstance(d, np.ndarray) for d in data)
2964        return data
2965
2966    @staticmethod
2967    def get_batch_size(tensor_sizes: Mapping[TensorId, Mapping[AxisId, int]]) -> int:
2968        batch_size = 1
2969        tensor_with_batchsize: Optional[TensorId] = None
2970        for tid in tensor_sizes:
2971            for aid, s in tensor_sizes[tid].items():
2972                if aid != BATCH_AXIS_ID or s == 1 or s == batch_size:
2973                    continue
2974
2975                if batch_size != 1:
2976                    assert tensor_with_batchsize is not None
2977                    raise ValueError(
2978                        f"batch size mismatch for tensors '{tensor_with_batchsize}' ({batch_size}) and '{tid}' ({s})"
2979                    )
2980
2981                batch_size = s
2982                tensor_with_batchsize = tid
2983
2984        return batch_size
2985
2986    def get_output_tensor_sizes(
2987        self, input_sizes: Mapping[TensorId, Mapping[AxisId, int]]
2988    ) -> Dict[TensorId, Dict[AxisId, Union[int, _DataDepSize]]]:
2989        """Returns the tensor output sizes for given **input_sizes**.
2990        Only if **input_sizes** has a valid input shape, the tensor output size is exact.
2991        Otherwise it might be larger than the actual (valid) output"""
2992        batch_size = self.get_batch_size(input_sizes)
2993        ns = self.get_ns(input_sizes)
2994
2995        tensor_sizes = self.get_tensor_sizes(ns, batch_size=batch_size)
2996        return tensor_sizes.outputs
2997
2998    def get_ns(self, input_sizes: Mapping[TensorId, Mapping[AxisId, int]]):
2999        """get parameter `n` for each parameterized axis
3000        such that the valid input size is >= the given input size"""
3001        ret: Dict[Tuple[TensorId, AxisId], ParameterizedSize_N] = {}
3002        axes = {t.id: {a.id: a for a in t.axes} for t in self.inputs}
3003        for tid in input_sizes:
3004            for aid, s in input_sizes[tid].items():
3005                size_descr = axes[tid][aid].size
3006                if isinstance(size_descr, ParameterizedSize):
3007                    ret[(tid, aid)] = size_descr.get_n(s)
3008                elif size_descr is None or isinstance(size_descr, (int, SizeReference)):
3009                    pass
3010                else:
3011                    assert_never(size_descr)
3012
3013        return ret
3014
3015    def get_tensor_sizes(
3016        self, ns: Mapping[Tuple[TensorId, AxisId], ParameterizedSize_N], batch_size: int
3017    ) -> _TensorSizes:
3018        axis_sizes = self.get_axis_sizes(ns, batch_size=batch_size)
3019        return _TensorSizes(
3020            {
3021                t: {
3022                    aa: axis_sizes.inputs[(tt, aa)]
3023                    for tt, aa in axis_sizes.inputs
3024                    if tt == t
3025                }
3026                for t in {tt for tt, _ in axis_sizes.inputs}
3027            },
3028            {
3029                t: {
3030                    aa: axis_sizes.outputs[(tt, aa)]
3031                    for tt, aa in axis_sizes.outputs
3032                    if tt == t
3033                }
3034                for t in {tt for tt, _ in axis_sizes.outputs}
3035            },
3036        )
3037
3038    def get_axis_sizes(
3039        self,
3040        ns: Mapping[Tuple[TensorId, AxisId], ParameterizedSize_N],
3041        batch_size: Optional[int] = None,
3042        *,
3043        max_input_shape: Optional[Mapping[Tuple[TensorId, AxisId], int]] = None,
3044    ) -> _AxisSizes:
3045        """Determine input and output block shape for scale factors **ns**
3046        of parameterized input sizes.
3047
3048        Args:
3049            ns: Scale factor `n` for each axis (keyed by (tensor_id, axis_id))
3050                that is parameterized as `size = min + n * step`.
3051            batch_size: The desired size of the batch dimension.
3052                If given **batch_size** overwrites any batch size present in
3053                **max_input_shape**. Default 1.
3054            max_input_shape: Limits the derived block shapes.
3055                Each axis for which the input size, parameterized by `n`, is larger
3056                than **max_input_shape** is set to the minimal value `n_min` for which
3057                this is still true.
3058                Use this for small input samples or large values of **ns**.
3059                Or simply whenever you know the full input shape.
3060
3061        Returns:
3062            Resolved axis sizes for model inputs and outputs.
3063        """
3064        max_input_shape = max_input_shape or {}
3065        if batch_size is None:
3066            for (_t_id, a_id), s in max_input_shape.items():
3067                if a_id == BATCH_AXIS_ID:
3068                    batch_size = s
3069                    break
3070            else:
3071                batch_size = 1
3072
3073        all_axes = {
3074            t.id: {a.id: a for a in t.axes} for t in chain(self.inputs, self.outputs)
3075        }
3076
3077        inputs: Dict[Tuple[TensorId, AxisId], int] = {}
3078        outputs: Dict[Tuple[TensorId, AxisId], Union[int, _DataDepSize]] = {}
3079
3080        def get_axis_size(a: Union[InputAxis, OutputAxis]):
3081            if isinstance(a, BatchAxis):
3082                if (t_descr.id, a.id) in ns:
3083                    logger.warning(
3084                        "Ignoring unexpected size increment factor (n) for batch axis"
3085                        + " of tensor '{}'.",
3086                        t_descr.id,
3087                    )
3088                return batch_size
3089            elif isinstance(a.size, int):
3090                if (t_descr.id, a.id) in ns:
3091                    logger.warning(
3092                        "Ignoring unexpected size increment factor (n) for fixed size"
3093                        + " axis '{}' of tensor '{}'.",
3094                        a.id,
3095                        t_descr.id,
3096                    )
3097                return a.size
3098            elif isinstance(a.size, ParameterizedSize):
3099                if (t_descr.id, a.id) not in ns:
3100                    raise ValueError(
3101                        "Size increment factor (n) missing for parametrized axis"
3102                        + f" '{a.id}' of tensor '{t_descr.id}'."
3103                    )
3104                n = ns[(t_descr.id, a.id)]
3105                s_max = max_input_shape.get((t_descr.id, a.id))
3106                if s_max is not None:
3107                    n = min(n, a.size.get_n(s_max))
3108
3109                return a.size.get_size(n)
3110
3111            elif isinstance(a.size, SizeReference):
3112                if (t_descr.id, a.id) in ns:
3113                    logger.warning(
3114                        "Ignoring unexpected size increment factor (n) for axis '{}'"
3115                        + " of tensor '{}' with size reference.",
3116                        a.id,
3117                        t_descr.id,
3118                    )
3119                assert not isinstance(a, BatchAxis)
3120                ref_axis = all_axes[a.size.tensor_id][a.size.axis_id]
3121                assert not isinstance(ref_axis, BatchAxis)
3122                ref_key = (a.size.tensor_id, a.size.axis_id)
3123                ref_size = inputs.get(ref_key, outputs.get(ref_key))
3124                assert ref_size is not None, ref_key
3125                assert not isinstance(ref_size, _DataDepSize), ref_key
3126                return a.size.get_size(
3127                    axis=a,
3128                    ref_axis=ref_axis,
3129                    ref_size=ref_size,
3130                )
3131            elif isinstance(a.size, DataDependentSize):
3132                if (t_descr.id, a.id) in ns:
3133                    logger.warning(
3134                        "Ignoring unexpected increment factor (n) for data dependent"
3135                        + " size axis '{}' of tensor '{}'.",
3136                        a.id,
3137                        t_descr.id,
3138                    )
3139                return _DataDepSize(a.size.min, a.size.max)
3140            else:
3141                assert_never(a.size)
3142
3143        # first resolve all , but the `SizeReference` input sizes
3144        for t_descr in self.inputs:
3145            for a in t_descr.axes:
3146                if not isinstance(a.size, SizeReference):
3147                    s = get_axis_size(a)
3148                    assert not isinstance(s, _DataDepSize)
3149                    inputs[t_descr.id, a.id] = s
3150
3151        # resolve all other input axis sizes
3152        for t_descr in self.inputs:
3153            for a in t_descr.axes:
3154                if isinstance(a.size, SizeReference):
3155                    s = get_axis_size(a)
3156                    assert not isinstance(s, _DataDepSize)
3157                    inputs[t_descr.id, a.id] = s
3158
3159        # resolve all output axis sizes
3160        for t_descr in self.outputs:
3161            for a in t_descr.axes:
3162                assert not isinstance(a.size, ParameterizedSize)
3163                s = get_axis_size(a)
3164                outputs[t_descr.id, a.id] = s
3165
3166        return _AxisSizes(inputs=inputs, outputs=outputs)
3167
3168    @model_validator(mode="before")
3169    @classmethod
3170    def _convert(cls, data: Dict[str, Any]) -> Dict[str, Any]:
3171        cls.convert_from_old_format_wo_validation(data)
3172        return data
3173
3174    @classmethod
3175    def convert_from_old_format_wo_validation(cls, data: Dict[str, Any]) -> None:
3176        """Convert metadata following an older format version to this classes' format
3177        without validating the result.
3178        """
3179        if (
3180            data.get("type") == "model"
3181            and isinstance(fv := data.get("format_version"), str)
3182            and fv.count(".") == 2
3183        ):
3184            fv_parts = fv.split(".")
3185            if any(not p.isdigit() for p in fv_parts):
3186                return
3187
3188            fv_tuple = tuple(map(int, fv_parts))
3189
3190            assert cls.implemented_format_version_tuple[0:2] == (0, 5)
3191            if fv_tuple[:2] in ((0, 3), (0, 4)):
3192                m04 = _ModelDescr_v0_4.load(data)
3193                if isinstance(m04, InvalidDescr):
3194                    try:
3195                        updated = _model_conv.convert_as_dict(
3196                            m04  # pyright: ignore[reportArgumentType]
3197                        )
3198                    except Exception as e:
3199                        logger.error(
3200                            "Failed to convert from invalid model 0.4 description."
3201                            + f"\nerror: {e}"
3202                            + "\nProceeding with model 0.5 validation without conversion."
3203                        )
3204                        updated = None
3205                else:
3206                    updated = _model_conv.convert_as_dict(m04)
3207
3208                if updated is not None:
3209                    data.clear()
3210                    data.update(updated)
3211
3212            elif fv_tuple[:2] == (0, 5):
3213                # bump patch version
3214                data["format_version"] = cls.implemented_format_version

Specification of the fields used in a bioimage.io-compliant RDF to describe AI models with pretrained weights. These fields are typically stored in a YAML file which we call a model resource description file (model RDF).

implemented_format_version: ClassVar[Literal['0.5.4']] = '0.5.4'
implemented_type: ClassVar[Literal['model']] = 'model'

bioimage.io-wide unique resource identifier assigned by bioimage.io; version unspecific.

authors: Annotated[List[bioimageio.spec.generic.v0_3.Author], MinLen(min_length=1)]

The authors are the creators of the model RDF and the primary points of contact.

documentation: Annotated[Union[bioimageio.spec._internal.url.HttpUrl, bioimageio.spec._internal.io.RelativeFilePath, Annotated[pathlib.Path, PathType(path_type='file'), FieldInfo(annotation=NoneType, required=True, title='FilePath')]], FieldInfo(annotation=NoneType, required=True, metadata=[_PydanticGeneralMetadata(union_mode='left_to_right')]), AfterValidator(func=<function wo_special_file_name at 0x7f5fe201bd80>), PlainSerializer(func=<function _package_serializer at 0x7f5fe20b6d40>, return_type=PydanticUndefined, when_used='unless-none'), WithSuffix(suffix='.md', case_sensitive=True), FieldInfo(annotation=NoneType, required=True, examples=['https://raw.githubusercontent.com/bioimage-io/spec-bioimage-io/main/example_descriptions/models/unet2d_nuclei_broad/README.md', 'README.md'])]

URL or relative path to a markdown file with additional documentation. The recommended documentation file name is README.md. An .md suffix is mandatory. The documentation should include a '#[#] Validation' (sub)section with details on how to quantitatively validate the model on unseen data.

inputs: Annotated[Sequence[bioimageio.spec.model.v0_5.InputTensorDescr], MinLen(min_length=1)]

Describes the input tensors expected by this model.

name: Annotated[str, RestrictCharacters(alphabet='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_+- ()'), MinLen(min_length=5), MaxLen(max_length=128), AfterWarner(func=<function as_warning.<locals>.wrapper at 0x7f5fe02e4860>, severity=20, msg='Name longer than 64 characters.', context={'typ': Annotated[Any, MaxLen(max_length=64)]})]

A human-readable name of this model. It should be no longer than 64 characters and may only contain letter, number, underscore, minus, parentheses and spaces. We recommend to chose a name that refers to the model's task and image modality.

outputs: Annotated[Sequence[bioimageio.spec.model.v0_5.OutputTensorDescr], MinLen(min_length=1)]

Describes the output tensors.

The persons that have packaged and uploaded this model. Only required if those persons differ from the authors.

The model from which this model is derived, e.g. by fine-tuning the weights.

run_mode: Annotated[Optional[bioimageio.spec.model.v0_4.RunMode], AfterWarner(func=<function as_warning.<locals>.wrapper at 0x7f5fe02e4ea0>, severity=30, msg="Run mode '{value}' has limited support across consumer softwares.", context={'typ': None})]

Custom run mode for this model: for more complex prediction procedures like test time data augmentation that currently cannot be expressed in the specification. No standard run modes are defined yet.

Timestamp in ISO 8601 format with a few restrictions listed here. (In Python a datetime object is valid, too).

training_data: Annotated[Union[NoneType, bioimageio.spec.dataset.v0_3.LinkedDataset, DatasetDescr, bioimageio.spec.dataset.v0_2.DatasetDescr], FieldInfo(annotation=NoneType, required=True, metadata=[_PydanticGeneralMetadata(union_mode='left_to_right')])]

The dataset used to train this model

weights: Annotated[bioimageio.spec.model.v0_5.WeightsDescr, WrapSerializer(func=<function package_weights at 0x7f5fe1ef1b20>, return_type=PydanticUndefined, when_used='always')]

The weights for this model. Weights can be given for different formats, but should otherwise be equivalent. The available weight formats determine which consumers can use this model.

def get_input_test_arrays(self) -> List[numpy.ndarray[tuple[Any, ...], numpy.dtype[Any]]]:
2956    def get_input_test_arrays(self) -> List[NDArray[Any]]:
2957        data = [load_array(ipt.test_tensor) for ipt in self.inputs]
2958        assert all(isinstance(d, np.ndarray) for d in data)
2959        return data
def get_output_test_arrays(self) -> List[numpy.ndarray[tuple[Any, ...], numpy.dtype[Any]]]:
2961    def get_output_test_arrays(self) -> List[NDArray[Any]]:
2962        data = [load_array(out.test_tensor) for out in self.outputs]
2963        assert all(isinstance(d, np.ndarray) for d in data)
2964        return data
@staticmethod
def get_batch_size( tensor_sizes: Mapping[bioimageio.spec.model.v0_5.TensorId, Mapping[bioimageio.spec.model.v0_5.AxisId, int]]) -> int:
2966    @staticmethod
2967    def get_batch_size(tensor_sizes: Mapping[TensorId, Mapping[AxisId, int]]) -> int:
2968        batch_size = 1
2969        tensor_with_batchsize: Optional[TensorId] = None
2970        for tid in tensor_sizes:
2971            for aid, s in tensor_sizes[tid].items():
2972                if aid != BATCH_AXIS_ID or s == 1 or s == batch_size:
2973                    continue
2974
2975                if batch_size != 1:
2976                    assert tensor_with_batchsize is not None
2977                    raise ValueError(
2978                        f"batch size mismatch for tensors '{tensor_with_batchsize}' ({batch_size}) and '{tid}' ({s})"
2979                    )
2980
2981                batch_size = s
2982                tensor_with_batchsize = tid
2983
2984        return batch_size
def get_output_tensor_sizes( self, input_sizes: Mapping[bioimageio.spec.model.v0_5.TensorId, Mapping[bioimageio.spec.model.v0_5.AxisId, int]]) -> Dict[bioimageio.spec.model.v0_5.TensorId, Dict[bioimageio.spec.model.v0_5.AxisId, Union[int, bioimageio.spec.model.v0_5._DataDepSize]]]:
2986    def get_output_tensor_sizes(
2987        self, input_sizes: Mapping[TensorId, Mapping[AxisId, int]]
2988    ) -> Dict[TensorId, Dict[AxisId, Union[int, _DataDepSize]]]:
2989        """Returns the tensor output sizes for given **input_sizes**.
2990        Only if **input_sizes** has a valid input shape, the tensor output size is exact.
2991        Otherwise it might be larger than the actual (valid) output"""
2992        batch_size = self.get_batch_size(input_sizes)
2993        ns = self.get_ns(input_sizes)
2994
2995        tensor_sizes = self.get_tensor_sizes(ns, batch_size=batch_size)
2996        return tensor_sizes.outputs

Returns the tensor output sizes for given input_sizes. Only if input_sizes has a valid input shape, the tensor output size is exact. Otherwise it might be larger than the actual (valid) output

def get_ns( self, input_sizes: Mapping[bioimageio.spec.model.v0_5.TensorId, Mapping[bioimageio.spec.model.v0_5.AxisId, int]]):
2998    def get_ns(self, input_sizes: Mapping[TensorId, Mapping[AxisId, int]]):
2999        """get parameter `n` for each parameterized axis
3000        such that the valid input size is >= the given input size"""
3001        ret: Dict[Tuple[TensorId, AxisId], ParameterizedSize_N] = {}
3002        axes = {t.id: {a.id: a for a in t.axes} for t in self.inputs}
3003        for tid in input_sizes:
3004            for aid, s in input_sizes[tid].items():
3005                size_descr = axes[tid][aid].size
3006                if isinstance(size_descr, ParameterizedSize):
3007                    ret[(tid, aid)] = size_descr.get_n(s)
3008                elif size_descr is None or isinstance(size_descr, (int, SizeReference)):
3009                    pass
3010                else:
3011                    assert_never(size_descr)
3012
3013        return ret

get parameter n for each parameterized axis such that the valid input size is >= the given input size

def get_tensor_sizes( self, ns: Mapping[Tuple[bioimageio.spec.model.v0_5.TensorId, bioimageio.spec.model.v0_5.AxisId], int], batch_size: int) -> bioimageio.spec.model.v0_5._TensorSizes:
3015    def get_tensor_sizes(
3016        self, ns: Mapping[Tuple[TensorId, AxisId], ParameterizedSize_N], batch_size: int
3017    ) -> _TensorSizes:
3018        axis_sizes = self.get_axis_sizes(ns, batch_size=batch_size)
3019        return _TensorSizes(
3020            {
3021                t: {
3022                    aa: axis_sizes.inputs[(tt, aa)]
3023                    for tt, aa in axis_sizes.inputs
3024                    if tt == t
3025                }
3026                for t in {tt for tt, _ in axis_sizes.inputs}
3027            },
3028            {
3029                t: {
3030                    aa: axis_sizes.outputs[(tt, aa)]
3031                    for tt, aa in axis_sizes.outputs
3032                    if tt == t
3033                }
3034                for t in {tt for tt, _ in axis_sizes.outputs}
3035            },
3036        )
def get_axis_sizes( self, ns: Mapping[Tuple[bioimageio.spec.model.v0_5.TensorId, bioimageio.spec.model.v0_5.AxisId], int], batch_size: Optional[int] = None, *, max_input_shape: Optional[Mapping[Tuple[bioimageio.spec.model.v0_5.TensorId, bioimageio.spec.model.v0_5.AxisId], int]] = None) -> bioimageio.spec.model.v0_5._AxisSizes:
3038    def get_axis_sizes(
3039        self,
3040        ns: Mapping[Tuple[TensorId, AxisId], ParameterizedSize_N],
3041        batch_size: Optional[int] = None,
3042        *,
3043        max_input_shape: Optional[Mapping[Tuple[TensorId, AxisId], int]] = None,
3044    ) -> _AxisSizes:
3045        """Determine input and output block shape for scale factors **ns**
3046        of parameterized input sizes.
3047
3048        Args:
3049            ns: Scale factor `n` for each axis (keyed by (tensor_id, axis_id))
3050                that is parameterized as `size = min + n * step`.
3051            batch_size: The desired size of the batch dimension.
3052                If given **batch_size** overwrites any batch size present in
3053                **max_input_shape**. Default 1.
3054            max_input_shape: Limits the derived block shapes.
3055                Each axis for which the input size, parameterized by `n`, is larger
3056                than **max_input_shape** is set to the minimal value `n_min` for which
3057                this is still true.
3058                Use this for small input samples or large values of **ns**.
3059                Or simply whenever you know the full input shape.
3060
3061        Returns:
3062            Resolved axis sizes for model inputs and outputs.
3063        """
3064        max_input_shape = max_input_shape or {}
3065        if batch_size is None:
3066            for (_t_id, a_id), s in max_input_shape.items():
3067                if a_id == BATCH_AXIS_ID:
3068                    batch_size = s
3069                    break
3070            else:
3071                batch_size = 1
3072
3073        all_axes = {
3074            t.id: {a.id: a for a in t.axes} for t in chain(self.inputs, self.outputs)
3075        }
3076
3077        inputs: Dict[Tuple[TensorId, AxisId], int] = {}
3078        outputs: Dict[Tuple[TensorId, AxisId], Union[int, _DataDepSize]] = {}
3079
3080        def get_axis_size(a: Union[InputAxis, OutputAxis]):
3081            if isinstance(a, BatchAxis):
3082                if (t_descr.id, a.id) in ns:
3083                    logger.warning(
3084                        "Ignoring unexpected size increment factor (n) for batch axis"
3085                        + " of tensor '{}'.",
3086                        t_descr.id,
3087                    )
3088                return batch_size
3089            elif isinstance(a.size, int):
3090                if (t_descr.id, a.id) in ns:
3091                    logger.warning(
3092                        "Ignoring unexpected size increment factor (n) for fixed size"
3093                        + " axis '{}' of tensor '{}'.",
3094                        a.id,
3095                        t_descr.id,
3096                    )
3097                return a.size
3098            elif isinstance(a.size, ParameterizedSize):
3099                if (t_descr.id, a.id) not in ns:
3100                    raise ValueError(
3101                        "Size increment factor (n) missing for parametrized axis"
3102                        + f" '{a.id}' of tensor '{t_descr.id}'."
3103                    )
3104                n = ns[(t_descr.id, a.id)]
3105                s_max = max_input_shape.get((t_descr.id, a.id))
3106                if s_max is not None:
3107                    n = min(n, a.size.get_n(s_max))
3108
3109                return a.size.get_size(n)
3110
3111            elif isinstance(a.size, SizeReference):
3112                if (t_descr.id, a.id) in ns:
3113                    logger.warning(
3114                        "Ignoring unexpected size increment factor (n) for axis '{}'"
3115                        + " of tensor '{}' with size reference.",
3116                        a.id,
3117                        t_descr.id,
3118                    )
3119                assert not isinstance(a, BatchAxis)
3120                ref_axis = all_axes[a.size.tensor_id][a.size.axis_id]
3121                assert not isinstance(ref_axis, BatchAxis)
3122                ref_key = (a.size.tensor_id, a.size.axis_id)
3123                ref_size = inputs.get(ref_key, outputs.get(ref_key))
3124                assert ref_size is not None, ref_key
3125                assert not isinstance(ref_size, _DataDepSize), ref_key
3126                return a.size.get_size(
3127                    axis=a,
3128                    ref_axis=ref_axis,
3129                    ref_size=ref_size,
3130                )
3131            elif isinstance(a.size, DataDependentSize):
3132                if (t_descr.id, a.id) in ns:
3133                    logger.warning(
3134                        "Ignoring unexpected increment factor (n) for data dependent"
3135                        + " size axis '{}' of tensor '{}'.",
3136                        a.id,
3137                        t_descr.id,
3138                    )
3139                return _DataDepSize(a.size.min, a.size.max)
3140            else:
3141                assert_never(a.size)
3142
3143        # first resolve all , but the `SizeReference` input sizes
3144        for t_descr in self.inputs:
3145            for a in t_descr.axes:
3146                if not isinstance(a.size, SizeReference):
3147                    s = get_axis_size(a)
3148                    assert not isinstance(s, _DataDepSize)
3149                    inputs[t_descr.id, a.id] = s
3150
3151        # resolve all other input axis sizes
3152        for t_descr in self.inputs:
3153            for a in t_descr.axes:
3154                if isinstance(a.size, SizeReference):
3155                    s = get_axis_size(a)
3156                    assert not isinstance(s, _DataDepSize)
3157                    inputs[t_descr.id, a.id] = s
3158
3159        # resolve all output axis sizes
3160        for t_descr in self.outputs:
3161            for a in t_descr.axes:
3162                assert not isinstance(a.size, ParameterizedSize)
3163                s = get_axis_size(a)
3164                outputs[t_descr.id, a.id] = s
3165
3166        return _AxisSizes(inputs=inputs, outputs=outputs)

Determine input and output block shape for scale factors ns of parameterized input sizes.

Arguments:
  • ns: Scale factor n for each axis (keyed by (tensor_id, axis_id)) that is parameterized as size = min + n * step.
  • batch_size: The desired size of the batch dimension. If given batch_size overwrites any batch size present in max_input_shape. Default 1.
  • max_input_shape: Limits the derived block shapes. Each axis for which the input size, parameterized by n, is larger than max_input_shape is set to the minimal value n_min for which this is still true. Use this for small input samples or large values of ns. Or simply whenever you know the full input shape.
Returns:

Resolved axis sizes for model inputs and outputs.

@classmethod
def convert_from_old_format_wo_validation(cls, data: Dict[str, Any]) -> None:
3174    @classmethod
3175    def convert_from_old_format_wo_validation(cls, data: Dict[str, Any]) -> None:
3176        """Convert metadata following an older format version to this classes' format
3177        without validating the result.
3178        """
3179        if (
3180            data.get("type") == "model"
3181            and isinstance(fv := data.get("format_version"), str)
3182            and fv.count(".") == 2
3183        ):
3184            fv_parts = fv.split(".")
3185            if any(not p.isdigit() for p in fv_parts):
3186                return
3187
3188            fv_tuple = tuple(map(int, fv_parts))
3189
3190            assert cls.implemented_format_version_tuple[0:2] == (0, 5)
3191            if fv_tuple[:2] in ((0, 3), (0, 4)):
3192                m04 = _ModelDescr_v0_4.load(data)
3193                if isinstance(m04, InvalidDescr):
3194                    try:
3195                        updated = _model_conv.convert_as_dict(
3196                            m04  # pyright: ignore[reportArgumentType]
3197                        )
3198                    except Exception as e:
3199                        logger.error(
3200                            "Failed to convert from invalid model 0.4 description."
3201                            + f"\nerror: {e}"
3202                            + "\nProceeding with model 0.5 validation without conversion."
3203                        )
3204                        updated = None
3205                else:
3206                    updated = _model_conv.convert_as_dict(m04)
3207
3208                if updated is not None:
3209                    data.clear()
3210                    data.update(updated)
3211
3212            elif fv_tuple[:2] == (0, 5):
3213                # bump patch version
3214                data["format_version"] = cls.implemented_format_version

Convert metadata following an older format version to this classes' format without validating the result.

implemented_format_version_tuple: ClassVar[Tuple[int, int, int]] = (0, 5, 4)
model_config: ClassVar[pydantic.config.ConfigDict] = {'allow_inf_nan': False, 'extra': 'forbid', 'frozen': False, 'model_title_generator': <function _node_title_generator>, 'populate_by_name': True, 'revalidate_instances': 'never', 'use_attribute_docstrings': True, 'validate_assignment': True, 'validate_default': False, 'validate_return': True, 'validate_by_alias': True, 'validate_by_name': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

def model_post_init(self: pydantic.main.BaseModel, context: Any, /) -> None:
337def init_private_attributes(self: BaseModel, context: Any, /) -> None:
338    """This function is meant to behave like a BaseModel method to initialise private attributes.
339
340    It takes context as an argument since that's what pydantic-core passes when calling it.
341
342    Args:
343        self: The BaseModel instance.
344        context: The context.
345    """
346    if getattr(self, '__pydantic_private__', None) is None:
347        pydantic_private = {}
348        for name, private_attr in self.__private_attributes__.items():
349            default = private_attr.get_default()
350            if default is not PydanticUndefined:
351                pydantic_private[name] = default
352        object_setattr(self, '__pydantic_private__', pydantic_private)

This function is meant to behave like a BaseModel method to initialise private attributes.

It takes context as an argument since that's what pydantic-core passes when calling it.

Arguments:
  • self: The BaseModel instance.
  • context: The context.
class NotebookDescr(bioimageio.spec.generic.v0_3.GenericDescrBase):
31class NotebookDescr(GenericDescrBase):
32    """Bioimage.io description of a Jupyter notebook."""
33
34    implemented_type: ClassVar[Literal["notebook"]] = "notebook"
35    if TYPE_CHECKING:
36        type: Literal["notebook"] = "notebook"
37    else:
38        type: Literal["notebook"]
39
40    id: Optional[NotebookId] = None
41    """bioimage.io-wide unique resource identifier
42    assigned by bioimage.io; version **un**specific."""
43
44    parent: Optional[NotebookId] = None
45    """The description from which this one is derived"""
46
47    source: NotebookSource
48    """The Jupyter notebook"""

Bioimage.io description of a Jupyter notebook.

implemented_type: ClassVar[Literal['notebook']] = 'notebook'
id: Optional[bioimageio.spec.notebook.v0_3.NotebookId]

bioimage.io-wide unique resource identifier assigned by bioimage.io; version unspecific.

parent: Optional[bioimageio.spec.notebook.v0_3.NotebookId]

The description from which this one is derived

source: Union[Annotated[bioimageio.spec._internal.url.HttpUrl, WithSuffix(suffix='.ipynb', case_sensitive=True)], Annotated[pathlib.Path, PathType(path_type='file'), FieldInfo(annotation=NoneType, required=True, title='FilePath'), WithSuffix(suffix='.ipynb', case_sensitive=True)], Annotated[bioimageio.spec._internal.io.RelativeFilePath, WithSuffix(suffix='.ipynb', case_sensitive=True)]]

The Jupyter notebook

implemented_format_version_tuple: ClassVar[Tuple[int, int, int]] = (0, 3, 0)
model_config: ClassVar[pydantic.config.ConfigDict] = {'allow_inf_nan': False, 'extra': 'forbid', 'frozen': False, 'model_title_generator': <function _node_title_generator>, 'populate_by_name': True, 'revalidate_instances': 'never', 'use_attribute_docstrings': True, 'validate_assignment': True, 'validate_default': False, 'validate_return': True, 'validate_by_alias': True, 'validate_by_name': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

def model_post_init(self: pydantic.main.BaseModel, context: Any, /) -> None:
337def init_private_attributes(self: BaseModel, context: Any, /) -> None:
338    """This function is meant to behave like a BaseModel method to initialise private attributes.
339
340    It takes context as an argument since that's what pydantic-core passes when calling it.
341
342    Args:
343        self: The BaseModel instance.
344        context: The context.
345    """
346    if getattr(self, '__pydantic_private__', None) is None:
347        pydantic_private = {}
348        for name, private_attr in self.__private_attributes__.items():
349            default = private_attr.get_default()
350            if default is not PydanticUndefined:
351                pydantic_private[name] = default
352        object_setattr(self, '__pydantic_private__', pydantic_private)

This function is meant to behave like a BaseModel method to initialise private attributes.

It takes context as an argument since that's what pydantic-core passes when calling it.

Arguments:
  • self: The BaseModel instance.
  • context: The context.
ResourceDescr = typing.Union[typing.Annotated[typing.Union[typing.Annotated[typing.Union[typing.Annotated[bioimageio.spec.application.v0_2.ApplicationDescr, FieldInfo(annotation=NoneType, required=True, title='application 0.2')], typing.Annotated[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')], typing.Annotated[typing.Union[typing.Annotated[bioimageio.spec.dataset.v0_2.DatasetDescr, FieldInfo(annotation=NoneType, required=True, title='dataset 0.2')], typing.Annotated[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')], typing.Annotated[typing.Union[typing.Annotated[bioimageio.spec.model.v0_4.ModelDescr, FieldInfo(annotation=NoneType, required=True, title='model 0.4')], typing.Annotated[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')], typing.Annotated[typing.Union[typing.Annotated[NotebookDescr, FieldInfo(annotation=NoneType, required=True, title='notebook 0.2')], typing.Annotated[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)], typing.Annotated[typing.Union[typing.Annotated[bioimageio.spec.generic.v0_2.GenericDescr, FieldInfo(annotation=NoneType, required=True, title='generic 0.2')], typing.Annotated[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')]]
def save_bioimageio_package_as_folder( source: Union[Annotated[Union[bioimageio.spec._internal.url.HttpUrl, bioimageio.spec._internal.io.RelativeFilePath, Annotated[pathlib.Path, PathType(path_type='file'), FieldInfo(annotation=NoneType, required=True, title='FilePath')]], FieldInfo(annotation=NoneType, required=True, metadata=[_PydanticGeneralMetadata(union_mode='left_to_right')])], str, pydantic.networks.HttpUrl, zipfile.ZipFile, Dict[str, YamlValue], Mapping[str, YamlValueView], Annotated[Union[Annotated[Union[Annotated[bioimageio.spec.application.v0_2.ApplicationDescr, FieldInfo(annotation=NoneType, required=True, title='application 0.2')], Annotated[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[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[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[NotebookDescr, FieldInfo(annotation=NoneType, required=True, title='notebook 0.2')], Annotated[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[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')]], /, *, output_path: Union[Annotated[pathlib.Path, PathType(path_type='new')], Annotated[pathlib.Path, PathType(path_type='dir')], NoneType] = None, weights_priority_order: Optional[Sequence[Literal['keras_hdf5', 'onnx', 'pytorch_state_dict', 'tensorflow_js', 'tensorflow_saved_model_bundle', 'torchscript']]] = None) -> Annotated[pathlib.Path, PathType(path_type='dir')]:
150def save_bioimageio_package_as_folder(
151    source: Union[BioimageioYamlSource, ResourceDescr],
152    /,
153    *,
154    output_path: Union[NewPath, DirectoryPath, None] = None,
155    weights_priority_order: Optional[  # model only
156        Sequence[
157            Literal[
158                "keras_hdf5",
159                "onnx",
160                "pytorch_state_dict",
161                "tensorflow_js",
162                "tensorflow_saved_model_bundle",
163                "torchscript",
164            ]
165        ]
166    ] = None,
167) -> DirectoryPath:
168    """Write the content of a bioimage.io resource package to a folder.
169
170    Args:
171        source: bioimageio resource description
172        output_path: file path to write package to
173        weights_priority_order: If given only the first weights format present in the model is included.
174                                If none of the prioritized weights formats is found all are included.
175
176    Returns:
177        directory path to bioimageio package folder
178    """
179    package_content = _prepare_resource_package(
180        source,
181        weights_priority_order=weights_priority_order,
182    )
183    if output_path is None:
184        output_path = Path(mkdtemp())
185    else:
186        output_path = Path(output_path)
187
188    output_path.mkdir(exist_ok=True, parents=True)
189    for name, src in package_content.items():
190        if isinstance(src, collections.abc.Mapping):
191            write_yaml(src, output_path / name)
192        else:
193            with (output_path / name).open("wb") as dest:
194                _ = shutil.copyfileobj(src, dest)
195
196    return output_path

Write the content of a bioimage.io resource package to a folder.

Arguments:
  • source: bioimageio resource description
  • output_path: file path to write package to
  • weights_priority_order: If given only the first weights format present in the model is included. If none of the prioritized weights formats is found all are included.
Returns:

directory path to bioimageio package folder

def save_bioimageio_package_to_stream( source: Union[Annotated[Union[bioimageio.spec._internal.url.HttpUrl, bioimageio.spec._internal.io.RelativeFilePath, Annotated[pathlib.Path, PathType(path_type='file'), FieldInfo(annotation=NoneType, required=True, title='FilePath')]], FieldInfo(annotation=NoneType, required=True, metadata=[_PydanticGeneralMetadata(union_mode='left_to_right')])], str, pydantic.networks.HttpUrl, zipfile.ZipFile, Dict[str, YamlValue], Mapping[str, YamlValueView], Annotated[Union[Annotated[Union[Annotated[bioimageio.spec.application.v0_2.ApplicationDescr, FieldInfo(annotation=NoneType, required=True, title='application 0.2')], Annotated[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[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[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[NotebookDescr, FieldInfo(annotation=NoneType, required=True, title='notebook 0.2')], Annotated[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[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')]], /, *, compression: int = 8, compression_level: int = 1, output_stream: Optional[IO[bytes]] = None, weights_priority_order: Optional[Sequence[Literal['keras_hdf5', 'onnx', 'pytorch_state_dict', 'tensorflow_js', 'tensorflow_saved_model_bundle', 'torchscript']]] = None) -> IO[bytes]:
263def save_bioimageio_package_to_stream(
264    source: Union[BioimageioYamlSource, ResourceDescr],
265    /,
266    *,
267    compression: int = ZIP_DEFLATED,
268    compression_level: int = 1,
269    output_stream: Union[IO[bytes], None] = None,
270    weights_priority_order: Optional[  # model only
271        Sequence[
272            Literal[
273                "keras_hdf5",
274                "onnx",
275                "pytorch_state_dict",
276                "tensorflow_js",
277                "tensorflow_saved_model_bundle",
278                "torchscript",
279            ]
280        ]
281    ] = None,
282) -> IO[bytes]:
283    """Package a bioimageio resource into a stream.
284
285    Args:
286        rd: bioimageio resource description
287        compression: The numeric constant of compression method.
288        compression_level: Compression level to use when writing files to the archive.
289                           See https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile
290        output_stream: stream to write package to
291        weights_priority_order: If given only the first weights format present in the model is included.
292                                If none of the prioritized weights formats is found all are included.
293
294    Note: this function bypasses safety checks and does not load/validate the model after writing.
295
296    Returns:
297        stream of zipped bioimageio package
298    """
299    if output_stream is None:
300        output_stream = BytesIO()
301
302    package_content = _prepare_resource_package(
303        source,
304        weights_priority_order=weights_priority_order,
305    )
306
307    write_zip(
308        output_stream,
309        package_content,
310        compression=compression,
311        compression_level=compression_level,
312    )
313
314    return output_stream

Package a bioimageio resource into a stream.

Arguments:
  • rd: bioimageio resource description
  • compression: The numeric constant of compression method.
  • compression_level: Compression level to use when writing files to the archive. See https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile
  • output_stream: stream to write package to
  • weights_priority_order: If given only the first weights format present in the model is included. If none of the prioritized weights formats is found all are included.

Note: this function bypasses safety checks and does not load/validate the model after writing.

Returns:

stream of zipped bioimageio package

def save_bioimageio_package( source: Union[Annotated[Union[bioimageio.spec._internal.url.HttpUrl, bioimageio.spec._internal.io.RelativeFilePath, Annotated[pathlib.Path, PathType(path_type='file'), FieldInfo(annotation=NoneType, required=True, title='FilePath')]], FieldInfo(annotation=NoneType, required=True, metadata=[_PydanticGeneralMetadata(union_mode='left_to_right')])], str, pydantic.networks.HttpUrl, zipfile.ZipFile, Dict[str, YamlValue], Mapping[str, YamlValueView], Annotated[Union[Annotated[Union[Annotated[bioimageio.spec.application.v0_2.ApplicationDescr, FieldInfo(annotation=NoneType, required=True, title='application 0.2')], Annotated[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[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[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[NotebookDescr, FieldInfo(annotation=NoneType, required=True, title='notebook 0.2')], Annotated[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[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')]], /, *, compression: int = 8, compression_level: int = 1, output_path: Union[Annotated[pathlib.Path, PathType(path_type='new')], Annotated[pathlib.Path, PathType(path_type='file')], NoneType] = None, weights_priority_order: Optional[Sequence[Literal['keras_hdf5', 'onnx', 'pytorch_state_dict', 'tensorflow_js', 'tensorflow_saved_model_bundle', 'torchscript']]] = None, allow_invalid: bool = False) -> Annotated[pathlib.Path, PathType(path_type='file')]:
199def save_bioimageio_package(
200    source: Union[BioimageioYamlSource, ResourceDescr],
201    /,
202    *,
203    compression: int = ZIP_DEFLATED,
204    compression_level: int = 1,
205    output_path: Union[NewPath, FilePath, None] = None,
206    weights_priority_order: Optional[  # model only
207        Sequence[
208            Literal[
209                "keras_hdf5",
210                "onnx",
211                "pytorch_state_dict",
212                "tensorflow_js",
213                "tensorflow_saved_model_bundle",
214                "torchscript",
215            ]
216        ]
217    ] = None,
218    allow_invalid: bool = False,
219) -> FilePath:
220    """Package a bioimageio resource as a zip file.
221
222    Args:
223        rd: bioimageio resource description
224        compression: The numeric constant of compression method.
225        compression_level: Compression level to use when writing files to the archive.
226                           See https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile
227        output_path: file path to write package to
228        weights_priority_order: If given only the first weights format present in the model is included.
229                                If none of the prioritized weights formats is found all are included.
230
231    Returns:
232        path to zipped bioimageio package
233    """
234    package_content = _prepare_resource_package(
235        source,
236        weights_priority_order=weights_priority_order,
237    )
238    if output_path is None:
239        output_path = Path(
240            NamedTemporaryFile(suffix=".bioimageio.zip", delete=False).name
241        )
242    else:
243        output_path = Path(output_path)
244
245    write_zip(
246        output_path,
247        package_content,
248        compression=compression,
249        compression_level=compression_level,
250    )
251    with get_validation_context().replace(warning_level=ERROR):
252        if isinstance((exported := load_description(output_path)), InvalidDescr):
253            exported.validation_summary.display()
254            msg = f"Exported package at '{output_path}' is invalid."
255            if allow_invalid:
256                logger.error(msg)
257            else:
258                raise ValueError(msg)
259
260    return output_path

Package a bioimageio resource as a zip file.

Arguments:
  • rd: bioimageio resource description
  • compression: The numeric constant of compression method.
  • compression_level: Compression level to use when writing files to the archive. See https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile
  • output_path: file path to write package to
  • weights_priority_order: If given only the first weights format present in the model is included. If none of the prioritized weights formats is found all are included.
Returns:

path to zipped bioimageio package

def save_bioimageio_yaml_only( rd: Union[Annotated[Union[Annotated[Union[Annotated[bioimageio.spec.application.v0_2.ApplicationDescr, FieldInfo(annotation=NoneType, required=True, title='application 0.2')], Annotated[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[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[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[NotebookDescr, FieldInfo(annotation=NoneType, required=True, title='notebook 0.2')], Annotated[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[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')], Dict[str, YamlValue], InvalidDescr], /, file: Union[Annotated[pathlib.Path, PathType(path_type='new')], Annotated[pathlib.Path, PathType(path_type='file')], TextIO], *, exclude_unset: bool = True, exclude_defaults: bool = False):
202def save_bioimageio_yaml_only(
203    rd: Union[ResourceDescr, BioimageioYamlContent, InvalidDescr],
204    /,
205    file: Union[NewPath, FilePath, TextIO],
206    *,
207    exclude_unset: bool = True,
208    exclude_defaults: bool = False,
209):
210    """write the metadata of a resource description (`rd`) to `file`
211    without writing any of the referenced files in it.
212
213    Args:
214        rd: bioimageio resource description
215        file: file or stream to save to
216        exclude_unset: Exclude fields that have not explicitly be set.
217        exclude_defaults: Exclude fields that have the default value (even if set explicitly).
218
219    Note: To save a resource description with its associated files as a package,
220    use `save_bioimageio_package` or `save_bioimageio_package_as_folder`.
221    """
222    if isinstance(rd, ResourceDescrBase):
223        content = dump_description(
224            rd, exclude_unset=exclude_unset, exclude_defaults=exclude_defaults
225        )
226    else:
227        content = rd
228
229    write_yaml(cast(YamlValue, content), file)

write the metadata of a resource description (rd) to file without writing any of the referenced files in it.

Arguments:
  • rd: bioimageio resource description
  • file: file or stream to save to
  • exclude_unset: Exclude fields that have not explicitly be set.
  • exclude_defaults: Exclude fields that have the default value (even if set explicitly).

Note: To save a resource description with its associated files as a package, use save_bioimageio_package or save_bioimageio_package_as_folder.

settings = Settings(allow_pickle=False, cache_path=PosixPath('/home/runner/.cache/bioimageio'), collection_http_pattern='https://hypha.aicell.io/bioimage-io/artifacts/{bioimageio_id}/files/rdf.yaml', id_map='https://uk1s3.embassy.ebi.ac.uk/public-datasets/bioimage.io/id_map.json', id_map_draft='https://uk1s3.embassy.ebi.ac.uk/public-datasets/bioimage.io/id_map_draft.json', perform_io_checks=True, resolve_draft=True, log_warnings=True, github_username=None, github_token=None, CI='true', user_agent=None)
SpecificResourceDescr = typing.Annotated[typing.Union[typing.Annotated[typing.Union[typing.Annotated[bioimageio.spec.application.v0_2.ApplicationDescr, FieldInfo(annotation=NoneType, required=True, title='application 0.2')], typing.Annotated[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')], typing.Annotated[typing.Union[typing.Annotated[bioimageio.spec.dataset.v0_2.DatasetDescr, FieldInfo(annotation=NoneType, required=True, title='dataset 0.2')], typing.Annotated[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')], typing.Annotated[typing.Union[typing.Annotated[bioimageio.spec.model.v0_4.ModelDescr, FieldInfo(annotation=NoneType, required=True, title='model 0.4')], typing.Annotated[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')], typing.Annotated[typing.Union[typing.Annotated[NotebookDescr, FieldInfo(annotation=NoneType, required=True, title='notebook 0.2')], typing.Annotated[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)]
def update_format( source: Union[Annotated[Union[Annotated[Union[Annotated[bioimageio.spec.application.v0_2.ApplicationDescr, FieldInfo(annotation=NoneType, required=True, title='application 0.2')], Annotated[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[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[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[NotebookDescr, FieldInfo(annotation=NoneType, required=True, title='notebook 0.2')], Annotated[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[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')], Annotated[Union[bioimageio.spec._internal.url.HttpUrl, bioimageio.spec._internal.io.RelativeFilePath, Annotated[pathlib.Path, PathType(path_type='file'), FieldInfo(annotation=NoneType, required=True, title='FilePath')]], FieldInfo(annotation=NoneType, required=True, metadata=[_PydanticGeneralMetadata(union_mode='left_to_right')])], str, pydantic.networks.HttpUrl, zipfile.ZipFile, Dict[str, YamlValue], InvalidDescr], /, *, output: Union[pathlib.Path, TextIO, NoneType] = None, exclude_defaults: bool = True, perform_io_checks: Optional[bool] = None) -> Union[Annotated[Union[ApplicationDescr, DatasetDescr, ModelDescr, NotebookDescr], Discriminator(discriminator='type', custom_error_type=None, custom_error_message=None, custom_error_context=None)], GenericDescr, InvalidDescr]:
258def update_format(
259    source: Union[
260        ResourceDescr,
261        PermissiveFileSource,
262        ZipFile,
263        BioimageioYamlContent,
264        InvalidDescr,
265    ],
266    /,
267    *,
268    output: Union[Path, TextIO, None] = None,
269    exclude_defaults: bool = True,
270    perform_io_checks: Optional[bool] = None,
271) -> Union[LatestResourceDescr, InvalidDescr]:
272    """Update a resource description.
273
274    Notes:
275    - Invalid **source** descriptions may fail to update.
276    - The updated description might be invalid (even if the **source** was valid).
277    """
278
279    if isinstance(source, ResourceDescrBase):
280        root = source.root
281        source = dump_description(source)
282    else:
283        root = None
284
285    if isinstance(source, collections.abc.Mapping):
286        descr = build_description(
287            source,
288            context=get_validation_context().replace(
289                root=root, perform_io_checks=perform_io_checks
290            ),
291            format_version=LATEST,
292        )
293
294    else:
295        descr = load_description(
296            source,
297            perform_io_checks=perform_io_checks,
298            format_version=LATEST,
299        )
300
301    if output is not None:
302        save_bioimageio_yaml_only(descr, file=output, exclude_defaults=exclude_defaults)
303
304    return descr

Update a resource description.

Notes:

  • Invalid source descriptions may fail to update.
  • The updated description might be invalid (even if the source was valid).
def update_hashes( source: Union[Annotated[Union[bioimageio.spec._internal.url.HttpUrl, bioimageio.spec._internal.io.RelativeFilePath, Annotated[pathlib.Path, PathType(path_type='file'), FieldInfo(annotation=NoneType, required=True, title='FilePath')]], FieldInfo(annotation=NoneType, required=True, metadata=[_PydanticGeneralMetadata(union_mode='left_to_right')])], str, pydantic.networks.HttpUrl, zipfile.ZipFile, Annotated[Union[Annotated[Union[Annotated[bioimageio.spec.application.v0_2.ApplicationDescr, FieldInfo(annotation=NoneType, required=True, title='application 0.2')], Annotated[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[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[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[NotebookDescr, FieldInfo(annotation=NoneType, required=True, title='notebook 0.2')], Annotated[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[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')], Dict[str, YamlValue]], /) -> Union[Annotated[Union[Annotated[Union[Annotated[bioimageio.spec.application.v0_2.ApplicationDescr, FieldInfo(annotation=NoneType, required=True, title='application 0.2')], Annotated[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[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[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[NotebookDescr, FieldInfo(annotation=NoneType, required=True, title='notebook 0.2')], Annotated[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[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')], InvalidDescr]:
307def update_hashes(
308    source: Union[PermissiveFileSource, ZipFile, ResourceDescr, BioimageioYamlContent],
309    /,
310) -> Union[ResourceDescr, InvalidDescr]:
311    """Update hash values of the files referenced in **source**."""
312    if isinstance(source, ResourceDescrBase):
313        root = source.root
314        source = dump_description(source)
315    else:
316        root = None
317
318    context = get_validation_context().replace(
319        update_hashes=True, root=root, perform_io_checks=True
320    )
321    with context:
322        if isinstance(source, collections.abc.Mapping):
323            return build_description(source)
324        else:
325            return load_description(source, perform_io_checks=True)

Update hash values of the files referenced in source.

def validate_format( data: Dict[str, YamlValue], /, *, format_version: Union[Literal['latest', 'discover'], str] = 'discover', context: Optional[ValidationContext] = None) -> ValidationSummary:
204def validate_format(
205    data: BioimageioYamlContent,
206    /,
207    *,
208    format_version: Union[Literal["discover", "latest"], str] = DISCOVER,
209    context: Optional[ValidationContext] = None,
210) -> ValidationSummary:
211    """Validate a dictionary holding a bioimageio description.
212    See `bioimagieo.spec.load_description_and_validate_format_only`
213    to validate a file source.
214
215    Args:
216        data: Dictionary holding the raw bioimageio.yaml content.
217        format_version: Format version to (update to and) use for validation.
218        context: Validation context, see `bioimagieo.spec.ValidationContext`
219
220    Note:
221        Use `bioimagieo.spec.load_description_and_validate_format_only` to validate a
222        file source instead of loading the YAML content and creating the appropriate
223        `ValidationContext`.
224
225        Alternatively you can use `bioimagieo.spec.load_description` and access the
226        `validation_summary` attribute of the returned object.
227    """
228    with context or get_validation_context():
229        rd = build_description(data, format_version=format_version)
230
231    assert rd.validation_summary is not None
232    return rd.validation_summary

Validate a dictionary holding a bioimageio description. See bioimagieo.spec.load_description_and_validate_format_only to validate a file source.

Arguments:
  • data: Dictionary holding the raw bioimageio.yaml content.
  • format_version: Format version to (update to and) use for validation.
  • context: Validation context, see bioimagieo.spec.ValidationContext
Note:

Use bioimagieo.spec.load_description_and_validate_format_only to validate a file source instead of loading the YAML content and creating the appropriate ValidationContext.

Alternatively you can use bioimagieo.spec.load_description and access the validation_summary attribute of the returned object.

@dataclass(frozen=True)
class ValidationContext(bioimageio.spec._internal.validation_context.ValidationContextBase):
 57@dataclass(frozen=True)
 58class ValidationContext(ValidationContextBase):
 59    """A validation context used to control validation of bioimageio resources.
 60
 61    For example a relative file path in a bioimageio description requires the **root**
 62    context to evaluate if the file is available and, if **perform_io_checks** is true,
 63    if it matches its expected SHA256 hash value.
 64    """
 65
 66    _context_tokens: "List[Token[Optional[ValidationContext]]]" = field(
 67        init=False,
 68        default_factory=cast(
 69            "Callable[[], List[Token[Optional[ValidationContext]]]]", list
 70        ),
 71    )
 72
 73    cache: Union[
 74        DiskCache[RootHttpUrl], MemoryCache[RootHttpUrl], NoopCache[RootHttpUrl]
 75    ] = field(default=settings.disk_cache)
 76    disable_cache: bool = False
 77    """Disable caching downloads to `settings.cache_path`
 78    and (re)download them to memory instead."""
 79
 80    root: Union[RootHttpUrl, DirectoryPath, ZipFile] = Path()
 81    """Url/directory/archive serving as base to resolve any relative file paths."""
 82
 83    warning_level: WarningLevel = 50
 84    """Treat warnings of severity `s` as validation errors if `s >= warning_level`."""
 85
 86    log_warnings: bool = settings.log_warnings
 87    """If `True` warnings are logged to the terminal
 88
 89    Note: This setting does not affect warning entries
 90        of a generated `bioimageio.spec.ValidationSummary`.
 91    """
 92
 93    progressbar_factory: Optional[Callable[[], Progressbar]] = None
 94    """Callable to return a tqdm-like progressbar.
 95
 96    Currently this is only used for file downloads."""
 97
 98    raise_errors: bool = False
 99    """Directly raise any validation errors
100    instead of aggregating errors and returning a `bioimageio.spec.InvalidDescr`. (for debugging)"""
101
102    @property
103    def summary(self):
104        if isinstance(self.root, ZipFile):
105            if self.root.filename is None:
106                root = "in-memory"
107            else:
108                root = Path(self.root.filename)
109        else:
110            root = self.root
111
112        return ValidationContextSummary(
113            root=root,
114            file_name=self.file_name,
115            perform_io_checks=self.perform_io_checks,
116            known_files=copy(self.known_files),
117            update_hashes=self.update_hashes,
118        )
119
120    def __enter__(self):
121        self._context_tokens.append(_validation_context_var.set(self))
122        return self
123
124    def __exit__(self, type, value, traceback):  # type: ignore
125        _validation_context_var.reset(self._context_tokens.pop(-1))
126
127    def replace(  # TODO: probably use __replace__ when py>=3.13
128        self,
129        root: Optional[Union[RootHttpUrl, DirectoryPath, ZipFile]] = None,
130        warning_level: Optional[WarningLevel] = None,
131        log_warnings: Optional[bool] = None,
132        file_name: Optional[str] = None,
133        perform_io_checks: Optional[bool] = None,
134        known_files: Optional[Dict[str, Optional[Sha256]]] = None,
135        raise_errors: Optional[bool] = None,
136        update_hashes: Optional[bool] = None,
137    ) -> Self:
138        if known_files is None and root is not None and self.root != root:
139            # reset known files if root changes, but no new known_files are given
140            known_files = {}
141
142        return self.__class__(
143            root=self.root if root is None else root,
144            warning_level=(
145                self.warning_level if warning_level is None else warning_level
146            ),
147            log_warnings=self.log_warnings if log_warnings is None else log_warnings,
148            file_name=self.file_name if file_name is None else file_name,
149            perform_io_checks=(
150                self.perform_io_checks
151                if perform_io_checks is None
152                else perform_io_checks
153            ),
154            known_files=self.known_files if known_files is None else known_files,
155            raise_errors=self.raise_errors if raise_errors is None else raise_errors,
156            update_hashes=(
157                self.update_hashes if update_hashes is None else update_hashes
158            ),
159        )
160
161    @property
162    def source_name(self) -> str:
163        if self.file_name is None:
164            return "in-memory"
165        else:
166            try:
167                if isinstance(self.root, Path):
168                    source = (self.root / self.file_name).absolute()
169                else:
170                    parsed = urlsplit(str(self.root))
171                    path = list(parsed.path.strip("/").split("/")) + [self.file_name]
172                    source = urlunsplit(
173                        (
174                            parsed.scheme,
175                            parsed.netloc,
176                            "/".join(path),
177                            parsed.query,
178                            parsed.fragment,
179                        )
180                    )
181            except ValueError:
182                return self.file_name
183            else:
184                return str(source)

A validation context used to control validation of bioimageio resources.

For example a relative file path in a bioimageio description requires the root context to evaluate if the file is available and, if perform_io_checks is true, if it matches its expected SHA256 hash value.

ValidationContext( file_name: Optional[str] = None, perform_io_checks: bool = True, known_files: Dict[str, Optional[bioimageio.spec._internal.io_basics.Sha256]] = <factory>, update_hashes: bool = False, cache: Union[genericache.disk_cache.DiskCache[bioimageio.spec._internal.root_url.RootHttpUrl], genericache.memory_cache.MemoryCache[bioimageio.spec._internal.root_url.RootHttpUrl], genericache.noop_cache.NoopCache[bioimageio.spec._internal.root_url.RootHttpUrl]] = <genericache.disk_cache.DiskCache object>, disable_cache: bool = False, root: Union[bioimageio.spec._internal.root_url.RootHttpUrl, Annotated[pathlib.Path, PathType(path_type='dir')], zipfile.ZipFile] = PosixPath('.'), warning_level: Literal[20, 30, 35, 50] = 50, log_warnings: bool = True, progressbar_factory: Optional[Callable[[], bioimageio.spec._internal.progress.Progressbar]] = None, raise_errors: bool = False)
cache: Union[genericache.disk_cache.DiskCache[bioimageio.spec._internal.root_url.RootHttpUrl], genericache.memory_cache.MemoryCache[bioimageio.spec._internal.root_url.RootHttpUrl], genericache.noop_cache.NoopCache[bioimageio.spec._internal.root_url.RootHttpUrl]] = <genericache.disk_cache.DiskCache object>
disable_cache: bool = False

Disable caching downloads to settings.cache_path and (re)download them to memory instead.

root: Union[bioimageio.spec._internal.root_url.RootHttpUrl, Annotated[pathlib.Path, PathType(path_type='dir')], zipfile.ZipFile] = PosixPath('.')

Url/directory/archive serving as base to resolve any relative file paths.

warning_level: Literal[20, 30, 35, 50] = 50

Treat warnings of severity s as validation errors if s >= warning_level.

log_warnings: bool = True

If True warnings are logged to the terminal

Note: This setting does not affect warning entries of a generated bioimageio.spec.ValidationSummary.

progressbar_factory: Optional[Callable[[], bioimageio.spec._internal.progress.Progressbar]] = None

Callable to return a tqdm-like progressbar.

Currently this is only used for file downloads.

raise_errors: bool = False

Directly raise any validation errors instead of aggregating errors and returning a bioimageio.spec.InvalidDescr. (for debugging)

summary
102    @property
103    def summary(self):
104        if isinstance(self.root, ZipFile):
105            if self.root.filename is None:
106                root = "in-memory"
107            else:
108                root = Path(self.root.filename)
109        else:
110            root = self.root
111
112        return ValidationContextSummary(
113            root=root,
114            file_name=self.file_name,
115            perform_io_checks=self.perform_io_checks,
116            known_files=copy(self.known_files),
117            update_hashes=self.update_hashes,
118        )
def replace( self, root: Union[bioimageio.spec._internal.root_url.RootHttpUrl, Annotated[pathlib.Path, PathType(path_type='dir')], zipfile.ZipFile, NoneType] = None, warning_level: Optional[Literal[20, 30, 35, 50]] = None, log_warnings: Optional[bool] = None, file_name: Optional[str] = None, perform_io_checks: Optional[bool] = None, known_files: Optional[Dict[str, Optional[bioimageio.spec._internal.io_basics.Sha256]]] = None, raise_errors: Optional[bool] = None, update_hashes: Optional[bool] = None) -> Self:
127    def replace(  # TODO: probably use __replace__ when py>=3.13
128        self,
129        root: Optional[Union[RootHttpUrl, DirectoryPath, ZipFile]] = None,
130        warning_level: Optional[WarningLevel] = None,
131        log_warnings: Optional[bool] = None,
132        file_name: Optional[str] = None,
133        perform_io_checks: Optional[bool] = None,
134        known_files: Optional[Dict[str, Optional[Sha256]]] = None,
135        raise_errors: Optional[bool] = None,
136        update_hashes: Optional[bool] = None,
137    ) -> Self:
138        if known_files is None and root is not None and self.root != root:
139            # reset known files if root changes, but no new known_files are given
140            known_files = {}
141
142        return self.__class__(
143            root=self.root if root is None else root,
144            warning_level=(
145                self.warning_level if warning_level is None else warning_level
146            ),
147            log_warnings=self.log_warnings if log_warnings is None else log_warnings,
148            file_name=self.file_name if file_name is None else file_name,
149            perform_io_checks=(
150                self.perform_io_checks
151                if perform_io_checks is None
152                else perform_io_checks
153            ),
154            known_files=self.known_files if known_files is None else known_files,
155            raise_errors=self.raise_errors if raise_errors is None else raise_errors,
156            update_hashes=(
157                self.update_hashes if update_hashes is None else update_hashes
158            ),
159        )
source_name: str
161    @property
162    def source_name(self) -> str:
163        if self.file_name is None:
164            return "in-memory"
165        else:
166            try:
167                if isinstance(self.root, Path):
168                    source = (self.root / self.file_name).absolute()
169                else:
170                    parsed = urlsplit(str(self.root))
171                    path = list(parsed.path.strip("/").split("/")) + [self.file_name]
172                    source = urlunsplit(
173                        (
174                            parsed.scheme,
175                            parsed.netloc,
176                            "/".join(path),
177                            parsed.query,
178                            parsed.fragment,
179                        )
180                    )
181            except ValueError:
182                return self.file_name
183            else:
184                return str(source)
class ValidationSummary(pydantic.main.BaseModel):
241class ValidationSummary(BaseModel, extra="allow"):
242    """Summarizes output of all bioimageio validations and tests
243    for one specific `ResourceDescr` instance."""
244
245    name: str
246    """Name of the validation"""
247    source_name: str
248    """Source of the validated bioimageio description"""
249    id: Optional[str] = None
250    """ID of the resource being validated"""
251    type: str
252    """Type of the resource being validated"""
253    format_version: str
254    """Format version of the resource being validated"""
255    status: Literal["passed", "valid-format", "failed"]
256    """overall status of the bioimageio validation"""
257    metadata_completeness: Annotated[float, annotated_types.Interval(ge=0, le=1)] = 0.0
258    """Estimate of completeness of the metadata in the resource description.
259
260    Note: This completeness estimate may change with subsequent releases
261        and should be considered bioimageio.spec version specific.
262    """
263
264    details: List[ValidationDetail]
265    """List of validation details"""
266    env: Set[InstalledPackage] = Field(
267        default_factory=lambda: {
268            InstalledPackage(name="bioimageio.spec", version=VERSION)
269        }
270    )
271    """List of selected, relevant package versions"""
272
273    saved_conda_list: Optional[str] = None
274
275    @field_serializer("saved_conda_list")
276    def _save_conda_list(self, value: Optional[str]):
277        return self.conda_list
278
279    @property
280    def conda_list(self):
281        if self.saved_conda_list is None:
282            p = subprocess.run(
283                ["conda", "list"],
284                stdout=subprocess.PIPE,
285                stderr=subprocess.STDOUT,
286                shell=True,
287                text=True,
288            )
289            self.saved_conda_list = (
290                p.stdout or f"`conda list` exited with {p.returncode}"
291            )
292
293        return self.saved_conda_list
294
295    @property
296    def status_icon(self):
297        if self.status == "passed":
298            return "✔️"
299        elif self.status == "valid-format":
300            return "🟡"
301        else:
302            return "❌"
303
304    @property
305    def errors(self) -> List[ErrorEntry]:
306        return list(chain.from_iterable(d.errors for d in self.details))
307
308    @property
309    def warnings(self) -> List[WarningEntry]:
310        return list(chain.from_iterable(d.warnings for d in self.details))
311
312    def format(
313        self,
314        *,
315        width: Optional[int] = None,
316        include_conda_list: bool = False,
317    ):
318        """Format summary as Markdown string"""
319        return self._format(
320            width=width, target="md", include_conda_list=include_conda_list
321        )
322
323    format_md = format
324
325    def format_html(
326        self,
327        *,
328        width: Optional[int] = None,
329        include_conda_list: bool = False,
330    ):
331        md_with_html = self._format(
332            target="html", width=width, include_conda_list=include_conda_list
333        )
334        return markdown.markdown(
335            md_with_html, extensions=["tables", "fenced_code", "nl2br"]
336        )
337
338    # TODO: fix bug which casuses extensive white space between the info table and details table
339    # (the generated markdown seems fine)
340    @no_type_check
341    def display(
342        self,
343        *,
344        width: Optional[int] = None,
345        include_conda_list: bool = False,
346        tab_size: int = 4,
347        soft_wrap: bool = True,
348    ) -> None:
349        try:  # render as HTML in Jupyter notebook
350            from IPython.core.getipython import get_ipython
351            from IPython.display import display_html
352        except ImportError:
353            pass
354        else:
355            if get_ipython() is not None:
356                _ = display_html(
357                    self.format_html(
358                        width=width, include_conda_list=include_conda_list
359                    ),
360                    raw=True,
361                )
362                return
363
364        # render with rich
365        self._format(
366            target=rich.console.Console(
367                width=width,
368                tab_size=tab_size,
369                soft_wrap=soft_wrap,
370            ),
371            width=width,
372            include_conda_list=include_conda_list,
373        )
374
375    def add_detail(self, detail: ValidationDetail):
376        if detail.status == "failed":
377            self.status = "failed"
378        elif detail.status != "passed":
379            assert_never(detail.status)
380
381        self.details.append(detail)
382
383    def log(
384        self,
385        to: Union[Literal["display"], Path, Sequence[Union[Literal["display"], Path]]],
386    ) -> List[Path]:
387        """Convenience method to display the validation summary in the terminal and/or
388        save it to disk. See `save` for details."""
389        if to == "display":
390            display = True
391            save_to = []
392        elif isinstance(to, Path):
393            display = False
394            save_to = [to]
395        else:
396            display = "display" in to
397            save_to = [p for p in to if p != "display"]
398
399        if display:
400            self.display()
401
402        return self.save(save_to)
403
404    def save(
405        self, path: Union[Path, Sequence[Path]] = Path("{id}_summary_{now}")
406    ) -> List[Path]:
407        """Save the validation/test summary in JSON, Markdown or HTML format.
408
409        Returns:
410            List of file paths the summary was saved to.
411
412        Notes:
413        - Format is chosen based on the suffix: `.json`, `.md`, `.html`.
414        - If **path** has no suffix it is assumed to be a direcotry to which a
415          `summary.json`, `summary.md` and `summary.html` are saved to.
416        """
417        if isinstance(path, (str, Path)):
418            path = [Path(path)]
419
420        # folder to file paths
421        file_paths: List[Path] = []
422        for p in path:
423            if p.suffix:
424                file_paths.append(p)
425            else:
426                file_paths.extend(
427                    [
428                        p / "summary.json",
429                        p / "summary.md",
430                        p / "summary.html",
431                    ]
432                )
433
434        now = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ")
435        for p in file_paths:
436            p = Path(str(p).format(id=self.id or "bioimageio", now=now))
437            if p.suffix == ".json":
438                self.save_json(p)
439            elif p.suffix == ".md":
440                self.save_markdown(p)
441            elif p.suffix == ".html":
442                self.save_html(p)
443            else:
444                raise ValueError(f"Unknown summary path suffix '{p.suffix}'")
445
446        return file_paths
447
448    def save_json(
449        self, path: Path = Path("summary.json"), *, indent: Optional[int] = 2
450    ):
451        """Save validation/test summary as JSON file."""
452        json_str = self.model_dump_json(indent=indent)
453        path.parent.mkdir(exist_ok=True, parents=True)
454        _ = path.write_text(json_str, encoding="utf-8")
455        logger.info("Saved summary to {}", path.absolute())
456
457    def save_markdown(self, path: Path = Path("summary.md")):
458        """Save rendered validation/test summary as Markdown file."""
459        formatted = self.format_md()
460        path.parent.mkdir(exist_ok=True, parents=True)
461        _ = path.write_text(formatted, encoding="utf-8")
462        logger.info("Saved Markdown formatted summary to {}", path.absolute())
463
464    def save_html(self, path: Path = Path("summary.html")) -> None:
465        """Save rendered validation/test summary as HTML file."""
466        path.parent.mkdir(exist_ok=True, parents=True)
467
468        html = self.format_html()
469        _ = path.write_text(html, encoding="utf-8")
470        logger.info("Saved HTML formatted summary to {}", path.absolute())
471
472    @classmethod
473    def load_json(cls, path: Path) -> Self:
474        """Load validation/test summary from a suitable JSON file"""
475        json_str = Path(path).read_text(encoding="utf-8")
476        return cls.model_validate_json(json_str)
477
478    @field_validator("env", mode="before")
479    def _convert_dict(cls, value: List[Union[List[str], Dict[str, str]]]):
480        """convert old env value for backwards compatibility"""
481        if isinstance(value, list):
482            return [
483                (
484                    (v["name"], v["version"], v.get("build", ""), v.get("channel", ""))
485                    if isinstance(v, dict) and "name" in v and "version" in v
486                    else v
487                )
488                for v in value
489            ]
490        else:
491            return value
492
493    def _format(
494        self,
495        *,
496        target: Union[rich.console.Console, Literal["html", "md"]],
497        width: Optional[int],
498        include_conda_list: bool,
499    ):
500        return _format_summary(
501            self,
502            target=target,
503            width=width or 100,
504            include_conda_list=include_conda_list,
505        )

Summarizes output of all bioimageio validations and tests for one specific ResourceDescr instance.

name: str

Name of the validation

source_name: str

Source of the validated bioimageio description

id: Optional[str]

ID of the resource being validated

type: str

Type of the resource being validated

format_version: str

Format version of the resource being validated

status: Literal['passed', 'valid-format', 'failed']

overall status of the bioimageio validation

metadata_completeness: Annotated[float, Interval(gt=None, ge=0, lt=None, le=1)]

Estimate of completeness of the metadata in the resource description.

Note: This completeness estimate may change with subsequent releases and should be considered bioimageio.spec version specific.

List of validation details

List of selected, relevant package versions

saved_conda_list: Optional[str]
conda_list
279    @property
280    def conda_list(self):
281        if self.saved_conda_list is None:
282            p = subprocess.run(
283                ["conda", "list"],
284                stdout=subprocess.PIPE,
285                stderr=subprocess.STDOUT,
286                shell=True,
287                text=True,
288            )
289            self.saved_conda_list = (
290                p.stdout or f"`conda list` exited with {p.returncode}"
291            )
292
293        return self.saved_conda_list
status_icon
295    @property
296    def status_icon(self):
297        if self.status == "passed":
298            return "✔️"
299        elif self.status == "valid-format":
300            return "🟡"
301        else:
302            return "❌"
errors: List[bioimageio.spec.summary.ErrorEntry]
304    @property
305    def errors(self) -> List[ErrorEntry]:
306        return list(chain.from_iterable(d.errors for d in self.details))
warnings: List[bioimageio.spec.summary.WarningEntry]
308    @property
309    def warnings(self) -> List[WarningEntry]:
310        return list(chain.from_iterable(d.warnings for d in self.details))
def format( self, *, width: Optional[int] = None, include_conda_list: bool = False):
312    def format(
313        self,
314        *,
315        width: Optional[int] = None,
316        include_conda_list: bool = False,
317    ):
318        """Format summary as Markdown string"""
319        return self._format(
320            width=width, target="md", include_conda_list=include_conda_list
321        )

Format summary as Markdown string

def format_md( self, *, width: Optional[int] = None, include_conda_list: bool = False):
312    def format(
313        self,
314        *,
315        width: Optional[int] = None,
316        include_conda_list: bool = False,
317    ):
318        """Format summary as Markdown string"""
319        return self._format(
320            width=width, target="md", include_conda_list=include_conda_list
321        )

Format summary as Markdown string

def format_html( self, *, width: Optional[int] = None, include_conda_list: bool = False):
325    def format_html(
326        self,
327        *,
328        width: Optional[int] = None,
329        include_conda_list: bool = False,
330    ):
331        md_with_html = self._format(
332            target="html", width=width, include_conda_list=include_conda_list
333        )
334        return markdown.markdown(
335            md_with_html, extensions=["tables", "fenced_code", "nl2br"]
336        )
@no_type_check
def display( self, *, width: Optional[int] = None, include_conda_list: bool = False, tab_size: int = 4, soft_wrap: bool = True) -> None:
340    @no_type_check
341    def display(
342        self,
343        *,
344        width: Optional[int] = None,
345        include_conda_list: bool = False,
346        tab_size: int = 4,
347        soft_wrap: bool = True,
348    ) -> None:
349        try:  # render as HTML in Jupyter notebook
350            from IPython.core.getipython import get_ipython
351            from IPython.display import display_html
352        except ImportError:
353            pass
354        else:
355            if get_ipython() is not None:
356                _ = display_html(
357                    self.format_html(
358                        width=width, include_conda_list=include_conda_list
359                    ),
360                    raw=True,
361                )
362                return
363
364        # render with rich
365        self._format(
366            target=rich.console.Console(
367                width=width,
368                tab_size=tab_size,
369                soft_wrap=soft_wrap,
370            ),
371            width=width,
372            include_conda_list=include_conda_list,
373        )
def add_detail(self, detail: bioimageio.spec.summary.ValidationDetail):
375    def add_detail(self, detail: ValidationDetail):
376        if detail.status == "failed":
377            self.status = "failed"
378        elif detail.status != "passed":
379            assert_never(detail.status)
380
381        self.details.append(detail)
def log( self, to: Union[Literal['display'], pathlib.Path, Sequence[Union[Literal['display'], pathlib.Path]]]) -> List[pathlib.Path]:
383    def log(
384        self,
385        to: Union[Literal["display"], Path, Sequence[Union[Literal["display"], Path]]],
386    ) -> List[Path]:
387        """Convenience method to display the validation summary in the terminal and/or
388        save it to disk. See `save` for details."""
389        if to == "display":
390            display = True
391            save_to = []
392        elif isinstance(to, Path):
393            display = False
394            save_to = [to]
395        else:
396            display = "display" in to
397            save_to = [p for p in to if p != "display"]
398
399        if display:
400            self.display()
401
402        return self.save(save_to)

Convenience method to display the validation summary in the terminal and/or save it to disk. See save for details.

def save( self, path: Union[pathlib.Path, Sequence[pathlib.Path]] = PosixPath('{id}_summary_{now}')) -> List[pathlib.Path]:
404    def save(
405        self, path: Union[Path, Sequence[Path]] = Path("{id}_summary_{now}")
406    ) -> List[Path]:
407        """Save the validation/test summary in JSON, Markdown or HTML format.
408
409        Returns:
410            List of file paths the summary was saved to.
411
412        Notes:
413        - Format is chosen based on the suffix: `.json`, `.md`, `.html`.
414        - If **path** has no suffix it is assumed to be a direcotry to which a
415          `summary.json`, `summary.md` and `summary.html` are saved to.
416        """
417        if isinstance(path, (str, Path)):
418            path = [Path(path)]
419
420        # folder to file paths
421        file_paths: List[Path] = []
422        for p in path:
423            if p.suffix:
424                file_paths.append(p)
425            else:
426                file_paths.extend(
427                    [
428                        p / "summary.json",
429                        p / "summary.md",
430                        p / "summary.html",
431                    ]
432                )
433
434        now = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ")
435        for p in file_paths:
436            p = Path(str(p).format(id=self.id or "bioimageio", now=now))
437            if p.suffix == ".json":
438                self.save_json(p)
439            elif p.suffix == ".md":
440                self.save_markdown(p)
441            elif p.suffix == ".html":
442                self.save_html(p)
443            else:
444                raise ValueError(f"Unknown summary path suffix '{p.suffix}'")
445
446        return file_paths

Save the validation/test summary in JSON, Markdown or HTML format.

Returns:

List of file paths the summary was saved to.

Notes:

  • Format is chosen based on the suffix: .json, .md, .html.
  • If path has no suffix it is assumed to be a direcotry to which a summary.json, summary.md and summary.html are saved to.
def save_json( self, path: pathlib.Path = PosixPath('summary.json'), *, indent: Optional[int] = 2):
448    def save_json(
449        self, path: Path = Path("summary.json"), *, indent: Optional[int] = 2
450    ):
451        """Save validation/test summary as JSON file."""
452        json_str = self.model_dump_json(indent=indent)
453        path.parent.mkdir(exist_ok=True, parents=True)
454        _ = path.write_text(json_str, encoding="utf-8")
455        logger.info("Saved summary to {}", path.absolute())

Save validation/test summary as JSON file.

def save_markdown(self, path: pathlib.Path = PosixPath('summary.md')):
457    def save_markdown(self, path: Path = Path("summary.md")):
458        """Save rendered validation/test summary as Markdown file."""
459        formatted = self.format_md()
460        path.parent.mkdir(exist_ok=True, parents=True)
461        _ = path.write_text(formatted, encoding="utf-8")
462        logger.info("Saved Markdown formatted summary to {}", path.absolute())

Save rendered validation/test summary as Markdown file.

def save_html(self, path: pathlib.Path = PosixPath('summary.html')) -> None:
464    def save_html(self, path: Path = Path("summary.html")) -> None:
465        """Save rendered validation/test summary as HTML file."""
466        path.parent.mkdir(exist_ok=True, parents=True)
467
468        html = self.format_html()
469        _ = path.write_text(html, encoding="utf-8")
470        logger.info("Saved HTML formatted summary to {}", path.absolute())

Save rendered validation/test summary as HTML file.

@classmethod
def load_json(cls, path: pathlib.Path) -> Self:
472    @classmethod
473    def load_json(cls, path: Path) -> Self:
474        """Load validation/test summary from a suitable JSON file"""
475        json_str = Path(path).read_text(encoding="utf-8")
476        return cls.model_validate_json(json_str)

Load validation/test summary from a suitable JSON file

model_config: ClassVar[pydantic.config.ConfigDict] = {'extra': 'allow'}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].