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

54 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-30 13:23 +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 "keras_v3", 

23 "onnx", 

24 "pytorch_state_dict", 

25 "tensorflow_saved_model_bundle", 

26 "torchscript", 

27] 

28 

29QuantileMethod = Literal[ 

30 "inverted_cdf", 

31 # "averaged_inverted_cdf", 

32 # "closest_observation", 

33 # "interpolated_inverted_cdf", 

34 # "hazen", 

35 # "weibull", 

36 "linear", 

37 # "median_unbiased", 

38 # "normal_unbiased", 

39] 

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

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

42 

43Note: 

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

45 

46!!! warning 

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

48 Current implementations: 

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

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

51 

52""" 

53 

54DTypeStr = Literal[ 

55 "bool", 

56 "float32", 

57 "float64", 

58 "int8", 

59 "int16", 

60 "int32", 

61 "int64", 

62 "uint8", 

63 "uint16", 

64 "uint32", 

65 "uint64", 

66] 

67 

68 

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

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

71 

72 

73class _LeftRight(NamedTuple): 

74 left: int 

75 right: int 

76 

77 @classmethod 

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

79 if isinstance(like, cls): 

80 return like 

81 elif isinstance(like, tuple): 

82 return cls(*like) 

83 elif isinstance(like, int): 

84 return cls(like, like) 

85 else: 

86 assert_never(like) 

87 

88 

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

90 

91 

92class CropWidth(_LeftRight): 

93 pass 

94 

95 

96CropWidthLike = _LeftRightLike[CropWidth] 

97CropWhere = _Where 

98 

99 

100class Halo(_LeftRight): 

101 pass 

102 

103 

104HaloLike = _LeftRightLike[Halo] 

105 

106 

107class OverlapWidth(_LeftRight): 

108 pass 

109 

110 

111class PadWidth(_LeftRight): 

112 pass 

113 

114 

115PadWidthLike = _LeftRightLike[PadWidth] 

116PadMode = Literal["constant", "edge", "reflect", "symmetric"] 

117PadWhere = _Where 

118 

119 

120class SliceInfo(NamedTuple): 

121 start: int 

122 stop: int 

123 

124 

125SampleId = Hashable 

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

127MemberId = v0_5.TensorId 

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

129 

130BlocksizeParameter = Union[ 

131 v0_5.ParameterizedSize_N, 

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

133] 

134""" 

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

136`bioimageio.spec.model.v0_5.ParameterizedSize`. 

137""" 

138 

139T = TypeVar("T") 

140PerMember = Mapping[MemberId, T] 

141 

142BlockIndex = int 

143TotalNumberOfBlocks = int 

144 

145 

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

147V = TypeVar("V") 

148 

149Frozen = MappingProxyType 

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

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

152# immutable.""" 

153 

154# __slots__ = ("mapping",) 

155 

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

157# super().__init__() 

158# self.mapping = deepcopy( 

159# mapping 

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

161 

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

163# return self.mapping[key] 

164 

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

166# return iter(self.mapping) 

167 

168# def __len__(self) -> int: 

169# return len(self.mapping) 

170 

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

172# return key in self.mapping 

173 

174# def __repr__(self) -> str: 

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