from __future__ import annotations from typing import Dict, Iterable, Sequence import numpy as np from .diversity import selection_diversity def clustering_metrics(cluster_map: Dict[str, int], hypercluster_map: Dict[int, int]) -> Dict[str, float]: return { "ligand_count": float(len(cluster_map)), "cluster_count": float(len(set(cluster_map.values())) if cluster_map else 0), "hypercluster_count": float(len(set(hypercluster_map.values())) if hypercluster_map else 0), } def score_metrics(scores: Sequence[float]) -> Dict[str, float]: if not scores: return { "evaluated_ligand_count": 0.0, "mean_score": 0.0, "best_score": 0.0, } arr = np.asarray(scores, dtype=float) return { "evaluated_ligand_count": float(arr.size), "mean_score": float(np.mean(arr)), "best_score": float(np.min(arr)), } def enrichment_metrics(scores: Sequence[float], labels: Sequence[int] | None, topk: int = 10) -> Dict[str, float]: if labels is None or not scores or len(labels) != len(scores): return {"topk_hit_rate": 0.0, "enrichment_like": 0.0} idx_sorted = np.argsort(np.asarray(scores, dtype=float)) labels_arr = np.asarray(labels, dtype=int) k = min(topk, len(idx_sorted)) top_hits = int(np.sum(labels_arr[idx_sorted[:k]])) baseline = float(np.mean(labels_arr)) if labels_arr.size else 0.0 hit_rate = top_hits / max(k, 1) enrichment = (hit_rate / baseline) if baseline > 0 else 0.0 return {"topk_hit_rate": float(hit_rate), "enrichment_like": float(enrichment)} def diversity_metric(selected_fingerprints: Iterable[np.ndarray]) -> Dict[str, float]: return {"selection_diversity": selection_diversity(selected_fingerprints)}