Skip to content

NDCube.fill_masked() method#829

Merged
DanRyanIrish merged 43 commits into
sunpy:mainfrom
PCJY:NDCubefill
May 13, 2025
Merged

NDCube.fill_masked() method#829
DanRyanIrish merged 43 commits into
sunpy:mainfrom
PCJY:NDCubefill

Conversation

@PCJY
Copy link
Copy Markdown
Contributor

@PCJY PCJY commented Mar 14, 2025

PR Description

This pull request aims to resolve the issue proposed by @DanRyanIrish , #828 .

Copy link
Copy Markdown
Member

@DanRyanIrish DanRyanIrish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @PCJY. Here are some suggested improvements.

Once you've handled these, you should also consider how to handle the case where the cube (including the uncertainty) has a unit.

Then this PR will also need tests.

Comment thread changelog/829.feature.rst Outdated
Comment thread ndcube/ndcube.py Outdated
Comment thread ndcube/ndcube.py Outdated
Comment thread ndcube/ndcube.py Outdated
Comment thread ndcube/ndcube.py Outdated
Comment thread ndcube/ndcube.py Outdated
Comment thread ndcube/ndcube.py Outdated
Comment thread ndcube/ndcube.py Outdated
Comment thread ndcube/ndcube.py Outdated
PCJY and others added 7 commits March 16, 2025 00:57
Co-authored-by: DanRyanIrish <ryand5@tcd.ie>
rename the method

Co-authored-by: DanRyanIrish <ryand5@tcd.ie>
Co-authored-by: DanRyanIrish <ryand5@tcd.ie>
Co-authored-by: DanRyanIrish <ryand5@tcd.ie>
remove code about kwargs when fill_in_place is True.

Co-authored-by: DanRyanIrish <ryand5@tcd.ie>
Already know that self.mask is not None, now, check whether uncertainty_fill_value and self.uncertainty is None, raise an error if self.uncertainty is None.

Co-authored-by: DanRyanIrish <ryand5@tcd.ie>
Filling in the uncertainty_fill_value

Co-authored-by: DanRyanIrish <ryand5@tcd.ie>
@DanRyanIrish DanRyanIrish added this to the 2.4.0 milestone Mar 17, 2025
PCJY and others added 2 commits March 17, 2025 13:32
Co-authored-by: DanRyanIrish <ryand5@tcd.ie>
Co-authored-by: DanRyanIrish <ryand5@tcd.ie>
@DanRyanIrish
Copy link
Copy Markdown
Member

@PCJY Let me know when this is ready for another review. Also, be sure to pull the latest changes from the main branch into this branch.

@PCJY
Copy link
Copy Markdown
Contributor Author

PCJY commented Mar 18, 2025

Hi @DanRyanIrish I have just implemented the method more, finished adding tests for the method. Apologies for the delay. I also handled units by keeping the unit the same (which might not be what you were trying to suggest, because you might mean: the fill_value itself has a unit? ).

Comment thread ndcube/ndcube.py Outdated
Comment thread ndcube/ndcube.py Outdated
Comment thread ndcube/ndcube.py Outdated
Comment thread ndcube/conftest.py Outdated
Comment thread ndcube/ndcube.py Outdated
Comment thread ndcube/tests/helpers.py Outdated
Comment thread ndcube/tests/test_ndcube.py Outdated
Comment thread ndcube/tests/test_ndcube.py Outdated
Comment thread ndcube/tests/test_ndcube.py Outdated
(1.0 * u.cm, 0.02, False, False), # what if the fill_value has a unit??
]
)
def test_fill_masked(ndcube_2d_ln_lt_mask_uncert_unit, fill_value, uncertainty_fill_value, unmask, fill_in_place):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest two separate tests for fill_in_place equal True and False. I also suggest that you provide the expected cube in the parameterisation. This would be much easier if your test cube only had a few data elements, e.g. a 2x3 array.

Where it's feasible, it's better to explicitly define the expected value, rather than calculate it based on the inputs provided. This leaves more possibility for the same error to exist in the code you're testing and your calculation of the expected value.

