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

54 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-02-13 09:46 +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 

28QuantileMethod = Literal[ 

29 "inverted_cdf", 

30 # "averaged_inverted_cdf", 

31 # "closest_observation", 

32 # "interpolated_inverted_cdf", 

33 # "hazen", 

34 # "weibull", 

35 "linear", 

36 # "median_unbiased", 

37 # "normal_unbiased", 

38] 

39"""Methods to use when the desired quantile lies between two data points. 

40See https://numpy.org/devdocs/reference/generated/numpy.quantile.html#numpy-quantile for details. 

41 

42Note: 

43 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) 

44 

45!!! warning 

46 Limited choices to map more easily to bioimageio.spec descriptions. 

47 Current implementations: 

48 - [bioimageio.spec.model.v0_5.ClipKwargs][] implies "inverted_cdf" for sample quantiles and "linear" (numpy's default) for dataset quantiles. 

49 - [bioimageio.spec.model.v0_5.ScaleRangeKwargs][] implies "linear" (numpy's default) 

50 

51""" 

52 

53DTypeStr = Literal[ 

54 "bool", 

55 "float32", 

56 "float64", 

57 "int8", 

58 "int16", 

59 "int32", 

60 "int64", 

61 "uint8", 

62 "uint16", 

63 "uint32", 

64 "uint64", 

65] 

66 

67 

68_LeftRight_T = TypeVar("_LeftRight_T", bound="_LeftRight") 

69_LeftRightLike = Union[int, Tuple[int, int], _LeftRight_T] 

70 

71 

72class _LeftRight(NamedTuple): 

73 left: int 

74 right: int 

75 

76 @classmethod 

77 def create(cls, like: _LeftRightLike[Self]) -> Self: 

78 if isinstance(like, cls): 

79 return like 

80 elif isinstance(like, tuple): 

81 return cls(*like) 

82 elif isinstance(like, int): 

83 return cls(like, like) 

84 else: 

85 assert_never(like) 

86 

87 

88_Where = Literal["left", "right", "left_and_right"] 

89 

90 

91class CropWidth(_LeftRight): 

92 pass 

93 

94 

95CropWidthLike = _LeftRightLike[CropWidth] 

96CropWhere = _Where 

97 

98 

99class Halo(_LeftRight): 

100 pass 

101 

102 

103HaloLike = _LeftRightLike[Halo] 

104 

105 

106class OverlapWidth(_LeftRight): 

107 pass 

108 

109 

110class PadWidth(_LeftRight): 

111 pass 

112 

113 

114PadWidthLike = _LeftRightLike[PadWidth] 

115PadMode = Literal["edge", "reflect", "symmetric"] 

116PadWhere = _Where 

117 

118 

119class SliceInfo(NamedTuple): 

120 start: int 

121 stop: int 

122 

123 

124SampleId = Hashable 

125"""ID of a sample, see `bioimageio.core.sample.Sample`""" 

126MemberId = v0_5.TensorId 

127"""ID of a `Sample` member, see `bioimageio.core.sample.Sample`""" 

128 

129BlocksizeParameter = Union[ 

130 v0_5.ParameterizedSize_N, 

131 Mapping[Tuple[MemberId, AxisId], v0_5.ParameterizedSize_N], 

132] 

133""" 

134Parameter to determine a concrete size for paramtrized axis sizes defined by 

135`bioimageio.spec.model.v0_5.ParameterizedSize`. 

136""" 

137 

138T = TypeVar("T") 

139PerMember = Mapping[MemberId, T] 

140 

141BlockIndex = int 

142TotalNumberOfBlocks = int 

143 

144 

145K = TypeVar("K", bound=Hashable) 

146V = TypeVar("V") 

147 

148Frozen = MappingProxyType 

149# class Frozen(Mapping[K, V]): # adapted from xarray.core.utils.Frozen 

150# """Wrapper around an object implementing the mapping interface to make it 

151# immutable.""" 

152 

153# __slots__ = ("mapping",) 

154 

155# def __init__(self, mapping: Mapping[K, V]): 

156# super().__init__() 

157# self.mapping = deepcopy( 

158# mapping 

159# ) # added deepcopy (compared to xarray.core.utils.Frozen) 

160 

161# def __getitem__(self, key: K) -> V: 

162# return self.mapping[key] 

163 

164# def __iter__(self) -> Iterator[K]: 

165# return iter(self.mapping) 

166 

167# def __len__(self) -> int: 

168# return len(self.mapping) 

169 

170# def __contains__(self, key: object) -> bool: 

171# return key in self.mapping 

172 

173# def __repr__(self) -> str: 

174# return f"{type(self).__name__}({self.mapping!r})"