Permalink
Browse files

Pasted the clean and stable code in test_align.py from PR1598.

  • Loading branch information...
parichit committed Jul 31, 2018
1 parent 30a1641 commit 30ab5356ac1f03158eb3a619a2dbbf95cd00d832
Showing with 54 additions and 0 deletions.
  1. +54 −0 dipy/workflows/tests/test_align.py
@@ -1,5 +1,6 @@
import numpy.testing as npt

import numpy as np
import nibabel as nib
from nibabel.tmpdirs import TemporaryDirectory
from numpy.testing import run_module_suite
@@ -14,6 +15,7 @@
from dipy.align.transforms import Transform, regtransforms
from dipy.io.image import save_nifti
from dipy.workflows.align import ImageRegistrationFlow
from dipy.workflows.align import ApplyTransformFlow


def test_reslice():
@@ -154,5 +156,57 @@ def check_existense(movedfile, affine_mat_file):
test_err()


def test_apply_transform():
with TemporaryDirectory() as temp_out_dir:

factors = {
('TRANSLATION', 3): (2.0, None, np.array([2.3, 4.5, 1.7])),
('RIGID', 3): (0.1, None, np.array([0.1, 0.15, -0.11, 2.3, 4.5,
1.7])),
('AFFINE', 3): (0.1, None, np.array([0.99, -0.05, 0.03, 1.3,
0.05, 0.99, -0.10, 2.5,
-0.07, 0.10, 0.99, -1.4]))}

for i in factors.keys():
static, moving, static_g2w, moving_g2w, smask, mmask, M = \
setup_random_transform(transform=regtransforms[i],
rfactor=factors[i][0])

some = str(i[0]) + '_static.nii.gz'
one = str(i[0]) + '_moving.nii.gz'

save_nifti(pjoin(temp_out_dir, some), data=static,
affine=static_g2w)

save_nifti(pjoin(temp_out_dir, one), data=moving,
affine=moving_g2w)

static_image_file = pjoin(temp_out_dir, 'TRANSLATION_static.nii.gz')
moving_image_file = pjoin(temp_out_dir, 'TRANSLATION_moving.nii.gz')

out_moved = pjoin(temp_out_dir, "trans_moved.nii.gz")
out_affine = pjoin(temp_out_dir, "trans_affine.txt")

image_registeration_flow = ImageRegistrationFlow()

image_registeration_flow.run(static_image_file,
moving_image_file,
transform='trans',
out_dir=temp_out_dir,
out_moved=out_moved,
out_affine=out_affine,
level_iters=[100, 10, 1])

npt.assert_equal(os.path.exists(out_moved), True)
npt.assert_equal(os.path.exists(out_affine), True)

apply_trans = ApplyTransformFlow()
two = pjoin(temp_out_dir, '*moving*')
apply_trans.run(static_image_file, two, out_affine)

# Checking for the transformed file.
assert os.path.exists(pjoin(temp_out_dir, 'transformed.nii.gz'))


if __name__ == "__main__":
run_module_suite()

0 comments on commit 30ab535

Please sign in to comment.