""" | |
Minimal NetworkX helpers for MedGenesis graphs. | |
""" | |
import networkx as nx | |
from typing import List, Dict, Tuple | |
def build_nx(nodes: List[Dict], edges: List[Dict]) -> nx.Graph: | |
G = nx.Graph() | |
for n in nodes: | |
G.add_node(n["id"]) | |
for e in edges: | |
G.add_edge(e["source"], e["target"]) | |
return G | |
def get_density(G: nx.Graph) -> float: | |
return nx.density(G) | |
def get_top_hubs(G: nx.Graph, k: int = 5) -> List[Tuple[str, float]]: | |
dc = nx.degree_centrality(G) | |
return sorted(dc.items(), key=lambda x: x[1], reverse=True)[:k] | |