Spaces:
Sleeping
Sleeping
Update genesis/visualization.py
Browse files- genesis/visualization.py +43 -13
genesis/visualization.py
CHANGED
@@ -1,8 +1,21 @@
|
|
1 |
# genesis/visualization.py
|
|
|
|
|
|
|
|
|
|
|
2 |
import os
|
3 |
-
|
|
|
|
|
4 |
import plotly.graph_objects as go
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
# =========================
|
7 |
# CONFIGURATION
|
8 |
# =========================
|
@@ -17,23 +30,32 @@ COLOR_EDGE = "#AAAAAA"
|
|
17 |
# =========================
|
18 |
# NEO4J CONNECTION
|
19 |
# =========================
|
20 |
-
driver =
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
"""Run a Cypher query and return results."""
|
|
|
|
|
|
|
24 |
with driver.session() as session:
|
25 |
return list(session.run(query, params or {}))
|
26 |
|
27 |
# =========================
|
28 |
# GRAPH UTILS
|
29 |
# =========================
|
30 |
-
def create_plotly_graph(nodes, edges, title):
|
31 |
"""Creates an interactive Plotly network graph."""
|
32 |
-
node_x, node_y, node_text, node_color = [], [], [], []
|
33 |
-
edge_x, edge_y = [], []
|
34 |
-
|
35 |
-
# Layout algorithm (spring layout for better spacing)
|
36 |
import networkx as nx
|
|
|
37 |
G = nx.Graph()
|
38 |
for node_id, label, color in nodes:
|
39 |
G.add_node(node_id, label=label, color=color)
|
@@ -42,12 +64,14 @@ def create_plotly_graph(nodes, edges, title):
|
|
42 |
|
43 |
pos = nx.spring_layout(G, seed=42, k=0.5)
|
44 |
|
|
|
45 |
for src, dst in edges:
|
46 |
x0, y0 = pos[src]
|
47 |
x1, y1 = pos[dst]
|
48 |
edge_x.extend([x0, x1, None])
|
49 |
edge_y.extend([y0, y1, None])
|
50 |
|
|
|
51 |
for node_id, label, color in nodes:
|
52 |
x, y = pos[node_id]
|
53 |
node_x.append(x)
|
@@ -90,9 +114,10 @@ def create_plotly_graph(nodes, edges, title):
|
|
90 |
# =========================
|
91 |
# PATHWAY GRAPH
|
92 |
# =========================
|
93 |
-
def generate_pathway_graph(pathway_name):
|
94 |
"""
|
95 |
Generate an interactive graph for a given metabolic pathway.
|
|
|
96 |
"""
|
97 |
query = """
|
98 |
MATCH (p:Pathway {name: $name})-[r:INVOLVES]->(m:Molecule)
|
@@ -105,9 +130,13 @@ def generate_pathway_graph(pathway_name):
|
|
105 |
|
106 |
nodes = [(pathway_name, pathway_name, COLOR_PATHWAY_NODE)]
|
107 |
edges = []
|
|
|
|
|
108 |
for record in results:
|
109 |
mol_name = record["molecule"]
|
110 |
-
|
|
|
|
|
111 |
edges.append((pathway_name, mol_name))
|
112 |
|
113 |
return create_plotly_graph(nodes, edges, f"Metabolic Pathway: {pathway_name}")
|
@@ -115,7 +144,7 @@ def generate_pathway_graph(pathway_name):
|
|
115 |
# =========================
|
116 |
# FUNDING NETWORK
|
117 |
# =========================
|
118 |
-
def generate_funding_network(industry_keyword):
|
119 |
"""
|
120 |
Generate an interactive funding network graph for companies in a given biotech domain.
|
121 |
"""
|
@@ -152,4 +181,5 @@ def generate_funding_network(industry_keyword):
|
|
152 |
# CLEANUP
|
153 |
# =========================
|
154 |
def close_driver():
|
155 |
-
driver
|
|
|
|
1 |
# genesis/visualization.py
|
2 |
+
"""
|
3 |
+
Visualization utilities for GENESIS-AI
|
4 |
+
Generates interactive pathway and funding network graphs from Neo4j data.
|
5 |
+
"""
|
6 |
+
|
7 |
import os
|
8 |
+
import logging
|
9 |
+
from typing import List, Tuple, Optional
|
10 |
+
|
11 |
import plotly.graph_objects as go
|
12 |
|
13 |
+
# Optional Neo4j import
|
14 |
+
try:
|
15 |
+
from neo4j import GraphDatabase
|
16 |
+
except ImportError:
|
17 |
+
GraphDatabase = None
|
18 |
+
|
19 |
# =========================
|
20 |
# CONFIGURATION
|
21 |
# =========================
|
|
|
30 |
# =========================
|
31 |
# NEO4J CONNECTION
|
32 |
# =========================
|
33 |
+
driver = None
|
34 |
+
if GraphDatabase and NEO4J_URI and NEO4J_USER and NEO4J_PASSWORD:
|
35 |
+
try:
|
36 |
+
driver = GraphDatabase.driver(NEO4J_URI, auth=(NEO4J_USER, NEO4J_PASSWORD))
|
37 |
+
logging.info("[Neo4j] Connected for visualization.")
|
38 |
+
except Exception as e:
|
39 |
+
logging.error(f"[Neo4j] Connection failed: {e}")
|
40 |
+
driver = None
|
41 |
+
else:
|
42 |
+
logging.info("[Neo4j] No URI/user/password set β skipping graph_tools connection.")
|
43 |
+
|
44 |
+
def run_neo4j_query(query: str, params: dict = None) -> list:
|
45 |
"""Run a Cypher query and return results."""
|
46 |
+
if not driver:
|
47 |
+
logging.warning("[Neo4j] No active connection β returning empty result.")
|
48 |
+
return []
|
49 |
with driver.session() as session:
|
50 |
return list(session.run(query, params or {}))
|
51 |
|
52 |
# =========================
|
53 |
# GRAPH UTILS
|
54 |
# =========================
|
55 |
+
def create_plotly_graph(nodes: List[Tuple[str, str, str]], edges: List[Tuple[str, str]], title: str):
|
56 |
"""Creates an interactive Plotly network graph."""
|
|
|
|
|
|
|
|
|
57 |
import networkx as nx
|
58 |
+
|
59 |
G = nx.Graph()
|
60 |
for node_id, label, color in nodes:
|
61 |
G.add_node(node_id, label=label, color=color)
|
|
|
64 |
|
65 |
pos = nx.spring_layout(G, seed=42, k=0.5)
|
66 |
|
67 |
+
edge_x, edge_y = [], []
|
68 |
for src, dst in edges:
|
69 |
x0, y0 = pos[src]
|
70 |
x1, y1 = pos[dst]
|
71 |
edge_x.extend([x0, x1, None])
|
72 |
edge_y.extend([y0, y1, None])
|
73 |
|
74 |
+
node_x, node_y, node_text, node_color = [], [], [], []
|
75 |
for node_id, label, color in nodes:
|
76 |
x, y = pos[node_id]
|
77 |
node_x.append(x)
|
|
|
114 |
# =========================
|
115 |
# PATHWAY GRAPH
|
116 |
# =========================
|
117 |
+
def generate_pathway_graph(pathway_name: str) -> Optional[go.Figure]:
|
118 |
"""
|
119 |
Generate an interactive graph for a given metabolic pathway.
|
120 |
+
Only takes pathway_name β matches pipeline.py signature.
|
121 |
"""
|
122 |
query = """
|
123 |
MATCH (p:Pathway {name: $name})-[r:INVOLVES]->(m:Molecule)
|
|
|
130 |
|
131 |
nodes = [(pathway_name, pathway_name, COLOR_PATHWAY_NODE)]
|
132 |
edges = []
|
133 |
+
seen_nodes = {pathway_name}
|
134 |
+
|
135 |
for record in results:
|
136 |
mol_name = record["molecule"]
|
137 |
+
if mol_name not in seen_nodes:
|
138 |
+
nodes.append((mol_name, mol_name, "#00BFFF")) # Blue for molecules
|
139 |
+
seen_nodes.add(mol_name)
|
140 |
edges.append((pathway_name, mol_name))
|
141 |
|
142 |
return create_plotly_graph(nodes, edges, f"Metabolic Pathway: {pathway_name}")
|
|
|
144 |
# =========================
|
145 |
# FUNDING NETWORK
|
146 |
# =========================
|
147 |
+
def generate_funding_network(industry_keyword: str) -> Optional[go.Figure]:
|
148 |
"""
|
149 |
Generate an interactive funding network graph for companies in a given biotech domain.
|
150 |
"""
|
|
|
181 |
# CLEANUP
|
182 |
# =========================
|
183 |
def close_driver():
|
184 |
+
if driver:
|
185 |
+
driver.close()
|