This file is copied (possibly with major modifications) from the
sources of the pycpd project - siavashk/pycpd.
It remains licensed as the rest of PyCPD (MIT license as of October 2010).
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
#
# See COPYING file distributed along with the PyCPD package for the
# copyright and license terms.
#
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
Affine image registration module consisting of the following classes:
AffineMap: encapsulates the necessary information to perform affine
transforms between two domains, defined by a static and a moving
image. The domain of the transform is the set of points in the
static image’s grid, and the codomain is the set of points in
the moving image. When we call the transform method, AffineMap
maps each point x of the domain (static grid) to the codomain
(moving grid) and interpolates the moving image at that point
to obtain the intensity value to be placed at x in the resulting
grid. The transform_inverse method performs the opposite operation
mapping points in the codomain to points in the domain.
ParzenJointHistogram: computes the marginal and joint distributions of
intensities of a pair of images, using Parzen windows
[1] with a cubic spline kernel, as proposed by
Mattes et al.[2]. It also computes the gradient of the joint
histogram w.r.t. the parameters of a given transform.
MutualInformationMetric: computes the value and gradient of the mutual
information metric the way Optimizer needs them. That is, given
a set of transform parameters, it will use ParzenJointHistogram
to compute the value and gradient of the joint intensity histogram
evaluated at the given parameters, and evaluate the value and
gradient of the histogram’s mutual information.
AffineRegistration: it runs the multi-resolution registration, putting
all the pieces together. It needs to create the scale space of the
images and run the multi-resolution registration by using the Metric
and the Optimizer at each level of the Gaussian pyramid. At each
level, it will setup the metric to compute value and gradient of the
metric with the input images with different levels of smoothing.
Register a 2D/3D source image (moving) to a 2D/3D target image (static).
Parameters:
moving, staticarray or nib.Nifti1Image or str.
Either as a 2D/3D array or as a nifti image object, or as
a string containing the full path to a nifti file.
moving_affine, static_affine4x4 array, optional.
Must be provided for data provided as an array. If provided together
with Nifti1Image or str data, this input will over-ride the affine
that is stored in the data input. Default: use the affine stored
in data.
metricstring, optional
The metric to be optimized. One of CC, EM, SSD,
Default: ‘CC’ => CCMetric.
dim: int (either 2 or 3), optional
The dimensions of the image domain. Default: 3
level_iterslist of int, optional
the number of iterations at each level of the Gaussian Pyramid (the
length of the list defines the number of pyramid levels to be
used). Default: [10, 10, 5].
metric_kwargsdict, optional
Parameters for initialization of the metric object. If not provided,
uses the default settings of each metric.
Returns:
warped_movingndarray
The data in moving, warped towards the static data.
forwardndarray (…, 3)
The vector field describing the forward warping from the source to the
target.
backwardndarray (…, 3)
The vector field describing the backward warping from the target to the
source.
Register DWI data to a template through the B0 volumes.
Parameters:
dwi4D array, nifti image or str
Containing the DWI data, or full path to a nifti file with DWI.
gtabGradientTable or sequence of strings
The gradients associated with the DWI data, or a sequence with
(fbval, fbvec), full paths to bvals and bvecs files.
dwi_affine4x4 array, optional
An affine transformation associated with the DWI. Required if data
is provided as an array. If provided together with nifti/path,
will over-ride the affine that is in the nifti.
template3D array, nifti image or str
Containing the data for the template, or full path to a nifti file
with the template data.
template_affine4x4 array, optional
An affine transformation associated with the template. Required if data
is provided as an array. If provided together with nifti/path,
will over-ride the affine that is in the nifti.
reg_methodstr,
One of “syn” or “aff”, which designates which registration method is
used. Either syn, which uses the syn_registration() function
or affine_registration() function. Default: “syn”.
If reg_method is “syn”, a DiffeomorphicMap class instance that can be
used to transform between the two spaces. Otherwise, if reg_method is
“aff”, a 4x4 matrix encoding the affine transform.
Notes
This function assumes that the DWI data is already internally registered.
See register_dwi_series().
The data in the file is organized with shape (X, Y, Z, 3, 2), such
that the forward mapping in each voxel is in data[i, j, k, :, 0] and
the backward mapping in each voxel is in data[i, j, k, :, 1].
Resample an image (moving) from one space to another (static).
Parameters:
movingarray, nifti image or str
Containing the data for the moving object, or full path to a nifti file
with the moving data.
staticarray, nifti image or str
Containing the data for the static object, or full path to a nifti file
with the moving data.
moving_affine4x4 array, optional
An affine transformation associated with the moving object. Required if
data is provided as an array. If provided together with nifti/path,
will over-ride the affine that is in the nifti.
static_affine4x4 array, optional
An affine transformation associated with the static object. Required if
data is provided as an array. If provided together with nifti/path,
will over-ride the affine that is in the nifti.
between_affine: 4x4 array, optional
If an additional affine is needed between the two spaces.
Default: identity (no additional registration).
Returns:
A Nifti1Image class instance with the data from the moving object
Find the affine transformation between two 3D images. Alternatively, find
the combination of several linear transformations.
Parameters:
movingarray, nifti image or str
Containing the data for the moving object, or full path to a nifti file
with the moving data.
staticarray, nifti image or str
Containing the data for the static object, or full path to a nifti file
with the moving data.
moving_affine4x4 array, optional
An affine transformation associated with the moving object. Required if
data is provided as an array. If provided together with nifti/path,
will over-ride the affine that is in the nifti.
static_affine4x4 array, optional
An affine transformation associated with the static object. Required if
data is provided as an array. If provided together with nifti/path,
will over-ride the affine that is in the nifti.
pipelinelist of str, optional
Sequence of transforms to use in the gradual fitting. Default: gradual
fit of the full affine (executed from left to right):
["center_of_mass","translation","rigid","affine"]
Alternatively, any other combination of the following registration
methods might be used: center_of_mass, translation, rigid,
rigid_isoscaling, rigid_scaling and affine.
starting_affine: 4x4 array, optional
Initial guess for the transformation between the spaces.
Default: identity.
metricstr, optional.
Currently only supports ‘MI’ for MutualInformationMetric.
level_iterssequence, optional
AffineRegistration key-word argument: the number of iterations at each
scale of the scale space. level_iters[0] corresponds to the coarsest
scale, level_iters[-1] the finest, where n is the length of the
sequence. By default, a 3-level scale space with iterations
sequence equal to [10000, 1000, 100] will be used.
sigmassequence of floats, optional
AffineRegistration key-word argument: custom smoothing parameter to
build the scale space (one parameter for each scale). By default,
the sequence of sigmas will be [3, 1, 0].
factorssequence of floats, optional
AffineRegistration key-word argument: custom scale factors to build the
scale space (one factor for each scale). By default, the sequence of
factors will be [4, 2, 1].
ret_metricboolean, optional
Set it to True to return the value of the optimized coefficients and
the optimization quality metric.
moving_maskarray, shape (S’, R’, C’) or (R’, C’), optional
moving image mask that defines which pixels in the moving image
are used to calculate the mutual information.
static_maskarray, shape (S, R, C) or (R, C), optional
static image mask that defines which pixels in the static image
are used to calculate the mutual information.
nbinsint, optional
MutualInformationMetric key-word argument: the number of bins to be
used for computing the intensity histograms. The default is 32.
sampling_proportionNone or float in interval (0, 1], optional
MutualInformationMetric key-word argument: There are two types of
sampling: dense and sparse. Dense sampling uses all voxels for
estimating the (joint and marginal) intensity histograms, while
sparse sampling uses a subset of them. If sampling_proportion is
None, then dense sampling is used. If sampling_proportion is a
floating point value in (0,1] then sparse sampling is used,
where sampling_proportion specifies the proportion of voxels to
be used. The default is None (dense sampling).
Returns:
resampledarray with moving data resampled to the static space
after computing the affine transformation.
final_affinethe affine 4x4 associated with the transformation.
xoptthe value of the optimized coefficients.
foptthe value of the optimization quality metric.
Notes
Performs a gradual registration between the two inputs, using a pipeline
that gradually approximates the final registration. If the final default
step (affine) is omitted, the resulting affine may not have all 12
degrees of freedom adjusted.
series4D array or nib.Nifti1Image class instance or str
The data is 4D with the last dimension separating different 3D volumes
refint or 3D array or nib.Nifti1Image class instance or str
If this is an int, this is the index of the reference image within the
series. Otherwise it is an array of data to register with (associated
with a ref_affine required) or a nifti img or full path to a file
containing one.
pipelinesequence, optional
Sequence of transforms to do for each volume in the series.
Default: (executed from left to right):
[center_of_mass, translation, rigid, affine]
series_affine, ref_affine4x4 arrays, optional.
The affine. If provided, this input will over-ride the affine provided
together with the nifti img or file.
static_maskarray, shape (S, R, C) or (R, C), optional
static image mask that defines which pixels in the static image
are used to calculate the mutual information.
Returns:
xformed, affines4D array with transformed data and a (4,4,n) array
with 4x4 matrices associated with each of the volumes of the input moving
data that was used to transform it into register with the static data.
Register a DWI series to the mean of the B0 images in that series.
all first registered to the first B0 volume
Parameters:
data4D array or nibabel Nifti1Image class instance or str
Diffusion data. Either as a 4D array or as a nifti image object, or as
a string containing the full path to a nifti file.
gtaba GradientTable class instance or tuple of strings
If provided as a tuple of strings, these are assumed to be full paths
to the bvals and bvecs files (in that order).
affine4x4 array, optional.
Must be provided for data provided as an array. If provided together
with Nifti1Image or str data, this input will over-ride the affine
that is stored in the data input. Default: use the affine stored
in data.
b0_refint, optional.
Which b0 volume to use as reference. Default: 0
pipelinelist of callables, optional.
The transformations to perform in sequence (from left to right):
Default: [center_of_mass,translation,rigid,affine]
static_maskarray, shape (S, R, C) or (R, C), optional
static image mask that defines which pixels in the static image
are used to calculate the mutual information.
Returns:
xform_img, affine_array: a Nifti1Image containing the registered data and
using the affine of the original data and a list containing the affine
Apply a motion correction to a DWI dataset (Between-Volumes Motion correction)
Parameters:
data4D array or nibabel Nifti1Image class instance or str
Diffusion data. Either as a 4D array or as a nifti image object, or as
a string containing the full path to a nifti file.
gtaba GradientTable class instance or tuple of strings
If provided as a tuple of strings, these are assumed to be full paths
to the bvals and bvecs files (in that order).
affine4x4 array, optional.
Must be provided for data provided as an array. If provided together
with Nifti1Image or str data, this input will over-ride the affine
that is stored in the data input. Default: use the affine stored
in data.
b0_refint, optional.
Which b0 volume to use as reference. Default: 0
pipelinelist of callables, optional.
The transformations to perform in sequence (from left to right):
Default: [center_of_mass,translation,rigid,affine]
static_maskarray, shape (S, R, C) or (R, C), optional
static image mask that defines which pixels in the static image
are used to calculate the mutual information.
Returns:
xform_img, affine_array: a Nifti1Image containing the registered data and
using the affine of the original data and a list containing the affine
Update a point cloud using the new estimate of the deformable
transformation.
Parameters:
Y: numpy array, optional
Array of points to transform - use to predict on new set of points.
Best for predicting on new points not used to run initial
registration. If None, self.Y used.
Set the affine transform (operating in physical space).
Also sets self.affine_inv - the inverse of affine, or None if
there is no inverse.
Parameters:
affinearray, shape (dim + 1, dim + 1)
the matrix representing the affine transform operating in
physical space. The domain and co-domain information
remains unchanged. If None, then self represents the identity
transformation.
Transform the input image from co-domain to domain space.
By default, the transformed image is sampled at a grid defined by
self.domain_shape and self.domain_grid2world. If such
information was not provided then sampling_grid_shape is mandatory.
Parameters:
image2D or 3D array
the image to be transformed
interpolationstring, either ‘linear’ or ‘nearest’
the type of interpolation to be used, either ‘linear’
(for k-linear interpolation) or ‘nearest’ for nearest neighbor
image_grid2worldarray, shape (dim + 1, dim + 1), optional
the grid-to-world transform associated with image.
If None (the default), then the grid-to-world transform is assumed
to be the identity.
the shape of the grid where the transformed image must be sampled.
If None (the default), then self.codomain_shape is used instead
(which must have been set at initialization, otherwise an exception
will be raised).
sampling_grid2worldarray, shape (dim + 1, dim + 1), optional
the grid-to-world transform associated with the sampling grid
(specified by sampling_grid_shape, or by default
self.codomain_shape). If None (the default), then the
grid-to-world transform is assumed to be the identity.
resample_onlyBoolean, optional
If False (the default) the affine transform is applied normally.
If True, then the affine transform is not applied, and the input
image is just re-sampled on the domain grid of this transform.
Returns:
transformedarray
the transformed image, sampled at the requested grid, with shape
sampling_grid_shape or self.codomain_shape.
Transform the input image from domain to co-domain space.
By default, the transformed image is sampled at a grid defined by
self.codomain_shape and self.codomain_grid2world. If such
information was not provided then sampling_grid_shape is mandatory.
Parameters:
image2D or 3D array
the image to be transformed
interpolationstring, either ‘linear’ or ‘nearest’
the type of interpolation to be used, either ‘linear’
(for k-linear interpolation) or ‘nearest’ for nearest neighbor
image_grid2worldarray, shape (dim + 1, dim + 1), optional
the grid-to-world transform associated with image.
If None (the default), then the grid-to-world transform is assumed
to be the identity.
the shape of the grid where the transformed image must be sampled.
If None (the default), then self.codomain_shape is used instead
(which must have been set at initialization, otherwise an exception
will be raised).
sampling_grid2worldarray, shape (dim + 1, dim + 1), optional
the grid-to-world transform associated with the sampling grid
(specified by sampling_grid_shape, or by default
self.codomain_shape). If None (the default), then the
grid-to-world transform is assumed to be the identity.
resample_onlyBoolean, optional
If False (the default) the affine transform is applied normally.
If True, then the affine transform is not applied, and the input
image is just re-sampled on the domain grid of this transform.
Returns:
transformedarray
the transformed image, sampled at the requested grid, with shape
sampling_grid_shape or self.codomain_shape.
We need to change the sign so we can use standard minimization
algorithms.
Parameters:
paramsarray, shape (n,)
the parameter vector of the transform currently used by the metric
(the transform name is provided when self.setup is called), n is
the number of parameters of the transform
Returns:
neg_mifloat
the negative mutual information of the input images after
transforming the moving image by the currently set transform
with params parameters
Numeric value of the metric and its gradient at given parameters.
Parameters:
paramsarray, shape (n,)
the parameter vector of the transform currently used by the metric
(the transform name is provided when self.setup is called), n is
the number of parameters of the transform
Returns:
neg_mifloat
the negative mutual information of the input images after
transforming the moving image by the currently set transform
with params parameters
Numeric value of the metric’s gradient at the given parameters.
Parameters:
paramsarray, shape (n,)
the parameter vector of the transform currently used by the metric
(the transform name is provided when self.setup is called), n is
the number of parameters of the transform
the pre-aligning matrix (an affine transform) that roughly aligns
the moving image towards the static image. If None, no
pre-alignment is performed. If a pre-alignment matrix is available,
it is recommended to provide this matrix as starting_affine
instead of manually transforming the moving image to reduce
interpolation artifacts. The default is None, implying no
pre-alignment is performed.
static_maskarray, shape (S, R, C) or (R, C), optional
static image mask that defines which pixels in the static image
are used to calculate the mutual information.
moving_maskarray, shape (S’, R’, C’) or (R’, C’), optional
moving image mask that defines which pixels in the moving image
are used to calculate the mutual information.
the image to be used as reference during optimization.
moving2D or 3D array
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
transforminstance of Transform
the transformation with respect to whose parameters the gradient
must be computed
params0array, shape (n,)
parameters from which to start the optimization. If None, the
optimization will start at the identity transform. n is the
number of parameters of the specified transformation.
the voxel-to-space transformation associated with the moving
image. The default is None, implying the transform is the
identity.
starting_affinestring, or matrix, or None, optional
If string:
‘mass’: align centers of gravity
‘voxel-origin’: align physical coordinates of voxel (0,0,0)
‘centers’: align physical coordinates of central voxels
If matrix:
array, shape (dim+1, dim+1).
If None:
Start from identity.
ret_metricboolean, optional
if True, it returns the parameters for measuring the
similarity between the images (default ‘False’).
The metric containing optimal parameters and
the distance between the images.
static_maskarray, shape (S, R, C) or (R, C), optional
static image mask that defines which pixels in the static image
are used to calculate the mutual information.
moving_maskarray, shape (S’, R’, C’) or (R’, C’), optional
moving image mask that defines which pixels in the moving image
are used to calculate the mutual information.
Returns:
affine_mapinstance of AffineMap
the affine resulting affine transformation
xoptoptimal parameters
the optimal parameters (translation, rotation shear etc.)
foptSimilarity metric
the value of the function at the optimal parameters.
Estimates the inversion error of the displacement fields by computing
statistics of the residual vectors obtained after composing the forward
and backward displacement fields.
Returns:
residualarray, shape (R, C) or (S, R, C)
the displacement field resulting from composing the forward and
backward displacement fields of this transformation (the residual
should be zero for a perfect diffeomorphism)
statsarray, shape (3,)
statistics from the norms of the vectors of the residual
displacement field: maximum, mean and standard deviation
Notes
Since the forward and backward displacement fields have the same
discretization, the final composition is given by
comp[i] = forward[ i + Dinv * backward[i]]
where Dinv is the space-to-grid transformation of the displacement
fields
Constructs a simplified version of this Diffeomorhic Map
The simplified version incorporates the pre-align transform, as well as
the domain and codomain affine transforms into the displacement field.
The resulting transformation may be regarded as operating on the
image spaces given by the domain and codomain discretization. As a
result, self.prealign, self.disp_grid2world, self.domain_grid2world and
self.codomain affine will be None (denoting Identity) in the resulting
diffeomorphic map.
Some operations are performed faster if we know in advance if a matrix
is the identity (so we can skip the actual matrix-vector
multiplication). This function returns None if the given object
is None or the ‘identity’ string. It returns the same object if it is
a numpy array. It raises an exception otherwise.
Parameters:
objobject
any object
Returns:
objobject
the same object given as argument if obj is None or a numpy
array. None if obj is the ‘identity’ string.
Transforms the input image under this transformation in the forward
direction. It uses the “is_inverse” flag to switch between “forward”
and “backward” (if is_inverse is False, then transform(…) warps the
image forwards, else it warps the image backwards).
Parameters:
imagearray, shape (s, r, c) if dim = 3 or (r, c) if dim = 2
the image to be warped under this transformation in the forward
direction
interpolationstring, either ‘linear’ or ‘nearest’
the type of interpolation to be used for warping, either ‘linear’
(for k-linear interpolation) or ‘nearest’ for nearest neighbor
image_world2gridarray, shape (dim+1, dim+1)
the transformation bringing world (space) coordinates to voxel
coordinates of the image given as input
out_shapearray, shape (dim,)
the number of slices, rows and columns of the desired warped image
out_grid2worldthe transformation bringing voxel coordinates of the
warped image to physical space
Returns:
warpedarray, shape = out_shape or self.codomain_shape if None
the warped image under this transformation in the forward direction
Notes
See _warp_forward and _warp_backward documentation for further
information.
Transforms the input image under this transformation in the backward
direction. It uses the “is_inverse” flag to switch between “forward”
and “backward” (if is_inverse is False, then transform_inverse(…)
warps the image backwards, else it warps the image forwards)
Parameters:
imagearray, shape (s, r, c) if dim = 3 or (r, c) if dim = 2
the image to be warped under this transformation in the forward
direction
interpolationstring, either ‘linear’ or ‘nearest’
the type of interpolation to be used for warping, either ‘linear’
(for k-linear interpolation) or ‘nearest’ for nearest neighbor
image_world2gridarray, shape (dim+1, dim+1)
the transformation bringing world (space) coordinates to voxel
coordinates of the image given as input
out_shapearray, shape (dim,)
the number of slices, rows, and columns of the desired warped image
out_grid2worldthe transformation bringing voxel coordinates of the
warped image to physical space
Returns:
warpedarray, shape = out_shape or self.codomain_shape if None
warped image under this transformation in the backward direction
Notes
See _warp_forward and _warp_backward documentation for further
information.
Applies this diffeomorphic map to the list of points (or streamlines)
given by points. We assume that the points’ coordinates are mapped
to world coordinates by applying the coord2world affine transform.
The warped coordinates are given in world coordinates unless
world2coord affine transform is specified, in which case the warped
points will be transformed to the corresponding coordinate system.
Warp the list of points in the backward direction.
Applies this diffeomorphic map to the list of points (or streamlines)
given by points. We assume that the points’ coordinates are mapped
to world coordinates by applying the coord2world affine transform.
The warped coordinates are given in world coordinates unless
world2coord affine transform is specified, in which case the warped
points will be transformed to the corresponding coordinate system.
Composition of this DiffeomorphicMap with a given endomorphism
Creates a new DiffeomorphicMap C with the same properties as self and
composes its displacement fields with phi’s corresponding fields.
The resulting diffeomorphism is of the form C(x) = phi(self(x)) with
inverse C^{-1}(y) = self^{-1}(phi^{-1}(y)). We assume that phi is an
endomorphism with the same discretization and domain affine as self
to ensure that the composition inherits self’s properties (we also
assume that the pre-aligning matrix of phi is None or identity).
Parameters:
phiDiffeomorphicMap object
the endomorphism to be warped by this diffeomorphic map
Returns:
compositionthe composition of this diffeomorphic map with the
endomorphism given as input
Notes
The problem with our current representation of a DiffeomorphicMap is
that the set of Diffeomorphism that can be represented this way (a
pre-aligning matrix followed by a non-linear endomorphism given as a
displacement field) is not closed under the composition operation.
Supporting a general DiffeomorphicMap class, closed under composition,
may be extremely costly computationally, and the kind of
transformations we actually need for Avants’ mid-point algorithm (SyN)
are much simpler.
This is the main function each specialized class derived from this must
implement. Upon completion, the deformation field must be available
from the forward transformation model.
Sets the number of iterations at each pyramid level
Establishes the maximum number of iterations to be performed at each
level of the Gaussian pyramid, similar to ANTS.
Parameters:
level_iterslist
the number of iterations at each level of the Gaussian pyramid.
level_iters[0] corresponds to the finest level, level_iters[n-1]
the coarsest, where n is the length of the list
the image to be used as reference during optimization. The
displacement fields will have the same discretization as the static
image.
movingarray, shape (S, R, C) or (R, C)
the image to be used as “moving” during optimization. Since the
deformation fields’ discretization is the same as the static image,
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 ‘prealign’
matrix
static_grid2worldarray, shape (dim+1, dim+1)
the voxel-to-space transformation associated to the static image
moving_grid2worldarray, shape (dim+1, dim+1)
the voxel-to-space transformation associated to the moving image
prealignarray, shape (dim+1, dim+1)
the affine transformation (operating on the physical space)
pre-aligning the moving image towards the static
Returns:
static_to_refDiffeomorphicMap object
the diffeomorphic map that brings the moving image towards the
static one in the forward direction (i.e. by calling
static_to_ref.transform) and the static image towards the
moving one in the backward direction (i.e. by calling
static_to_ref.transform_inverse).
Composition of the current displacement field with the given field
Interpolates new displacement at the locations defined by
current_displacement. Equivalently, computes the composition C of the
given displacement fields as C(x) = B(A(x)), where A is
current_displacement and B is new_displacement. This function is
intended to be used with deformation fields of the same sampling
(e.g. to be called by a registration algorithm).
Parameters:
current_displacementarray, shape (R’, C’, 2) or (S’, R’, C’, 3)
the displacement field defining where to interpolate
new_displacement
new_displacementarray, shape (R, C, 2) or (S, R, C, 3)
the displacement field to be warped by current_displacement
disp_world2gridarray, shape (dim+1, dim+1)
the space-to-grid transform associated with the displacements’
grid (we assume that both displacements are discretized over the
same grid)
time_scalingfloat
scaling factor applied to d2. The effect may be interpreted as
moving d1 displacements along a factor (time_scaling) of d2.
Returns:
updatedarray, shape (the same as new_displacement)
the warped displacement field
mean_normthe mean norm of all vectors in current_displacement
Extracts the rotational and spacing components from a matrix
Extracts the rotational and spacing (voxel dimensions) components from a
matrix. An image gradient represents the local variation of the image’s
gray values per voxel. Since we are iterating on the physical space, we
need to compute the gradients as variation per millimeter, so we need to
divide each gradient’s component by the voxel size along the corresponding
axis, that’s what the spacings are used for. Since the image’s gradients
are oriented along the grid axes, we also need to re-orient the gradients
to be given in physical space coordinates.
Parameters:
affinearray, shape (k, k), k = 3, 4
the matrix transforming grid coordinates to physical space.
Releases the resources no longer needed by the metric
This method is called by the RegistrationOptimizer after the required
iterations have been computed (forward and / or backward) so that the
SimilarityMetric can safely delete any data it computed as part of the
initialization
Prepares the metric to compute one displacement field iteration.
This method will be called before any compute_forward or
compute_backward call, this allows the Metric to pre-compute any useful
information for speeding up the update computations. This
initialization was needed in ANTS because the updates are called once
per voxel. In Python this is unpractical, though.
Informs the metric how many pyramid levels are above the current one
Informs this metric the number of pyramid levels above the current one.
The metric may change its behavior (e.g. number of inner iterations)
accordingly
Parameters:
levelsint
the number of levels above the current Gaussian Pyramid level
Informs the metric how many pyramid levels are below the current one
Informs this metric the number of pyramid levels below the current one.
The metric may change its behavior (e.g. number of inner iterations)
accordingly
Parameters:
levelsint
the number of levels below the current Gaussian Pyramid level
Sets the moving image being compared against the static one.
Sets the moving image. The default behavior (of this abstract class) is
simply to assign the reference to an attribute, but
generalizations of the metric may need to perform other operations
Sets the static image being compared against the moving one.
Sets the static image. The default behavior (of this abstract class) is
simply to assign the reference to an attribute, but
generalizations of the metric may need to perform other operations
This is called by the optimizer just after setting the moving image
This method allows the metric to compute any useful
information from knowing how the current static image was generated
(as the transformation of an original static image). This method is
called by the optimizer just after it sets the static image.
Transformation will be an instance of DiffeomorficMap or None if
the original_moving_image equals self.moving_image.
Parameters:
original_moving_imagearray, shape (R, C) or (S, R, C)
original image from which the current moving image was generated
transformationDiffeomorphicMap object
the transformation that was applied to the original image to generate
the current moving image
This is called by the optimizer just after setting the static image.
This method allows the metric to compute any useful
information from knowing how the current static image was generated
(as the transformation of an original static image). This method is
called by the optimizer just after it sets the static image.
Transformation will be an instance of DiffeomorficMap or None
if the original_static_image equals self.moving_image.
Parameters:
original_static_imagearray, shape (R, C) or (S, R, C)
original image from which the current static image was generated
transformationDiffeomorphicMap object
the transformation that was applied to original image to generate
the current static image
Prepares the metric to compute one displacement field iteration.
Pre-computes the cross-correlation factors for efficient computation
of the gradient of the Cross Correlation w.r.t. the displacement field.
It also pre-computes the image gradients in the physical space by
re-orienting the gradients in the voxel space using the corresponding
affine transformations.
if True, computes the Demons step in the forward direction
(warping the moving towards the static image). If False,
computes the backward step (warping the static image to the
moving image)
Returns:
displacementarray, shape (R, C, 2) or (S, R, C, 3)
Computes the Gauss-Newton energy minimization step
Computes the Newton step to minimize this energy, i.e., minimizes the
linearized energy function with respect to the
regularized displacement field (this step does not require
post-smoothing, as opposed to the demons step, which does not include
regularization). To accelerate convergence we use the multi-grid
Gauss-Seidel algorithm proposed by Bruhn and Weickert[3].
Parameters:
forward_stepboolean
if True, computes the Newton step in the forward direction
(warping the moving towards the static image). If False,
computes the backward step (warping the static image to the
moving image)
Returns:
displacementarray, shape (R, C, 2) or (S, R, C, 3)
Prepares the metric to compute one displacement field iteration.
Pre-computes the transfer functions (hidden random variables) and
variances of the estimators. Also pre-computes the gradient of both
input images. Note that once the images are transformed to the opposite
modality, the gradient of the transformed images can be used with the
gradient of the corresponding modality in the same fashion as
diff-demons does for mono-modality images. If the flag
self.use_double_gradient is True these gradients are averaged.
This is called by the optimizer just after setting the moving image.
EMMetric takes advantage of the image dynamics by computing the
current moving image mask from the original_moving_image mask (warped
by nearest neighbor interpolation)
Parameters:
original_moving_imagearray, shape (R, C) or (S, R, C)
the original moving image from which the current moving image was
generated, the current moving image is the one that was provided
via ‘set_moving_image(…)’, which may not be the same as the
original moving image but a warped version of it.
transformationDiffeomorphicMap object
the transformation that was applied to the original_moving_image
to generate the current moving image
This is called by the optimizer just after setting the static image.
EMMetric takes advantage of the image dynamics by computing the
current static image mask from the originalstaticImage mask (warped
by nearest neighbor interpolation)
Parameters:
original_static_imagearray, shape (R, C) or (S, R, C)
the original static image from which the current static image was
generated, the current static image is the one that was provided
via ‘set_static_image(…)’, which may not be the same as the
original static image but a warped version of it (even the static
image changes during Symmetric Normalization, not only the moving
one).
transformationDiffeomorphicMap object
the transformation that was applied to the original_static_image
to generate the current static image
Computes the demons step proposed by Vercauteren et al.[4] for
the SSD metric.
Parameters:
forward_stepboolean
if True, computes the Demons step in the forward direction
(warping the moving towards the static image). If False,
computes the backward step (warping the static image to the
moving image)
Returns:
displacementarray, shape (R, C, 2) or (S, R, C, 3)
Computes the Gauss-Newton energy minimization step
Minimizes the linearized energy function (Newton step) defined by the
sum of squared differences of corresponding pixels of the input images
with respect to the displacement field.
Parameters:
forward_stepboolean
if True, computes the Newton step in the forward direction
(warping the moving towards the static image). If False,
computes the backward step (warping the static image to the
moving image)
Multi-resolution Gauss-Seidel solver using V-type cycles
Multi-resolution Gauss-Seidel solver: solves the Gauss-Newton linear system
by first filtering (GS-iterate) the current level, then solves for the
residual at a coarser resolution and finally refines the solution at the
current resolution. This scheme corresponds to the V-cycle proposed by
Bruhn and Weickert[3].
Parameters:
nint
number of levels of the multi-resolution algorithm (it will be called
recursively until level n == 0)
kint
the number of iterations at each multi-resolution level
delta_fieldarray, shape (R, C)
the difference between the static and moving image (the ‘derivative
w.r.t. time’ in the optical flow model)
sigma_sq_fieldarray, shape (R, C)
the variance of the gray level value at each voxel, according to the
EM model (for SSD, it is 1 for all voxels). Inf and 0 values
are processed specially to support infinite and zero variance.
gradient_fieldarray, shape (R, C, 2)
the gradient of the moving image
targetarray, shape (R, C, 2)
right-hand side of the linear system to be solved in the Weickert’s
multi-resolution algorithm
lambda_paramfloat
smoothness parameter, the larger its value the smoother the
displacement field
displacementarray, shape (R, C, 2)
the displacement field to start the optimization from
Returns:
energythe energy of the EM (or SSD if sigmafield[…]==1) metric at this
Multi-resolution Gauss-Seidel solver using V-type cycles
Multi-resolution Gauss-Seidel solver: solves the linear system by first
filtering (GS-iterate) the current level, then solves for the residual
at a coarser resolution and finally refines the solution at the current
resolution. This scheme corresponds to the V-cycle proposed by
Bruhn and Weickert[3].
Parameters:
nint
number of levels of the multi-resolution algorithm (it will be called
recursively until level n == 0)
kint
the number of iterations at each multi-resolution level
delta_fieldarray, shape (S, R, C)
the difference between the static and moving image (the ‘derivative
w.r.t. time’ in the optical flow model)
sigma_sq_fieldarray, shape (S, R, C)
the variance of the gray level value at each voxel, according to the
EM model (for SSD, it is 1 for all voxels). Inf and 0 values
are processed specially to support infinite and zero variance.
gradient_fieldarray, shape (S, R, C, 3)
the gradient of the moving image
targetarray, shape (S, R, C, 3)
right-hand side of the linear system to be solved in the Weickert’s
multi-resolution algorithm
lambda_paramfloat
smoothness parameter, the larger its value the smoother the
displacement field
displacementarray, shape (S, R, C, 3)
the displacement field to start the optimization from
Returns:
energythe energy of the EM (or SSD if sigmafield[…]==1) metric at this
Reslice data with new voxel resolution defined by new_zooms.
Parameters:
dataarray, shape (I,J,K) or (I,J,K,N)
3d volume or 4d volume with datasets
affinearray, shape (4,4)
mapping from voxel coordinates to world coordinates
zoomstuple, shape (3,)
voxel size for (i,j,k) dimensions
new_zoomstuple, shape (3,)
new voxel size for (i,j,k) after resampling
orderint, from 0 to 5
order of interpolation for resampling/reslicing,
0 nearest interpolation, 1 trilinear etc..
if you don’t want any smoothing 0 is the option you need.
modestring (‘constant’, ‘nearest’, ‘reflect’ or ‘wrap’)
Points outside the boundaries of the input are filled according
to the given mode.
cvalfloat
Value used for points outside the boundaries of the input if
mode=’constant’.
num_processesint, optional
Split the calculation to a pool of children processes. This only
applies to 4D data arrays. Default is 1. If < 0 the maximal number
of cores minus num_processes+1 is used (enter -1 to use as many
cores as possible). 0 raises an error.
Returns the voxel-to-space transformation associated with the
sub-sampled image at a particular resolution of the scale space (note
that this object does not explicitly subsample the smoothed images, but
only provides the properties the sub-sampled images must have).
Parameters:
levelint, 0 <= from_level < L, (L = number of resolutions)
the scale space level to get affine transform from
Returns:
the affine (voxel-to-space) transform at the requested resolution
Returns the space-to-voxel transformation associated with the
sub-sampled image at a particular resolution of the scale space (note
that this object does not explicitly subsample the smoothed images, but
only provides the properties the sub-sampled images must have).
Parameters:
levelint, 0 <= from_level < L, (L = number of resolutions)
the scale space level to get the inverse transform from
Returns:
the inverse (space-to-voxel) transform at the requested resolution or
Shape the sub-sampled image must have at a particular level.
Returns the shape the sub-sampled image must have at a particular
resolution of the scale space (note that this object does not
explicitly subsample the smoothed images, but only provides the
properties the sub-sampled images must have).
Parameters:
levelint, 0 <= from_level < L, (L = number of resolutions)
the scale space level to get the sub-sampled shape from
Returns:
the sub-sampled shape at the requested resolution or None if an
Ratio of voxel size from pyramid level from_level to to_level.
Given two scale space resolutions a = from_level, b = to_level,
returns the ratio of voxels size at level b to voxel size at level a
(the factor that must be used to multiply voxels at level a to
‘expand’ them to level b).
Parameters:
from_levelint, 0 <= from_level < L, (L = number of resolutions)
the resolution to expand voxels from
to_levelint, 0 <= to_level < from_level
the resolution to expand voxels to
Returns:
factorsarray, shape (k,), k = 2, 3
the expand factors (a scalar for each voxel dimension)
Adjustment factor for input-spacing to reflect voxel sizes at level.
Returns the scaling factor that needs to be applied to the input
spacing (the voxel sizes of the image at level 0 of the scale space) to
transform them to voxel sizes at the requested level.
Parameters:
levelint, 0 <= from_level < L, (L = number of resolutions)
the scale space level to get the scalings from
Returns:
the scaling factors from the original spacing to the spacings at the
Spacings the sub-sampled image must have at a particular level.
Returns the spacings (voxel sizes) the sub-sampled image must have at a
particular resolution of the scale space (note that this object does
not explicitly subsample the smoothed images, but only provides the
properties the sub-sampled images must have).
Parameters:
levelint, 0 <= from_level < L, (L = number of resolutions)
the scale space level to get the sub-sampled shape from
Returns:
the spacings (voxel sizes) at the requested resolution or None if an
Bundle-based Minimum Distance for joint optimization.
This cost function is used by the StreamlineLinearRegistration class when
running halfway streamline linear registration for unbiased groupwise
bundle registration and atlasing.
It computes the BMD distance after moving both static and moving bundles to
a halfway space in between both.
Methods
setup(static, moving)
distance(xopt)
Notes
In this metric both static and moving bundles are treated equally (i.e.,
there is no static reference bundle as both are intended to move). The
naming convention is kept for consistency.
List of affine parameters as an 1D vector. These affine parameters
are used to derive the corresponding halfway transformation
parameters for each bundle.
Transformation (4, 4) matrix to start the registration. mat
is applied to moving. Default value None which means that initial
transformation will be generated by shifting the centers of moving
and static sets of streamlines to the origin.
We minimize the distance between moving streamlines as they align
with the static streamlines.
Parameters:
tndarray
t is a vector of affine transformation parameters with
size at least 6.
If the size is 6, t is interpreted as translation + rotation.
If the size is 7, t is interpreted as translation + rotation +
isotropic scaling.
If size is 12, t is interpreted as translation + rotation +
scaling + shearing.
staticlist
Static streamlines
movinglist
Moving streamlines. These will be transformed to align with
the static streamlines
num_threadsint, optional
Number of threads. If -1 then all available threads will be used.
MDF-based pairwise distance optimization function (MIN).
We minimize the distance between moving streamlines as they align
with the static streamlines.
Parameters:
tndarray
t is a vector of affine transformation parameters with
size at least 6.
If size is 6, t is interpreted as translation + rotation.
If size is 7, t is interpreted as translation + rotation +
isotropic scaling.
If size is 12, t is interpreted as translation + rotation +
scaling + shearing.
MDF-based pairwise distance optimization function (MIN).
We minimize the distance between moving streamlines as they align
with the static streamlines.
Parameters:
tarray
1D array. t is a vector of affine transformation parameters with
size at least 6.
If the size is 6, t is interpreted as translation + rotation.
If the size is 7, t is interpreted as translation + rotation +
isotropic scaling.
If size is 12, t is interpreted as translation + rotation +
scaling + shearing.
staticarray
N*M x 3 array. All the points of the static streamlines. With order of
streamlines intact. Where N is the number of streamlines and M
is the number of points per streamline.
movingarray
K*M x 3 array. All the points of the moving streamlines. With order of
streamlines intact. Where K is the number of streamlines and M
is the number of points per streamline.
block_sizeint
Number of points per streamline. All streamlines in static and moving
should have the same number of points M.
num_threadsint, optional
Number of threads to be used for OpenMP parallelization. If None
(default) the value of OMP_NUM_THREADS environment variable is used
if it is set, otherwise all available threads are used. If < 0 the
maximal number of threads minus \(|num_threads + 1|\) is used (enter -1 to
use as many threads as possible). 0 raises an error.
Returns:
cost: float
Notes
This is a faster implementation of bundle_min_distance, which requires
that all the points of each streamline are allocated into an ndarray
(of shape N*M by 3, with N the number of points per streamline and M the
number of streamlines). This can be done by calling
dipy.tracking.streamlines.unlist_streamlines.
MDF-based pairwise distance optimization function (MIN).
We minimize the distance between moving streamlines as they align
with the static streamlines.
Parameters:
tarray
1D array. t is a vector of affine transformation parameters with
size at least 6.
If the size is 6, t is interpreted as translation + rotation.
If the size is 7, t is interpreted as translation + rotation +
isotropic scaling.
If size is 12, t is interpreted as translation + rotation +
scaling + shearing.
staticarray
N*M x 3 array. All the points of the static streamlines. With order of
streamlines intact. Where N is the number of streamlines and M
is the number of points per streamline.
movingarray
K*M x 3 array. All the points of the moving streamlines. With order of
streamlines intact. Where K is the number of streamlines and M
is the number of points per streamline.
block_sizeint
Number of points per streamline. All streamlines in static and moving
should have the same number of points M.
This is a utility function that allows for example to do affine
registration using Streamline-based Linear Registration (SLR)
[6] by starting with translation first,
then rigid, then similarity, scaling and finally affine.
Similarly, if for example, you want to perform rigid then you start with
translation first. This progressive strategy can help with finding the
optimal parameters of the final transformation.
Parameters:
staticStreamlines
Static streamlines.
movingStreamlines
Moving streamlines.
metricStreamlineDistanceMetric
Distance metric for registration optimization.
x0string
Could be any of ‘translation’, ‘rigid’, ‘similarity’, ‘scaling’,
‘affine’
boundsarray
Boundaries of registration parameters. See variable DEFAULT_BOUNDS
for example.
methodstring
L_BFGS_B’ or ‘Powell’ optimizers can be used. Default is ‘L_BFGS_B’.
verbosebool, optional.
If True, log messages.
num_threadsint, optional
Number of threads to be used for OpenMP parallelization. If None
(default) the value of OMP_NUM_THREADS environment variable is used
if it is set, otherwise all available threads are used. If < 0 the
maximal number of threads minus \(|num_threads + 1|\) is used (enter -1 to
use as many threads as possible). 0 raises an error. Only metrics
using OpenMP will use this variable.
Utility function for registering large tractograms.
For efficiency, we apply the registration on cluster centroids and remove
small clusters.
See [5], [6] and
[7] for details about the methods involved.
Parameters:
staticStreamlines
Fixed or reference set of streamlines.
movingstreamlines
Moving streamlines.
x0str, optional.
rigid, similarity or affine transformation model
rm_small_clustersint, optional
Remove clusters that have less than rm_small_clusters
maxiterint, optional
Maximum number of iterations to perform.
select_randomint, optional.
If not, None selects a random number of streamlines to apply clustering
verbosebool, optional
If True, logs information about optimization.
greater_thanint, optional
Keep streamlines that have length greater than this value.
less_thanint, optional
Keep streamlines have length less than this value.
qbx_thrvariable int
Thresholds for QuickBundlesX.
nb_ptsint, optional
Number of points for discretizing each streamline.
progressiveboolean, optional
True to enable progressive registration.
rngnp.random.Generator
If None creates random generator in function.
num_threadsint, optional
Number of threads to be used for OpenMP parallelization. If None
(default) the value of OMP_NUM_THREADS environment variable is used
if it is set, otherwise all available threads are used. If < 0 the
maximal number of threads minus \(|num_threads + 1|\) is used (enter -1 to
use as many threads as possible). 0 raises an error. Only metrics
using OpenMP will use this variable.
Notes
The order of operations is the following. First short or long streamlines
are removed. Second, the tractogram or a random selection of the tractogram
is clustered with QuickBundles. Then SLR [6] is
applied.
Function to perform unbiased groupwise bundle registration
All bundles are moved to the same space by iteratively applying halfway
streamline linear registration in pairs. With each iteration, bundles get
closer to each other until the procedure converges and there is no more
improvement.
List with streamlines of the bundles to be registered.
x0str, optional
rigid, similarity or affine transformation model.
tolfloat, optional
Tolerance value to be used to assume convergence.
max_iterint, optional
Maximum number of iterations. Depending on the number of bundles to be
registered this may need to be larger.
qbx_thrvariable int, optional
Thresholds for Quickbundles used for clustering streamlines and reduce
computational time. If None, no clustering is performed. Higher values
cluster streamlines into a smaller number of centroids.
nb_ptsint, optional
Number of points for discretizing each streamline.
select_randomint, optional
Maximum number of streamlines for each bundle. If None, all the
streamlines are used.
This is a 1D vector of affine transformation parameters with
size at least 3.
If the size is 3, t is interpreted as translation.
If the size is 6, t is interpreted as translation + rotation.
If the size is 7, t is interpreted as translation + rotation +
isotropic scaling.
If the size is 9, t is interpreted as translation + rotation +
anisotropic scaling.
If size is 12, t is interpreted as translation + rotation +
scaling + shearing.
Bundle shape difference analysis using magnitude from BundleWarp
displacements and BUAN.
Depending on the number of points of a streamline, and the number of
segments requested, multiple points may be considered for the computation
of a given segment; a segment may contain information from a single point;
or some segments may not contain information from any points. In the latter
case, the segment will contain an np.nan value. The point-to-segment
mapping is defined by the assignment_map(): for each segment index,
the point information of the matching index positions, as returned by
assignment_map(), are considered for the computation.
Parameters:
moving_alignedStreamlines
Linearly (affinely) moved bundle
deformed_bundleStreamlines
Nonlinearly (warped) moved bundle
no_disksint, optional
Number of segments to be created along the length of the bundle
plottingBoolean, optional
Plot bundle shape profile
Returns:
shape_profilenp.ndarray
Float array containing bundlewarp displacement magnitudes along the
length of the bundle