| import json |
| import networkx as nx |
| import matplotlib.pyplot as plt |
| import os |
|
|
| |
| index_file_path = "graphs/index.json" |
|
|
| |
| domain_colors = { |
| "Legislation": "red", |
| "Healthcare Systems": "blue", |
| "Healthcare Policies": "green", |
| "Default": "grey" |
| } |
|
|
| |
| def load_index_data(file_path): |
| with open(file_path, "r") as file: |
| return json.load(file) |
|
|
| |
| def build_graph(data): |
| G = nx.DiGraph() |
| for entity_id, entity_info in data["entities"].items(): |
| label = entity_info.get("label", entity_id) |
| domain = entity_info.get("inherits_from", "Default") |
| color = domain_colors.get(domain, "grey") |
| G.add_node(entity_id, label=label, color=color) |
|
|
| |
| file_path = entity_info.get("file_path") |
| if file_path and os.path.exists(file_path): |
| with open(file_path, "r") as f: |
| entity_data = json.load(f) |
| for rel in entity_data.get("relationships", []): |
| G.add_edge(rel["source"], rel["target"], label=rel["attributes"]["relationship"]) |
| |
| |
| for relationship in data["relationships"]: |
| G.add_edge(relationship["source"], relationship["target"], label=relationship["attributes"].get("relationship", "related_to")) |
|
|
| return G |
|
|
| |
| def visualize_graph(G, title="Inferred Contextual Relationships"): |
| pos = nx.spring_layout(G) |
| plt.figure(figsize=(15, 10)) |
|
|
| |
| node_colors = [G.nodes[node].get("color", "grey") for node in G.nodes] |
| nx.draw_networkx_nodes(G, pos, node_size=3000, node_color=node_colors, alpha=0.8) |
|
|
| |
| nx.draw_networkx_labels(G, pos, font_size=10, font_weight="bold") |
|
|
| |
| nx.draw_networkx_edges(G, pos, arrowstyle="->", arrowsize=20, edge_color="gray", connectionstyle="arc3,rad=0.1") |
| edge_labels = {(u, v): d["label"] for u, v, d in G.edges(data=True)} |
| nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_color="red", font_size=8) |
|
|
| |
| plt.title(title) |
| plt.axis("off") |
| plt.savefig("graph_visualization.pdf") |
| plt.show() |
|
|
| |
| if __name__ == "__main__": |
| |
| data = load_index_data(index_file_path) |
|
|
| |
| G = build_graph(data) |
|
|
| |
| visualize_graph(G) |