coco_pipe.transforms.subject_alignment#

Subject alignment of embeddings: LEACE, Euclidean and Riemannian alignment.

Attributes#

Classes#

SubjectAlignment

Base class for subject-alignment estimators.

LeaceEraser

Erase the directions in which subjects differ most (rank-limited LEACE).

EuclideanAlign

Per-subject mean or CORAL alignment, including unseen subjects.

RiemannAlign

Per-subject Riemannian recentering followed by tangent projection.

Functions#

tokens_to_covariances(tokens, *[, shrinkage])

The feature covariance of each window's tokens, shrunk to be positive definite.

make_subject_transform(name, **params)

Instantiate a registered subject transform by name.

Module Contents#

class coco_pipe.transforms.subject_alignment.SubjectAlignment#

Bases: sklearn.base.BaseEstimator, sklearn.base.TransformerMixin

Base class for subject-alignment estimators.

label is the transform’s part of the desc label of the embedding files it writes (see embedding_label()).

name = 'identity'#
label = 'Identity'#
fold_local: bool = False#
get_metadata_routing()#

Ask for each row’s subject in fit and transform, and for its condition in fit when fit takes one.

fingerprint()#
Return type:

str

class coco_pipe.transforms.subject_alignment.LeaceEraser(*, eta2=0.5, max_rank=None)#

Bases: SubjectAlignment

Erase the directions in which subjects differ most (rank-limited LEACE).

Each direction v of the feature space has a subject eta^2, the share of its variance that lies between subjects: the solutions of S_b v = eta^2 S_t v, with S_t the total covariance and S_b the between-subject covariance of the subject means. The eraser removes the r directions of largest eta^2 with LEACE’s oblique projector P = I - S_t V_r V_r^T (V_r^T S_t V_r = I): no linear function of the erased data then varies along them, and the rest of the space is kept. Erasing every direction in which subject means differ (full LEACE) leaves nothing once there are more subjects than features.

r is the number of directions with eta^2 of at least eta2, at most max_rank: eta2 leaves alone the directions that are mostly within-subject variance, max_rank bounds how much is erased, and eta2=0 with max_rank erases exactly that many.

With condition in fit(), S_b is taken from the subject x condition cell means around their condition’s mean, so a subject with more windows of one condition does not count that as identity, and a difference between conditions shared by the subjects is kept.

The projector is fixed after fit(), so it applies to subjects it has not seen, without their groups or conditions.

Parameters:
  • eta2 (float, default=0.5) – Erase the directions whose subject eta^2 is at least this.

  • max_rank (int or None, default=None) – Erase at most this many of them, the largest eta^2 first.

Variables:
  • rank (int) – Number of directions erased.

  • eta2 (ndarray of shape (n_features,)) – Subject eta^2 of every direction, largest first.

  • components (ndarray of shape (n_features, n_features)) – Every direction as a row, in the order of eta2_; the first rank_ are erased. (X - mu_) @ components_.T is each row’s score on them, with unit total variance.

  • n_subjects (int) – Number of subjects fitted.

References

Belrose, N. et al. (2023). LEACE: Perfect linear concept erasure in closed form. NeurIPS.

name = 'leace'#
label = 'Leace'#
fold_local = True#
eta2 = 0.5#
max_rank = None#
fit(X, y=None, groups=None, condition=None)#

Find the directions to erase.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – The vectors, e.g. one embedding per window.

  • y (None) – Ignored.

  • groups (array-like of shape (n_samples,)) – The subject of each row.

  • condition (array-like of shape (n_samples,), optional) – The condition of each row, e.g. eyes open or closed.

Raises:

ValueError – If the eraser would remove 95% of the feature directions or more.

transform(X, groups=None)#
fit_transform(X, y=None, groups=None, condition=None, **fit_params)#

Fit the eraser and erase the same observations.

class coco_pipe.transforms.subject_alignment.EuclideanAlign(mode='coral', *, shrinkage=True, channel_axis=None)#

Bases: SubjectAlignment

Per-subject mean or CORAL alignment, including unseen subjects.

X is (window, feature) or tokens (window, ..., feature): a subject’s mean, and for "coral" its feature covariance, are taken over all its tokens. With channel_axis, each channel’s tokens are centered on their own mean, and "coral" whitens every channel with the subject’s one covariance of the centered tokens. With shrinkage, "coral" scales each subject’s output to the identity’s mean squared norm (the number of features), so it does not depend on how many samples the subject has. The output keeps the shape and float dtype of X.

Each subject loses its mean, so a trait constant within a subject (a diagnosis, age) is removed with it; and when a subject’s samples mix conditions in uneven shares, its reference moves with its share. Use it for targets that vary within a subject, or pass subject x condition as groups to take a reference per condition.

Parameters:
  • mode (str)

  • shrinkage (bool)

  • channel_axis (int | None)

mode = 'coral'#
name = 'ea_coral'#
shrinkage = True#
channel_axis = None#
property label: str#

EaMean or EaCoral, followed by Ch with channel_axis.

Return type:

str

fit(X, y=None, groups=None)#
transform(X, groups=None)#
fit_transform(X, y=None, groups=None, **fit_params)#

Fit subject statistics and transform the same observations.

coco_pipe.transforms.subject_alignment.tokens_to_covariances(tokens, *, shrinkage=0.1)#

The feature covariance of each window’s tokens, shrunk to be positive definite.

Each window’s tokens are centered on their mean first, so the matrix describes how the tokens co-vary, not where they sit. A window has rank at most T - 1, often below d (e.g. 64 tokens of 256 features), so the covariance is moved by shrinkage toward the identity scaled to its mean variance: every eigenvalue is then at least shrinkage * trace / d, positive, as the Riemannian mean and the tangent map need. A window whose tokens are all the same is refused.

Parameters:
Return type:

numpy.ndarray

class coco_pipe.transforms.subject_alignment.RiemannAlign(*, shrinkage=0.1)#

Bases: SubjectAlignment

Per-subject Riemannian recentering followed by tangent projection.

Parameters:

shrinkage (float)

name = 'ra'#
label = 'Ra'#
shrinkage = 0.1#
fit(X, y=None, groups=None)#
transform(X, groups=None)#
fit_transform(X, y=None, groups=None, **fit_params)#

Fit subject references and transform the same token windows.

coco_pipe.transforms.subject_alignment.VECTOR_TRANSFORMS#
coco_pipe.transforms.subject_alignment.TOKEN_TRANSFORMS#
coco_pipe.transforms.subject_alignment.make_subject_transform(name, **params)#

Instantiate a registered subject transform by name.

Parameters:
  • name (str)

  • params (Any)

Return type:

SubjectAlignment