-
-
Notifications
You must be signed in to change notification settings - Fork 50
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
Added new features to the ndcube._add_ method #794
base: main
Are you sure you want to change the base?
Changes from 5 commits
9c38077
1d5d2ab
aaa9ef0
ed4f61e
ea43a1d
a891ff9
f575e2c
8951635
58e4363
bcf4fb9
c4d639a
bd317e3
e0375ec
0158737
9074f45
9e267d3
d8c2db9
5f422f5
3369223
f17da78
344b6f7
7ff78aa
5852daa
5dcb8ff
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 |
---|---|---|
|
@@ -1035,37 +1035,43 @@ | |
return self._new_instance(data=-self.data) | ||
|
||
def __add__(self, value): | ||
kwargs = {} | ||
if isinstance(value, NDData) and value.wcs is None: | ||
if self.unit is not None and value.unit is not None: | ||
value_data = value.data * value.unit.to(self.unit) | ||
value_data = (value.data * value.unit).to_value(self.unit) | ||
elif self.unit is None: | ||
value_data = value.data | ||
else: | ||
raise TypeError("Cannot add unitless NDData to a unitful NDCube.") | ||
|
||
# addition | ||
new_data = self.data + value_data | ||
# combine the uncertainty | ||
new_uncertainty = None | ||
if self.uncertainty is not None and value.uncertainty is not None: | ||
new_uncertainty = self.uncertainty.propagate( | ||
np.add, value.uncertainty, correlation=0 | ||
np.add, value.uncertainty, result_data = value.data, correlation=0 | ||
) | ||
kwargs["uncertainty"] = new_uncertainty | ||
elif self.uncertainty is not None: | ||
new_uncertainty = self.uncertainty | ||
kwargs["uncertainty"] = new_uncertainty | ||
elif value.uncertainty is not None: | ||
new_uncertainty = value.uncertainty | ||
else: | ||
new_uncertainty = None | ||
|
||
# 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 | ||
new_mask = result_ma.mask | ||
|
||
# extract new mask and new data | ||
kwargs["mask"] = result_ma.mask | ||
kwargs["data"] = result_ma.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. As mentioned in above comment, I think it makes sense to do this before the uncertainty propagation so you can use the |
||
|
||
# return the new NDCube instance | ||
return self._new_instance( | ||
data=new_data, uncertainty=new_uncertainty, mask=new_mask | ||
) | ||
return self._new_instance(**kwargs) | ||
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. Move this line to the end of the method and use the kwargs["data"] = self.data + value.to_value(cube_unit) |
||
|
||
if hasattr(value, 'unit'): | ||
if isinstance(value, u.Quantity): | ||
# NOTE: if the cube does not have units, we cannot | ||
|
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.
The
result_data
needs to be the result of the operation. So, assuming you moved the addition of the datas using the masked array to before the uncertainty propagation, you could do: