Coverage for src / bioimageio / core / common.py: 100%
54 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-02-13 09:46 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-02-13 09:46 +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 "onnx",
23 "pytorch_state_dict",
24 "tensorflow_saved_model_bundle",
25 "torchscript",
26]
28QuantileMethod = Literal[
29 "inverted_cdf",
30 # "averaged_inverted_cdf",
31 # "closest_observation",
32 # "interpolated_inverted_cdf",
33 # "hazen",
34 # "weibull",
35 "linear",
36 # "median_unbiased",
37 # "normal_unbiased",
38]
39"""Methods to use when the desired quantile lies between two data points.
40See https://numpy.org/devdocs/reference/generated/numpy.quantile.html#numpy-quantile for details.
42Note:
43 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)
45!!! warning
46 Limited choices to map more easily to bioimageio.spec descriptions.
47 Current implementations:
48 - [bioimageio.spec.model.v0_5.ClipKwargs][] implies "inverted_cdf" for sample quantiles and "linear" (numpy's default) for dataset quantiles.
49 - [bioimageio.spec.model.v0_5.ScaleRangeKwargs][] implies "linear" (numpy's default)
51"""
53DTypeStr = Literal[
54 "bool",
55 "float32",
56 "float64",
57 "int8",
58 "int16",
59 "int32",
60 "int64",
61 "uint8",
62 "uint16",
63 "uint32",
64 "uint64",
65]
68_LeftRight_T = TypeVar("_LeftRight_T", bound="_LeftRight")
69_LeftRightLike = Union[int, Tuple[int, int], _LeftRight_T]
72class _LeftRight(NamedTuple):
73 left: int
74 right: int
76 @classmethod
77 def create(cls, like: _LeftRightLike[Self]) -> Self:
78 if isinstance(like, cls):
79 return like
80 elif isinstance(like, tuple):
81 return cls(*like)
82 elif isinstance(like, int):
83 return cls(like, like)
84 else:
85 assert_never(like)
88_Where = Literal["left", "right", "left_and_right"]
91class CropWidth(_LeftRight):
92 pass
95CropWidthLike = _LeftRightLike[CropWidth]
96CropWhere = _Where
99class Halo(_LeftRight):
100 pass
103HaloLike = _LeftRightLike[Halo]
106class OverlapWidth(_LeftRight):
107 pass
110class PadWidth(_LeftRight):
111 pass
114PadWidthLike = _LeftRightLike[PadWidth]
115PadMode = Literal["edge", "reflect", "symmetric"]
116PadWhere = _Where
119class SliceInfo(NamedTuple):
120 start: int
121 stop: int
124SampleId = Hashable
125"""ID of a sample, see `bioimageio.core.sample.Sample`"""
126MemberId = v0_5.TensorId
127"""ID of a `Sample` member, see `bioimageio.core.sample.Sample`"""
129BlocksizeParameter = Union[
130 v0_5.ParameterizedSize_N,
131 Mapping[Tuple[MemberId, AxisId], v0_5.ParameterizedSize_N],
132]
133"""
134Parameter to determine a concrete size for paramtrized axis sizes defined by
135`bioimageio.spec.model.v0_5.ParameterizedSize`.
136"""
138T = TypeVar("T")
139PerMember = Mapping[MemberId, T]
141BlockIndex = int
142TotalNumberOfBlocks = int
145K = TypeVar("K", bound=Hashable)
146V = TypeVar("V")
148Frozen = MappingProxyType
149# class Frozen(Mapping[K, V]): # adapted from xarray.core.utils.Frozen
150# """Wrapper around an object implementing the mapping interface to make it
151# immutable."""
153# __slots__ = ("mapping",)
155# def __init__(self, mapping: Mapping[K, V]):
156# super().__init__()
157# self.mapping = deepcopy(
158# mapping
159# ) # added deepcopy (compared to xarray.core.utils.Frozen)
161# def __getitem__(self, key: K) -> V:
162# return self.mapping[key]
164# def __iter__(self) -> Iterator[K]:
165# return iter(self.mapping)
167# def __len__(self) -> int:
168# return len(self.mapping)
170# def __contains__(self, key: object) -> bool:
171# return key in self.mapping
173# def __repr__(self) -> str:
174# return f"{type(self).__name__}({self.mapping!r})"