Coverage for src/bioimageio/core/common.py: 96%
70 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 09:46 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 09:46 +0000
1from __future__ import annotations
3from collections.abc import Hashable, Mapping
4from dataclasses import dataclass
5from typing import (
6 Literal,
7 NamedTuple,
8 TypeAlias,
9 TypeVar,
10)
12from typing_extensions import Self, assert_never
14from bioimageio.spec.model import v0_5
16SupportedWeightsFormat: TypeAlias = Literal[
17 "keras_hdf5",
18 "keras_v3",
19 "onnx",
20 "pytorch_state_dict",
21 "tensorflow_saved_model_bundle",
22 "torchscript",
23]
25QuantileMethod: TypeAlias = Literal[
26 "inverted_cdf",
27 # "averaged_inverted_cdf",
28 # "closest_observation",
29 # "interpolated_inverted_cdf",
30 # "hazen",
31 # "weibull",
32 "linear",
33 # "median_unbiased",
34 # "normal_unbiased",
35]
36"""Methods to use when the desired quantile lies between two data points.
37See https://numpy.org/devdocs/reference/generated/numpy.quantile.html#numpy-quantile for details.
39Note:
40 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)
42!!! warning
43 Limited choices to map more easily to bioimageio.spec descriptions.
44 Current implementations:
45 - [bioimageio.spec.model.v0_5.ClipKwargs][] implies "inverted_cdf" for sample quantiles and "linear" (numpy's default) for dataset quantiles.
46 - [bioimageio.spec.model.v0_5.ScaleRangeKwargs][] implies "linear" (numpy's default)
48"""
50DTypeStr: TypeAlias = Literal[
51 "bool",
52 "float32",
53 "float64",
54 "int8",
55 "int16",
56 "int32",
57 "int64",
58 "uint8",
59 "uint16",
60 "uint32",
61 "uint64",
62]
65_LeftRight_T = TypeVar("_LeftRight_T", bound="_LeftRight")
66_LeftRightLike = int | tuple[int, int] | _LeftRight_T
69class _LeftRight(NamedTuple):
70 left: int
71 right: int
73 @classmethod
74 def create(cls, like: _LeftRightLike[Self]) -> Self:
75 if isinstance(like, cls):
76 return like
77 elif isinstance(like, tuple):
78 return cls(*like)
79 elif isinstance(like, int):
80 return cls(like, like)
81 else:
82 assert_never(like)
85_Where: TypeAlias = Literal["left", "right", "left_and_right"]
88class CropWidth(_LeftRight):
89 pass
92CropWidthLike: TypeAlias = _LeftRightLike[CropWidth]
93CropWhere: TypeAlias = _Where
96class Halo(_LeftRight):
97 pass
100HaloLike: TypeAlias = _LeftRightLike[Halo]
103class OverlapWidth(_LeftRight):
104 pass
107class PadWidth(_LeftRight):
108 pass
111PadWidthLike: TypeAlias = _LeftRightLike[PadWidth]
112Padding: TypeAlias = v0_5.Padding
113PadMode: TypeAlias = Literal["constant", "edge", "reflect", "symmetric"] | Padding
114PadWhere: TypeAlias = _Where
117@dataclass(frozen=True, slots=True)
118class SliceInfo:
119 start: int
120 stop: int
123@dataclass(frozen=True, slots=True)
124class SliceInfoWithOverlap(SliceInfo):
125 """SliceInfo aware of the fact that it overlaps with neighboring blocks."""
127 start_wo_overlap: int
128 stop_wo_overlap: int
131SampleId = Hashable
132"""ID of a sample, see `bioimageio.core.sample.Sample`"""
133MemberId = v0_5.TensorId
134"""ID of a `Sample` member, see `bioimageio.core.sample.Sample`"""
136BlocksizeParameter: TypeAlias = (
137 v0_5.ParameterizedSize_N
138 | Mapping[tuple[MemberId, v0_5.AxisId], v0_5.ParameterizedSize_N]
139)
140"""
141Parameter to determine a concrete size for paramtrized axis sizes defined by
142`bioimageio.spec.model.v0_5.ParameterizedSize`.
143"""
145_T = TypeVar("_T")
146# PerMember = Union[
147# Mapping[MemberId, _T], Mapping[str, _T], Mapping[Union[MemberId, str], _T]
148# ]
149PerMember = Mapping[MemberId, _T]
150BlockLoc = tuple[int, ...]
151BlockLocShape = tuple[int, ...]
154class DataDepSize(NamedTuple):
155 min: int
156 max: int | None
159def resolve_pad_mode(mode: PadMode):
160 constant_value = None
161 if isinstance(mode, str):
162 mode_name = mode
163 elif isinstance(mode, v0_5.ConstantPadding): 163 ↛ 164line 163 didn't jump to line 164 because the condition on line 163 was never true
164 mode_name = mode.mode
165 constant_value = mode.value
166 elif isinstance(
167 mode, (v0_5.EdgePadding, v0_5.ReflectPadding, v0_5.SymmetricPadding)
168 ):
169 mode_name = mode.mode
170 else:
171 assert_never(mode)
173 return mode_name, constant_value