mgbam commited on
Commit
f3dd8bc
·
verified ·
1 Parent(s): 5417053

Update mcp/knowledge_graph.py

Browse files
Files changed (1) hide show
  1. mcp/knowledge_graph.py +48 -30
mcp/knowledge_graph.py CHANGED
@@ -1,43 +1,61 @@
1
  # mcp/knowledge_graph.py
2
 
3
- from streamlit_agraph import agraph, Node, Edge, Config
 
4
 
5
- def build_agraph(papers, umls, drugs):
6
  """
7
- Build nodes and edges for streamlit-agraph visualization.
 
8
  """
 
9
  nodes, edges = [], []
10
 
11
- # Map concepts
12
  for c in umls:
13
- if c.get("cui"):
14
- cid = f"concept_{c['cui']}"
15
- nodes.append(Node(id=cid, label=c["name"], size=25, color="#00b894"))
16
-
17
- # Map drugs
18
- for i, drug_report in enumerate(drugs):
19
- if drug_report:
20
- did = f"drug_{i}"
21
- dname = drug_report.get("drug_name", f"drug_{i}")
22
- nodes.append(Node(id=did, label=dname, size=25, color="#d35400"))
23
-
24
- # Map papers
25
- for i, p in enumerate(papers):
26
- pid = f"paper_{i}"
27
- nodes.append(Node(id=pid, label=f"P{i+1}", tooltip=p["title"], size=15, color="#0984e3"))
28
- # connect to concepts
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  for c in umls:
30
- if c["name"].lower() in (p["title"] + p["summary"]).lower():
31
- edges.append(Edge(source=pid, target=f"concept_{c['cui']}", label="mentions"))
32
- # connect to drugs
33
- for j, drug_report in enumerate(drugs):
34
- dname = drug_report.get("drug_name", "")
35
- if dname and dname.lower() in (p["title"] + p["summary"]).lower():
36
- edges.append(Edge(source=pid, target=f"drug_{j}", label="mentions"))
37
-
 
38
  config = Config(
39
  width="100%", height="600", directed=False,
40
- nodeHighlightBehavior=True, highlightColor="#f0a",
41
- collapsible=True
 
42
  )
43
  return nodes, edges, config
 
1
  # mcp/knowledge_graph.py
2
 
3
+ from streamlit_agraph import Node, Edge, Config
4
+ import re
5
 
6
+ def build_agraph(papers, umls, drug_safety):
7
  """
8
+ Build interactive agraph nodes and edges.
9
+ Handles drug_safety entries that may be dict or list.
10
  """
11
+
12
  nodes, edges = [], []
13
 
14
+ # Add UMLS concept nodes
15
  for c in umls:
16
+ cui = c.get("cui")
17
+ name = c.get("name", "")
18
+ if cui and name:
19
+ nid = f"concept_{cui}"
20
+ nodes.append(Node(id=nid, label=name, size=25, color="#00b894"))
21
+
22
+ # Add drug nodes, handling list or dict
23
+ drug_names = []
24
+ for i, dr in enumerate(drug_safety):
25
+ if not dr:
26
+ continue
27
+
28
+ # Normalize to single dict
29
+ recs = dr if isinstance(dr, list) else [dr]
30
+ for j, rec in enumerate(recs):
31
+ # Attempt to extract a drug name
32
+ dn = rec.get("drug_name") or rec.get("patient", {}).get("drug", "") or rec.get("medicinalproduct", "")
33
+ dn = dn or f"drug_{i}_{j}"
34
+ did = f"drug_{i}_{j}"
35
+ drug_names.append((did, dn))
36
+ nodes.append(Node(id=did, label=dn, size=25, color="#d35400"))
37
+
38
+ # Add paper nodes and link to concepts & drugs
39
+ for pi, p in enumerate(papers):
40
+ pid = f"paper_{pi}"
41
+ nodes.append(Node(id=pid, label=f"P{pi+1}", tooltip=p["title"], size=15, color="#0984e3"))
42
+
43
+ text = f"{p.get('title','')} {p.get('summary','')}".lower()
44
+ # Link to concepts
45
  for c in umls:
46
+ cname = c.get("name", "")
47
+ cui = c.get("cui")
48
+ if cname and cui and cname.lower() in text:
49
+ edges.append(Edge(source=pid, target=f"concept_{cui}", label="mentions"))
50
+ # Link to drugs
51
+ for did, dn in drug_names:
52
+ if dn.lower() in text:
53
+ edges.append(Edge(source=pid, target=did, label="mentions"))
54
+
55
  config = Config(
56
  width="100%", height="600", directed=False,
57
+ nodeHighlightBehavior=True, highlightColor="#f1c40f",
58
+ collapsible=True,
59
+ node={"labelProperty": "label"}
60
  )
61
  return nodes, edges, config