| from __future__ import annotations | |
| from typing import Dict, List, Tuple | |
| import numpy as np | |
| from .faiss_index import index_search | |
| def search_index(index, query_embeddings: np.ndarray, k: int) -> Tuple[np.ndarray, np.ndarray]: | |
| scores, indices = index_search(index, query_embeddings, k) | |
| return scores, indices | |
| def recall_at_k( | |
| retrieved_indices: np.ndarray, | |
| ground_truth_indices: List[int], | |
| k: int, | |
| ) -> float: | |
| if len(ground_truth_indices) == 0: | |
| return 0.0 | |
| hits = 0 | |
| total = 0 | |
| for row, gt in zip(retrieved_indices, ground_truth_indices): | |
| if gt < 0: | |
| continue | |
| total += 1 | |
| if gt in row[:k]: | |
| hits += 1 | |
| if total == 0: | |
| return 0.0 | |
| return hits / float(total) | |
| def build_smiles_to_index(smiles: List[str]) -> Dict[str, int]: | |
| lookup: Dict[str, int] = {} | |
| for i, smi in enumerate(smiles): | |
| if smi not in lookup: | |
| lookup[smi] = i | |
| return lookup | |