Skip to content

Commit 40ec4e5

Browse files
committed
Change how affine_to_rasmm is handled. Can now set affine_to_rasmm to None when streamlines' space is unknown
1 parent 3b753dc commit 40ec4e5

File tree

10 files changed

+154
-100
lines changed

10 files changed

+154
-100
lines changed

nibabel/streamlines/array_sequence.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ def nb_elements(self):
112112
""" Total number of elements in this array sequence. """
113113
return self._data.shape[0]
114114

115+
@property
116+
def data(self):
117+
""" Elements in this array sequence. """
118+
return self._data
119+
115120
def append(self, element):
116121
""" Appends `element` to this array sequence.
117122

nibabel/streamlines/header.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ class Field:
1616
ORIGIN = "origin"
1717
VOXEL_TO_RASMM = "voxel_to_rasmm"
1818
VOXEL_ORDER = "voxel_order"
19-
ENDIAN = "endian"
19+
ENDIANNESS = "endianness"

nibabel/streamlines/tests/test_streamlines.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,13 @@ def setup():
6666
'mean_torsion': mean_torsion,
6767
'mean_colors': mean_colors}
6868

69-
DATA['empty_tractogram'] = Tractogram()
70-
DATA['simple_tractogram'] = Tractogram(DATA['streamlines'])
69+
DATA['empty_tractogram'] = Tractogram(affine_to_rasmm=np.eye(4))
70+
DATA['simple_tractogram'] = Tractogram(DATA['streamlines'],
71+
affine_to_rasmm=np.eye(4))
7172
DATA['complex_tractogram'] = Tractogram(DATA['streamlines'],
7273
DATA['data_per_streamline'],
73-
DATA['data_per_point'])
74+
DATA['data_per_point'],
75+
affine_to_rasmm=np.eye(4))
7476

7577

7678
def test_is_supported_detect_format():
@@ -167,7 +169,8 @@ def test_load_complex_file(self):
167169
else:
168170
assert_true(type(tfile.tractogram), LazyTractogram)
169171

170-
tractogram = Tractogram(DATA['streamlines'])
172+
tractogram = Tractogram(DATA['streamlines'],
173+
affine_to_rasmm=np.eye(4))
171174

172175
if tfile.SUPPORTS_DATA_PER_POINT:
173176
tractogram.data_per_point = DATA['data_per_point']
@@ -180,7 +183,8 @@ def test_load_complex_file(self):
180183
tractogram)
181184

182185
def test_save_tractogram_file(self):
183-
tractogram = Tractogram(DATA['streamlines'])
186+
tractogram = Tractogram(DATA['streamlines'],
187+
affine_to_rasmm=np.eye(4))
184188
trk_file = trk.TrkFile(tractogram)
185189

186190
# No need for keyword arguments.
@@ -204,7 +208,7 @@ def test_save_tractogram_file(self):
204208
assert_tractogram_equal(tfile.tractogram, tractogram)
205209

206210
def test_save_empty_file(self):
207-
tractogram = Tractogram()
211+
tractogram = Tractogram(affine_to_rasmm=np.eye(4))
208212
for ext, cls in nib.streamlines.FORMATS.items():
209213
with InTemporaryDirectory():
210214
filename = 'streamlines' + ext
@@ -213,7 +217,8 @@ def test_save_empty_file(self):
213217
assert_tractogram_equal(tfile.tractogram, tractogram)
214218

215219
def test_save_simple_file(self):
216-
tractogram = Tractogram(DATA['streamlines'])
220+
tractogram = Tractogram(DATA['streamlines'],
221+
affine_to_rasmm=np.eye(4))
217222
for ext, cls in nib.streamlines.FORMATS.items():
218223
with InTemporaryDirectory():
219224
filename = 'streamlines' + ext
@@ -224,7 +229,8 @@ def test_save_simple_file(self):
224229
def test_save_complex_file(self):
225230
complex_tractogram = Tractogram(DATA['streamlines'],
226231
DATA['data_per_streamline'],
227-
DATA['data_per_point'])
232+
DATA['data_per_point'],
233+
affine_to_rasmm=np.eye(4))
228234

229235
for ext, cls in nib.streamlines.FORMATS.items():
230236
with InTemporaryDirectory():
@@ -242,7 +248,8 @@ def test_save_complex_file(self):
242248
assert_equal(len(w), 1)
243249
assert_true(issubclass(w[0].category, Warning))
244250

245-
tractogram = Tractogram(DATA['streamlines'])
251+
tractogram = Tractogram(DATA['streamlines'],
252+
affine_to_rasmm=np.eye(4))
246253

247254
if cls.SUPPORTS_DATA_PER_POINT:
248255
tractogram.data_per_point = DATA['data_per_point']

nibabel/streamlines/tests/test_tractogram.py

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ def setup():
5757
DATA['simple_tractogram'] = Tractogram(DATA['streamlines'])
5858
DATA['tractogram'] = Tractogram(DATA['streamlines'],
5959
DATA['data_per_streamline'],
60-
DATA['data_per_point'])
60+
DATA['data_per_point'],
61+
affine_to_rasmm=np.eye(4))
6162

6263
DATA['streamlines_func'] = lambda: (e for e in DATA['streamlines'])
6364
fa_func = lambda: (e for e in DATA['fa'])
@@ -74,7 +75,8 @@ def setup():
7475

7576
DATA['lazy_tractogram'] = LazyTractogram(DATA['streamlines_func'],
7677
DATA['data_per_streamline_func'],
77-
DATA['data_per_point_func'])
78+
DATA['data_per_point_func'],
79+
affine_to_rasmm=np.eye(4))
7880

7981

8082
def check_tractogram_item(tractogram_item,
@@ -276,6 +278,7 @@ def test_tractogram_creation(self):
276278
# Create an empty tractogram.
277279
tractogram = Tractogram()
278280
check_tractogram(tractogram)
281+
assert_true(tractogram.affine_to_rasmm is None)
279282

280283
# Create a tractogram with only streamlines
281284
tractogram = Tractogram(streamlines=DATA['streamlines'])
@@ -284,7 +287,7 @@ def test_tractogram_creation(self):
284287
# Create a tractogram with a given affine_to_rasmm.
285288
affine = np.diag([1, 2, 3, 1])
286289
tractogram = Tractogram(affine_to_rasmm=affine)
287-
assert_array_equal(tractogram.get_affine_to_rasmm(), affine)
290+
assert_array_equal(tractogram.affine_to_rasmm, affine)
288291

289292
# Create a tractogram with streamlines and other data.
290293
tractogram = Tractogram(DATA['streamlines'],
@@ -447,9 +450,10 @@ def test_tractogram_apply_affine(self):
447450
streamlines=[s*scaling for s in DATA['streamlines']],
448451
data_per_streamline=DATA['data_per_streamline'],
449452
data_per_point=DATA['data_per_point'])
450-
assert_array_equal(transformed_tractogram.get_affine_to_rasmm(),
453+
assert_array_equal(transformed_tractogram.affine_to_rasmm,
451454
np.dot(np.eye(4), np.linalg.inv(affine)))
452-
# Make sure streamlines of the original tractogram have not been modified.
455+
# Make sure streamlines of the original tractogram have not been
456+
# modified.
453457
assert_arrays_equal(tractogram.streamlines, DATA['streamlines'])
454458

455459
# Apply the affine to the streamlines in-place.
@@ -462,7 +466,7 @@ def test_tractogram_apply_affine(self):
462466

463467
# Apply affine again and check the affine_to_rasmm.
464468
transformed_tractogram = tractogram.apply_affine(affine)
465-
assert_array_equal(transformed_tractogram.get_affine_to_rasmm(),
469+
assert_array_equal(transformed_tractogram.affine_to_rasmm,
466470
np.dot(np.eye(4), np.dot(np.linalg.inv(affine),
467471
np.linalg.inv(affine))))
468472

@@ -474,10 +478,16 @@ def test_tractogram_apply_affine(self):
474478

475479
tractogram.apply_affine(affine)
476480
tractogram.apply_affine(np.linalg.inv(affine))
477-
assert_array_almost_equal(tractogram.get_affine_to_rasmm(), np.eye(4))
481+
assert_array_almost_equal(tractogram.affine_to_rasmm, np.eye(4))
478482
for s1, s2 in zip(tractogram.streamlines, DATA['streamlines']):
479483
assert_array_almost_equal(s1, s2)
480484

485+
# Test removing affine_to_rasmm
486+
tractogram = DATA['tractogram'].copy()
487+
tractogram.affine_to_rasmm = None
488+
tractogram.apply_affine(affine)
489+
assert_true(tractogram.affine_to_rasmm is None)
490+
481491
def test_tractogram_to_world(self):
482492
tractogram = DATA['tractogram'].copy()
483493
affine = np.random.RandomState(1234).randn(4, 4)
@@ -486,30 +496,35 @@ def test_tractogram_to_world(self):
486496
# Apply the affine to the streamlines, then bring them back
487497
# to world space in a lazy manner.
488498
transformed_tractogram = tractogram.apply_affine(affine)
489-
assert_array_equal(transformed_tractogram.get_affine_to_rasmm(),
499+
assert_array_equal(transformed_tractogram.affine_to_rasmm,
490500
np.linalg.inv(affine))
491501

492502
tractogram_world = transformed_tractogram.to_world(lazy=True)
493503
assert_true(type(tractogram_world) is LazyTractogram)
494-
assert_array_almost_equal(tractogram_world.get_affine_to_rasmm(),
504+
assert_array_almost_equal(tractogram_world.affine_to_rasmm,
495505
np.eye(4))
496506
for s1, s2 in zip(tractogram_world.streamlines, DATA['streamlines']):
497507
assert_array_almost_equal(s1, s2)
498508

499509
# Bring them back streamlines to world space in a in-place manner.
500510
tractogram_world = transformed_tractogram.to_world()
501511
assert_true(tractogram_world is tractogram)
502-
assert_array_almost_equal(tractogram.get_affine_to_rasmm(), np.eye(4))
512+
assert_array_almost_equal(tractogram.affine_to_rasmm, np.eye(4))
503513
for s1, s2 in zip(tractogram.streamlines, DATA['streamlines']):
504514
assert_array_almost_equal(s1, s2)
505515

506516
# Calling to_world twice should do nothing.
507517
tractogram_world2 = transformed_tractogram.to_world()
508518
assert_true(tractogram_world2 is tractogram)
509-
assert_array_almost_equal(tractogram.get_affine_to_rasmm(), np.eye(4))
519+
assert_array_almost_equal(tractogram.affine_to_rasmm, np.eye(4))
510520
for s1, s2 in zip(tractogram.streamlines, DATA['streamlines']):
511521
assert_array_almost_equal(s1, s2)
512522

523+
# Calling to_world when affine_to_rasmm is None should fail.
524+
tractogram = DATA['tractogram'].copy()
525+
tractogram.affine_to_rasmm = None
526+
assert_raises(ValueError, tractogram.to_world)
527+
513528

514529
class TestLazyTractogram(unittest.TestCase):
515530

@@ -534,6 +549,7 @@ def test_lazy_tractogram_creation(self):
534549
# Empty `LazyTractogram`
535550
tractogram = LazyTractogram()
536551
check_tractogram(tractogram)
552+
assert_true(tractogram.affine_to_rasmm is None)
537553

538554
# Create tractogram with streamlines and other data
539555
tractogram = LazyTractogram(DATA['streamlines_func'],
@@ -627,9 +643,9 @@ def test_lazy_tractogram_apply_affine(self):
627643
transformed_tractogram = tractogram.apply_affine(affine)
628644
assert_true(transformed_tractogram is not tractogram)
629645
assert_array_equal(tractogram._affine_to_apply, np.eye(4))
630-
assert_array_equal(tractogram.get_affine_to_rasmm(), np.eye(4))
646+
assert_array_equal(tractogram.affine_to_rasmm, np.eye(4))
631647
assert_array_equal(transformed_tractogram._affine_to_apply, affine)
632-
assert_array_equal(transformed_tractogram.get_affine_to_rasmm(),
648+
assert_array_equal(transformed_tractogram.affine_to_rasmm,
633649
np.dot(np.eye(4), np.linalg.inv(affine)))
634650
check_tractogram(transformed_tractogram,
635651
streamlines=[s*scaling for s in DATA['streamlines']],
@@ -640,10 +656,15 @@ def test_lazy_tractogram_apply_affine(self):
640656
transformed_tractogram = transformed_tractogram.apply_affine(affine)
641657
assert_array_equal(transformed_tractogram._affine_to_apply,
642658
np.dot(affine, affine))
643-
assert_array_equal(transformed_tractogram.get_affine_to_rasmm(),
659+
assert_array_equal(transformed_tractogram.affine_to_rasmm,
644660
np.dot(np.eye(4), np.dot(np.linalg.inv(affine),
645661
np.linalg.inv(affine))))
646662

663+
# Calling to_world when affine_to_rasmm is None should fail.
664+
tractogram = DATA['lazy_tractogram'].copy()
665+
tractogram.affine_to_rasmm = None
666+
assert_raises(ValueError, tractogram.to_world)
667+
647668
def test_tractogram_to_world(self):
648669
tractogram = DATA['lazy_tractogram'].copy()
649670
affine = np.random.RandomState(1234).randn(4, 4)
@@ -652,22 +673,27 @@ def test_tractogram_to_world(self):
652673
# Apply the affine to the streamlines, then bring them back
653674
# to world space in a lazy manner.
654675
transformed_tractogram = tractogram.apply_affine(affine)
655-
assert_array_equal(transformed_tractogram.get_affine_to_rasmm(),
676+
assert_array_equal(transformed_tractogram.affine_to_rasmm,
656677
np.linalg.inv(affine))
657678

658679
tractogram_world = transformed_tractogram.to_world()
659680
assert_true(tractogram_world is not transformed_tractogram)
660-
assert_array_almost_equal(tractogram_world.get_affine_to_rasmm(),
681+
assert_array_almost_equal(tractogram_world.affine_to_rasmm,
661682
np.eye(4))
662683
for s1, s2 in zip(tractogram_world.streamlines, DATA['streamlines']):
663684
assert_array_almost_equal(s1, s2)
664685

665686
# Calling to_world twice should do nothing.
666687
tractogram_world = tractogram_world.to_world()
667-
assert_array_almost_equal(tractogram_world.get_affine_to_rasmm(), np.eye(4))
688+
assert_array_almost_equal(tractogram_world.affine_to_rasmm, np.eye(4))
668689
for s1, s2 in zip(tractogram_world.streamlines, DATA['streamlines']):
669690
assert_array_almost_equal(s1, s2)
670691

692+
# Calling to_world when affine_to_rasmm is None should fail.
693+
tractogram = DATA['lazy_tractogram'].copy()
694+
tractogram.affine_to_rasmm = None
695+
assert_raises(ValueError, tractogram.to_world)
696+
671697
def test_lazy_tractogram_copy(self):
672698
# Create a copy of the lazy tractogram.
673699
tractogram = DATA['lazy_tractogram'].copy()

0 commit comments

Comments
 (0)