coco_pipe.decoding.foundation_models.estimators#

Clone-safe sklearn estimators backed by lazily loaded foundation models.

Classes#

FrozenBackboneTransformer

Target-independent frozen feature extractor suitable for sklearn pipelines.

FoundationClassifier

Lazy trainable foundation-model classifier with grouped validation.

Functions#

clear_frozen_embedding_cache()

Empty the shared frozen-backbone embedding cache.

Module Contents#

coco_pipe.decoding.foundation_models.estimators.clear_frozen_embedding_cache()#

Empty the shared frozen-backbone embedding cache.

Return type:

None

class coco_pipe.decoding.foundation_models.estimators.FrozenBackboneTransformer(model_key, backend='auto', device='auto', pooling='mean', sfreq=None, ch_names=None, cache_embeddings=False, backend_kwargs=None)#

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

Target-independent frozen feature extractor suitable for sklearn pipelines.

Parameters:
backend_ = None#
prepared_ = None#
backend = 'auto'#
device = 'auto'#
pooling = 'mean'#
sfreq = None#
ch_names = None#
cache_embeddings = False#
backend_kwargs = None#
fit(X, y=None)#
Parameters:
transform(X)#
Parameters:

X (numpy.ndarray)

Return type:

numpy.ndarray

release_memory()#

Release the fitted backend’s torch modules (see BackendBase).

Return type:

None

class coco_pipe.decoding.foundation_models.estimators.FoundationClassifier(model_key, backend='auto', train_mode='full', device='auto', n_outputs=None, sfreq=None, ch_names=None, trainer=None, lora=None, backend_kwargs=None, checkpoints=None, class_weight='balanced', random_state=42)#

Bases: sklearn.utils.metadata_routing.AutoRequestMixin, sklearn.base.ClassifierMixin, sklearn.base.BaseEstimator

Lazy trainable foundation-model classifier with grouped validation.

Parameters:
backend_ = None#
prepared_ = None#
checkpoint_path_ = None#
backend = 'auto'#
train_mode = 'full'#
device = 'auto'#
n_outputs = None#
sfreq = None#
ch_names = None#
trainer = None#
lora = None#
backend_kwargs = None#
checkpoints = None#
class_weight = 'balanced'#
random_state = 42#
get_metadata_routing()#

Ask for each window’s group in fit (its sample weight is asked for through AutoRequestMixin).

fit(X, y, groups=None, sample_weight=None)#

Fine-tune the model on labelled windows.

Parameters:
  • X (np.ndarray of shape (n_windows, n_channels, n_times)) – Windows in microvolts.

  • y (np.ndarray of shape (n_windows,)) – Class labels.

  • groups (np.ndarray of shape (n_windows,), optional) – The group (e.g. subject) of each window: the validation split keeps each group on one side.

  • sample_weight (np.ndarray of shape (n_windows,), optional) – Weights of each window’s loss, rescaled to average 1, e.g. coco_pipe.utils.balanced_sample_weights(). Needs class_weight=None.

Returns:

self

Return type:

FoundationClassifier

Raises:

ValueError – If sample_weight is given with a class_weight.

restore_checkpoint(path=None)#

Restore every saved backend component into this fitted estimator.

Parameters:

path (str | pathlib.Path | None)

predict(X)#
Parameters:

X (numpy.ndarray)

Return type:

numpy.ndarray

predict_proba(X)#
Parameters:

X (numpy.ndarray)

Return type:

numpy.ndarray

transform(X)#

Return the window embeddings of the fitted backbone.

The vector FrozenBackboneTransformer gives, here from the weights this fit left; for CTNet, Deep4Net, EEGNet and ShallowFBCSPNet, the flattened input of their final layer. With ExperimentConfig(keep_test_embeddings=True), each outer fold keeps those of its test rows.

Parameters:

X (numpy.ndarray)

Return type:

numpy.ndarray

release_memory()#

Release the fitted backend’s torch modules (see BackendBase).

Return type:

None

get_training_history()#
Return type:

list[dict[str, Any]]

get_checkpoint_manifest()#
Return type:

dict[str, Any]

get_model_card_info()#
Return type:

dict[str, Any]

get_artifact_metadata()#
Return type:

dict[str, Any]