coco_pipe.decoding.foundation_models.extraction#
Reusable extraction and preflight utilities for EEG foundation models.
Classes#
Preflight result for one model/training-mode/input combination. |
|
Window embeddings plus extraction provenance. |
|
Compose model loading, metadata binding, extraction, and pooling. |
Functions#
|
Check registry support and obvious signal incompatibilities without loading. |
|
Drop MNE's inclusive-endpoint extra sample from epoched windows. |
|
Extract one recording with several extractors, resampling it once. |
Module Contents#
- class coco_pipe.decoding.foundation_models.extraction.CapabilityResult#
Preflight result for one model/training-mode/input combination.
- coco_pipe.decoding.foundation_models.extraction.check_capability(model_key, train_mode, sfreq=None, ch_names=None, backend='auto', backend_kwargs=None)#
Check registry support and obvious signal incompatibilities without loading.
- Parameters:
model_key (str)
train_mode (str)
sfreq (float | None)
ch_names (collections.abc.Sequence[str] | None)
backend (str)
backend_kwargs (collections.abc.Mapping[str, Any] | None)
- Return type:
- coco_pipe.decoding.foundation_models.extraction.normalize_inclusive_endpoint(container, *, segment_duration, expected_sfreq, model_key='model', on_mismatch='error')#
Drop MNE’s inclusive-endpoint extra sample from epoched windows.
MNE epochs span
[tmin, tmax]inclusively, yieldingexpected_n_times + 1samples; foundation backends want the half-open count. This trims the trailing sample (recording it inmeta) when the off-by-one is observed, sopreparedoes not raise on the mismatch.Returns
(container, None)when the window is already correct or was normalized. On any other length mismatch, returns(None, reason)ifon_mismatch == "skip"and otherwise raisesValueError.- Parameters:
container (coco_pipe.io.DataContainer)
segment_duration (float)
expected_sfreq (float)
model_key (str)
on_mismatch (str)
- Return type:
tuple[coco_pipe.io.DataContainer | None, str | None]
- class coco_pipe.decoding.foundation_models.extraction.FoundationEmbeddingResult#
Window embeddings plus extraction provenance.
token_embeddingsholds the native backbone feature tensor, without reshaping or normalization, and is populated only when the extractor is built withstore_tokens=True.channel_embeddings(window, channel, feature) holds each channel’s tokens averaged over time patches, andpatch_embeddings(window, time_patch, feature) each time patch’s tokens averaged over channels. Each is populated when the backend’s tokens have its axis and another one to average over (CBraMod, REVE and LaBraM without channel interpolation have both), unless the extractor caches window embeddings. They are not normalized, whatevernormalize_embeddingssays.extra_window_embeddingsmaps each of the extractor’sextra_poolingsto its window vectors, pooled from the same tokens aswindow_embeddingsand normalized like them.- window_embeddings: numpy.ndarray#
- window_start: numpy.ndarray#
- window_stop: numpy.ndarray#
- window_index: numpy.ndarray#
- token_embeddings: numpy.ndarray | None = None#
- channel_embeddings: numpy.ndarray | None = None#
- patch_embeddings: numpy.ndarray | None = None#
- extra_window_embeddings: dict[str, numpy.ndarray]#
- class coco_pipe.decoding.foundation_models.extraction.FoundationEmbeddingExtractor(model_key, *, backend='auto', device='auto', pretrained=True, seed=None, pooling='mean', extra_poolings=(), normalize_embeddings=True, resample=True, cache_embeddings=False, store_tokens=False, batch_size=None, backend_kwargs=None, model=None)#
Compose model loading, metadata binding, extraction, and pooling.
poolingmakes the window vectors;extra_poolingsnames other poolings the backend offers (("attention",)for REVE), whose vectors come from the same forward pass, inextra_window_embeddings.pretrained=Falsewith an integerseedgives the model’s architecture random weights drawn from the seed, the same for every recording. The metadata records the weights of the model used (weights, andseedfor random ones).- Parameters:
model_key (str)
backend (str)
device (str)
pretrained (bool)
seed (int | None)
pooling (str)
extra_poolings (collections.abc.Sequence[str])
normalize_embeddings (bool)
resample (bool)
cache_embeddings (bool)
store_tokens (bool)
batch_size (int | None)
backend_kwargs (collections.abc.Mapping[str, Any] | None)
model (Any | None)
- backend = 'auto'#
- device = 'auto'#
- pretrained = True#
- seed = None#
- pooling = 'mean'#
- extra_poolings#
- normalize_embeddings = True#
- resample = True#
- cache_embeddings = False#
- store_tokens = False#
- batch_size = None#
- backend_kwargs#
- model = None#
- clear_cache()#
Drop all memoized window embeddings.
- Return type:
None
- extract(epochs, *, signal_metadata, window_start=None, window_stop=None, window_index=None, metadata=None)#
Extract one embedding per input window.
window_indexis the number of each window in its recording; by default the windows are numbered in the order given.- Parameters:
epochs (numpy.ndarray)
signal_metadata (coco_pipe.decoding._specs.SignalMetadata)
window_start (collections.abc.Sequence[int] | None)
window_stop (collections.abc.Sequence[int] | None)
window_index (collections.abc.Sequence[int] | None)
metadata (collections.abc.Mapping[str, Any] | None)
- Return type:
- extract_recording(recording)#
Extract the windows of one recording the BIDS loader read, by condition.
recordingholds the loader’s rows of one recording, with theirconditionandepochcoordinates. Each condition’s result numbers its windows by their epoch, and their samples count as if the epochs were back to back.write_recording_embeddings()takes the results.extract_weight_sets()extracts a recording with several extractors, resampling it once.- Parameters:
recording (coco_pipe.io.DataContainer)
- Return type:
- coco_pipe.decoding.foundation_models.extraction.extract_weight_sets(extractors, recording)#
Extract one recording with several extractors, resampling it once.
The extractors are typically one model’s weight sets, its pretrained weights and random ones of several seeds, each keeping its model loaded. Each condition’s windows are resampled once for all the extractors whose models take the same rate, instead of once per extractor; the models’ own input scaling stays theirs. Returns, in the order of
extractors, what each one’sextract_recording()returns.- Parameters:
extractors (collections.abc.Sequence[FoundationEmbeddingExtractor])
recording (coco_pipe.io.DataContainer)
- Return type: