Skip to content

Commit

Permalink
ImageSequence slicing: handle negative offset (#485)
Browse files Browse the repository at this point in the history
When image files are numbered from 0, the resulting ImageSequence has batch_offset -1. This situation was handled incorrectly after #438; this fixes the bug and adds a test.

Closes dials/dials#2011.
  • Loading branch information
dwpaley authored Feb 16, 2022
1 parent 2010053 commit 67aab21
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
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

0 comments on commit 67aab21

Please sign in to comment.