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

46 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2024-11-19 09:02 +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 

18DTypeStr = Literal[ 

19 "bool", 

20 "float32", 

21 "float64", 

22 "int8", 

23 "int16", 

24 "int32", 

25 "int64", 

26 "uint8", 

27 "uint16", 

28 "uint32", 

29 "uint64", 

30] 

31 

32 

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

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

35 

36 

37class _LeftRight(NamedTuple): 

38 left: int 

39 right: int 

40 

41 @classmethod 

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

43 if isinstance(like, cls): 

44 return like 

45 elif isinstance(like, tuple): 

46 return cls(*like) 

47 elif isinstance(like, int): 

48 return cls(like, like) 

49 else: 

50 assert_never(like) 

51 

52 

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

54 

55 

56class CropWidth(_LeftRight): 

57 pass 

58 

59 

60CropWidthLike = _LeftRightLike[CropWidth] 

61CropWhere = _Where 

62 

63 

64class Halo(_LeftRight): 

65 pass 

66 

67 

68HaloLike = _LeftRightLike[Halo] 

69 

70 

71class OverlapWidth(_LeftRight): 

72 pass 

73 

74 

75class PadWidth(_LeftRight): 

76 pass 

77 

78 

79PadWidthLike = _LeftRightLike[PadWidth] 

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

81PadWhere = _Where 

82 

83 

84class SliceInfo(NamedTuple): 

85 start: int 

86 stop: int 

87 

88 

89SampleId = Hashable 

90MemberId = v0_5.TensorId 

91T = TypeVar("T") 

92PerMember = Mapping[MemberId, T] 

93 

94BlockIndex = int 

95TotalNumberOfBlocks = int 

96 

97 

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

99V = TypeVar("V") 

100 

101Frozen = MappingProxyType 

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

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

104# immutable.""" 

105 

106# __slots__ = ("mapping",) 

107 

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

109# super().__init__() 

110# self.mapping = deepcopy( 

111# mapping 

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

113 

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

115# return self.mapping[key] 

116 

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

118# return iter(self.mapping) 

119 

120# def __len__(self) -> int: 

121# return len(self.mapping) 

122 

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

124# return key in self.mapping 

125 

126# def __repr__(self) -> str: 

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