-
-
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
Open
PCJY
wants to merge
24
commits into
sunpy:main
Choose a base branch
from
PCJY:nddataArithmetic
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
9c38077
Added new features to the ndcube._add_ method
PCJY 1d5d2ab
Merge branch 'main' of https://github.com/sunpy/ndcube into nddataAri…
PCJY aaa9ef0
Update ndcube/ndcube.py
PCJY ed4f61e
Update ndcube/ndcube.py
PCJY ea43a1d
Modified the _add_ method further.
PCJY a891ff9
Merge branch 'nddataArithmetic' of https://github.com/PCJY/ndcube int…
PCJY f575e2c
Further modifies the _add_ method.
PCJY 8951635
Added a changelog file for this new feature.
PCJY 58e4363
Merge branch 'main' of https://github.com/sunpy/ndcube into nddataAri…
PCJY bcf4fb9
Added a new method test_cube_add_uncertainty_and_mask to test_ndcube.py.
PCJY c4d639a
Modified the test_cube_add_uncertainty_and_mask method in test_ndcube.py
PCJY bd317e3
Modified the test_cube_add_uncertainty_and_mask further.
PCJY e0375ec
Fixed how the masks are combined.
PCJY 0158737
Set masked uncertainty entries to 0.
PCJY 9074f45
Moved uncertainty combination out of the mask-combining If Statements.
PCJY 9e267d3
Merge branch 'main' of https://github.com/sunpy/ndcube into nddataAri…
PCJY d8c2db9
Merge branch 'main' into nddataArithmetic
PCJY 5f422f5
Removed mask-dealing in the add method.
PCJY 3369223
Merge branch 'nddataArithmetic' of https://github.com/PCJY/ndcube int…
PCJY f17da78
Removed mask-dealing in the Add method.
PCJY 344b6f7
use a conditional statement to still check whether there is a mask.
PCJY 7ff78aa
Changed mask to False and removed mask-checking in test_cube_add_unce…
PCJY 5852daa
Added placeholders for using the new parameters and modified the no-m…
PCJY 5dcb8ff
Set default of operation_ignores_mask to be True.
PCJY File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
|
||
import astropy.nddata | ||
import astropy.units as u | ||
from astropy.nddata import NDData | ||
from astropy.units import UnitsError | ||
from astropy.wcs.utils import _split_matrix | ||
|
||
|
@@ -1034,6 +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_value(self.unit) | ||
elif self.unit is None: | ||
value_data = value.data | ||
else: | ||
raise TypeError("Cannot add unitless NDData to a unitful NDCube.") | ||
|
||
# combine the uncertainty | ||
if self.uncertainty is not None and value.uncertainty is not None: | ||
new_uncertainty = self.uncertainty.propagate( | ||
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 | ||
|
||
# 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) | ||
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 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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: