Skip to content

NDCube.fill_masked() method #829

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

Merged
merged 43 commits into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
43 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
8b6d9bc
Small changes from previous meeting.
PCJY Apr 3, 2025
1f59a44
Changed test arguments.
PCJY Apr 8, 2025
c26563a
Fixing bugs in tests.
PCJY Apr 9, 2025
c8a86a5
Fixed coverage issue by adding more test cases.
PCJY Apr 9, 2025
e60eb24
Exclude defensive assertions from coverage test.
PCJY Apr 9, 2025
b7b9f9f
Merge branch 'main' of https://github.com/sunpy/ndcube into NDCubefill
PCJY Apr 9, 2025
4983a54
Update ndcube/tests/helpers.py
PCJY Apr 11, 2025
d40e887
Changed code for Single-Bool-True-Mask case.
PCJY Apr 11, 2025
9c7aa14
Change fixture.
PCJY Apr 14, 2025
764dc66
Update ndcube/tests/helpers.py
PCJY Apr 14, 2025
3c8456d
Merge branch 'main' of https://github.com/sunpy/ndcube into NDCubefill
PCJY May 13, 2025
c85c74a
Merge branch 'NDCubefill' of https://github.com/PCJY/ndcube into NDCu…
PCJY May 13, 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`` 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.
44 changes: 44 additions & 0 deletions ndcube/ndcube.py
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,50 @@
return self[tuple(item)]


def fill(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.
"""
# ...code implementation here.
kwargs = {}
kwargs["data"] = self.data
kwargs["mask"] = self.mask
kwargs["uncertainty"] = self.uncertainty

Check warning on line 1356 in ndcube/ndcube.py

View check run for this annotation

Codecov / codecov/patch

ndcube/ndcube.py#L1353-L1356

Added lines #L1353 - L1356 were not covered by tests


if (fill_in_place is False):

Check warning on line 1359 in ndcube/ndcube.py

View check run for this annotation

Codecov / codecov/patch

ndcube/ndcube.py#L1359

Added line #L1359 was not covered by tests
# If there is a not None mask and a not None fill_value, do: change the corresponding data to fill_value.
if (fill_value is not None and self.mask is not None):
kwargs["data"][self.mask] = fill_value # Boolean indexing in Python.

Check warning on line 1362 in ndcube/ndcube.py

View check run for this annotation

Codecov / codecov/patch

ndcube/ndcube.py#L1361-L1362

Added lines #L1361 - L1362 were not covered by tests
# 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 (unmask):
kwargs["mask"] = False

Check warning on line 1367 in ndcube/ndcube.py

View check run for this annotation

Codecov / codecov/patch

ndcube/ndcube.py#L1366-L1367

Added lines #L1366 - L1367 were not covered by tests

if (self.mask is not None and uncertainty_fill_value is not None):
kwargs["uncertainty"][self.mask] = uncertainty_fill_value

Check warning on line 1370 in ndcube/ndcube.py

View check run for this annotation

Codecov / codecov/patch

ndcube/ndcube.py#L1369-L1370

Added lines #L1369 - L1370 were not covered by tests

# if fill_in_place is True, then change self directly? without creating kwargs?

return kwargs

Check warning on line 1374 in ndcube/ndcube.py

View check run for this annotation

Codecov / codecov/patch

ndcube/ndcube.py#L1374

Added line #L1374 was not covered by tests


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