Permalink
Browse files

Initial commit for the image registration code.

1) Developed a new workflow for image registration.
2) Developed a testing script for the workflow to replicate and validate run time output and error.

Future Work

1) Link the test script for automated execution in Nose.
2) Create a function to replicate error behavior for wrong input.
3) Add few more checks for validating the format and type of input parameters in the workflow.
  • Loading branch information...
Parichit Sharma
Parichit Sharma committed Jun 11, 2018
1 parent 845161e commit 6c15d3acd556966c0ce4fe43635b364a58ac7b3b
Showing with 124 additions and 0 deletions.
  1. +15 −0 dipy/io/image.py
  2. +109 −0 dipy/workflows/tests/test_image_registration.py
@@ -1,6 +1,7 @@
from __future__ import division, print_function, absolute_import

import nibabel as nib
import numpy as np


def load_nifti(fname, return_img=False, return_voxsize=False,
@@ -24,3 +25,17 @@ def load_nifti(fname, return_img=False, return_voxsize=False,
def save_nifti(fname, data, affine, hdr=None):
result_img = nib.Nifti1Image(data, affine, header=hdr)
result_img.to_filename(fname)


def save_affine_matrix(fname, affine):

"""
Parameters
---------
fname : str
File path to save the affine matrix.
affine : numpy array
The object containing the affine matrix.
"""
np.savetxt(fname, affine)

@@ -0,0 +1,109 @@
import os.path
from os.path import join as pjoin
import shutil
import numpy as np
import numpy.testing as npt

from time import sleep

from nibabel.tmpdirs import TemporaryDirectory
from dipy.align.tests.test_parzenhist import setup_random_transform
from dipy.align.transforms import (Transform,
regtransforms)
from dipy.io.image import save_nifti
from glob import glob

from dipy.workflows.image_registration import ImageRegistrationFlow

folder_path = '/Users/schmuck/Documents/register_workflow_test/test'

def test_image_registration():

image_registeration_flow = ImageRegistrationFlow()

with TemporaryDirectory() as temp_out_dir:

static, moving, static_g2w, moving_g2w, smask, mmask, M = setup_random_transform(
transform=regtransforms[('TRANSLATION', 3)], rfactor=2.0)

save_nifti(pjoin(folder_path,'mask.nii.gz'),data=smask,affine=static_g2w)

save_nifti(pjoin(temp_out_dir,'b0.nii.gz'), data=static,
affine=static_g2w)
save_nifti(pjoin(temp_out_dir,'t1.nii.gz'), data=moving,
affine=moving_g2w)

static_image_file = pjoin(temp_out_dir,'b0.nii.gz')
moving_image_file = pjoin(temp_out_dir,'t1.nii.gz')

out_moved = pjoin(temp_out_dir, "com_moved.nii.gz")
out_affine = pjoin(temp_out_dir, "com_affine.txt")

image_registeration_flow.run(str(static_image_file),
str(moving_image_file),
transform='com',
out_dir=temp_out_dir
,out_moved=out_moved,
out_affine=out_affine)

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

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

print('trans')
image_registeration_flow.run(str(static_image_file),
str(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)

out_moved = pjoin(temp_out_dir, "rigid_moved.nii.gz")
out_affine = pjoin(temp_out_dir, "rigid_affine.txt")

image_registeration_flow.run(str(static_image_file),
str(moving_image_file),
transform='rigid',
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)

out_moved = pjoin(temp_out_dir, "affine_moved.nii.gz")
out_affine = pjoin(temp_out_dir, "affine_affine.txt")

print('affine')

image_registeration_flow.run(str(static_image_file),
str(moving_image_file),
transform='affine',
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)

copy_output(temp_out_dir,folder_path)


def copy_output(temp_directory_path, folder_path):

out_files = list(glob(pjoin(temp_directory_path,'*.nii.gz')) + glob(pjoin(temp_directory_path,'*.txt')))

for out_file in out_files:
shutil.copy(out_file, folder_path)


if __name__ == '__main__':
test_image_registration()

0 comments on commit 6c15d3a

Please sign in to comment.