@@ -389,34 +389,73 @@ def test_tractogram_apply_affine(self):
389389 # Apply the affine to the streamline in a lazy manner.
390390 transformed_tractogram = tractogram .apply_affine (affine , lazy = True )
391391 assert_true (type (transformed_tractogram ) is LazyTractogram )
392- assert_true (check_iteration (transformed_tractogram ))
393- assert_equal (len (transformed_tractogram ), len (DATA ['streamlines' ]))
394- for s1 , s2 in zip (transformed_tractogram .streamlines ,
395- DATA ['streamlines' ]):
396- assert_array_almost_equal (s1 , s2 * scaling )
397-
398- for s1 , s2 in zip (transformed_tractogram .streamlines ,
399- tractogram .streamlines ):
400- assert_array_almost_equal (s1 , s2 * scaling )
401-
392+ check_tractogram (transformed_tractogram ,
393+ streamlines = [s * scaling for s in DATA ['streamlines' ]],
394+ data_per_streamline = DATA ['data_per_streamline' ],
395+ data_per_point = DATA ['data_per_point' ])
402396 assert_array_equal (transformed_tractogram .get_affine_to_rasmm (),
403397 np .dot (np .eye (4 ), np .linalg .inv (affine )))
398+ # Make sure streamlines of the original tractogram have not been modified.
399+ assert_arrays_equal (tractogram .streamlines , DATA ['streamlines' ])
404400
405401 # Apply the affine to the streamlines in-place.
406402 transformed_tractogram = tractogram .apply_affine (affine )
407403 assert_true (transformed_tractogram is tractogram )
408- assert_true (check_iteration (transformed_tractogram ))
409- assert_equal (len (transformed_tractogram ), len (DATA ['streamlines' ]))
410- for s1 , s2 in zip (transformed_tractogram .streamlines ,
411- DATA ['streamlines' ]):
412- assert_array_almost_equal (s1 , s2 * scaling )
404+ check_tractogram (tractogram ,
405+ streamlines = [s * scaling for s in DATA ['streamlines' ]],
406+ data_per_streamline = DATA ['data_per_streamline' ],
407+ data_per_point = DATA ['data_per_point' ])
413408
414409 # Apply affine again and check the affine_to_rasmm.
415410 transformed_tractogram = tractogram .apply_affine (affine )
416411 assert_array_equal (transformed_tractogram .get_affine_to_rasmm (),
417412 np .dot (np .eye (4 ), np .dot (np .linalg .inv (affine ),
418413 np .linalg .inv (affine ))))
419414
415+ # Check that applying an affine and its inverse give us back the
416+ # original streamlines.
417+ tractogram = DATA ['tractogram' ].copy ()
418+ affine = np .random .RandomState (1234 ).randn (4 , 4 )
419+ affine [- 1 ] = [0 , 0 , 0 , 1 ] # Remove perspective projection.
420+
421+ tractogram .apply_affine (affine )
422+ tractogram .apply_affine (np .linalg .inv (affine ))
423+ assert_array_almost_equal (tractogram .get_affine_to_rasmm (), np .eye (4 ))
424+ for s1 , s2 in zip (tractogram .streamlines , DATA ['streamlines' ]):
425+ assert_array_almost_equal (s1 , s2 )
426+
427+ def test_tractogram_to_world (self ):
428+ tractogram = DATA ['tractogram' ].copy ()
429+ affine = np .random .RandomState (1234 ).randn (4 , 4 )
430+ affine [- 1 ] = [0 , 0 , 0 , 1 ] # Remove perspective projection.
431+
432+ # Apply the affine to the streamlines, then bring them back
433+ # to world space in a lazy manner.
434+ transformed_tractogram = tractogram .apply_affine (affine )
435+ assert_array_equal (transformed_tractogram .get_affine_to_rasmm (),
436+ np .linalg .inv (affine ))
437+
438+ tractogram_world = transformed_tractogram .to_world (lazy = True )
439+ assert_true (type (tractogram_world ) is LazyTractogram )
440+ assert_array_almost_equal (tractogram_world .get_affine_to_rasmm (),
441+ np .eye (4 ))
442+ for s1 , s2 in zip (tractogram_world .streamlines , DATA ['streamlines' ]):
443+ assert_array_almost_equal (s1 , s2 )
444+
445+ # Bring them back streamlines to world space in a in-place manner.
446+ tractogram_world = transformed_tractogram .to_world ()
447+ assert_true (tractogram_world is tractogram )
448+ assert_array_almost_equal (tractogram .get_affine_to_rasmm (), np .eye (4 ))
449+ for s1 , s2 in zip (tractogram .streamlines , DATA ['streamlines' ]):
450+ assert_array_almost_equal (s1 , s2 )
451+
452+ # Calling to_world twice should do nothing.
453+ tractogram_world2 = transformed_tractogram .to_world ()
454+ assert_true (tractogram_world2 is tractogram )
455+ assert_array_almost_equal (tractogram .get_affine_to_rasmm (), np .eye (4 ))
456+ for s1 , s2 in zip (tractogram .streamlines , DATA ['streamlines' ]):
457+ assert_array_almost_equal (s1 , s2 )
458+
420459
421460class TestLazyTractogram (unittest .TestCase ):
422461
@@ -531,18 +570,47 @@ def test_lazy_tractogram_apply_affine(self):
531570
532571 tractogram = DATA ['lazy_tractogram' ].copy ()
533572
534- tractogram .apply_affine (affine )
535- assert_true (check_iteration (tractogram ))
536- assert_equal (len (tractogram ), len (DATA ['streamlines' ]))
537- for s1 , s2 in zip (tractogram .streamlines , DATA ['streamlines' ]):
538- assert_array_almost_equal (s1 , s2 * scaling )
573+ transformed_tractogram = tractogram .apply_affine (affine )
574+ assert_true (transformed_tractogram is tractogram )
575+ assert_array_equal (tractogram ._affine_to_apply , affine )
576+ assert_array_equal (tractogram .get_affine_to_rasmm (),
577+ np .dot (np .eye (4 ), np .linalg .inv (affine )))
578+ check_tractogram (tractogram ,
579+ streamlines = [s * scaling for s in DATA ['streamlines' ]],
580+ data_per_streamline = DATA ['data_per_streamline' ],
581+ data_per_point = DATA ['data_per_point' ])
539582
540583 # Apply affine again and check the affine_to_rasmm.
541584 transformed_tractogram = tractogram .apply_affine (affine )
585+ assert_array_equal (tractogram ._affine_to_apply , np .dot (affine , affine ))
542586 assert_array_equal (transformed_tractogram .get_affine_to_rasmm (),
543587 np .dot (np .eye (4 ), np .dot (np .linalg .inv (affine ),
544588 np .linalg .inv (affine ))))
545589
590+ def test_tractogram_to_world (self ):
591+ tractogram = DATA ['lazy_tractogram' ].copy ()
592+ affine = np .random .RandomState (1234 ).randn (4 , 4 )
593+ affine [- 1 ] = [0 , 0 , 0 , 1 ] # Remove perspective projection.
594+
595+ # Apply the affine to the streamlines, then bring them back
596+ # to world space in a lazy manner.
597+ tractogram .apply_affine (affine )
598+ assert_array_equal (tractogram .get_affine_to_rasmm (),
599+ np .linalg .inv (affine ))
600+
601+ tractogram_world = tractogram .to_world ()
602+ assert_true (tractogram_world is tractogram )
603+ assert_array_almost_equal (tractogram .get_affine_to_rasmm (),
604+ np .eye (4 ))
605+ for s1 , s2 in zip (tractogram .streamlines , DATA ['streamlines' ]):
606+ assert_array_almost_equal (s1 , s2 )
607+
608+ # Calling to_world twice should do nothing.
609+ tractogram .to_world ()
610+ assert_array_almost_equal (tractogram .get_affine_to_rasmm (), np .eye (4 ))
611+ for s1 , s2 in zip (tractogram .streamlines , DATA ['streamlines' ]):
612+ assert_array_almost_equal (s1 , s2 )
613+
546614 def test_lazy_tractogram_copy (self ):
547615 # Create a copy of the lazy tractogram.
548616 tractogram = DATA ['lazy_tractogram' ].copy ()
0 commit comments