Update mcp/graph_utils.py
Browse files- mcp/graph_utils.py +6 -55
mcp/graph_utils.py
CHANGED
|
@@ -1,69 +1,20 @@
|
|
| 1 |
"""
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
Key features
|
| 5 |
-
────────────
|
| 6 |
-
• Accepts edge dictionaries in either Streamlit-agraph or PyVis style:
|
| 7 |
-
{"source": "n1", "target": "n2"} ← agraph
|
| 8 |
-
{"from": "n1", "to": "n2"} ← PyVis
|
| 9 |
-
• Silently skips malformed edges (no KeyError).
|
| 10 |
-
• Provides three public helpers:
|
| 11 |
-
build_nx(nodes, edges) → networkx.Graph
|
| 12 |
-
get_top_hubs(G, k=5) → List[(node_id, degree_centrality)]
|
| 13 |
-
get_density(G) → float (0–1)
|
| 14 |
"""
|
| 15 |
-
|
| 16 |
-
from __future__ import annotations
|
| 17 |
-
from typing import List, Dict, Tuple
|
| 18 |
import networkx as nx
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
# ────────────────────────────────────────────────────────────────────
|
| 22 |
-
# Internal helpers
|
| 23 |
-
# ────────────────────────────────────────────────────────────────────
|
| 24 |
-
def _edge_ends(e: Dict) -> Tuple[str, str] | None:
|
| 25 |
-
"""Return (src, dst) tuple if both ends exist; else None."""
|
| 26 |
-
src = e.get("source") or e.get("from")
|
| 27 |
-
dst = e.get("target") or e.get("to")
|
| 28 |
-
if src and dst:
|
| 29 |
-
return src, dst
|
| 30 |
-
return None
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
# ────────────────────────────────────────────────────────────────────
|
| 34 |
-
# Public API
|
| 35 |
-
# ────────────────────────────────────────────────────────────────────
|
| 36 |
def build_nx(nodes: List[Dict], edges: List[Dict]) -> nx.Graph:
|
| 37 |
-
"""
|
| 38 |
-
Convert agraph / PyVis node+edge dicts into a NetworkX Graph.
|
| 39 |
-
|
| 40 |
-
Nodes: must contain "id" (a unique string)
|
| 41 |
-
Edges: accepted shapes → {"source":, "target":} or {"from":, "to":}
|
| 42 |
-
"""
|
| 43 |
G = nx.Graph()
|
| 44 |
-
|
| 45 |
-
# Add nodes with label attribute (used by Metrics tab)
|
| 46 |
for n in nodes:
|
| 47 |
-
G.add_node(n["id"]
|
| 48 |
-
|
| 49 |
-
# Add edges (skip malformed)
|
| 50 |
for e in edges:
|
| 51 |
-
|
| 52 |
-
if ends:
|
| 53 |
-
G.add_edge(*ends)
|
| 54 |
-
|
| 55 |
return G
|
| 56 |
|
|
|
|
|
|
|
| 57 |
|
| 58 |
def get_top_hubs(G: nx.Graph, k: int = 5) -> List[Tuple[str, float]]:
|
| 59 |
-
"""
|
| 60 |
-
Return top-k nodes by degree-centrality.
|
| 61 |
-
Example output: [('TP53', 0.42), ('EGFR', 0.36), ...]
|
| 62 |
-
"""
|
| 63 |
dc = nx.degree_centrality(G)
|
| 64 |
return sorted(dc.items(), key=lambda x: x[1], reverse=True)[:k]
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
def get_density(G: nx.Graph) -> float:
|
| 68 |
-
"""Graph density in [0, 1]."""
|
| 69 |
-
return nx.density(G)
|
|
|
|
| 1 |
"""
|
| 2 |
+
Minimal NetworkX helpers for MedGenesis graphs.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
"""
|
|
|
|
|
|
|
|
|
|
| 4 |
import networkx as nx
|
| 5 |
+
from typing import List, Dict, Tuple
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def build_nx(nodes: List[Dict], edges: List[Dict]) -> nx.Graph:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
G = nx.Graph()
|
|
|
|
|
|
|
| 9 |
for n in nodes:
|
| 10 |
+
G.add_node(n["id"])
|
|
|
|
|
|
|
| 11 |
for e in edges:
|
| 12 |
+
G.add_edge(e["source"], e["target"])
|
|
|
|
|
|
|
|
|
|
| 13 |
return G
|
| 14 |
|
| 15 |
+
def get_density(G: nx.Graph) -> float:
|
| 16 |
+
return nx.density(G)
|
| 17 |
|
| 18 |
def get_top_hubs(G: nx.Graph, k: int = 5) -> List[Tuple[str, float]]:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
dc = nx.degree_centrality(G)
|
| 20 |
return sorted(dc.items(), key=lambda x: x[1], reverse=True)[:k]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|