-
-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds the functionality of Ufunc to NDCube #666
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -883,27 +883,64 @@ | |
new_cube._global_coords = deepcopy(self.global_coords) | ||
return new_cube | ||
|
||
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): | ||
if method == '__call__': | ||
if ufunc == np.add: | ||
if isinstance(inputs[0], NDCube) and kwargs.get("dunder"): | ||
new_data = inputs[0].data + inputs[1] | ||
return new_data | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean that This looks different to the implementation for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
elif isinstance(inputs[1] , NDCube) and kwargs.get("dunder"): | ||
return (self.__radd__(inputs[0])) | ||
elif isinstance(inputs[0], NDCube) and not kwargs.get("dunder"): | ||
return (self.__add__(inputs[1])) | ||
else: | ||
return (self.__radd__(inputs[0])) | ||
elif ufunc == np.subtract: | ||
if not kwargs.get("dunder"): | ||
if isinstance(inputs[0], NDCube): | ||
return (self.__sub__(inputs[1])) | ||
else: | ||
return (self.__rsub__(inputs[0])) | ||
return (self.__rsub__(inputs[0])) | ||
elif ufunc == np.multiply: | ||
if isinstance(inputs[0], NDCube) and kwargs.get("dunder"): | ||
new_data = inputs[0].data * inputs[1] | ||
return new_data | ||
elif isinstance(inputs[1], NDCube) and kwargs.get("dunder"): | ||
return (self.__rmul__(inputs[0])) | ||
elif isinstance(inputs[0], NDCube) and not kwargs.get("dunder"): | ||
return(self.__mul__(inputs[1])) | ||
else: | ||
return (self.__rmul__(inputs[0])) | ||
elif ufunc == np.isinf: | ||
if isinstance(inputs[0], NDCube): | ||
return (np.isinf(inputs[0].data)) | ||
else: | ||
return NotImplemented | ||
|
||
def __neg__(self): | ||
return self._new_instance_from_op(-self.data, deepcopy(self.unit), | ||
deepcopy(self.uncertainty)) | ||
|
||
def __add__(self, value): | ||
if getattr(value, '__array_ufunc__', None) is None: | ||
return NotImplemented | ||
if hasattr(value, 'unit'): | ||
if isinstance(value, u.Quantity): | ||
# NOTE: if the cube does not have units, we cannot | ||
# perform arithmetic between a unitful quantity. | ||
# This forces a conversion to a dimensionless quantity | ||
# so that an error is thrown if value is not dimensionless | ||
cube_unit = u.Unit('') if self.unit is None else self.unit | ||
new_data = self.data + value.to_value(cube_unit) | ||
new_data = self.__array_ufunc__(np.add, '__call__', self, value.to_value(cube_unit), dunder =True) | ||
else: | ||
# NOTE: This explicitly excludes other NDCube objects and NDData objects | ||
# which could carry a different WCS than the NDCube | ||
return NotImplemented | ||
elif self.unit not in (None, u.Unit("")): | ||
raise TypeError("Cannot add a unitless object to an NDCube with a unit.") | ||
else: | ||
new_data = self.data + value | ||
new_data = self.__array_ufunc__(np.add, '__call__', self, value, dunder =True) | ||
return self._new_instance_from_op(new_data, deepcopy(self.unit), deepcopy(self.uncertainty)) | ||
|
||
def __radd__(self, value): | ||
|
@@ -916,6 +953,8 @@ | |
return self.__neg__().__add__(value) | ||
|
||
def __mul__(self, value): | ||
if getattr(value, '__array_ufunc__', None) is None: | ||
return NotImplemented | ||
if hasattr(value, 'unit'): | ||
if isinstance(value, u.Quantity): | ||
# NOTE: if the cube does not have units, set the unit | ||
|
@@ -929,7 +968,7 @@ | |
return NotImplemented | ||
else: | ||
new_unit = self.unit | ||
new_data = self.data * value | ||
new_data = self.__array_ufunc__(np.multiply, '__call__', self, value, dunder =True) | ||
new_uncertainty = (type(self.uncertainty)(self.uncertainty.array * value) | ||
if self.uncertainty is not None else None) | ||
new_cube = self._new_instance_from_op(new_data, new_unit, new_uncertainty) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
"dunder"
a standard kwarg or custom for this implementation? And what does it mean?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have used the
dunder
(not a standard kwarg) kwarg here to distinguish between the operators and numpy ufuncs to properly handle the operator overloading.