Skip to content

Commit

Permalink
Merge pull request #268 from DanRyanIrish/1.3.1
Browse files Browse the repository at this point in the history
Cherry-pick 1.3.1 commits to 1.3 branch
  • Loading branch information
DanRyanIrish authored Apr 17, 2020
2 parents 57397e3 + dcf4c0f commit 3734efa
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog/251.trivial.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Simplify and speed up implementation of NDCubeSequence slicing.
1 change: 1 addition & 0 deletions changelog/264.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix NDCollection.aligned_dimensions so it doesnt crash when components of collection are NDCubeSequences.
3 changes: 2 additions & 1 deletion ndcube/ndcollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ def aligned_dimensions(self):
"""
if self.aligned_axes is not None:
return self[self._first_key].dimensions[np.array(self.aligned_axes[self._first_key])]
return np.asanyarray(self[self._first_key].dimensions, dtype=object)[
np.array(self.aligned_axes[self._first_key])]

@property
def aligned_world_axis_physical_types(self):
Expand Down
21 changes: 17 additions & 4 deletions ndcube/ndcube_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,25 @@ def cube_like_world_axis_physical_types(self):
def __getitem__(self, item):
if isinstance(item, numbers.Integral):
return self.data[item]
elif isinstance(item, slice):
else:
result = copy.deepcopy(self)
result.data = self.data[item]
if isinstance(item, slice):
result.data = self.data[item]
else:
if isinstance(item[0], numbers.Integral):
result = result.data[item[0]][item[1:]]
else:
item0_is_length1_slice = False
if isinstance(item[0], slice):
start = 0 if item[0].start is None else item[0].start
stop = len(self.data) if item[0].stop is None else item[0].stop
if stop - start == 1:
item0_is_length1_slice = True
if item0_is_length1_slice:
result.data = [result.data[start][item[1:]]]
else:
result.data = [cube[item[1:]] for cube in result.data[item[0]]]
return result
else:
return utils.sequence.slice_sequence(self, item)

@property
def index_as_cube(self):
Expand Down
8 changes: 8 additions & 0 deletions ndcube/tests/test_ndcollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest
import numpy as np
import astropy.wcs
import astropy.units as u

from ndcube import NDCube, NDCubeSequence, NDCollection
from ndcube.tests import helpers
Expand Down Expand Up @@ -130,3 +131,10 @@ def test_collection_update_collecton_input():
expected = NDCollection([("cube0", cube0), ("cube1", cube1_alt), ("cube2", cube2)],
aligned_axes)
helpers.assert_collections_equal(orig_collection, expected)


@pytest.mark.parametrize("collection, expected_aligned_dimensions", [
(cube_collection, [4, 5]*u.pix),
(seq_collection, np.array([2*u.pix, 3*u.pix, 4*u.pix, 5*u.pix], dtype=object))])
def test_aligned_dimensions(collection, expected_aligned_dimensions):
assert all(collection.aligned_dimensions == expected_aligned_dimensions)

0 comments on commit 3734efa

Please sign in to comment.