From c24fe2e692c586aa5d7b887b86bef5dd65830bd5 Mon Sep 17 00:00:00 2001 From: DanRyanIrish Date: Mon, 13 Apr 2020 10:33:13 -0400 Subject: [PATCH] Merge pull request #251 from hayesla/ndcube_sequence_slicing First step to simplify NDCubeSequence slicing implementation --- changelog/251.trivial.rst | 1 + ndcube/ndcube_sequence.py | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 changelog/251.trivial.rst diff --git a/changelog/251.trivial.rst b/changelog/251.trivial.rst new file mode 100644 index 000000000..dc5e35050 --- /dev/null +++ b/changelog/251.trivial.rst @@ -0,0 +1 @@ +Simplify and speed up implementation of NDCubeSequence slicing. \ No newline at end of file diff --git a/ndcube/ndcube_sequence.py b/ndcube/ndcube_sequence.py index 558983c6d..eb5a35d75 100644 --- a/ndcube/ndcube_sequence.py +++ b/ndcube/ndcube_sequence.py @@ -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):