Coverage for src / bioimageio / core / common.py: 100%
54 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-27 22:06 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-27 22:06 +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]
116PadMode = Literal["constant", "edge", "reflect", "symmetric"]
117PadWhere = _Where
120class SliceInfo(NamedTuple):
121 start: int
122 stop: int
125SampleId = Hashable
126"""ID of a sample, see `bioimageio.core.sample.Sample`"""
127MemberId = v0_5.TensorId
128"""ID of a `Sample` member, see `bioimageio.core.sample.Sample`"""
130BlocksizeParameter = Union[
131 v0_5.ParameterizedSize_N,
132 Mapping[Tuple[MemberId, AxisId], v0_5.ParameterizedSize_N],
133]
134"""
135Parameter to determine a concrete size for paramtrized axis sizes defined by
136`bioimageio.spec.model.v0_5.ParameterizedSize`.
137"""
139T = TypeVar("T")
140PerMember = Mapping[MemberId, T]
142BlockIndex = int
143TotalNumberOfBlocks = int
146K = TypeVar("K", bound=Hashable)
147V = TypeVar("V")
149Frozen = MappingProxyType
150# class Frozen(Mapping[K, V]): # adapted from xarray.core.utils.Frozen
151# """Wrapper around an object implementing the mapping interface to make it
152# immutable."""
154# __slots__ = ("mapping",)
156# def __init__(self, mapping: Mapping[K, V]):
157# super().__init__()
158# self.mapping = deepcopy(
159# mapping
160# ) # added deepcopy (compared to xarray.core.utils.Frozen)
162# def __getitem__(self, key: K) -> V:
163# return self.mapping[key]
165# def __iter__(self) -> Iterator[K]:
166# return iter(self.mapping)
168# def __len__(self) -> int:
169# return len(self.mapping)
171# def __contains__(self, key: object) -> bool:
172# return key in self.mapping
174# def __repr__(self) -> str:
175# return f"{type(self).__name__}({self.mapping!r})"