"""RAG branch: hierarchical, information-weighted search over the disease profiles. For each symptom of the patient, a disease receives the information content (IC) of the most specific common ancestor; scores are summed and slightly penalised for very long profiles. This is a variant of the Phenomizer / Resnik similarity. """ from __future__ import annotations from collections import defaultdict import numpy as np from .knowledge_base import KnowledgeBase class HierarchicalRAG: def __init__(self, kb: KnowledgeBase, alpha: float = 0.25): self.kb = kb self.alpha = alpha self.ids = [d["code"] for d in kb.diseases] extended = [kb.expand([s["hpo"] for s in d["symptoms"]]) for d in kb.diseases] index: dict[str, list[int]] = defaultdict(list) for i, terms in enumerate(extended): for t in terms: index[t].append(i) self.index = {t: np.array(v) for t, v in index.items()} self.n = len(self.ids) self.ic = {t: -np.log(len(v) / self.n) for t, v in self.index.items()} self.size = np.array([len(e) for e in extended]) def scores(self, codes: list[str]) -> np.ndarray: total = np.zeros(self.n) for h in codes: best = np.zeros(self.n) for a in self.kb.ancestors(h): if a in self.index: idx = self.index[a] best[idx] = np.maximum(best[idx], self.ic[a]) total += best return total / np.log(self.size + np.e) ** self.alpha def search(self, codes: list[str], k: int = 20) -> list[str]: """HPO codes -> the k best diseases (distinct groups), most likely first.""" scores = self.scores(codes) result, seen = [], set() for i in np.argsort(-scores): g = self.kb.group(self.ids[i]) if g not in seen: seen.add(g) result.append(g) if len(result) == k: break return result