Download mcp/graph_metrics.py from mgbam/MCP_Res: direct link, hf CLI and curl.
- Browser
- Download file 790 Bytes
-
https://huggingface.co/spaces/mgbam/MCP_Res/resolve/4db62cc047e617ab7e48c536c69df338ead3d4f3/mcp/graph_metrics.py
- Command line
-
hf download hf://spaces/mgbam/MCP_Res@4db62cc047e617ab7e48c536c69df338ead3d4f3/mcp/graph_metrics.py
-
curl -L -o graph_metrics.py https://huggingface.co/spaces/mgbam/MCP_Res/resolve/4db62cc047e617ab7e48c536c69df338ead3d4f3/mcp/graph_metrics.py
790 Bytes
| # mcp/graph_metrics.py | |
| from typing import List, Dict, Tuple | |
| import networkx as nx | |
| def _edge_endpoints(e: Dict) -> Tuple[str,str] | None: | |
| src = e.get("source") or e.get("from") | |
| dst = e.get("target") or e.get("to") | |
| return (src,dst) if src and dst else None | |
| def build_nx(nodes: List[Dict], edges: List[Dict]) -> nx.Graph: | |
| G = nx.Graph() | |
| for n in nodes: | |
| nid = n.get("id") | |
| if nid: | |
| G.add_node(nid, label=n.get("label", nid)) | |
| for e in edges: | |
| pts = _edge_endpoints(e) | |
| if pts: | |
| G.add_edge(*pts) | |
| return G | |
| def get_top_hubs(G: nx.Graph, k: int = 5): | |
| dc = nx.degree_centrality(G) | |
| return sorted(dc.items(), key=lambda x: x[1], reverse=True)[:k] | |
| def get_density(G: nx.Graph) -> float: | |
| return nx.density(G) | |