terra.gene_embed_dataset#
- terra.gene_embed_dataset(dataset, model_folder_path, emb_layer=None, cell_gene_ids=[], neighborhood_gene_ids=[], batch_size=128, pin_memory=False, num_workers=12, return_gene=False, return_gene_per_data=False, compute_cosine_with_list=[], return_distance=False, return_cosine_sim=False, return_receptor_average=False, include_spatial_cell_emb=True, description='', ignore_spc_tokens=True)#
Compute per-gene embeddings or co-occurrence statistics over a tokenized dataset.
- Parameters:
dataset (
Dataset) – Tokenized Hugging Face dataset.model_folder_path (
str) – Path to the folder containing the model config, token dictionary, and normalization factors.emb_layer (
Optional[int] (default:None)) – Layer for which to retrieve the embedding.cell_gene_ids (
list(default:[])) – List with gene IDs for which cell gene embeddings will be retrieved.neighborhood_gene_ids (
list(default:[])) – List with gene IDs for which neighborhood gene embeddings will be retrived.batch_size (
int(default:128)) – Dataloader param.pin_memory (
bool(default:False)) – Dataloader param.num_workers (
int(default:12)) – Number of workers used.return_gene (
bool(default:False)) – IfTruewill return per-gene cell and neighborhood embeddings (and, wheninclude_spatial_cell_embisTrue, spatially contextualized cell gene embeddings).return_gene_per_data (
bool(default:False)) – IfTruewill return gene_embedding for each gene per dataset.compute_cosine_with_list (
list[str] (default:[])) – A list that defines the items with which we want to compute cosine similarity. It could have value ofcellor/andneighborhood.return_distance (
bool(default:False)) – IfTruewill compute and return distance between cosine sim of cell_neb and cell_cell matrix.return_cosine_sim (
bool(default:False)) – IfTruewill compute and return cosine_sim matrix.return_receptor_average (
bool(default:False)) – IfTruewill compute and return receptor average embeddings for cell-neighborhood gene pairs.include_spatial_cell_emb (
bool(default:True)) – IfTrue, also return gene embeddings for spatially contextualized cell embedding that attends to the neighborhood.description (
str(default:'')) – Description for task that is currently using this function.ignore_spc_tokens (
bool(default:True)) – Whether to ignore special tokens when retrieving layer embeddings.
- Returns:
output (
tuple | dict | None) – The return value depends on which flag is set (checked in this order): - Ifreturn_geneisTrue: a tuple of per-gene embedding dictionaries keyed by gene ID. Withinclude_spatial_cell_embit is(cell_gene_emb, neighborhood_gene_emb, spatial_cell_gene_emb); otherwise(cell_gene_emb, neighborhood_gene_emb). - Ifreturn_gene_per_dataisTrue: a tuple of per-gene(sum, count)accumulator dictionaries. Withinclude_spatial_cell_embit is(cell_gene_emb_per_data, neighborhood_gene_emb_per_data, spatial_cell_gene_emb_per_data); otherwise the first two. - Ifreturn_cosine_simisTrue: a single dict mapping each entry ofcompute_cosine_with_listto(sum_cos_sim, pair_count, cell_count). - Ifreturn_distanceisTrue: a 2-tuple of lists(emd_list, emd_matrix_list). - Ifreturn_receptor_averageisTrue: a single dict keyed by(cell_gene_id, neighborhood_gene_id, 'present'|'absent')mapping to[sum, count]. - If none of the above flags is set:None.