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

50 statements  

« prev     ^ index     » next       coverage.py v7.14.2, created at 2026-06-22 16:54 +0000

1from __future__ import annotations 

2 

3from typing import ( 

4 Hashable, 

5 Literal, 

6 Mapping, 

7 NamedTuple, 

8 Tuple, 

9 TypeVar, 

10 Union, 

11) 

12 

13from typing_extensions import Self, TypeAlias, assert_never 

14 

15from bioimageio.spec.model import v0_5 

16 

17SupportedWeightsFormat = Literal[ 

18 "keras_hdf5", 

19 "keras_v3", 

20 "onnx", 

21 "pytorch_state_dict", 

22 "tensorflow_saved_model_bundle", 

23 "torchscript", 

24] 

25 

26QuantileMethod = Literal[ 

27 "inverted_cdf", 

28 # "averaged_inverted_cdf", 

29 # "closest_observation", 

30 # "interpolated_inverted_cdf", 

31 # "hazen", 

32 # "weibull", 

33 "linear", 

34 # "median_unbiased", 

35 # "normal_unbiased", 

36] 

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

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

39 

40Note: 

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

42 

43!!! warning 

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

45 Current implementations: 

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

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

48 

49""" 

50 

51DTypeStr = Literal[ 

52 "bool", 

53 "float32", 

54 "float64", 

55 "int8", 

56 "int16", 

57 "int32", 

58 "int64", 

59 "uint8", 

60 "uint16", 

61 "uint32", 

62 "uint64", 

63] 

64 

65 

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

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

68 

69 

70class _LeftRight(NamedTuple): 

71 left: int 

72 right: int 

73 

74 @classmethod 

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

76 if isinstance(like, cls): 

77 return like 

78 elif isinstance(like, tuple): 

79 return cls(*like) 

80 elif isinstance(like, int): 

81 return cls(like, like) 

82 else: 

83 assert_never(like) 

84 

85 

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

87 

88 

89class CropWidth(_LeftRight): 

90 pass 

91 

92 

93CropWidthLike = _LeftRightLike[CropWidth] 

94CropWhere = _Where 

95 

96 

97class Halo(_LeftRight): 

98 pass 

99 

100 

101HaloLike = _LeftRightLike[Halo] 

102 

103 

104class OverlapWidth(_LeftRight): 

105 pass 

106 

107 

108class PadWidth(_LeftRight): 

109 pass 

110 

111 

112PadWidthLike: TypeAlias = _LeftRightLike[PadWidth] 

113Padding: TypeAlias = v0_5.Padding 

114PadMode: TypeAlias = Union[Literal["constant", "edge", "reflect", "symmetric"], Padding] 

115PadWhere: TypeAlias = _Where 

116 

117 

118class SliceInfo(NamedTuple): 

119 start: int 

120 stop: int 

121 

122 

123SampleId = Hashable 

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

125MemberId = v0_5.TensorId 

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

127 

128BlocksizeParameter: TypeAlias = Union[ 

129 v0_5.ParameterizedSize_N, 

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

131] 

132""" 

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

134`bioimageio.spec.model.v0_5.ParameterizedSize`. 

135""" 

136 

137_T = TypeVar("_T") 

138PerMember = Mapping[MemberId, _T] 

139 

140BlockIndex = int 

141TotalNumberOfBlocks = int