File size: 2,101 Bytes
64ed965
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
import sys
import json
import networkx as nx
import matplotlib.pyplot as plt


domain_colors = {
    "Legislation": "red",
    "Healthcare Systems": "blue",
    "Healthcare Policies": "green",
    "Default": "grey"
}


# Path setup
sys.path.append(os.path.join(os.path.dirname(__file__), "../"))
from app.services.agn_service.build_graph import build_graph
from app.services.agn_service.visualize_graph import visualize_graph

# Paths to files
index_file_path = "graphs/index.json"
output_image = "expanded_graph_visualization.png"

# Load index data
def load_index_data(file_path):
    with open(file_path, "r") as file:
        return json.load(file)

# Build the graph with entities and relationships from the updated index
def build_expanded_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)

        # Load additional relationships if specified in the entity data
        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"])

    # Add new relationships from index.json
    for relationship in data["relationships"]:
        G.add_edge(relationship["source"], relationship["target"], label=relationship["attributes"].get("relationship", "related_to"))

    return G

# Main execution to build and visualize the expanded graph
if __name__ == "__main__":
    data = load_index_data(index_file_path)
    G = build_expanded_graph(data)
    
    if G:
        visualize_graph(G, output_file=output_image)
        print(f"Expanded graph visualization saved as {output_image}")
    else:
        print("Failed to build expanded graph.")