Tools for using spherical harmonic models to fit diffusion data
References
Aganj, I., et al. 2009. ODF Reconstruction in Q-Ball Imaging With Solid
Angle Consideration.
Descoteaux, M., et al. 2007. Regularized, fast, and robust analytical
Q-ball imaging.
Tristan-Vega, A., et al. 2010. A new methodology for estimation of fiber
populations in white matter of the brain with Funk-Radon transform.
Tristan-Vega, A., et al. 2009. Estimation of fiber orientation probability
density functions in high angular resolution diffusion imaging.
Note about the Transpose:
In the literature the matrix representation of these methods is often written
as Y = Bx where B is some design matrix and Y and x are column vectors. In our
case the input data, a dwi stored as a nifti file for example, is stored as row
vectors (ndarrays) of the form (x, y, z, n), where n is the number of diffusion
directions. We could transpose and reshape the data to be (n, x*y*z), so that
we could directly plug it into the above equation. However, I have chosen to
keep the data as is and implement the relevant equations rewritten in the
following form: Y.T = x.T B.T, or in python syntax data = np.dot(sh_coef, B.T)
where data is Y.T and sh_coef is x.T.
The matrix needed to sample ODFs from coefficients of the model.
Parameters:
sphere (Sphere) – Points used to sample ODF.
Returns:
sampling_matrix – The size of the matrix will be (N, M) where N is the number of
vertices on sphere and M is the number of coefficients needed by
the model.
Where the last dimension, C, is made of a flattened array of $l$x$m$
coefficients, where $l$ are the SH orders, and $m = 2l+1$,
So l=1 has 1 coeffecient, l=2 has 5, … l=8 has 17 and so on.
A l=2 SH coefficient matrix will then be composed of a IxJxKx6 volume.
The power, $n$ is usually set to $n=2$.
The final AP image is then shifted by -log(norm_factor), to be strictly
non-negative. Remaining values < 0 are discarded (set to 0), per default,
and this option is controlled through the non_negative keyword argument.
Applies the Residual Bootstraps to the data given H and R
data must be normalized, ie 0 < data <= 1
This function, and the bootstrap_data_voxel function, calculate
residual-bootsrap samples given a Hat matrix and a Residual matrix. These
samples can be used for non-parametric statistics or for bootstrap
probabilistic tractography:
r_rh (ndarray) – Rotational harmonics coefficients for the single fiber response
function. Each element rh[i] is associated with spherical harmonics
of degree 2*i.
n (ndarray) – The degree of spherical harmonic function associated with each row of
the deconvolution matrix. Only even degrees are allowed
Returns:
R – Deconvolution matrix with shape (N, N)
Return type:
ndarray (N, N)
qsiprep.utils.shm.gen_dirac(m, n, theta, phi)[source]
Generate Dirac delta function orientated in (theta, phi) on the sphere
The spherical harmonics (SH) representation of this Dirac is returned as
coefficients to spherical harmonic functions produced by
shm.real_sph_harm.
Parameters:
m (ndarray (N,)) – The order of the spherical harmonic function associated with each
coefficient.
n (ndarray (N,)) – The degree of the spherical harmonic function associated with each
coefficient.
theta (float [0, 2*pi]) – The azimuthal (longitudinal) coordinate.
phi (float [0, pi]) – The polar (colatitudinal) coordinate.
See also
shm.real_sph_harm, shm.real_sym_sh_basis
Returns:
dirac – SH coefficients representing the Dirac function. The shape of this is
(m + 2) * (m + 1) / 2.
Return random integers from low (inclusive) to high (exclusive).
Return random integers from the “discrete uniform” distribution of
the specified dtype in the “half-open” interval [low, high). If
high is None (the default), then results are from [0, low).
Note
New code should use the ~numpy.random.Generator.integers
method of a ~numpy.random.Generator instance instead;
please see the Quick start.
Parameters:
low (int or array-like of ints) – Lowest (signed) integers to be drawn from the distribution (unless
high=None, in which case this parameter is one above the
highest such integer).
high (int or array-like of ints, optional) – If provided, one above the largest (signed) integer to be drawn
from the distribution (see above for behavior if high=None).
If array-like, must contain integer values
size (int or tuple of ints, optional) – Output shape. If the given shape is, e.g., (m,n,k), then
m*n*k samples are drawn. Default is None, in which case a
single value is returned.
dtype (dtype, optional) – Desired dtype of the result. Byteorder must be native.
The default value is int.
Added in version 1.11.0.
Returns:
out – size-shaped array of random integers from the appropriate
distribution, or a single such random int if size not provided.
Samples a real symmetric spherical harmonic basis at point on the sphere
Samples the basis functions up to order sh_order at points on the sphere
given by theta and phi. The basis functions are defined here the same
way as in fibernavigator [1]_ where the real harmonic $Y^m_n$ is defined to
be:
Imag($Y^m_n$) * sqrt(2) if m > 0
$Y^0_n$ if m = 0
Real($Y^|m|_n$) * sqrt(2) if m < 0
This may take scalar or array arguments. The inputs will be broadcasted
against each other.
Parameters:
sh_order (int) – even int > 0, max spherical harmonic degree
theta (float [0, 2*pi]) – The azimuthal (longitudinal) coordinate.
phi (float [0, pi]) – The polar (colatitudinal) coordinate.
Returns:
y_mn (real float) – The real harmonic $Y^m_n$ sampled at theta and phi
Compute real spherical harmonics as in mrtrix, where the real harmonic
$Y^m_n$ is defined to be:
Real($Y^m_n$) if m > 0
$Y^0_n$ if m = 0
Imag($Y^|m|_n$) if m < 0
This may take scalar or array arguments. The inputs will be broadcasted
against each other.
Parameters:
sh_order (int) – The maximum degree or the spherical harmonic basis.
theta (float [0, pi]) – The polar (colatitudinal) coordinate.
phi (float [0, 2*pi]) – The azimuthal (longitudinal) coordinate.
Returns:
y_mn (real float) – The real harmonic $Y^m_n$ sampled at theta and phi as
implemented in mrtrix. Warning: the basis is Tournier et al
2004 and 2007 is slightly different.
sf (ndarray) – Values of a function on the given sphere.
sphere (Sphere) – The points on which the sf is defined.
sh_order (int, optional) – Maximum SH order in the SH fit. For sh_order, there will be
(sh_order+1)*(sh_order_2)/2 SH coefficients (default 4).
basis_type ({None, ‘mrtrix’, ‘fibernav’}) – None for the default dipy basis,
mrtrix for the MRtrix basis, and
fibernav for the FiberNavigator basis
(default None).
smooth (float, optional) – Lambda-regularization in the SH fit (default 0.0).
Returns:
sh – SH coefficients representing the input function.
Spherical harmonics (SH) to rotational harmonics (RH)
Calculate the rotational harmonic decomposition up to
harmonic order m, degree n for an axially and antipodally
symmetric function. Note that all m!=0 coefficients
will be ignored as axial symmetry is assumed. Hence, there
will be (sh_order/2+1) non-zero coefficients.
Parameters:
r_sh (ndarray (N,)) – ndarray of SH coefficients for the single fiber response function.
These coefficients must correspond to the real spherical harmonic
functions produced by shm.real_sph_harm.
m (ndarray (N,)) – The order of the spherical harmonic function associated with each
coefficient.
n (ndarray (N,)) – The degree of the spherical harmonic function associated with each
coefficient.
Returns:
r_rh – Rotational harmonics coefficients representing the input r_sh
Spherical harmonics (SH) to spherical function (SF).
Parameters:
sh (ndarray) – SH coefficients representing a spherical function.
sphere (Sphere) – The points on which to sample the spherical function.
sh_order (int, optional) – Maximum SH order in the SH fit. For sh_order, there will be
(sh_order+1)*(sh_order_2)/2 SH coefficients (default 4).
basis_type ({None, ‘mrtrix’, ‘fibernav’}) – None for the default dipy basis,
mrtrix for the MRtrix basis, and
fibernav for the FiberNavigator basis
(default None).
Matrix that transforms Spherical harmonics (SH) to spherical
function (SF).
Parameters:
sphere (Sphere) – The points on which to sample the spherical function.
sh_order (int, optional) – Maximum SH order in the SH fit. For sh_order, there will be
(sh_order+1)*(sh_order_2)/2 SH coefficients (default 4).
basis_type ({None, ‘mrtrix’, ‘fibernav’}) – None for the default dipy basis,
mrtrix for the MRtrix basis, and
fibernav for the FiberNavigator basis
(default None).
return_inv (bool) – If True then the inverse of the matrix is also returned
smooth (float, optional) – Lambda-regularization in the SH fit (default 0.0).
Returns:
B (ndarray) – Matrix that transforms spherical harmonics to spherical function
sf=np.dot(sh,B).
In the literature this inverse is often written $(B^{T}B+L^{2})^{-1}B^{T}$.
However here this inverse is implemented using the pseudo-inverse because
it is more numerically stable than the direct implementation of the matrix
product.
Returns the degree (n) and order (m) of all the symmetric spherical
harmonics of degree less then or equal to sh_order. The results, m_list
and n_list are kx1 arrays, where k depends on sh_order. They can be
passed to real_sph_harm().
Parameters:
sh_order (int) – even int > 0, max degree to return
Returns:
m_list (array) – orders of even spherical harmonics
n_list (array) – degrees of even spherical harmonics
qsiprep.utils.shm.spherical_harmonics(m, n, theta, phi)[source]
Compute spherical harmonics
This may take scalar or array arguments. The inputs will be broadcasted
against each other.
Parameters:
m (int |m|<=n) – The order of the harmonic.
n (int >=0) – The degree of the harmonic.
theta (float [0, 2*pi]) – The azimuthal (longitudinal) coordinate.
phi (float [0, pi]) – The polar (colatitudinal) coordinate.
Returns:
y_mn – The harmonic $Y^m_n$ sampled at theta and phi.
Return type:
complex float
Notes
This is a faster implementation of scipy.special.sph_harm for
scipy version < 0.15.0. For scipy 0.15 and onwards, we use the scipy
implementation of the function