File size: 2,027 Bytes
613af80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""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