Permalink
Browse files

Modifed the align.py workflow to include sYn_registration class.

  • Loading branch information...
parichit committed Jul 31, 2018
1 parent edce406 commit f222dbf94a44a3d2102093c83c4210ab2207e888
Showing with 119 additions and 39 deletions.
  1. +119 −39 dipy/workflows/align.py
@@ -12,6 +12,50 @@
AffineTransform3D
from dipy.io.image import save_nifti, load_nifti, load_affine_matrix, \
save_affine_matrix, save_quality_assur_metric
from dipy.align.imwarp import SymmetricDiffeomorphicRegistration
from dipy.align.metrics import CCMetric


class UtilMethods(object):

@staticmethod
def check_dimensions(static, moving):

"""
Check the dimensions of the input images.
Parameters
----------
static : array, shape (S, R, C) or (R, C)
the image to be used as reference during optimization.
moving: array, shape (S', R', C') or (R', C')
the image to be used as "moving" during optimization. It is
necessary to pre-align the moving image to ensure its domain
lies inside the domain of the deformation fields. This is assumed
to be accomplished by "pre-aligning" the moving image towards the
static using an affine transformation given by the
'starting_affine' matrix
"""
if len(static.shape) != len(moving.shape):
raise ValueError('Dimension mismatch: The'
' input images must have same number of '
'dimensions.')
@staticmethod
def check_metric(metric):
"""
Check the input metric type.
Parameters
----------
metric: string
The similarity metric.
(default 'MutualInformation' metric)
"""
if metric not in ['mi', 'cc']:
raise ValueError('Invalid similarity metric: Please provide'
' a valid metric.')


class ResliceFlow(Workflow):
@@ -326,45 +370,6 @@ def affine(self, static, static_grid2world, moving, moving_grid2world,
affreg, params0, transform,
affine)

@staticmethod
def check_dimensions(static, moving):

"""
Check the dimensions of the input images.
Parameters
----------
static : array, shape (S, R, C) or (R, C)
the image to be used as reference during optimization.
moving: array, shape (S', R', C') or (R', C')
the image to be used as "moving" during optimization. It is
necessary to pre-align the moving image to ensure its domain
lies inside the domain of the deformation fields. This is assumed
to be accomplished by "pre-aligning" the moving image towards the
static using an affine transformation given by the
'starting_affine' matrix
"""
if len(static.shape) != len(moving.shape):
raise ValueError('Dimension mismatch: The input images must '
'have same number of dimensions.')

@staticmethod
def check_metric(metric):
"""
Check the input metric type.
Parameters
----------
metric: string
The similarity metric.
(default 'MutualInformation' metric)
"""
if metric != 'mi':
raise ValueError('Invalid similarity metric: Please provide '
'a valid metric.')

def run(self, static_img_file, moving_img_file, transform='affine',
nbins=32, sampling_prop=None, metric='mi',
level_iters=[10000, 1000, 100], sigmas=[3.0, 1.0, 0.0],
@@ -578,3 +583,78 @@ def run(self, static_image_file, moving_image_files, affine_matrix_file,
transformed = img_transformation.transform(image_data)

save_nifti(out_dir+out_file, transformed, affine=static_grid2world)


class SynRegistrationFlow(Workflow):

def run(self, static_image_file, moving_image_file, affine_matrix_file,
level_iters=[10, 10, 5], metric="cc", out_dir='',
out_warped='warped_moved.nii.gz'):

"""
Parameters
----------
static_image_file : string
Path of the static image file.
moving_image_file : string
Path to the moving image file.
affine_matrix_file : string
The text file containing pre alignment information or the
affine matrix.
level_iters : variable int, optional
The number of iterations at each level of the gaussian pyramid.
By default, a 3-level scale space with iterations
sequence equal to [10, 10, 5] will be used. The 0-th
level corresponds to the finest resolution.
metric : string, optional
The metric to be used (Default cc, 'Cross Correlation metric').
out_dir : string, optional
Directory to save the transformed files (default '').
out_warped : string, optional
Name of the warped file. If no name is given then a
suffix 'transformed' will be appended to the name of the
original input file (default 'warped_moved.nii.gz').
"""

io = self.get_io_iterator()
util = UtilMethods()
util.check_metric(metric)

for static_file, moving_file, in_affine, \
warped_file in io:

print(static_file, moving_file, in_affine, warped_file)

# Loading the image data from the input files into object.
static_img_data = nib.load(static_file)
static_image = static_img_data.get_data()
static_grid2world = static_img_data.affine

moving_img_data = nib.load(moving_file)
moving_image = moving_img_data.get_data()
moving_grid2world = moving_img_data.affine

# Sanity check for the input image dimensions.
util.check_dimensions(static_image, moving_image)

# Loading the affine matrix.
affine_matrix = load_affine_matrix(in_affine)

metric = CCMetric(3)
sdr = SymmetricDiffeomorphicRegistration(metric, level_iters)

mapping = sdr.optimize(static_image, moving_image,
static_grid2world, moving_grid2world,
affine_matrix)

warped_moving = mapping.transform(moving_image)

# Saving the warped moving file and the alignment matrix.
save_nifti(warped_file, warped_moving, static_grid2world)

0 comments on commit f222dbf

Please sign in to comment.