Skip to content

Support Ellipsis in NDCube.__getitem__ #818

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
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

Diya910
Copy link

@Diya910 Diya910 commented Jan 31, 2025

PR Description

This PR adds support for the ellipsis (...) syntax in NDCube allowing users to slice NDCube objects using ellipsis notation.

Issue Reference:#741

@nabobalis nabobalis marked this pull request as draft January 31, 2025 05:31
@nabobalis
Copy link
Member

This will need some unit tests and a changelog entry.

@nabobalis nabobalis added this to the 2.4.0 milestone Jan 31, 2025
@Diya910
Copy link
Author

Diya910 commented Jan 31, 2025

there is no file named test_ndslicing.py . Should I make one and add test cases there or use test_ndcube.py where some slicing tests are present.

@nabobalis
Copy link
Member

I see no harm in creating a new dedicated slicing test file.

@Diya910
Copy link
Author

Diya910 commented Feb 2, 2025

pre-commit.ci autofix

@Diya910
Copy link
Author

Diya910 commented Feb 2, 2025

Should I add init.py to tests folder as error raised in pre-commit.ci

@nabobalis
Copy link
Member

Is there not an init file in that folder? If not, one needs to be added.

Comment on lines 9 to 13
@pytest.fixture
def sample_ndcube():
data = np.random.rand(4, 4, 4)
wcs = WCS(naxis=3)
return NDCube(data, wcs)
Copy link
Member

Choose a reason for hiding this comment

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

I thought there were other fixtures defined elsewhere which can be used for the test below.

Copy link
Author

Choose a reason for hiding this comment

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

let me see

@Diya910
Copy link
Author

Diya910 commented Feb 4, 2025

Is there not an init file in that folder? If not, one needs to be added.

I created tests folder in mixins. All tests folder have init.py file which is empty. I think I should add one.

@nabobalis
Copy link
Member

Sounds good

@Diya910
Copy link
Author

Diya910 commented Feb 6, 2025

any suggestions on the changes?

@nabobalis
Copy link
Member

I think there should be a few more tests on more complex ndcubes. But I will ping the maintainers for a review

Copy link
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 @Diya910. Thanks for this really helpful PR. I agree with @nabobalis that there should be a few more tests, though. I've left a code suggestion outlining some additional parameterized tests.

Also, there are slicing tests in test_ndcube.py. So the tests for this PR should probably go there too.

Finally, this branch needs to be updated with main.

Thanks again for the PR!

if isinstance(item, tuple) and Ellipsis in item:
if item.count(Ellipsis) > 1:
raise IndexError("An index can only have a single ellipsis ('...')")

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change

expanded_item.extend([slice(None)] * (len(self.shape) - len(item) + 1))
else:
expanded_item.append(i)

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change

Comment on lines +7 to +9
def test_ellipsis_usage(sample_ndcube):
sliced_cube = sample_ndcube[..., 1]
assert sliced_cube.data.shape == (5,10,12)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def test_ellipsis_usage(sample_ndcube):
sliced_cube = sample_ndcube[..., 1]
assert sliced_cube.data.shape == (5,10,12)
@pytest.mark.parametrize(("ndc","item","expected_shape")
[
("ndcube_4d_ln_l_t_lt", np.s_[..., 1], (5, 10, 12)),
("ndcube_4d_ln_l_t_lt", np.s_[..., 1:, 1], (5, 10, 11)),
("ndcube_4d_ln_l_t_lt", np.s_[1, ...], (10, 12, 8)),
("ndcube_4d_ln_l_t_lt", np.s_[1, 1:, ...], (9, 12, 8)),
("ndcube_4d_ln_l_t_lt", np.s_[1, ..., 1:], (10, 12, 7)),
("ndcube_4d_ln_l_t_lt", np.s_[1, 1:, ..., 1:], (9, 12, 7)),
],
indirect=("ndc",))
def test_ellipsis_usage(ndc):
sliced_cube = ndc[item]
assert sliced_cube.data.shape == expected_shape

@DanRyanIrish DanRyanIrish marked this pull request as ready for review May 14, 2025 11:42
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.

3 participants