Coverage for bioimageio/core/common.py: 100%
46 statements
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-19 09:02 +0000
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-19 09:02 +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
18DTypeStr = Literal[
19 "bool",
20 "float32",
21 "float64",
22 "int8",
23 "int16",
24 "int32",
25 "int64",
26 "uint8",
27 "uint16",
28 "uint32",
29 "uint64",
30]
33_LeftRight_T = TypeVar("_LeftRight_T", bound="_LeftRight")
34_LeftRightLike = Union[int, Tuple[int, int], _LeftRight_T]
37class _LeftRight(NamedTuple):
38 left: int
39 right: int
41 @classmethod
42 def create(cls, like: _LeftRightLike[Self]) -> Self:
43 if isinstance(like, cls):
44 return like
45 elif isinstance(like, tuple):
46 return cls(*like)
47 elif isinstance(like, int):
48 return cls(like, like)
49 else:
50 assert_never(like)
53_Where = Literal["left", "right", "left_and_right"]
56class CropWidth(_LeftRight):
57 pass
60CropWidthLike = _LeftRightLike[CropWidth]
61CropWhere = _Where
64class Halo(_LeftRight):
65 pass
68HaloLike = _LeftRightLike[Halo]
71class OverlapWidth(_LeftRight):
72 pass
75class PadWidth(_LeftRight):
76 pass
79PadWidthLike = _LeftRightLike[PadWidth]
80PadMode = Literal["edge", "reflect", "symmetric"]
81PadWhere = _Where
84class SliceInfo(NamedTuple):
85 start: int
86 stop: int
89SampleId = Hashable
90MemberId = v0_5.TensorId
91T = TypeVar("T")
92PerMember = Mapping[MemberId, T]
94BlockIndex = int
95TotalNumberOfBlocks = int
98K = TypeVar("K", bound=Hashable)
99V = TypeVar("V")
101Frozen = MappingProxyType
102# class Frozen(Mapping[K, V]): # adapted from xarray.core.utils.Frozen
103# """Wrapper around an object implementing the mapping interface to make it
104# immutable."""
106# __slots__ = ("mapping",)
108# def __init__(self, mapping: Mapping[K, V]):
109# super().__init__()
110# self.mapping = deepcopy(
111# mapping
112# ) # added deepcopy (compared to xarray.core.utils.Frozen)
114# def __getitem__(self, key: K) -> V:
115# return self.mapping[key]
117# def __iter__(self) -> Iterator[K]:
118# return iter(self.mapping)
120# def __len__(self) -> int:
121# return len(self.mapping)
123# def __contains__(self, key: object) -> bool:
124# return key in self.mapping
126# def __repr__(self) -> str:
127# return f"{type(self).__name__}({self.mapping!r})"