Singularity / scripts /dev /3.8_expand_graph.py
SlappAI's picture
dev scripts
64ed965
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.")