nifreeze.model.gpr module¶
Derivations from scikit-learn for Gaussian Processes.
- class nifreeze.model.gpr.DiffusionGPR(self, kernel: 'Kernel | None' = None, *, alpha: 'float' = 0.5, optimizer: "Literal['fmin_l_bfgs_b'] | Callable | None" = 'fmin_l_bfgs_b', n_restarts_optimizer: 'int' = 0, copy_X_train: 'bool' = True, normalize_y: 'bool' = True, n_targets: 'int | None' = None, random_state: 'int | None' = None, eval_gradient: 'bool' = True, tol: 'float | None' = None, disp: 'bool | int | None' = None, maxiter: 'int | None' = None, ftol: 'float | None' = None, gtol: 'float | None' = None, adaptive: 'bool | int | None' = None, fatol: 'float | None' = None)[source]¶
Bases:
GaussianProcessRegressorA Gaussian process (GP) regressor specialized for nifreeze.
This specialization of the default GP regressor is created to allow the following extended behaviors:
Pacify Scikit-learn’s estimator parameter checker to allow optimizers given by name (as a string) other than the default BFGS.
Enable custom options of optimizers. See
minimizefor the available options. Please note that only a few of them are currently supported.
In the future, this specialization would be the right place for hyperparameter optimization using cross-validation and such.
In principle, Scikit-Learn’s implementation normalizes the training data as in [1] (see FSL’s source code). From their paper (p. 167, end of first column):
Typically one just subtracts the mean (\(\bar{\mathbf{f}}\)) from \(\mathbf{f}\) and then add it back to \(f^{*}\), which is analogous to what is often done in “traditional” regression.
Finally, the parameter \(\sigma^2\) (the measurement noise) is modeled by adding a
WhiteKernelto the covariance kernel (seeGaussianProcessModel), in order to estimate \(\sigma^2\) Andersson and Sotiropoulos[1]:A note on optimisation
It is suggested, for example in Rasmussen and Williams (2006), that an optimisation method that uses derivative information should be used when finding the hyperparameters that maximise Eq. (12). The reason for that is that such methods typically use fewer steps, and when the cost of calculating the derivatives is small/moderate compared to calculating the functions itself (as is the case for Eq. (12)) then execution time can be much shorter. However, we found that for the multi-shell case a heuristic optimisation method such as the Nelder-Mead simplex method (Nelder and Mead, 1965) was frequently better at avoiding local maxima. Hence, that was the method we used for all optimisations in the present paper.
Multi-shell regression. For multi-shell modeling, use
MultiShellKernel, which updates the kernel \(k(\textbf{x}, \textbf{x'})\) following Eq. (14) in [1].\[k(\textbf{x}, \textbf{x'}) = C_{\theta}(\mathbf{g}, \mathbf{g'}; a) C_{b}(|b - b'|; \ell)\]and \(C_{b}\) is based the log of the b-values ratio, a measure of distance along the b-direction, according to Eq. (15) given by:
\[C_{b}(b, b'; \ell) = \exp\left( - \frac{(\log b - \log b')^2}{2 \ell^2} \right),\]\(b\) and \(b'\) being the b-values, and \(\mathbf{g}\) and \(\mathbf{g'}\) the unit diffusion-encoding gradient unit vectors of the shells; and \({a, \ell}\) some hyperparameters.
The full GP regression kernel \(\mathbf{K}\) is then updated for a 2-shell case as follows (Eq. (16) in [1]):
\[\begin{split}\begin{equation} \mathbf{K} = \left[ \begin{matrix} \lambda C_{\theta}(\theta (\mathbf{G}_{1}); a) + \sigma_{1}^{2} \mathbf{I} & \lambda C_{\theta}(\theta (\mathbf{G}_{2}, \mathbf{G}_{1}); a) C_{b}(b_{2}, b_{1}; \ell) \\ \lambda C_{\theta}(\theta (\mathbf{G}_{1}, \mathbf{G}_{2}); a) C_{b}(b_{1}, b_{2}; \ell) & \lambda C_{\theta}(\theta (\mathbf{G}_{2}); a) + \sigma_{2}^{2} \mathbf{I} \\ \end{matrix} \right] \end{equation}\end{split}\]References
- class nifreeze.model.gpr.ExponentialKriging(self, beta_a: 'float' = 0.01, beta_l: 'float' = 2.0, a_bounds: 'tuple[float, float]' = (0.1, 2.35), l_bounds: 'tuple[float, float]' = (0.001, 1000))[source]¶
Bases:
KernelA scikit-learn’s kernel for DWI signals.
- Parameters:
- __call__(X: ndarray, Y: ndarray | None = None, eval_gradient: bool = False) ndarray | tuple[ndarray, ndarray][source]¶
Return the kernel K(X, Y) and optionally its gradient.
- Parameters:
- Returns:
- diag(X: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]) ndarray[source]¶
Returns the diagonal of the kernel k(X, X).
The result of this method is identical to np.diag(self(X)); however, it can be evaluated more efficiently since only the diagonal is evaluated.
- property hyperparameter_a: sklearn.gaussian_process.kernels.Hyperparameter¶
- property hyperparameter_l: sklearn.gaussian_process.kernels.Hyperparameter¶
- class nifreeze.model.gpr.MultiShellKernel(self, orientation_kernel: 'Kernel | None' = None, radial_kernel: 'Kernel | None' = None, orientation_dims: 'Sequence[int]' = (0, 1, 2), bval_index: 'int' = 3) 'None'[source]¶
Bases:
KernelOperatorComposite kernel for multi-shell diffusion data.
Implements the multi-shell covariance of [1] (Eq. 14) as the product of an angular (orientation) kernel and a radial (b-value) kernel,
\[k(\mathbf{x}, \mathbf{x'}) = C_{\theta}(\mathbf{g}, \mathbf{g'}; a)\, C_{b}(b, b'; \ell),\]where the design matrix
Xis expected to hold the diffusion-encoding gradient components inorientation_dimsand the b-value inbval_index(default layout[gx, gy, gz, bval]). The defaultradial_kernel(RBF) applied to \(\log b\) reproduces Eq. (15),\[C_{b}(b, b'; \ell) = \exp\!\left(-\frac{(\log b - \log b')^2}{2\ell^2}\right).\]The b-value column must be strictly positive: \(\log b\) is undefined for b0 volumes, which must be handled/excluded upstream.
References
- __call__(X: ndarray, Y: ndarray | None = None, eval_gradient: bool = False) ndarray | tuple[ndarray, ndarray][source]¶
Call self as a function.
- diag(X: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]) ndarray[source]¶
- property hyperparameters: list[sklearn.gaussian_process.kernels.Hyperparameter]¶
Expose sub-kernel hyperparameters under the constructor’s names.
- k1: Kernel¶
- k2: Kernel¶
- class nifreeze.model.gpr.SphericalKriging(self, beta_a: 'float' = 1.38, beta_l: 'float' = 0.5, a_bounds: 'tuple[float, float]' = (0.1, 2.35), l_bounds: 'tuple[float, float]' = (0.001, 1000))[source]¶
Bases:
KernelA scikit-learn’s kernel for DWI signals.
- Parameters:
- __call__(X: ndarray, Y: ndarray | None = None, eval_gradient: bool = False) ndarray | tuple[ndarray, ndarray][source]¶
Return the kernel K(X, Y) and optionally its gradient.
- Parameters:
- Returns:
- diag(X: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]) ndarray[source]¶
Returns the diagonal of the kernel k(X, X).
The result of this method is identical to np.diag(self(X)); however, it can be evaluated more efficiently since only the diagonal is evaluated.
- property hyperparameter_a: sklearn.gaussian_process.kernels.Hyperparameter¶
- property hyperparameter_l: sklearn.gaussian_process.kernels.Hyperparameter¶
- nifreeze.model.gpr.compute_pairwise_angles(X: ndarray, Y: ndarray | None = None, closest_polarity: bool = True, dense_output: bool = True) ndarray[source]¶
Compute pairwise angles across diffusion gradient encoding directions.
Following [1], it computes the smallest of the angles between each pair if
closest_polarityisTrue, i.e.,\[\theta(\mathbf{g}, \mathbf{g'}) = \arccos(|\langle \mathbf{g}, \mathbf{g'} \rangle|)\]- Parameters:
X ({array-like, sparse matrix} of shape (n_samples_X, n_features)) – Input data.
Y ({array-like, sparse matrix} of shape (n_samples_Y, n_features), optional) – Input data. If
None, the output will be the pairwise similarities between all samples inX.dense_output (
bool, optional) – Whether to return dense output even when the input is sparse. IfFalse, the output is sparse if both input arrays are sparse.closest_polarity (
bool, optional) –Trueto consider the smallest of the two angles between the crossing lines resulting from reversing each vector pair.
- Returns:
Pairwise angles across diffusion gradient encoding directions.
- Return type:
References
Examples
>>> X = np.asarray([(1.0, -1.0), (0.0, 0.0), (0.0, 0.0)]).T >>> bool(np.isclose( ... compute_pairwise_angles(X, closest_polarity=False)[0, 1], ... np.pi, ... )) True >>> X = np.asarray([(1.0, -1.0), (0.0, 0.0), (0.0, 0.0)]).T >>> Y = np.asarray([(1.0, -1.0), (0.0, 0.0), (0.0, 0.0)]).T >>> bool(np.isclose( ... compute_pairwise_angles(X, Y, closest_polarity=False)[0, 1], ... np.pi, ... )) True >>> X = np.asarray([(1.0, -1.0), (0.0, 0.0), (0.0, 0.0)]).T >>> float(compute_pairwise_angles(X)[0, 1]) 0.0
- nifreeze.model.gpr.exponential_covariance(theta: ndarray, a: float) ndarray[source]¶
Compute the exponential covariance for given distances and scale parameter.
Implements \(C_{\theta}\), following Eq. (9) in [1]:
\[\begin{equation} C(\theta) = e^{-\theta/a} \,\, \text{for} \, 0 \leq \theta \leq \pi, \end{equation}\]\(\theta\) being computed as:
\[\theta(\mathbf{g}, \mathbf{g'}) = \arccos(|\langle \mathbf{g}, \mathbf{g'} \rangle|)\]- Parameters:
- Returns:
Exponential covariance values for the input distances.
- Return type:
References
- nifreeze.model.gpr.spherical_covariance(theta: ndarray, a: float) ndarray[source]¶
Compute the spherical covariance for given distances and scale parameter.
Implements \(C_{\theta}\), following Eq. (10) in [1]:
\[\begin{split}\begin{equation} C(\theta) = \begin{cases} 1 - \frac{3 \theta}{2 a} + \frac{\theta^3}{2 a^3} & \textnormal{if} \; \theta \leq a \\ 0 & \textnormal{if} \; \theta > a \end{cases} \end{equation}\end{split}\]\(\theta\) being computed as:
\[\theta(\mathbf{g}, \mathbf{g'}) = \arccos(|\langle \mathbf{g}, \mathbf{g'} \rangle|)\]- Parameters:
- Returns:
Spherical covariance values for the input distances.
- Return type:
References