Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
committed
Jun 11, 2018
1 parent
845161e
commit 6c15d3a
Showing
2 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |