Coverage for bioimageio/core/_magic_tensor_ops.py: 73%

154 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2024-11-19 09:02 +0000

1# this file was modified from the generated 

2# https://github.com/pydata/xarray/blob/cf3655968b8b12cc0ecd28fb324e63fb94d5e7e2/xarray/core/_typed_ops.py 

3# TODO: should we generate this ourselves? 

4# TODO: test these magic methods 

5import operator 

6from typing import Any, Callable 

7 

8from typing_extensions import Self 

9from xarray.core import nputils, ops 

10 

11 

12class MagicTensorOpsMixin: 

13 __slots__ = () 

14 _Compatible = Any 

15 

16 def _binary_op( 

17 self, 

18 other: _Compatible, 

19 f: Callable[[Any, Any], Any], 

20 reflexive: bool = False, 

21 ) -> Self: 

22 raise NotImplementedError 

23 

24 def __add__(self, other: _Compatible) -> Self: 

25 return self._binary_op(other, operator.add) 

26 

27 def __sub__(self, other: _Compatible) -> Self: 

28 return self._binary_op(other, operator.sub) 

29 

30 def __mul__(self, other: _Compatible) -> Self: 

31 return self._binary_op(other, operator.mul) 

32 

33 def __pow__(self, other: _Compatible) -> Self: 

34 return self._binary_op(other, operator.pow) 

35 

36 def __truediv__(self, other: _Compatible) -> Self: 

37 return self._binary_op(other, operator.truediv) 

38 

39 def __floordiv__(self, other: _Compatible) -> Self: 

40 return self._binary_op(other, operator.floordiv) 

41 

42 def __mod__(self, other: _Compatible) -> Self: 

43 return self._binary_op(other, operator.mod) 

44 

45 def __and__(self, other: _Compatible) -> Self: 

46 return self._binary_op(other, operator.and_) 

47 

48 def __xor__(self, other: _Compatible) -> Self: 

49 return self._binary_op(other, operator.xor) 

50 

51 def __or__(self, other: _Compatible) -> Self: 

52 return self._binary_op(other, operator.or_) 

53 

54 def __lshift__(self, other: _Compatible) -> Self: 

55 return self._binary_op(other, operator.lshift) 

56 

57 def __rshift__(self, other: _Compatible) -> Self: 

58 return self._binary_op(other, operator.rshift) 

59 

60 def __lt__(self, other: _Compatible) -> Self: 

61 return self._binary_op(other, operator.lt) 

62 

63 def __le__(self, other: _Compatible) -> Self: 

64 return self._binary_op(other, operator.le) 

65 

66 def __gt__(self, other: _Compatible) -> Self: 

67 return self._binary_op(other, operator.gt) 

68 

69 def __ge__(self, other: _Compatible) -> Self: 

70 return self._binary_op(other, operator.ge) 

71 

72 def __eq__(self, other: _Compatible) -> Self: # type: ignore[override] 

73 return self._binary_op( 

74 other, nputils.array_eq # pyright: ignore[reportUnknownArgumentType] 

75 ) 

76 

77 def __ne__(self, other: _Compatible) -> Self: # type: ignore[override] 

78 return self._binary_op( 

79 other, nputils.array_ne # pyright: ignore[reportUnknownArgumentType] 

80 ) 

81 

82 # When __eq__ is defined but __hash__ is not, then an object is unhashable, 

83 # and it should be declared as follows: 

84 __hash__: None # type:ignore[assignment] 

85 

86 def __radd__(self, other: _Compatible) -> Self: 

87 return self._binary_op(other, operator.add, reflexive=True) 

88 

89 def __rsub__(self, other: _Compatible) -> Self: 

90 return self._binary_op(other, operator.sub, reflexive=True) 

91 

92 def __rmul__(self, other: _Compatible) -> Self: 

93 return self._binary_op(other, operator.mul, reflexive=True) 

94 

95 def __rpow__(self, other: _Compatible) -> Self: 

96 return self._binary_op(other, operator.pow, reflexive=True) 

97 

98 def __rtruediv__(self, other: _Compatible) -> Self: 

99 return self._binary_op(other, operator.truediv, reflexive=True) 

100 

101 def __rfloordiv__(self, other: _Compatible) -> Self: 

102 return self._binary_op(other, operator.floordiv, reflexive=True) 

103 

104 def __rmod__(self, other: _Compatible) -> Self: 

105 return self._binary_op(other, operator.mod, reflexive=True) 

106 

107 def __rand__(self, other: _Compatible) -> Self: 

108 return self._binary_op(other, operator.and_, reflexive=True) 

109 

110 def __rxor__(self, other: _Compatible) -> Self: 

111 return self._binary_op(other, operator.xor, reflexive=True) 

112 

113 def __ror__(self, other: _Compatible) -> Self: 

114 return self._binary_op(other, operator.or_, reflexive=True) 

115 

116 def _inplace_binary_op( 

117 self, other: _Compatible, f: Callable[[Any, Any], Any] 

118 ) -> Self: 

119 raise NotImplementedError 

120 

121 def __iadd__(self, other: _Compatible) -> Self: 

122 return self._inplace_binary_op(other, operator.iadd) 

123 

124 def __isub__(self, other: _Compatible) -> Self: 

125 return self._inplace_binary_op(other, operator.isub) 

126 

127 def __imul__(self, other: _Compatible) -> Self: 

128 return self._inplace_binary_op(other, operator.imul) 

129 

130 def __ipow__(self, other: _Compatible) -> Self: 

131 return self._inplace_binary_op(other, operator.ipow) 

