77import curies
88import numpy as np
99import pandas as pd
10+ from tqdm import tqdm
11+ from typing_extensions import Unpack
1012
11- from pyobo .api .names import get_definition , get_name , get_references
13+ from pyobo .api .names import get_definition , get_id_name_mapping , get_name
14+ from pyobo .api .utils import get_version_from_kwargs
15+ from pyobo .constants import GetOntologyKwargs , check_should_force
16+ from pyobo .identifier_utils import wrap_norm_prefix
17+ from pyobo .utils .path import CacheArtifact , get_cache_path
1218
1319if TYPE_CHECKING :
1420 import sentence_transformers
@@ -31,38 +37,61 @@ def get_text_embedding_model() -> sentence_transformers.SentenceTransformer:
3137
3238def _get_text (
3339 reference : str | curies .Reference | curies .ReferenceTuple ,
40+ / ,
41+ * ,
42+ name : str | None = None ,
43+ ** kwargs : Unpack [GetOntologyKwargs ],
3444) -> str | None :
35- name = get_name (reference )
45+ if name is None :
46+ name = get_name (reference , ** kwargs )
3647 if name is None :
3748 return None
38- description = get_definition (reference )
49+ description = get_definition (reference , ** kwargs )
3950 if description :
4051 name += " " + description
4152 return name
4253
4354
55+ @wrap_norm_prefix
4456def get_text_embeddings_df (
4557 prefix : str ,
4658 * ,
4759 model : sentence_transformers .SentenceTransformer | None = None ,
60+ ** kwargs : Unpack [GetOntologyKwargs ],
4861) -> pd .DataFrame :
4962 """Get embeddings for all entities in the resource.
5063
5164 :param prefix: A reference, either as a string or Reference object
5265 :param model: A sentence transformer model. Defaults to ``all-MiniLM-L6-v2`` if not
5366 given.
67+ :param kwargs: The keyword arguments to forward to ontology getter functions for
68+ names, definitions, and version
69+
70+ :returns: A pandas dataframe with an index representing local unique identifiers and
71+ columns for the values of the model returned vectors
5472 """
73+ path = get_cache_path (
74+ prefix , CacheArtifact .embeddings , version = get_version_from_kwargs (prefix , kwargs )
75+ )
76+ if path .is_file () and not check_should_force (kwargs ):
77+ df = pd .read_csv (path , sep = "\t " ).set_index (0 )
78+ return df
79+
80+ id_to_name = get_id_name_mapping (prefix , ** kwargs )
81+
5582 luids , texts = [], []
56- for reference in get_references ( prefix ):
57- text = _get_text (reference )
83+ for identifier , name in tqdm ( id_to_name . items (), desc = f"[ { prefix } ] constructing text" ):
84+ text = _get_text (curies . ReferenceTuple ( prefix , identifier ), name = name , ** kwargs )
5885 if text is None :
5986 continue
60- luids .append (reference . identifier )
87+ luids .append (identifier )
6188 texts .append (text )
6289 if model is None :
6390 model = get_text_embedding_model ()
64- res = model .encode (texts )
65- return pd .DataFrame (res , index = luids )
91+ res = model .encode (texts , show_progress_bar = True )
92+ df = pd .DataFrame (res , index = luids )
93+ df .to_csv (path , sep = "\t " ) # index is important here!
94+ return df
6695
6796
6897def get_text_embedding (
0 commit comments