MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / __array_ufunc__

Method __array_ufunc__

monai/data/meta_tensor.py:324–343  ·  view source on GitHub ↗

For numpy interoperability, so that we can compute ``MetaTensor([1.0]) >= np.asarray([1.0])``. This is for pytorch > 1.8.

(self, ufunc, method, *inputs, **kwargs)

Source from the content-addressed store, hash-verified

322 return func(*_args, **_kwargs)
323
324 def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
325 """
326 For numpy interoperability, so that we can compute ``MetaTensor([1.0]) >= np.asarray([1.0])``.
327 This is for pytorch > 1.8.
328 """
329 try:
330 if not type(ufunc).__module__.startswith("numpy"):
331 return NotImplemented
332 except AttributeError:
333 return NotImplemented
334 if method != "__call__":
335 return NotImplemented
336 _inputs = map(MetaTensor._convert, inputs)
337 _kwargs = {k: MetaTensor._convert(v) for k, v in kwargs.items()}
338 if "out" in _kwargs:
339 return NotImplemented # not supported
340 try:
341 return getattr(ufunc, method)(*_inputs, **_kwargs)
342 except AttributeError:
343 return NotImplemented
344
345 @staticmethod
346 def get_default_affine(dtype=torch.float64) -> torch.Tensor:

Callers

nothing calls this directly

Calls 1

_convertMethod · 0.80

Tested by

no test coverage detected