Coverage for src/bioimageio/spec/utils.py: 92%
64 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-25 19:19 +0000
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-25 19:19 +0000
1"""Utility functions for bioimage.io specifications (mostly IO)."""
3from __future__ import annotations
5import json
6import shutil
7from typing import Any, TypedDict
9import pydantic
10from imageio.v3 import imread # pyright: ignore[reportUnknownVariableType]
11from loguru import logger
12from numpy.typing import NDArray
14from ._description import ensure_description_is_dataset as ensure_description_is_dataset
15from ._description import ensure_description_is_model as ensure_description_is_model
16from ._internal._settings import settings
17from ._internal.io import FileDescr
18from ._internal.io import download as download
19from ._internal.io import extract_file_name as extract_file_name
20from ._internal.io import get_reader as get_reader
21from ._internal.io import get_sha256 as get_sha256
22from ._internal.io import (
23 identify_bioimageio_yaml_file_name as identify_bioimageio_yaml_file_name,
24)
25from ._internal.io import interprete_file_source as interprete_file_source
26from ._internal.io import is_valid_bioimageio_yaml_name as is_valid_bioimageio_yaml_name
27from ._internal.io_basics import ZipPath
28from ._internal.io_utils import load_array as load_array
29from ._internal.io_utils import open_bioimageio_yaml as open_bioimageio_yaml
30from ._internal.io_utils import read_yaml as read_yaml
31from ._internal.io_utils import save_array as save_array
32from ._internal.io_utils import write_yaml as write_yaml
33from ._internal.types import PermissiveFileSource, RelativeFilePath
34from ._internal.utils import files
36get_file_name = extract_file_name
39class SpdxLicenseEntry(TypedDict):
40 isDeprecatedLicenseId: bool
41 isKnownByZenodo: bool
42 isOsiApproved: bool
43 licenseId: str
44 name: str
45 reference: str
48class SpdxLicenses(TypedDict):
49 licenseListVersion: str
50 licenses: list[SpdxLicenseEntry]
51 releaseDate: str
54def get_spdx_licenses() -> SpdxLicenses:
55 """get details of the SPDX licenses known to bioimageio.spec"""
56 with (
57 files("bioimageio.spec")
58 .joinpath("static/spdx_licenses.json")
59 .open("r", encoding="utf-8")
60 ) as f:
61 return json.load(f)
64def get_bioimageio_json_schema() -> dict[str, Any]:
65 """get the bioimageio specification as a JSON schema"""
66 with (
67 files("bioimageio.spec")
68 .joinpath("static/bioimageio_schema.json")
69 .open("r", encoding="utf-8")
70 ) as f:
71 return json.load(f)
74def load_image(
75 source: FileDescr | ZipPath | PermissiveFileSource,
76) -> NDArray[Any]:
77 """load a single image as numpy array
79 Args:
80 source: image source
81 """
83 source = _interprete_file_source(source)
85 if source.suffix == ".npy":
86 return load_array(source)
87 else:
88 reader = get_reader(source)
89 return imread(reader.read(), extension=source.suffix) # pyright: ignore[reportUnknownVariableType]
92def _interprete_file_source(source: FileDescr | ZipPath | PermissiveFileSource):
93 if isinstance(source, (FileDescr, ZipPath)):
94 parsed_source = source
95 elif isinstance(source, (str, pydantic.AnyUrl)):
96 parsed_source = interprete_file_source(source)
97 else:
98 parsed_source = source
100 if isinstance(parsed_source, RelativeFilePath):
101 parsed_source = parsed_source.absolute()
103 return parsed_source
106def empty_cache():
107 """Empty the bioimageio disk cache."""
109 shutil.rmtree(settings.cache_path)
110 settings.cache_path.mkdir(parents=True, exist_ok=True)
111 logger.info("Emptied cache at {}", settings.cache_path)