procrustes¶
Data alignment — SRM, Procrustes, and state alignment.
Methods:
| Name | Description |
|---|---|
align | Align subject data into a common response model. |
align_states | Align state weight maps by minimizing pairwise distance between group states. |
procrustes | Perform a Procrustes similarity analysis on two data sets. |
procrustes_distance | Test matrix similarity using Procrustes superposition. |
Classes¶
Methods¶
align¶
align(data, method = 'deterministic_srm', n_features = None, axis = 0, *args, **kwargs)Align subject data into a common response model.
This function is a convenience wrapper around HyperAlignment and SRM classes.
Can be used to hyperalign source data to target data using Hyperalignment from Dartmouth (i.e., procrustes transformation; see nltools.algorithms.procrustes) or Shared Response Model from Princeton (see nltools.algorithms.srm). (see nltools.data.BrainData.align for aligning a single Brain object to another). Common Model is shared response model or centered target data. Transformed data can be back projected to original data using Tranformation matrix. Inputs must be a list of BrainData instances or numpy arrays (observations by features).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | (list) A list of BrainData objects | required | |
method | (str) alignment method to use [‘probabilistic_srm’,‘deterministic_srm’,‘procrustes’] | ‘deterministic_srm’ | |
n_features | (int) number of features to align to common space. If None then will select number of voxels | None | |
axis | (int) axis to align on | 0 |
Returns:
| Name | Type | Description |
|---|---|---|
out | (dict) a dictionary containing a list of transformed subject matrices, a list of transformation matrices, the shared response matrix, and the intersubject correlation of the shared responses |
Examples:
Hyperalign using procrustes transform:
out = align(data, method=‘procrustes’)
Align using shared response model:
out = align(data, method=‘probabilistic_srm’, n_features=None)
Project aligned data into original data:
original_data = [np.dot(t.data,tm.T) for t,tm in zip(out[‘transformed’], out[‘transformation_matrix’])]
align_states¶
align_states(reference, target, *, metric = 'correlation', return_index = False, replace_zero_variance = False)Align state weight maps by minimizing pairwise distance between group states.
This function uses the Hungarian algorithm for state alignment, which is different from aligning multiple subjects’ data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reference | (np.array) reference pattern x state matrix | required | |
target | (np.array) target pattern x state matrix to align to reference | required | |
metric | (str) distance metric to use | ‘correlation’ | |
return_index | (bool) return index if True, return remapped data if False | False | |
replace_zero_variance | (bool) transform a vector with zero variance to random numbers from a uniform distribution. Useful for when using correlation as a distance metric to avoid NaNs. | False |
Returns:
If return_index=False (default): target[:, remapping], a single
ndarray of the target’s columns reordered to match the reference,
oriented pattern x state (same shape as target).
If return_index=True: the remapping index array (ndarray) that
reorders the target’s state columns.
procrustes¶
procrustes(data1, data2)Perform a Procrustes similarity analysis on two data sets.
For more comprehensive Procrustes-based alignment tasks, use
HyperAlignment and align() instead.
Each input matrix is a set of points or vectors (the rows of the matrix). The dimension of the space is the number of columns of each matrix. Given two identically sized matrices, procrustes standardizes both such that:
.
Both sets of points are centered around the origin. Procrustes then applies the optimal transform to the second matrix (including scaling/dilation, rotations, and reflections) to minimize , or the sum of the squares of the pointwise differences between the two input datasets. This function was not designed to handle datasets with different numbers of datapoints (rows). If two data sets have different dimensionality (different number of columns), this function will add columns of zeros to the smaller of the two.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data1 | Matrix whose n rows represent points in k (columns) space. data1 is the reference data; after it is standardized, the data from data2 will be transformed to fit the pattern in data1 (must have >1 unique points). | required | |
data2 | n rows of data in k space to be fit to data1. Must be the same shape (numrows, numcols) as data1 (must have >1 unique points). | required |
Returns:
| Name | Type | Description |
|---|---|---|
mtx1 | A standardized version of data1. | |
mtx2 | The orientation of data2 that best fits data1. Centered, but not necessarily . | |
disparity | as defined above. | |
R | The (N, N) matrix solution of the orthogonal Procrustes problem. Minimizes the Frobenius norm of dot(data1, R) - data2, subject to dot(R.T, R) == I. | |
scale | Sum of the singular values of dot(data1.T, data2). |
procrustes_distance¶
procrustes_distance(mat1, mat2, *, n_permute = 5000, tail = 2, n_jobs = -1, random_state = None)Test matrix similarity using Procrustes superposition.
Matrices need to match in size on their first dimension only, as the smaller matrix on the second dimension will be padded with zeros. After aligning two matrices using the Procrustes transformation, use the computed disparity between them (sum of squared error of elements) as a similarity metric. Shuffle the rows of one of the matrices and recompute the disparity to perform inference (Peres-Neto & Jackson, 2001).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mat1 | ndarray | 2d numpy array; must have same number of rows as mat2 | required |
mat2 | ndarray | 1d or 2d numpy array; must have same number of rows as mat1 | required |
n_permute | int | number of permutation iterations to perform | 5000 |
tail | int | str | 2 | ‘two’ (two-tailed, default) or 1 |
n_jobs | int | The number of CPUs to use to do permutation; default -1 (all) | -1 |
random_state | int, np.random.RandomState, or None | seed or generator for the permutation shuffling; default None | None |
Returns:
| Name | Type | Description |
|---|---|---|
dict | results with keys similarity (float in [0, 1]) and p (permuted p-value) |