-
-
Notifications
You must be signed in to change notification settings - Fork 53
Added new features to the ndcube.__add__ method #794
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
PCJY
wants to merge
113
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 all commits
Commits
Show all changes
113 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 b385643
Make NDCube.__add__ call the NDCube.add method.
PCJY 9941993
tidied up the __add__ method, copied the original test_cube_arithmeti…
PCJY 4568ae2
Only check whether value has unit if it is not an NDData
PCJY b1cf478
Merge branch 'main' of https://github.com/sunpy/ndcube into nddataAri…
PCJY bb2c541
Update ndcube/ndcube.py
PCJY 3f6ebed
Update ndcube/ndcube.py
PCJY 3b76d54
Update ndcube/tests/test_ndcube.py
PCJY e7701b2
Merge branch 'nddataArithmetic' of https://github.com/PCJY/ndcube int…
PCJY 64cc02e
Fix uncertainty propagation and ensure expected_uncertainty is numpy …
PCJY 48b313e
Apply suggestions from code review
PCJY 50d64c1
Merge branch 'main' of https://github.com/sunpy/ndcube into nddataAri…
PCJY 23fef8a
check value and unit of addition
PCJY ed9d5f1
Update ndcube/ndcube.py
PCJY 02f86b3
Merge branch 'main' of https://github.com/sunpy/ndcube into nddataAri…
PCJY 7ea75f3
change values for uncertainty in a fixture to fixed values.
PCJY f31768d
Merge branch 'nddataArithmetic' of https://github.com/PCJY/ndcube int…
PCJY efafb89
Merge branch 'main' into nddataArithmetic
PCJY a32e474
Merge branch 'nddataArithmetic' of https://github.com/PCJY/ndcube int…
PCJY 83d99cf
added unit in ndcube for kwargs['data'], changed values for uncertainty.
PCJY 3b5a0ce
new test method for units of both objects being None.
PCJY 9fbb9e3
Merge branch 'main' of https://github.com/sunpy/ndcube into nddataAri…
PCJY 1f0ffc6
within a new ndcube-dev env, removed any unit involved for now.
PCJY fd78f6f
Added new test case for only one of them having a unit.
PCJY b284e1f
Test case for both objects having the same unit, and causes TypeError.
PCJY 379faac
Fix test for adding nddata and ndcube uncertainties.
DanRyanIrish c6070c0
Added more test functions for full coverage.
PCJY d88838d
Fix pytest indirect issue: Added cube(request) fixture to correctly r…
PCJY f097110
Fix indirect fixture reference.
DanRyanIrish c920a10
Merge branch 'DanRyanIrish-nddataArithmetic' into nddataArithmetic
PCJY 5565408
Written all tests and fixed an error in ndcube with test results.
PCJY 51d26c1
Fixed a small error in a test function.
PCJY 46cef9d
changed assert_cubes_equal, fixed self-referring of tests.
PCJY 1eb0357
Update ndcube/tests/helpers.py
PCJY 553b1d7
Changed the naming of test functions.
PCJY 8f9baa4
Merge branch 'nddataArithmetic' of https://github.com/PCJY/ndcube int…
PCJY fdecacd
Changed the way to check whether both objects' uncertainty are None.
PCJY 3ab2ead
Three conditional scenarios.
PCJY fdee146
Rewrote the uncertainty results checking.
PCJY d755849
Rewrote the uncertainty checking again.
PCJY 5b9626f
Implementing mask.
PCJY 3f1b8ef
Implementing mask.
PCJY 22a673b
Added Fill() Method's skeleton.
PCJY 8bb8e9f
New PR, copied ndcube.py from the other branch nddataArithmetic.
PCJY 4e5dcd0
Changed the code to be consistent with main/ndcube.py instead.
PCJY 4ffb573
Implementing NDCube.fill().
PCJY fb36c80
Added changelog, implementing NDCube.fill().
PCJY e1919fa
Added changelog again
PCJY b06a1cc
If fill_in_place is False, then return kwargs.
PCJY 73cb945
Update changelog/829.feature.rst
PCJY 1715862
Update ndcube/ndcube.py
PCJY 20945f2
Update ndcube/ndcube.py
PCJY 157f4b3
Update ndcube/ndcube.py
PCJY 41d3b24
Update ndcube/ndcube.py
PCJY 1afe30a
Update ndcube/ndcube.py
PCJY f280941
Update ndcube/ndcube.py
PCJY fb00430
Update ndcube/ndcube.py
PCJY eb3d0e8
Update ndcube/ndcube.py
PCJY bad2a26
Implementing the fill_masked method.
PCJY 0cd0e60
Merge branch 'main' of https://github.com/sunpy/ndcube into NDCubefill
PCJY 468fcb2
About units.
PCJY a35feeb
Further implementing, preparing for testing.
PCJY 0cbfbd7
Added test for the fill_masked method.
PCJY 0d4055f
Fixed error about docstring.
PCJY be4639a
Changed the docstring again.
PCJY 74c648e
Update ndcube/ndcube.py
PCJY b7e99bd
Update ndcube/conftest.py
PCJY d422668
Update ndcube/ndcube.py
PCJY cc65f5d
Update ndcube/tests/helpers.py
PCJY 75bf81a
deal with unmasking after using self.mask
PCJY be1db6e
Notes from Meeting.
PCJY 68def22
Modified NDCube.fill_masked method and its tests.
PCJY 26811c8
Debugging
PCJY db285ba
Update ndcube/ndcube.py
PCJY 8b6d9bc
Small changes from previous meeting.
PCJY 1f59a44
Changed test arguments.
PCJY c26563a
Fixing bugs in tests.
PCJY c8a86a5
Fixed coverage issue by adding more test cases.
PCJY e60eb24
Exclude defensive assertions from coverage test.
PCJY b7b9f9f
Merge branch 'main' of https://github.com/sunpy/ndcube into NDCubefill
PCJY 4983a54
Update ndcube/tests/helpers.py
PCJY d40e887
Changed code for Single-Bool-True-Mask case.
PCJY bce2bb3
Merge branch 'NDCubefill' into nddataArithmetic
PCJY 8070808
Change fixture.
PCJY 9c7aa14
Change fixture.
PCJY 9753808
Merge branch 'NDCubefill' into nddataArithmetic
PCJY 6ac02e3
Removed the operation_ignore_mask argument and all the logics using it.
PCJY bed8dab
With-mask scenarios' uncertainty and mask setting.
PCJY 56a3cf7
Refined the conditions.
PCJY d0b9281
Added tests for the three with-mask scenarios.
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 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 @@ | ||
Allows addition of an ``NDCube`` and ``NDData`` (with the WCS of ``NDData`` being set to None), and combines their uncertainties and masks. |
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,2 @@ | ||
Added ``fill_masked`` method to ``NDCube``, a new feature which allows users to replace masked values and uncertainty values with user-given fill values, | ||
to change the mask values back to False or not (Default), and to set whether the new instance is returned (Default) or not. |
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 | ||||
---|---|---|---|---|---|---|
@@ -1,4 +1,5 @@ | ||||||
import abc | ||||||
import copy | ||||||
import inspect | ||||||
import numbers | ||||||
import textwrap | ||||||
|
@@ -11,6 +12,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 | ||||||
|
||||||
|
@@ -964,24 +966,98 @@ | |||||
def __neg__(self): | ||||||
return self._new_instance(data=-self.data) | ||||||
|
||||||
def __add__(self, value): | ||||||
if hasattr(value, 'unit'): | ||||||
def add(self, value, handle_mask=np.logical_and): | ||||||
""" | ||||||
Users are allowed to choose hether they want handle_mask to be AND / OR . | ||||||
""" | ||||||
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 and value.unit is None: | ||||||
value_data = value.data | ||||||
else: | ||||||
raise TypeError("Adding objects requires both have a unit or neither has a unit.") # change the test as well. | ||||||
|
||||||
# addition | ||||||
kwargs["data"] = self.data + value_data | ||||||
result_data = kwargs["data"] | ||||||
|
||||||
# check whether there is a mask. | ||||||
# Neither self nor value has a mask | ||||||
self_unmasked = self.mask is None or self.mask is False or not self.mask.any() | ||||||
value_unmasked = value.mask is None or value.mask is False or not value.mask.any() | ||||||
|
||||||
if (self_unmasked and value_unmasked): | ||||||
# combine the uncertainty, it can be propagated without any issue. | ||||||
kwargs["uncertainty"] = self.combine_uncertainty(value, result_data) | ||||||
|
||||||
elif (self_unmasked and not value_unmasked): | ||||||
kwargs["mask"] = value.mask # mask needs to be set. | ||||||
# combine the uncertainty | ||||||
kwargs["uncertainty"] = self.combine_uncertainty(value, result_data) | ||||||
|
||||||
elif (value_unmasked and not self_unmasked): | ||||||
kwargs["mask"] = self.mask | ||||||
# combine the uncertainty | ||||||
kwargs["uncertainty"] = self.combine_uncertainty(value, result_data) | ||||||
|
||||||
else: | ||||||
kwargs["mask"] = handle_mask(self.mask,value.mask) | ||||||
# combine the uncertainty | ||||||
kwargs["uncertainty"] = self.combine_uncertainty(value, result_data) | ||||||
|
||||||
|
||||||
elif 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 | ||||||
|
||||||
# return the new NDCube instance | ||||||
return self._new_instance(**kwargs) | ||||||
|
||||||
def __add__(self, value): | ||||||
# when value has a mask, raise error and point user to the add method. TODO | ||||||
# | ||||||
# check whether there is a mask. | ||||||
# Neither self nor value has a mask | ||||||
|
||||||
self_masked = not(self.mask is None or self.mask is False or not self.mask.any()) if hasattr(self, "mask") else False | ||||||
value_masked = not(value.mask is None or value.mask is False or not value.mask.any()) if hasattr(value, "mask") else False | ||||||
|
||||||
if (value_masked or (self_masked and hasattr(value,'uncertainty') and value.uncertainty is not None)): # value has a mask, | ||||||
# let the users call the add method, since the handle_mask keyword cannot be given by users here. | ||||||
raise TypeError('Please use the add method.') | ||||||
|
||||||
return self.add(value) # without any mask, the add method can be called here and will work properly without needing arguments to be passed. | ||||||
|
||||||
def combine_uncertainty(self, value, result_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. This should be a hidden method, i.e. not intended for users. Therefore, it should start with an
Suggested change
|
||||||
# combine the uncertainty; | ||||||
if self.uncertainty is not None and value.uncertainty is not None: | ||||||
if self.unit is not None: | ||||||
result_data *= self.unit | ||||||
return self.uncertainty.propagate( | ||||||
np.add, value, result_data=result_data, correlation=0 | ||||||
) | ||||||
|
||||||
if self.uncertainty is not None: | ||||||
return self.uncertainty | ||||||
if value.uncertainty is not None: | ||||||
return value.uncertainty | ||||||
return None | ||||||
|
||||||
def __radd__(self, value): | ||||||
return self.__add__(value) | ||||||
|
@@ -1330,6 +1406,69 @@ | |||||
return self[tuple(item)] | ||||||
|
||||||
|
||||||
def fill_masked(self, fill_value, uncertainty_fill_value=None, unmask=False, fill_in_place=False): | ||||||
""" | ||||||
Replaces masked data values with input value. | ||||||
|
||||||
Returns a new instance or alters values in place. | ||||||
|
||||||
Parameters | ||||||
---------- | ||||||
fill_value: `numbers.Number` or scalar `astropy.units.Quantity` | ||||||
The value to replace masked data with. | ||||||
unmask: `bool`, optional | ||||||
If True, the newly filled masked values are unmasked. If False, they remain masked | ||||||
Default=False | ||||||
uncertainty_fill_value: `numbers.Number` or scalar `astropy.units.Quantity`, optional | ||||||
The value to replace masked uncertainties with. | ||||||
fill_in_place: `bool`, optional | ||||||
If `True`, the masked values are filled in place. If `False`, a new instance is returned | ||||||
with masked values filled. Default=False. | ||||||
""" | ||||||
# variable creations for later use. | ||||||
# If fill_in_place is true, do: assign data and uncertainty to variables. | ||||||
if fill_in_place: | ||||||
new_data = self.data | ||||||
new_uncertainty = self.uncertainty | ||||||
# Unmasking in-place should be handled later. | ||||||
|
||||||
# If fill_in_place is false, do: create new storage place for data and uncertainty and mask. | ||||||
# TODO: is the logic repetitive? this else is the same with the if not fill_in_place below? No because the order matters. | ||||||
else: | ||||||
new_data = copy.deepcopy(self.data) | ||||||
new_uncertainty = copy.deepcopy(self.uncertainty) | ||||||
new_mask = False if unmask else copy.deepcopy(self.mask) # self.mask still exists. | ||||||
|
||||||
masked = ( | ||||||
False if self.mask is None or self.mask is False | ||||||
else self.mask is True if isinstance(self.mask, bool) | ||||||
else self.mask.any() | ||||||
) | ||||||
if masked: | ||||||
idx_mask = slice(None) if self.mask is True else self.mask # Ensure indexing mask can index the data array. | ||||||
if hasattr(fill_value, "unit"): | ||||||
fill_value = fill_value.to_value(self.unit) | ||||||
new_data[idx_mask] = fill_value # python will error based on whether data array can accept the passed value. | ||||||
|
||||||
if uncertainty_fill_value is not None: | ||||||
if not self.uncertainty: # or new_uncertainty | ||||||
raise TypeError("Cannot fill uncertainty as uncertainty is None.") | ||||||
if hasattr(uncertainty_fill_value, "unit"): | ||||||
uncertainty_fill_value = uncertainty_fill_value.to_value(self.unit) | ||||||
new_uncertainty.array[idx_mask] = uncertainty_fill_value | ||||||
|
||||||
if not fill_in_place: | ||||||
# Create kwargs dictionary and return a new instance. | ||||||
kwargs = {} | ||||||
kwargs['data'] = new_data | ||||||
kwargs['uncertainty'] = new_uncertainty | ||||||
kwargs['mask'] = new_mask | ||||||
return self._new_instance(**kwargs) | ||||||
if unmask: | ||||||
self.mask = False | ||||||
return None | ||||||
|
||||||
|
||||||
def _create_masked_array_for_rebinning(data, mask, operation_ignores_mask): | ||||||
m = None if (mask is None or mask is False or operation_ignores_mask) else mask | ||||||
if m is 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
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.
This can all be simplified to:
See comment below for reason for changing
combine_uncertainty
to_combine_uncertainty
.