alignment module

Collection of tools for alignment of neural data @Author: Mehrdad Kashefi

class alignment.CCA_svd(n_components=2)

Canonical Correlation Analysis using Singular Value Decomposition (SVD)

Parameters:

n_components (int) – number of CC components, default 2

fit(X, Y)

Fit the CCA model to the data

Parameters:
  • X (np.array) – data to fit the model (Samples x Units)

  • Y (np.array) – data to fit the model (Samples x Units)

score(X, Y)

Transforms new data in fitted CCA space and computes the R2 score

Parameters:
  • X (np.array) – data to fit the model (Samples x Units)

  • Y (np.array) – data to fit the model (Samples x Units)

transform(X, Y)

Transforms new data in fitted CCA space

Parameters:
  • X (np.array) – data to fit the model (Samples x Units)

  • Y (np.array) – data to fit the model (Samples x Units)

class alignment.Procrustes(scaling=True, reflection='best')

Procrustes analysis (Based on Matlab implementation) Procrustes analysis determines a linear transformation (translation, reflection, orthogonal rotation and scaling) of the points in Y to best conform them to the points in matrix X, using the sum of squared errors as the goodness of fit criterion.

d, Z, [tform] = procrustes(X, Y)

Parameters:
  • scaling (bool, default True) – Controls whether the solution includes a scaling component.

  • reflection (str, default 'best') – if ‘best’ (default), the transformation solution may or may not include a reflection component, depending on which fits the data best. setting reflection to True or False forces a solution with reflection or no reflection respectively.

fit(X, Y)

Fit the Procrustes model to the data

Parameters:
  • X (np.array) – data to fit the model (Samples x Units)

  • Y (np.array) – data to fit the model (Samples x Units)

Returns:
  • d (float) – the residual sum of squared errors, normalized according to a measure of the scale of X, ((X - X.mean(0))**2).sum()

  • Z (np.array) – the matrix of transformed Y-values

  • tform (dict) – a dict specifying the rotation, translation and scaling that maps X –> Y

score(X, Y)

Compute the score of the transformation.

Parameters:
  • X (np.array) – data to transform (Samples x Units)

  • Y (np.array) – data to transform (Samples x Units)

Returns:

score (float) – the R2 score of aligned data.

transform(Y)

Transform the Y matrix using the transformation matrix.

Parameters:

Y (np.array) – data to transform (Samples x Units)

Returns:

Z (np.array) – the matrix of transformed Y-values

alignment.get_dissimilarity_cond_avr(X, X_conds, Y, Y_conds, n_folds=2, n_times=1, method='CCA_svd')

Compute the dissimilarity between two datasets by first averaging across trial, and using cross-validation.

Parameters:
  • X (np.array) – First dataset (n_trials, n_time, n_chan)

  • X_conds (np.array) – An array of length n_trials containing the condition id for each trial in X

  • Y (np.array) – Second dataset (n_trials, n_time, n_chan)

  • Y_conds (np.array) – An array of length n_trials containing the condition id for each trial in Y

  • n_folds (int) – Number of folds for cross-validation

  • n_times (int) – Number of times to repeat the cross-validation

  • method (str) – Method to use for dissimilarity analysis (‘CCA_svd’, ‘CCA_sklearn’, ‘Procrustes’). Default is ‘CCA_svd’.

Returns:
  • df_score (pd.DataFrame) – DataFrame containing the scores for each fold and time

  • df (pd.DataFrame) – DataFrame containing extra information for CCA methods