Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
1 change: 1 addition & 0 deletions changelog/818.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added Ellipsis functionality to ndcube.mixins.ndslicing
13 changes: 13 additions & 0 deletions ndcube/mixins/ndslicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ def __getitem__(self, item):
meta = self.meta
self.meta = None

if isinstance(item, tuple) and Ellipsis in item:
if item.count(Ellipsis) > 1:
Comment thread
nabobalis marked this conversation as resolved.
raise IndexError("An index can only have a single ellipsis ('...')")

Comment thread
DanRyanIrish marked this conversation as resolved.
Outdated
expanded_item = []
for i in item:
if i is Ellipsis:
expanded_item.extend([slice(None)] * (len(self.shape) - len(item) + 1))
else:
expanded_item.append(i)

Comment thread
DanRyanIrish marked this conversation as resolved.
Outdated
item = tuple(expanded_item)

# Slice cube.
item = tuple(sanitize_slices(item, len(self.shape)))
sliced_cube = super().__getitem__(item)
Expand Down
20 changes: 20 additions & 0 deletions ndcube/mixins/tests/test_ndslicing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import numpy as np
import pytest
Comment thread
DanRyanIrish marked this conversation as resolved.

from astropy.wcs import WCS

from ndcube import NDCube


@pytest.fixture
def sample_ndcube():
data = np.random.rand(4, 4, 4)
wcs = WCS(naxis=3)
return NDCube(data, wcs)
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 thought there were other fixtures defined elsewhere which can be used for the test below.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

let me see


def test_ellipsis_usage(sample_ndcube):
sliced_cube = sample_ndcube[..., 1]
assert sliced_cube.data.shape == (4, 4)

with pytest.raises(IndexError, match="An index can only have a single ellipsis"):
sample_ndcube[..., ..., 1]
Comment thread
DanRyanIrish marked this conversation as resolved.
Outdated