Skip to content

Commit 01d3a92

Browse files
Merge pull request #462 from MarcCote/fix_tractogram_slicing
MRG: Copy affine_to_rasmm when slicing a Tractogram object Set the affine_to_rasmm when creating a new Tractogram object that is the result of advanced indexing or slicing. Add a test to check it is working properly.
2 parents d6945c4 + 264d16a commit 01d3a92

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

nibabel/streamlines/tests/test_tractogram.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
def setup():
2020
global DATA
21+
DATA['rng'] = np.random.RandomState(1234)
2122
DATA['streamlines'] = [np.arange(1*3, dtype="f4").reshape((1, 3)),
2223
np.arange(2*3, dtype="f4").reshape((2, 3)),
2324
np.arange(5*3, dtype="f4").reshape((5, 3))]
@@ -373,6 +374,13 @@ def test_tractogram_getitem(self):
373374
DATA['tractogram'].data_per_streamline[::-1],
374375
DATA['tractogram'].data_per_point[::-1])
375376

377+
# Make sure slicing conserves the affine_to_rasmm property.
378+
tractogram = DATA['tractogram'].copy()
379+
tractogram.affine_to_rasmm = DATA['rng'].rand(4, 4)
380+
tractogram_view = tractogram[::2]
381+
assert_array_equal(tractogram_view.affine_to_rasmm,
382+
tractogram.affine_to_rasmm)
383+
376384
def test_tractogram_add_new_data(self):
377385
# Tractogram with only streamlines
378386
t = DATA['simple_tractogram'].copy()

nibabel/streamlines/tractogram.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,8 @@ def __getitem__(self, idx):
335335
if isinstance(idx, (numbers.Integral, np.integer)):
336336
return TractogramItem(pts, data_per_streamline, data_per_point)
337337

338-
return Tractogram(pts, data_per_streamline, data_per_point)
338+
return Tractogram(pts, data_per_streamline, data_per_point,
339+
affine_to_rasmm=self.affine_to_rasmm)
339340

340341
def __len__(self):
341342
return len(self.streamlines)

0 commit comments

Comments
 (0)