Skip to content

Commit

Permalink
Further modifies the _add_ method.
Browse files Browse the repository at this point in the history
  • Loading branch information
PCJY committed Dec 23, 2024
1 parent a891ff9 commit f575e2c
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions ndcube/ndcube.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,10 +1044,21 @@ def __add__(self, value):
else:
raise TypeError("Cannot add unitless NDData to a unitful NDCube.")

Check warning on line 1045 in ndcube/ndcube.py

View check run for this annotation

Codecov / codecov/patch

ndcube/ndcube.py#L1045

Added line #L1045 was not covered by tests

# combine mask
self_ma = np.ma.MaskedArray(self.data, mask=self.mask)
value_ma = np.ma.MaskedArray(value_data, mask=value.mask)

Check warning on line 1049 in ndcube/ndcube.py

View check run for this annotation

Codecov / codecov/patch

ndcube/ndcube.py#L1048-L1049

Added lines #L1048 - L1049 were not covered by tests

# addition
result_ma = self_ma + value_ma

Check warning on line 1052 in ndcube/ndcube.py

View check run for this annotation

Codecov / codecov/patch

ndcube/ndcube.py#L1052

Added line #L1052 was not covered by tests

# extract new mask and new data
kwargs["mask"] = result_ma.mask
kwargs["data"] = result_ma.data

Check warning on line 1056 in ndcube/ndcube.py

View check run for this annotation

Codecov / codecov/patch

ndcube/ndcube.py#L1055-L1056

Added lines #L1055 - L1056 were not covered by tests

# combine the uncertainty
if self.uncertainty is not None and value.uncertainty is not None:
new_uncertainty = self.uncertainty.propagate(

Check warning on line 1060 in ndcube/ndcube.py

View check run for this annotation

Codecov / codecov/patch

ndcube/ndcube.py#L1059-L1060

Added lines #L1059 - L1060 were not covered by tests
np.add, value.uncertainty, result_data = value.data, correlation=0
np.add, value.uncertainty, result_data = kwargs["data"], correlation=0
)
kwargs["uncertainty"] = new_uncertainty
elif self.uncertainty is not None:
Expand All @@ -1058,37 +1069,25 @@ def __add__(self, value):
else:
new_uncertainty = None

Check warning on line 1070 in ndcube/ndcube.py

View check run for this annotation

Codecov / codecov/patch

ndcube/ndcube.py#L1070

Added line #L1070 was not covered by tests

# combine mask
self_ma = np.ma.MaskedArray(self.data, mask=self.mask)
value_ma = np.ma.MaskedArray(value_data, mask=value.mask)

# addition
result_ma = self_ma + value_ma

# extract new mask and new data
kwargs["mask"] = result_ma.mask
kwargs["data"] = result_ma.data

# return the new NDCube instance
return self._new_instance(**kwargs)

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)
kwargs["data"] = self.data + value.to_value(cube_unit)
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
return self._new_instance(data=new_data)
kwargs["data"] = self.data + value

Check warning on line 1087 in ndcube/ndcube.py

View check run for this annotation

Codecov / codecov/patch

ndcube/ndcube.py#L1087

Added line #L1087 was not covered by tests

# return the new NDCube instance
return self._new_instance(**kwargs)

def __radd__(self, value):
return self.__add__(value)
Expand Down

0 comments on commit f575e2c

Please sign in to comment.