coco_pipe.io.embeddings#
BIDS-compatible derivative I/O for foundation-model embeddings.
Functions#
|
Return the JSON sidecar paired with an embedding NPZ. |
Validate arrays, shape consistency, and sidecar presence. |
|
|
Atomically write an embedding NPZ and matching JSON sidecar. |
|
Write independent pooled and optional token derivatives from result. |
|
Discover valid |
|
Return the canonical recording/window ID for pooled or token artifacts. |
|
Load embedding artifacts into a DataContainer. |
|
Path to the merged per-(model, condition) embedding table. |
|
Load one merged per-(model, condition) embedding table as a 2-D container. |
|
Write a JSON run manifest indexing successful and failed artifacts. |
|
Write a BIDS derivative dataset_description.json. |
Module Contents#
- coco_pipe.io.embeddings.embedding_sidecar_path(path)#
Return the JSON sidecar paired with an embedding NPZ.
- Parameters:
path (str | pathlib.Path)
- Return type:
- coco_pipe.io.embeddings.validate_embedding_derivative(path)#
Validate arrays, shape consistency, and sidecar presence.
- Parameters:
path (str | pathlib.Path)
- Return type:
- coco_pipe.io.embeddings.save_embedding_derivative(result, path, metadata=None, overwrite=False)#
Atomically write an embedding NPZ and matching JSON sidecar.
- Parameters:
result (Any)
path (str | pathlib.Path)
metadata (collections.abc.Mapping[str, Any] | None)
overwrite (bool)
- Return type:
- coco_pipe.io.embeddings.save_embedding_outputs(result, embedding_path, *, token_path=None, metadata=None, pooled_metadata=None, overwrite=False)#
Write independent pooled and optional token derivatives from result.
embedding_pathandtoken_pathare independent derivative locations. Whenresult.token_embeddingsis present,token_pathis required.metadataapplies to both outputs, whilepooled_metadataapplies only to the pooled derivative. This keeps pooling-specific representation identity out of pooling-independent native-token metadata. Complete outputs are left untouched unlessoverwriteis true; a partial NPZ/sidecar pair is repaired.- Parameters:
result (Any)
embedding_path (str | pathlib.Path)
token_path (str | pathlib.Path | None)
metadata (collections.abc.Mapping[str, Any] | None)
pooled_metadata (collections.abc.Mapping[str, Any] | None)
overwrite (bool)
- Return type:
tuple[pathlib.Path, pathlib.Path | None]
- coco_pipe.io.embeddings.discover_embedding_derivatives(root, model_key=None, *, kind='embedding')#
Discover valid
"embedding"or"token"NPZ artifacts under a root.Artifact kind is identified by filename suffix (see
ARTIFACT_SUFFIX), whichsave_embedding_derivativeenforces. The run manifest (if present) may index either kind. When a requested model is not in that manifest, a recursive scan also finds independently materialized model variants.- Parameters:
root (str | pathlib.Path)
model_key (str | None)
kind (str)
- Return type:
- coco_pipe.io.embeddings.embedding_observation_id(metadata, path, window_position=None)#
Return the canonical recording/window ID for pooled or token artifacts.
- Parameters:
metadata (collections.abc.Mapping[str, Any])
path (str | pathlib.Path)
window_position (int | None)
- Return type:
- coco_pipe.io.embeddings.load_embedding_derivatives(paths, representation='recording', aggregate_by=None, model_key=None)#
Load embedding artifacts into a DataContainer.
representationselects both the on-disk array and the output rank:"recording"— the pooledrecording_embedding(2-Dobs x feature)"epoch"— per-epochwindow_embeddings(2-Dobs x feature)"token"— native per-epoch feature tensors from separate*_tokens.npzartifacts (arbitrary rank, beginning withobs)
A coarser
"subject"level is produced by the merge step, not here (it pools across recordings). Kinds are discovered by filename suffix (seeARTIFACT_SUFFIX); token and embedding artifacts are separate files that join only by shared observation ID.- Parameters:
paths (collections.abc.Sequence[str | pathlib.Path] | str | pathlib.Path)
representation (str)
aggregate_by (str | None)
model_key (str | None)
- Return type:
- coco_pipe.io.embeddings.combined_embedding_table_path(derivative_root, model_key, condition, representation='recording')#
Path to the merged per-(model, condition) embedding table.
Mirrors the
combined/<model>_<condition>_<representation>_embeddings.parquetlayout written by the merge step.representationis one ofAGGREGATION_LEVELS.- Parameters:
derivative_root (str | pathlib.Path)
model_key (str)
condition (str)
representation (str)
- Return type:
- coco_pipe.io.embeddings.load_combined_embedding_table(derivative_root, model_key, condition, representation='recording', aggregate_by=None)#
Load one merged per-(model, condition) embedding table as a 2-D container.
This reads the single parquet the merge step already materialized instead of rescanning every per-recording NPZ and filtering — one table read per condition, the same access pattern descriptors use.
representationis one ofAGGREGATION_LEVELS. The table carries id columns (subject/session/run/condition/recording_id/model_key[/window_index]) plusembedding_*feature columns; the former become coords, the latterX.- Parameters:
- Return type:
- coco_pipe.io.embeddings.write_embedding_manifest(root, records)#
Write a JSON run manifest indexing successful and failed artifacts.
- Parameters:
root (str | pathlib.Path)
records (collections.abc.Iterable[collections.abc.Mapping[str, Any]])
- Return type:
- coco_pipe.io.embeddings.write_embedding_dataset_description(root, name, bids_version, generated_by, source_datasets=None)#
Write a BIDS derivative dataset_description.json.
- Parameters:
root (str | pathlib.Path)
name (str)
bids_version (str)
generated_by (collections.abc.Sequence[collections.abc.Mapping[str, Any]])
source_datasets (collections.abc.Sequence[collections.abc.Mapping[str, Any]] | None)
- Return type: