Skip to content
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

ImageSequence slicing: handle negative offset #485

Merged
merged 3 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all 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 newsfragments/485.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Correctly handle slicing ImageSequences made from images starting with 0
20 changes: 10 additions & 10 deletions src/dxtbx/imageset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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"""
Expand Down
7 changes: 7 additions & 0 deletions tests/test_imageset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pickle
import shutil
from unittest import mock
import copy

import pytest

Expand Down Expand Up @@ -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()
Expand Down