bioimageio.core.common

  1from __future__ import annotations
  2
  3from types import MappingProxyType
  4from typing import (
  5    Hashable,
  6    Literal,
  7    Mapping,
  8    NamedTuple,
  9    Tuple,
 10    TypeVar,
 11    Union,
 12)
 13
 14from typing_extensions import Self, assert_never
 15
 16from bioimageio.spec.model import v0_5
 17
 18from .axis import AxisId
 19
 20SupportedWeightsFormat = Literal[
 21    "keras_hdf5",
 22    "onnx",
 23    "pytorch_state_dict",
 24    "tensorflow_saved_model_bundle",
 25    "torchscript",
 26]
 27
 28
 29DTypeStr = Literal[
 30    "bool",
 31    "float32",
 32    "float64",
 33    "int8",
 34    "int16",
 35    "int32",
 36    "int64",
 37    "uint8",
 38    "uint16",
 39    "uint32",
 40    "uint64",
 41]
 42
 43
 44_LeftRight_T = TypeVar("_LeftRight_T", bound="_LeftRight")
 45_LeftRightLike = Union[int, Tuple[int, int], _LeftRight_T]
 46
 47
 48class _LeftRight(NamedTuple):
 49    left: int
 50    right: int
 51
 52    @classmethod
 53    def create(cls, like: _LeftRightLike[Self]) -> Self:
 54        if isinstance(like, cls):
 55            return like
 56        elif isinstance(like, tuple):
 57            return cls(*like)
 58        elif isinstance(like, int):
 59            return cls(like, like)
 60        else:
 61            assert_never(like)
 62
 63
 64_Where = Literal["left", "right", "left_and_right"]
 65
 66
 67class CropWidth(_LeftRight):
 68    pass
 69
 70
 71CropWidthLike = _LeftRightLike[CropWidth]
 72CropWhere = _Where
 73
 74
 75class Halo(_LeftRight):
 76    pass
 77
 78
 79HaloLike = _LeftRightLike[Halo]
 80
 81
 82class OverlapWidth(_LeftRight):
 83    pass
 84
 85
 86class PadWidth(_LeftRight):
 87    pass
 88
 89
 90PadWidthLike = _LeftRightLike[PadWidth]
 91PadMode = Literal["edge", "reflect", "symmetric"]
 92PadWhere = _Where
 93
 94
 95class SliceInfo(NamedTuple):
 96    start: int
 97    stop: int
 98
 99
100SampleId = Hashable
101"""ID of a sample, see `bioimageio.core.sample.Sample`"""
102MemberId = v0_5.TensorId
103"""ID of a `Sample` member, see `bioimageio.core.sample.Sample`"""
104
105BlocksizeParameter = Union[
106    v0_5.ParameterizedSize_N,
107    Mapping[Tuple[MemberId, AxisId], v0_5.ParameterizedSize_N],
108]
109"""
110Parameter to determine a concrete size for paramtrized axis sizes defined by
111`bioimageio.spec.model.v0_5.ParameterizedSize`.
112"""
113
114T = TypeVar("T")
115PerMember = Mapping[MemberId, T]
116
117BlockIndex = int
118TotalNumberOfBlocks = int
119
120
121K = TypeVar("K", bound=Hashable)
122V = TypeVar("V")
123
124Frozen = MappingProxyType
125# class Frozen(Mapping[K, V]):  # adapted from xarray.core.utils.Frozen
126#     """Wrapper around an object implementing the mapping interface to make it
127#     immutable."""
128
129#     __slots__ = ("mapping",)
130
131#     def __init__(self, mapping: Mapping[K, V]):
132#         super().__init__()
133#         self.mapping = deepcopy(
134#             mapping
135#         )  # added deepcopy (compared to xarray.core.utils.Frozen)
136
137#     def __getitem__(self, key: K) -> V:
138#         return self.mapping[key]
139
140#     def __iter__(self) -> Iterator[K]:
141#         return iter(self.mapping)
142
143#     def __len__(self) -> int:
144#         return len(self.mapping)
145
146#     def __contains__(self, key: object) -> bool:
147#         return key in self.mapping
148
149#     def __repr__(self) -> str:
150#         return f"{type(self).__name__}({self.mapping!r})"
SupportedWeightsFormat = typing.Literal['keras_hdf5', 'onnx', 'pytorch_state_dict', 'tensorflow_saved_model_bundle', 'torchscript']
DTypeStr = typing.Literal['bool', 'float32', 'float64', 'int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64']
class CropWidth(typing.NamedTuple):
68class CropWidth(_LeftRight):
69    pass

_LeftRight(left, right)

CropWidth(left: int, right: int)

Create new instance of _LeftRight(left, right)

Inherited Members
_LeftRight
left
right
create
CropWidthLike = typing.Union[int, typing.Tuple[int, int], CropWidth]
CropWhere = typing.Literal['left', 'right', 'left_and_right']
class Halo(typing.NamedTuple):
76class Halo(_LeftRight):
77    pass

_LeftRight(left, right)

Halo(left: int, right: int)

Create new instance of _LeftRight(left, right)

Inherited Members
_LeftRight
left
right
create
HaloLike = typing.Union[int, typing.Tuple[int, int], Halo]
class OverlapWidth(typing.NamedTuple):
83class OverlapWidth(_LeftRight):
84    pass

_LeftRight(left, right)

OverlapWidth(left: int, right: int)

Create new instance of _LeftRight(left, right)

Inherited Members
_LeftRight
left
right
create
class PadWidth(typing.NamedTuple):
87class PadWidth(_LeftRight):
88    pass

_LeftRight(left, right)

PadWidth(left: int, right: int)

Create new instance of _LeftRight(left, right)

Inherited Members
_LeftRight
left
right
create
PadWidthLike = typing.Union[int, typing.Tuple[int, int], PadWidth]
PadMode = typing.Literal['edge', 'reflect', 'symmetric']
PadWhere = typing.Literal['left', 'right', 'left_and_right']
class SliceInfo(typing.NamedTuple):
96class SliceInfo(NamedTuple):
97    start: int
98    stop: int

SliceInfo(start, stop)

SliceInfo(start: int, stop: int)

Create new instance of SliceInfo(start, stop)

start: int

Alias for field number 0

stop: int

Alias for field number 1

SampleId = typing.Hashable

ID of a sample, see bioimageio.core.sample.Sample

ID of a Sample member, see bioimageio.core.sample.Sample

BlocksizeParameter = typing.Union[int, typing.Mapping[typing.Tuple[bioimageio.spec.model.v0_5.TensorId, bioimageio.spec.model.v0_5.AxisId], int]]

Parameter to determine a concrete size for paramtrized axis sizes defined by bioimageio.spec.model.v0_5.ParameterizedSize.

PerMember = typing.Mapping[bioimageio.spec.model.v0_5.TensorId, ~T]
BlockIndex = <class 'int'>
TotalNumberOfBlocks = <class 'int'>
Frozen = <class 'mappingproxy'>