diff --git a/newsfragments/485.bugfix b/newsfragments/485.bugfix new file mode 100644 index 000000000..8872060a1 --- /dev/null +++ b/newsfragments/485.bugfix @@ -0,0 +1 @@ +Correctly handle slicing ImageSequences made from images starting with 0 diff --git a/src/dxtbx/imageset.py b/src/dxtbx/imageset.py index da62209be..574779956 100644 --- a/src/dxtbx/imageset.py +++ b/src/dxtbx/imageset.py @@ -297,7 +297,9 @@ def __getitem__(self, item): An image or new Sequence object """ - if isinstance(item, slice): + if not isinstance(item, slice): + return self.get_corrected_data(item) + else: offset = self.get_scan().get_batch_offset() if item.step is not None: raise IndexError("Sequences must be sequential") @@ -316,18 +318,16 @@ def __getitem__(self, item): stop = len(self) else: stop -= offset - - return self.partial_set(start, stop) else: start = item.start or 0 stop = item.stop or (len(self) + offset) - if self.data().has_single_file_reader(): - reader = self.reader().copy(self.reader().paths(), stop - start) - else: - reader = self.reader().copy(self.reader().paths()) - return self.partial_set(reader, start - offset, stop - offset) - else: - return self.get_corrected_data(item) + start -= offset + stop -= offset + if self.data().has_single_file_reader(): + reader = self.reader().copy(self.reader().paths(), stop - start) + else: + reader = self.reader().copy(self.reader().paths()) + return self.partial_set(reader, start, stop) def get_template(self): """Return the template""" diff --git a/tests/test_imageset.py b/tests/test_imageset.py index 16b5b3921..66f9ac559 100644 --- a/tests/test_imageset.py +++ b/tests/test_imageset.py @@ -2,6 +2,7 @@ import pickle import shutil from unittest import mock +import copy import pytest @@ -393,6 +394,12 @@ def tst_get_item(self, sequence): with pytest.raises(IndexError): _ = sequence[3:7:2] + # Simulate a scan starting from image 0 + sequence_ = copy.deepcopy(sequence) + sequence_.get_scan().set_batch_offset(-1) + sequence3 = sequence_[3:7] + assert sequence3.get_array_range() == (4, 8) + @staticmethod def tst_paths(sequence, filenames1): filenames2 = sequence.paths()