coco_pipe.io.write_recording_embeddings#

coco_pipe.io.write_recording_embeddings(results, root, *, label, source, source_dataset, bad_channels=(), token_dtype='float32', skip_up_to_date=False)#

Write one recording’s embeddings, one file per view.

Each view the results hold (window_embeddings, token_embeddings, and so on, see EMBEDDING_VIEWS) goes to its own file, named by embedding_path() after the source recording, with the label embedding_label() gives for the weights and pooling: e.g. baseRandom3 for random weights of seed 3. The window vectors of each other pooling (extra_window_embeddings, e.g. REVE’s attention read-out) go to a window file of their own label (baseAttention), whose settings name that pooling. A file holds every condition, its windows in epoch order. Nothing is written if any check fails, and each file is written to a temporary name and then moved into place, so an existing file is replaced whole or not at all.

Parameters:
  • results (mapping of str to FoundationEmbeddingResult) – The extractor’s result for each condition of the recording. Its window_index holds the epoch numbers.

  • root (path-like) – The derivative dataset, e.g. derivatives/coco-pipe-cbramod.

  • label (str) – The label of the data embedded, e.g. "base" for epochs labelled desc-base.

  • source (path-like) – The file the epochs were read from, in its datatype folder of a BIDS dataset: the source coordinate of the BIDS loader’s rows, or a BIDSPath’s fpath. Its entities name the recording, and the embedding files go in the same datatype folder.

  • source_dataset (str) – The name of the source dataset in DatasetLinks, e.g. "preproc", for the BIDS URI of source.

  • bad_channels (str | sequence of str) – The recording’s bad channels, or the BIDS loader’s bad_channels coordinate (comma-separated).

  • token_dtype ({"float32", "float16"}) – The dtype of the token file; "float16" halves its size. The other views are float32.

  • skip_up_to_date (bool) – Keep a file that already holds the same windows with the same config_hash, as embeddings_up_to_date() tells, instead of replacing it. Its values are not compared.

Returns:

The path of each label and view, e.g. paths["baseRandom3", "window"].

Return type:

dict of (str, str) to BIDSPath

Raises:

ValueError – If the conditions come from different models or settings (their config_hash), differ in views, poolings, feature size or channels, hold non-finite values or share epoch numbers, if a channel axis does not match model_channels, if a pooling’s name is not letters and digits, if label is not a BIDS label, if source is not a file in a datatype folder of a subject, or if token_dtype is not "float32" or "float16".

Notes

The embeddings variable holds the view, as float32 (tokens as token_dtype; values beyond float16’s range are refused). Along window are the coordinates obs (<recording_id>_epoch-NNNN, as the BIDS loader names observations), recording_id, subject, session, task, run, condition, epoch, start and stop. Along recording are its source (a BIDS URI), sampling rate, channels, bad channels and the channels the model interpolated, zero-filled or dropped. A view with a channel axis names it with the channel coordinate: the channels the model saw (model_channels). The attributes are model_key, config (the extraction settings that are the same for every recording, as JSON text, see EMBEDDING_CONFIG_KEYS), config_hash (their hash) and description (the file’s label); a token file adds token_source and token_feature_axis, the other token axes being the observation axes. Other metadata in the results is not stored.