| 532 | |
| 533 | |
| 534 | def load_graph(path: Path) -> nx.Graph: |
| 535 | import json |
| 536 | from networkx.readwrite import json_graph |
| 537 | |
| 538 | try: |
| 539 | from graphify.paths import consumed, read_graph_json |
| 540 | raw = read_graph_json(path) |
| 541 | except (json.JSONDecodeError, OSError) as exc: |
| 542 | raise RuntimeError( |
| 543 | f"Cannot read graph file {path}: {exc}. " |
| 544 | "Re-run 'graphify extract' to regenerate it." |
| 545 | ) from exc |
| 546 | # Force directed so stored caller→callee direction survives the round-trip; |
| 547 | # mirrors serve.py and __main__.py (#1174). |
| 548 | raw = {**raw, "directed": True} |
| 549 | # Normalize the edge key: graphify's `extract` output uses "edges" while |
| 550 | # networkx's node_link_data default is "links". Without this, an edges-keyed |
| 551 | # graph.json raises an uncaught KeyError: 'links' here — every other loader |
| 552 | # (__main__.py) already normalizes this (#738; same class as #1198). |
| 553 | if "links" not in raw and "edges" in raw: |
| 554 | raw = dict(raw, links=raw["edges"]) |
| 555 | raw = dict(raw, nodes=consumed(raw["nodes"]), links=consumed(raw["links"])) |
| 556 | return json_graph.node_link_graph(raw, edges="links") |