Coverage for bioimageio/core/common.py: 100%

52 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-01 09:51 +0000

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})"