132 

133 def __itruediv__(self, other: _Compatible) -> Self: 

134 return self._inplace_binary_op(other, operator.itruediv) 

135 

136 def __ifloordiv__(self, other: _Compatible) -> Self: 

137 return self._inplace_binary_op(other, operator.ifloordiv) 

138 

139 def __imod__(self, other: _Compatible) -> Self: 

140 return self._inplace_binary_op(other, operator.imod) 

141 

142 def __iand__(self, other: _Compatible) -> Self: 

143 return self._inplace_binary_op(other, operator.iand) 

144 

145 def __ixor__(self, other: _Compatible) -> Self: 

146 return self._inplace_binary_op(other, operator.ixor) 

147 

148 def __ior__(self, other: _Compatible) -> Self: 

149 return self._inplace_binary_op(other, operator.ior) 

150 

151 def __ilshift__(self, other: _Compatible) -> Self: 

152 return self._inplace_binary_op(other, operator.ilshift) 

153 

154 def __irshift__(self, other: _Compatible) -> Self: 

155 return self._inplace_binary_op(other, operator.irshift) 

156 

157 def _unary_op(self, f: Callable[[Any], Any], *args: Any, **kwargs: Any) -> Self: 

158 raise NotImplementedError 

159 

160 def __neg__(self) -> Self: 

161 return self._unary_op(operator.neg) 

162 

163 def __pos__(self) -> Self: 

164 return self._unary_op(operator.pos) 

165 

166 def __abs__(self) -> Self: 

167 return self._unary_op(operator.abs) 

168 

169 def __invert__(self) -> Self: 

170 return self._unary_op(operator.invert) 

171 

172 def round(self, *args: Any, **kwargs: Any) -> Self: 

173 return self._unary_op( 

174 ops.round_, *args, **kwargs # pyright: ignore[reportUnknownArgumentType] 

175 ) 

176 

177 def argsort(self, *args: Any, **kwargs: Any) -> Self: 

178 return self._unary_op( 

179 ops.argsort, *args, **kwargs # pyright: ignore[reportUnknownArgumentType] 

180 ) 

181 

182 def conj(self, *args: Any, **kwargs: Any) -> Self: 

183 return self._unary_op( 

184 ops.conj, *args, **kwargs # pyright: ignore[reportUnknownArgumentType] 

185 ) 

186 

187 def conjugate(self, *args: Any, **kwargs: Any) -> Self: 

188 return self._unary_op( 

189 ops.conjugate, *args, **kwargs # pyright: ignore[reportUnknownArgumentType] 

190 ) 

191 

192 __add__.__doc__ = operator.add.__doc__ 

193 __sub__.__doc__ = operator.sub.__doc__ 

194 __mul__.__doc__ = operator.mul.__doc__ 

195 __pow__.__doc__ = operator.pow.__doc__ 

196 __truediv__.__doc__ = operator.truediv.__doc__ 

197 __floordiv__.__doc__ = operator.floordiv.__doc__ 

198 __mod__.__doc__ = operator.mod.__doc__ 

199 __and__.__doc__ = operator.and_.__doc__ 

200 __xor__.__doc__ = operator.xor.__doc__ 

201 __or__.__doc__ = operator.or_.__doc__ 

202 __lshift__.__doc__ = operator.lshift.__doc__ 

203 __rshift__.__doc__ = operator.rshift.__doc__ 

204 __lt__.__doc__ = operator.lt.__doc__ 

205 __le__.__doc__ = operator.le.__doc__ 

206 __gt__.__doc__ = operator.gt.__doc__ 

207 __ge__.__doc__ = operator.ge.__doc__ 

208 __eq__.__doc__ = nputils.array_eq.__doc__ 

209 __ne__.__doc__ = nputils.array_ne.__doc__ 

210 __radd__.__doc__ = operator.add.__doc__ 

211 __rsub__.__doc__ = operator.sub.__doc__ 

212 __rmul__.__doc__ = operator.mul.__doc__ 

213 __rpow__.__doc__ = operator.pow.__doc__ 

214 __rtruediv__.__doc__ = operator.truediv.__doc__ 

215 __rfloordiv__.__doc__ = operator.floordiv.__doc__ 

216 __rmod__.__doc__ = operator.mod.__doc__ 

217 __rand__.__doc__ = operator.and_.__doc__ 

218 __rxor__.__doc__ = operator.xor.__doc__ 

219 __ror__.__doc__ = operator.or_.__doc__ 

220 __iadd__.__doc__ = operator.iadd.__doc__ 

221 __isub__.__doc__ = operator.isub.__doc__ 

222 __imul__.__doc__ = operator.imul.__doc__ 

223 __ipow__.__doc__ = operator.ipow.__doc__ 

224 __itruediv__.__doc__ = operator.itruediv.__doc__ 

225 __ifloordiv__.__doc__ = operator.ifloordiv.__doc__ 

226 __imod__.__doc__ = operator.imod.__doc__ 

227 __iand__.__doc__ = operator.iand.__doc__ 

228 __ixor__.__doc__ = operator.ixor.__doc__ 

229 __ior__.__doc__ = operator.ior.__doc__ 

230 __ilshift__.__doc__ = operator.ilshift.__doc__ 

231 __irshift__.__doc__ = operator.irshift.__doc__ 

232 __neg__.__doc__ = operator.neg.__doc__ 

233 __pos__.__doc__ = operator.pos.__doc__ 

234 __abs__.__doc__ = operator.abs.__doc__ 

235 __invert__.__doc__ = operator.invert.__doc__