Coverage for src/bioimageio/spec/utils.py: 93%

61 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-18 09:17 +0000

1"""Utility functions for bioimage.io specifications (mostly IO).""" 

2 

3from __future__ import annotations 

4 

5import json 

6import shutil 

7from typing import Any, TypedDict 

8 

9from imageio.v3 import imread # pyright: ignore[reportUnknownVariableType] 

10from loguru import logger 

11from numpy.typing import NDArray 

12 

13from ._description import ensure_description_is_dataset as ensure_description_is_dataset 

14from ._description import ensure_description_is_model as ensure_description_is_model 

15from ._internal._settings import settings 

16from ._internal.io import FileDescr 

17from ._internal.io import download as download 

18from ._internal.io import extract_file_name as extract_file_name 

19from ._internal.io import get_reader as get_reader 

20from ._internal.io import get_sha256 as get_sha256 

21from ._internal.io import ( 

22 identify_bioimageio_yaml_file_name as identify_bioimageio_yaml_file_name, 

23) 

24from ._internal.io import interprete_file_source as interprete_file_source 

25from ._internal.io import is_valid_bioimageio_yaml_name as is_valid_bioimageio_yaml_name 

26from ._internal.io_basics import ZipPath 

27from ._internal.io_utils import load_array as load_array 

28from ._internal.io_utils import open_bioimageio_yaml as open_bioimageio_yaml 

29from ._internal.io_utils import read_yaml as read_yaml 

30from ._internal.io_utils import save_array as save_array 

31from ._internal.io_utils import write_yaml as write_yaml 

32from ._internal.types import PermissiveFileSource, RelativeFilePath 

33from ._internal.utils import files 

34 

35get_file_name = extract_file_name 

36 

37 

38class SpdxLicenseEntry(TypedDict): 

39 isDeprecatedLicenseId: bool 

40 isKnownByZenodo: bool 

41 isOsiApproved: bool 

42 licenseId: str 

43 name: str 

44 reference: str 

45 

46 

47class SpdxLicenses(TypedDict): 

48 licenseListVersion: str 

49 licenses: list[SpdxLicenseEntry] 

50 releaseDate: str 

51 

52 

53def get_spdx_licenses() -> SpdxLicenses: 

54 """get details of the SPDX licenses known to bioimageio.spec""" 

55 with ( 

56 files("bioimageio.spec") 

57 .joinpath("static/spdx_licenses.json") 

58 .open("r", encoding="utf-8") 

59 ) as f: 

60 return json.load(f) 

61 

62 

63def get_bioimageio_json_schema() -> dict[str, Any]: 

64 """get the bioimageio specification as a JSON schema""" 

65 with ( 

66 files("bioimageio.spec") 

67 .joinpath("static/bioimageio_schema.json") 

68 .open("r", encoding="utf-8") 

69 ) as f: 

70 return json.load(f) 

71 

72 

73def load_image( 

74 source: FileDescr | ZipPath | PermissiveFileSource, 

75) -> NDArray[Any]: 

76 """load a single image as numpy array 

77 

78 Args: 

79 source: image source 

80 """ 

81 

82 source = _interprete_file_source(source) 

83 

84 if source.suffix == ".npy": 

85 return load_array(source) 

86 else: 

87 reader = get_reader(source) 

88 return imread(reader.read(), extension=source.suffix) # pyright: ignore[reportUnknownVariableType] 

89 

90 

91def _interprete_file_source(source: FileDescr | ZipPath | PermissiveFileSource): 

92 if isinstance(source, (FileDescr, ZipPath)): 

93 parsed_source = source 

94 else: 

95 parsed_source = interprete_file_source(source) 

96 if isinstance(parsed_source, RelativeFilePath): 

97 parsed_source = parsed_source.absolute() 

98 

99 return parsed_source 

100 

101 

102def empty_cache(): 

103 """Empty the bioimageio disk cache.""" 

104 

105 shutil.rmtree(settings.cache_path) 

106 settings.cache_path.mkdir(parents=True, exist_ok=True) 

107 logger.info("Emptied cache at {}", settings.cache_path)