Coverage for src / bioimageio / core / common.py: 100%
55 statements
« prev ^ index » next coverage.py v7.14.0, created at 2026-05-18 12:35 +0000
« prev ^ index » next coverage.py v7.14.0, created at 2026-05-18 12:35 +0000
1from __future__ import annotations
3from types import MappingProxyType
4from typing import (
5 Hashable,
6 Literal,
7 Mapping,
8 NamedTuple,
9 Tuple,
10 TypeVar,
11 Union,
12)
14from typing_extensions import Self, assert_never
16from bioimageio.spec.model import v0_5
18from .axis import AxisId
20SupportedWeightsFormat = Literal[
21 "keras_hdf5",
22 "keras_v3",
23 "onnx",
24 "pytorch_state_dict",
25 "tensorflow_saved_model_bundle",
26 "torchscript",
27]
29QuantileMethod = Literal[
30 "inverted_cdf",
31 # "averaged_inverted_cdf",
32 # "closest_observation",
33 # "interpolated_inverted_cdf",
34 # "hazen",
35 # "weibull",
36 "linear",
37 # "median_unbiased",
38 # "normal_unbiased",
39]
40"""Methods to use when the desired quantile lies between two data points.
41See https://numpy.org/devdocs/reference/generated/numpy.quantile.html#numpy-quantile for details.
43Note:
44 Only relevant for `SampleQuantile` measures, as `DatasetQuantile` measures computed by [bioimageio.core.stat_calculators.][] are approximations (and use the "linear" method for each sample quantiles)
46!!! warning
47 Limited choices to map more easily to bioimageio.spec descriptions.
48 Current implementations:
49 - [bioimageio.spec.model.v0_5.ClipKwargs][] implies "inverted_cdf" for sample quantiles and "linear" (numpy's default) for dataset quantiles.
50 - [bioimageio.spec.model.v0_5.ScaleRangeKwargs][] implies "linear" (numpy's default)
52"""
54DTypeStr = Literal[
55 "bool",
56 "float32",
57 "float64",
58 "int8",
59 "int16",
60 "int32",
61 "int64",
62 "uint8",
63 "uint16",
64 "uint32",
65 "uint64",
66]
69_LeftRight_T = TypeVar("_LeftRight_T", bound="_LeftRight")
70_LeftRightLike = Union[int, Tuple[int, int], _LeftRight_T]
73class _LeftRight(NamedTuple):
74 left: int
75 right: int
77 @classmethod
78 def create(cls, like: _LeftRightLike[Self]) -> Self:
79 if isinstance(like, cls):
80 return like
81 elif isinstance(like, tuple):
82 return cls(*like)
83 elif isinstance(like, int):
84 return cls(like, like)
85 else:
86 assert_never(like)
89_Where = Literal["left", "right", "left_and_right"]
92class CropWidth(_LeftRight):
93 pass
96CropWidthLike = _LeftRightLike[CropWidth]
97CropWhere = _Where
100class Halo(_LeftRight):
101 pass
104HaloLike = _LeftRightLike[Halo]
107class OverlapWidth(_LeftRight):
108 pass
111class PadWidth(_LeftRight):
112 pass
115PadWidthLike = _LeftRightLike[PadWidth]
116Padding = v0_5.Padding
117PadMode = Union[Literal["constant", "edge", "reflect", "symmetric"], Padding]
118PadWhere = _Where
121class SliceInfo(NamedTuple):
122 start: int
123 stop: int
126SampleId = Hashable
127"""ID of a sample, see `bioimageio.core.sample.Sample`"""
128MemberId = v0_5.TensorId
129"""ID of a `Sample` member, see `bioimageio.core.sample.Sample`"""
131BlocksizeParameter = Union[
132 v0_5.ParameterizedSize_N,
133 Mapping[Tuple[MemberId, AxisId], v0_5.ParameterizedSize_N],
134]
135"""
136Parameter to determine a concrete size for paramtrized axis sizes defined by
137`bioimageio.spec.model.v0_5.ParameterizedSize`.
138"""
140T = TypeVar("T")
141PerMember = Mapping[MemberId, T]
143BlockIndex = int
144TotalNumberOfBlocks = int
147K = TypeVar("K", bound=Hashable)
148V = TypeVar("V")
150Frozen = MappingProxyType
151# class Frozen(Mapping[K, V]): # adapted from xarray.core.utils.Frozen
152# """Wrapper around an object implementing the mapping interface to make it
153# immutable."""
155# __slots__ = ("mapping",)
157# def __init__(self, mapping: Mapping[K, V]):
158# super().__init__()
159# self.mapping = deepcopy(
160# mapping
161# ) # added deepcopy (compared to xarray.core.utils.Frozen)
163# def __getitem__(self, key: K) -> V:
164# return self.mapping[key]
166# def __iter__(self) -> Iterator[K]:
167# return iter(self.mapping)
169# def __len__(self) -> int:
170# return len(self.mapping)
172# def __contains__(self, key: object) -> bool:
173# return key in self.mapping
175# def __repr__(self) -> str:
176# return f"{type(self).__name__}({self.mapping!r})"