Skip to content
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

NDCube.fill method #829

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
8bb8e9f
New PR, copied ndcube.py from the other branch nddataArithmetic.
PCJY Mar 14, 2025
4e5dcd0
Changed the code to be consistent with main/ndcube.py instead.
PCJY Mar 14, 2025
4ffb573
Implementing NDCube.fill().
PCJY Mar 14, 2025
fb36c80
Added changelog, implementing NDCube.fill().
PCJY Mar 14, 2025
e1919fa
Added changelog again
PCJY Mar 14, 2025
b06a1cc
If fill_in_place is False, then return kwargs.
PCJY Mar 14, 2025
73cb945
Update changelog/829.feature.rst
PCJY Mar 16, 2025
1715862
Update ndcube/ndcube.py
PCJY Mar 16, 2025
20945f2
Update ndcube/ndcube.py
PCJY Mar 16, 2025
157f4b3
Update ndcube/ndcube.py
PCJY Mar 16, 2025
41d3b24
Update ndcube/ndcube.py
PCJY Mar 16, 2025
1afe30a
Update ndcube/ndcube.py
PCJY Mar 16, 2025
f280941
Update ndcube/ndcube.py
PCJY Mar 16, 2025
fb00430
Update ndcube/ndcube.py
PCJY Mar 17, 2025
eb3d0e8
Update ndcube/ndcube.py
PCJY Mar 17, 2025
bad2a26
Implementing the fill_masked method.
PCJY Mar 17, 2025
0cd0e60
Merge branch 'main' of https://github.com/sunpy/ndcube into NDCubefill
PCJY Mar 17, 2025
468fcb2
About units.
PCJY Mar 17, 2025
a35feeb
Further implementing, preparing for testing.
PCJY Mar 18, 2025
0cbfbd7
Added test for the fill_masked method.
PCJY Mar 18, 2025
0d4055f
Fixed error about docstring.
PCJY Mar 18, 2025
be4639a
Changed the docstring again.
PCJY Mar 18, 2025
74c648e
Update ndcube/ndcube.py
PCJY Mar 18, 2025
b7e99bd
Update ndcube/conftest.py
PCJY Mar 18, 2025
d422668
Update ndcube/ndcube.py
PCJY Mar 18, 2025
cc65f5d
Update ndcube/tests/helpers.py
PCJY Mar 18, 2025
75bf81a
deal with unmasking after using self.mask
PCJY Mar 24, 2025
be1db6e
Notes from Meeting.
PCJY Mar 23, 2025
68def22
Modified NDCube.fill_masked method and its tests.
PCJY Mar 24, 2025
26811c8
Debugging
PCJY Mar 24, 2025
db285ba
Update ndcube/ndcube.py
PCJY Mar 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog/829.feature.rst
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.
48 changes: 48 additions & 0 deletions ndcube/ndcube.py
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,54 @@ def squeeze(self, axis=None):
return self[tuple(item)]


def fill_masked(self, fill_value, unmask=False, uncertainty_fill_value=None, 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.unit.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.unit.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.
"""
if fill_in_place:
new_data = self.data
new_uncertainty = self.uncertainty
# Unmasking in-place should be handled later.
else:
new_data = copy.deepcopy(self.data)
new_uncertainty = copy.deepcopy(self.uncertainty)
new_mask = False if unmask else copy.deepcopy(self.mask)


masked = False if (self.mask is None or self.mask is False or not self.mask.any()) else True
if masked:
idx_mask = slice(None) is self.mask is True else self.mask # Ensure indexing mask can index the data array.
kwargs["data"][self.mask] = fill_value # Boolean indexing in Python.
# else, do nothing to the data.

# if unmask is True, do: change the True mask values to False, otherwise, do nothing to the mask.

if uncertainty_fill_value is not None:
if not self.uncertainty:
raise TypeError("Cannot fill uncertainty as uncertainty is None.")
new_uncertainty.array[idx_mask] = uncertainty_fill_value

if not fill_in_place:
# Create kwargs dictionary and return a new instance.
elif unmask:
self.mask = False


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:
Expand Down
Loading