-
-
Notifications
You must be signed in to change notification settings - Fork 53
Support Subtract and Division of NDCube by NDData #880
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
Open
DanRyanIrish
wants to merge
37
commits into
sunpy:main
Choose a base branch
from
DanRyanIrish:nddataArithmetic2
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 all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
0838785
Enable subtraction of NDCube and NDData.
DanRyanIrish af3220f
Enable division of NDCube by NDData.
DanRyanIrish 2627534
Add 880 changelog.
DanRyanIrish 9144d50
Merge branch 'main' of https://github.com/sunpy/ndcube into nddataAri…
DanRyanIrish 30e4330
Add NDCube-NDData arithmetic tests for subtraction and division that …
DanRyanIrish f73e649
Remove in-place modification in NDCube arithmetic subtraction.
DanRyanIrish 5671bde
Remove duplicate test.
DanRyanIrish 83c9f3d
Fix bug whereby adding unitful NDCube and NDData did not output resul…
DanRyanIrish 5acbd35
Add more tests of dask-backed arithmetic operations.
DanRyanIrish 4baa3b0
Remove duplicate test.
DanRyanIrish 1bcd97f
First commit of docs explaining arithmetic operations.
DanRyanIrish f3f07b5
Next commit on docs explaining arithmetic operations.
DanRyanIrish f253650
Rename arithmetic operations docs file and next commit in arithmetic …
DanRyanIrish e09b892
Fix codestyle.
DanRyanIrish 4ee0a50
Merge branch 'main' of https://github.com/sunpy/ndcube into nddataAri…
DanRyanIrish 8ca5c5b
Fix bugs in arithmetic docs.
DanRyanIrish 8eff1b5
Next commit on arithmetic docs.
DanRyanIrish 237c90a
First complete draft of arithmetic docs.
DanRyanIrish 9f9805f
Updates to arithmetic docs.
DanRyanIrish a58a231
Updates to arithmetic docs.
DanRyanIrish 212d013
Fix bug raising error when two coordinate aware objects are combined …
DanRyanIrish 23a589b
Updates arithmetic docs.
DanRyanIrish 12faeac
Update arithmetic docs.
DanRyanIrish 7c949fb
Merge branch 'main' of https://github.com/sunpy/ndcube into nddataAri…
DanRyanIrish ffd916c
Merge branch 'to_nddata' into nddataArithmetic2
DanRyanIrish 8f1534f
Fix undefined variable in arithmetic docs.
DanRyanIrish d7a595d
More fixes to arithmetic docs.
DanRyanIrish fc2974b
Merge branch 'to_nddata' into nddataArithmetic2
DanRyanIrish f943aca
doc formatting fix.
DanRyanIrish 655dba9
Merge branch 'to_nddata' into nddataArithmetic2
DanRyanIrish cb3c15d
Fix test of to_nddata to ndcube.
DanRyanIrish ac980d7
Merge branch 'to_nddata' into nddataArithmetic2
DanRyanIrish 881168c
Mention motivating use case of arithmetic operations in NDCube.to_ndd…
DanRyanIrish 9088ed0
Merge branch 'to_nddata' into nddataArithmetic2
DanRyanIrish 2d70757
Merge branch 'to_nddata' of https://github.com/DanRyanIrish/ndcube in…
DanRyanIrish 1e7da64
Merge branch 'main' of https://github.com/sunpy/ndcube into nddataAri…
DanRyanIrish c2f932b
Merge branch 'main' of https://github.com/sunpy/ndcube into nddataAri…
DanRyanIrish 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 hidden or 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Adding unitful `~ndcube.NDCube` and ``astropy.nddata.NDData`` objects backed by ``dask`` did not preserve underlying arrays as ``dask`` arrays. This is now fixed. |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix bug where error was returned rather than raised with trying to perform arithmetic operation between `~ndcube.NDCube` and an object whose WCS attribute is not ``None``. |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Enable subtraction and division of `~ndcube.NDCube` by an `~astropy.nddata.NDData` instance, including uncertainty, mask and unit support. | ||
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 |
---|---|---|
|
@@ -14,3 +14,4 @@ Explaining ``ndcube`` | |
tabular_coordinates | ||
reproject | ||
visualization | ||
arithmetic |
This file contains hidden or 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
This file contains hidden or 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 | ||||
---|---|---|---|---|---|---|
|
@@ -980,34 +980,49 @@ def _arithmetic_handle_mask(self, self_mask, value_mask): | |||||
def _arithmetic_operate_with_nddata(self, operation, value): | ||||||
handle_mask = self._arithmetic_handle_mask | ||||||
if value.wcs is not None: | ||||||
return TypeError("Cannot add coordinate-aware NDCubes together.") | ||||||
|
||||||
raise TypeError("Cannot add coordinate-aware objects to NDCubes.") | ||||||
kwargs = {} | ||||||
if operation == "add": | ||||||
# Handle units | ||||||
if self.unit is not None and value.unit is not None: | ||||||
value_data = (value.data * value.unit).to_value(self.unit) | ||||||
value_data = value.data * (value.unit / self.unit).to(u.dimensionless_unscaled) | ||||||
elif self.unit is None and value.unit is None: | ||||||
value_data = value.data | ||||||
else: | ||||||
raise TypeError("Adding objects requires that both have a unit or neither has a unit.") # change the test as well. | ||||||
# Handle data and uncertainty | ||||||
kwargs["data"] = self.data + value_data | ||||||
uncert_op = np.add | ||||||
elif operation == "multiply": | ||||||
elif operation in ("multiply", "true_divide"): | ||||||
# Handle units | ||||||
if self.unit is not None or value.unit is not None: | ||||||
cube_unit = u.Unit('') if self.unit is None else self.unit | ||||||
value_unit = u.Unit('') if value.unit is None else value.unit | ||||||
kwargs["unit"] = cube_unit * value_unit | ||||||
kwargs["data"] = self.data * value.data | ||||||
uncert_op = np.multiply | ||||||
kwargs["unit"] = (cube_unit * value_unit if operation == "multiply" | ||||||
else cube_unit / value_unit) | ||||||
if operation == "multiply": | ||||||
kwargs["data"] = self.data * value.data | ||||||
uncert_op = np.multiply | ||||||
else: | ||||||
kwargs["data"] = self.data / value.data | ||||||
uncert_op = np.true_divide | ||||||
else: | ||||||
raise ValueError("Value of operation argument is not recognized.") | ||||||
kwargs["uncertainty"] = self._combine_uncertainty(uncert_op, value, kwargs["data"]) | ||||||
# Calculate uncertainty. | ||||||
new_uncert = self._combine_uncertainty(uncert_op, value, kwargs["data"]) | ||||||
if new_uncert: | ||||||
# New uncertainty object must be decoupled from its original | ||||||
# parent_nddata object. Set this to None here, and the parent_nddata | ||||||
# will become the new cube on instantiation. | ||||||
new_uncert.parent_nddata = None | ||||||
uncert_unit = kwargs.get("unit", self.unit) | ||||||
if uncert_unit: | ||||||
# Give uncertainty object the same units as the new NDCube. | ||||||
new_uncert.unit = uncert_unit | ||||||
kwargs["uncertainty"] = new_uncert | ||||||
kwargs["mask"] = handle_mask(self.mask, value.mask) | ||||||
|
||||||
return kwargs # return the new NDCube instance | ||||||
return kwargs | ||||||
|
||||||
def __add__(self, value): | ||||||
kwargs = {} | ||||||
|
@@ -1052,7 +1067,12 @@ def __radd__(self, value): | |||||
return self.__add__(value) | ||||||
|
||||||
def __sub__(self, value): | ||||||
return self.__add__(-value) | ||||||
if isinstance(value, NDData): | ||||||
new_value = copy.copy(value) | ||||||
new_value._data = -value.data | ||||||
else: | ||||||
new_value = -value | ||||||
return self.__add__(new_value) | ||||||
|
||||||
def __rsub__(self, value): | ||||||
return self.__neg__().__add__(value) | ||||||
|
@@ -1084,6 +1104,9 @@ def __rmul__(self, value): | |||||
return self.__mul__(value) | ||||||
|
||||||
def __truediv__(self, value): | ||||||
if isinstance(value, NDData): | ||||||
kwargs = self._arithmetic_operate_with_nddata("true_divide", value) | ||||||
return self._new_instance(**kwargs) | ||||||
return self.__mul__(1/value) | ||||||
|
||||||
def __rtruediv__(self, value): | ||||||
|
@@ -1483,6 +1506,10 @@ def to_nddata(self, | |||||
Attribute values can be altered on the output object by setting a kwarg with the new | ||||||
value, e.g. ``data=new_data``. | ||||||
Any attributes not supported by the new class (``nddata_type``), will be discarded. | ||||||
A motivating use case for this method is in enabling arithmetic operations between | ||||||
`~ndcube.NDCube` instances by removing coordinate-awareness. See the section of the | ||||||
ndcube documentation on | ||||||
'Enabling Arithmetic Operations between NDCubes with NDCube.to_nddata'. | ||||||
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. 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.
Suggested change
Should do it. |
||||||
|
||||||
Parameters | ||||||
---------- | ||||||
|
This file contains hidden or 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
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.