coco_pipe.transforms.subject_alignment#
Subject alignment of embeddings: LEACE, Euclidean and Riemannian alignment.
Attributes#
Classes#
Base class for subject-alignment estimators. |
|
Erase the directions in which subjects differ most (rank-limited LEACE). |
|
Per-subject mean or CORAL alignment, including unseen subjects. |
|
Per-subject Riemannian recentering followed by tangent projection. |
Functions#
|
The feature covariance of each window's tokens, shrunk to be positive definite. |
|
Instantiate a registered subject transform by name. |
Module Contents#
- class coco_pipe.transforms.subject_alignment.SubjectAlignment#
Bases:
sklearn.base.BaseEstimator,sklearn.base.TransformerMixinBase class for subject-alignment estimators.
labelis the transform’s part of thedesclabel of the embedding files it writes (seeembedding_label()).- name = 'identity'#
- label = 'Identity'#
- get_metadata_routing()#
Ask for each row’s subject in
fitandtransform, and for its condition infitwhenfittakes one.
- class coco_pipe.transforms.subject_alignment.LeaceEraser(*, eta2=0.5, max_rank=None)#
Bases:
SubjectAlignmentErase the directions in which subjects differ most (rank-limited LEACE).
Each direction
vof the feature space has a subject eta^2, the share of its variance that lies between subjects: the solutions ofS_b v = eta^2 S_t v, withS_tthe total covariance andS_bthe between-subject covariance of the subject means. The eraser removes therdirections of largest eta^2 with LEACE’s oblique projectorP = 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.ris the number of directions with eta^2 of at leasteta2, at mostmax_rank:eta2leaves alone the directions that are mostly within-subject variance,max_rankbounds how much is erased, andeta2=0withmax_rankerases exactly that many.With
conditioninfit(),S_bis 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:
- 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 firstrank_are erased.(X - mu_) @ components_.Tis 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:
SubjectAlignmentPer-subject mean or CORAL alignment, including unseen subjects.
Xis(window, feature)or tokens(window, ..., feature): a subject’s mean, and for"coral"its feature covariance, are taken over all its tokens. Withchannel_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. Withshrinkage,"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 ofX.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 conditionasgroupsto take a reference per condition.- mode = 'coral'#
- name = 'ea_coral'#
- shrinkage = True#
- channel_axis = None#
- 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 belowd(e.g. 64 tokens of 256 features), so the covariance is moved byshrinkagetoward the identity scaled to its mean variance: every eigenvalue is then at leastshrinkage * trace / d, positive, as the Riemannian mean and the tangent map need. A window whose tokens are all the same is refused.- Parameters:
tokens (numpy.ndarray)
shrinkage (float)
- Return type:
- class coco_pipe.transforms.subject_alignment.RiemannAlign(*, shrinkage=0.1)#
Bases:
SubjectAlignmentPer-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#