Comment thread ndcube/ndcube.py Outdated
Comment thread ndcube/ndcube.py Outdated
PCJY and others added 2 commits March 18, 2025 15:18
make kwargs ndcube and return it

Co-authored-by: DanRyanIrish <ryand5@tcd.ie>
Co-authored-by: DanRyanIrish <ryand5@tcd.ie>
@PCJY
Copy link
Copy Markdown
Contributor Author

PCJY commented Mar 24, 2025

Hi @DanRyanIrish , just so you know, the last three commits are the latest ones I made. Thank you.

Comment thread ndcube/ndcube.py Outdated
@PCJY
Copy link
Copy Markdown
Contributor Author

PCJY commented Apr 9, 2025

Hi @DanRyanIrish , I have finished debugging the tests (with the latest four commits):
1, There were some errors in the fixtures I used, so I fixed them.

2, I forgot to specify the argument "check_uncertainty_values" specifying whether it is necessary to check the uncertainty, as True, in the assert_cubes_equal method. So I fixed it.

3, The coverage file reminded me that I did not test the case when uncertainty_fill_value is not None but the NDCube object's uncertainty is None. So I added an individual test method to test this case, expecting it to raise a TypeError.
I wish to ask you a question: Do you think this is the best way to add this test case? I added it by adding an individual method because I am not sure what to put in the expected_cube parametrized argument for this scenario if I were to use the existing two test method structure.

4, Then the coverage file complains about not entering the two scenarios when one and only one of test_input and expected_cube has an uncertainty attribute. But I think the correct behaviour is that it does not enter these two blocks. So I used 'pragma no cover' to let the coverage file know it does not need to be covered by the tests.

Please let me know if there's anything you'd like me to adjust, thank you!

Copy link
Copy Markdown
Member

@DanRyanIrish DanRyanIrish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @PCJY. This is looking good. Just one comment about testing masks are equal.

I'd like to get @Cadair to give this a quick look if possible. But I think it's close to being done.

Comment thread ndcube/tests/helpers.py Outdated
Comment thread ndcube/ndcube.py
return self._new_instance(**kwargs)
if unmask:
self.mask = False
return None
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Cadair Do you have advice when it comes to methods to make changes in-place? Should this return None, or not return anything at all?

PCJY and others added 2 commits April 11, 2025 15:36
assert_cubes_equal(), check masks equal or not, in two scenarios

Co-authored-by: DanRyanIrish <ryand5@tcd.ie>
@PCJY
Copy link
Copy Markdown
Contributor Author

PCJY commented Apr 11, 2025

Hi @DanRyanIrish , thanks for your review and bringing up the single-Boolean case issue. In my latest commit, there a few new changes addressing the single-Boolean-True mask value test case, I think the test is more robust now.
Please let me know if you have any further feedback, thanks.

Comment thread ndcube/tests/helpers.py Outdated
PCJY and others added 2 commits April 14, 2025 10:45
Check the masks' type and then assert whether they are the same.

Co-authored-by: DanRyanIrish <ryand5@tcd.ie>
@ayshih ayshih changed the title NDCube.fill method NDCube.fill_masked() method Apr 14, 2025
@Cadair
Copy link
Copy Markdown
Member

Cadair commented May 1, 2025

I've encountered various astropy masked things recently, they use filled(): https://docs.astropy.org/en/stable/api/astropy.utils.masked.Masked.html maybe we want to consider that?

@ayshih
Copy link
Copy Markdown
Member

ayshih commented May 2, 2025

Both astropy.utils.masked.Masked and numpy.ma.MaskedArray name their analogous method filled(). The distinction I'd draw is that both of those classes are expressly for masked use – the word is in their names! – so including "masked" in a method name would be superfluous. In contrast, I don't feel like masking is at the forefront of all NDCube usage, so I think users need to be alerted that the method is relevant only for masked NDCube objects.

@DanRyanIrish DanRyanIrish merged commit 6092705 into sunpy:main May 13, 2025
20 checks passed
@DanRyanIrish DanRyanIrish mentioned this pull request Jun 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants