Spaces:
Sleeping
Sleeping
Create visualization.py
Browse files- visualization.py +137 -0
visualization.py
ADDED
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# genesis/visualization.py
|
2 |
+
import networkx as nx
|
3 |
+
import plotly.graph_objects as go
|
4 |
+
from typing import List, Dict
|
5 |
+
|
6 |
+
def generate_pathway_graph(entities: List[str], relationships: List[Dict]) -> go.Figure:
|
7 |
+
"""
|
8 |
+
Create an interactive graph visualization using Plotly.
|
9 |
+
entities: List of node labels
|
10 |
+
relationships: List of dicts {source: str, target: str, type: str}
|
11 |
+
"""
|
12 |
+
# Create graph
|
13 |
+
G = nx.Graph()
|
14 |
+
for entity in entities:
|
15 |
+
G.add_node(entity)
|
16 |
+
for rel in relationships:
|
17 |
+
G.add_edge(rel["source"], rel["target"], type=rel.get("type", ""))
|
18 |
+
|
19 |
+
pos = nx.spring_layout(G, seed=42)
|
20 |
+
|
21 |
+
# Build edge traces
|
22 |
+
edge_x, edge_y = [], []
|
23 |
+
for edge in G.edges():
|
24 |
+
x0, y0 = pos[edge[0]]
|
25 |
+
x1, y1 = pos[edge[1]]
|
26 |
+
edge_x.extend([x0, x1, None])
|
27 |
+
edge_y.extend([y0, y1, None])
|
28 |
+
|
29 |
+
edge_trace = go.Scatter(
|
30 |
+
x=edge_x, y=edge_y,
|
31 |
+
line=dict(width=1, color="#888"),
|
32 |
+
hoverinfo='none',
|
33 |
+
mode='lines'
|
34 |
+
)
|
35 |
+
|
36 |
+
# Build node traces
|
37 |
+
node_x, node_y, text_labels = [], [], []
|
38 |
+
for node in G.nodes():
|
39 |
+
x, y = pos[node]
|
40 |
+
node_x.append(x)
|
41 |
+
node_y.append(y)
|
42 |
+
text_labels.append(node)
|
43 |
+
|
44 |
+
node_trace = go.Scatter(
|
45 |
+
x=node_x, y=node_y,
|
46 |
+
mode='markers+text',
|
47 |
+
text=text_labels,
|
48 |
+
textposition="top center",
|
49 |
+
hoverinfo='text',
|
50 |
+
marker=dict(
|
51 |
+
showscale=True,
|
52 |
+
colorscale='Viridis',
|
53 |
+
size=20,
|
54 |
+
color=[len(G.adj[n]) for n in G.nodes()],
|
55 |
+
colorbar=dict(
|
56 |
+
thickness=15,
|
57 |
+
title='Connections',
|
58 |
+
xanchor='left',
|
59 |
+
titleside='right'
|
60 |
+
)
|
61 |
+
)
|
62 |
+
)
|
63 |
+
|
64 |
+
fig = go.Figure(data=[edge_trace, node_trace],
|
65 |
+
layout=go.Layout(
|
66 |
+
title="Biological Pathway / Network",
|
67 |
+
title_x=0.5,
|
68 |
+
showlegend=False,
|
69 |
+
hovermode='closest',
|
70 |
+
margin=dict(b=0, l=0, r=0, t=40),
|
71 |
+
annotations=[dict(
|
72 |
+
text="GENESIS-AI Interactive Graph",
|
73 |
+
showarrow=False,
|
74 |
+
xref="paper", yref="paper",
|
75 |
+
x=0.005, y=-0.002
|
76 |
+
)]
|
77 |
+
))
|
78 |
+
return fig
|
79 |
+
|
80 |
+
def generate_funding_network(companies: List[Dict]) -> go.Figure:
|
81 |
+
"""
|
82 |
+
Visualize funding relationships between biotech companies and investors.
|
83 |
+
companies: list of {name, investors}
|
84 |
+
"""
|
85 |
+
G = nx.Graph()
|
86 |
+
for c in companies:
|
87 |
+
company_node = c["name"]
|
88 |
+
G.add_node(company_node, type="company")
|
89 |
+
investors = c.get("investors", "").split(", ")
|
90 |
+
for inv in investors:
|
91 |
+
if inv.strip():
|
92 |
+
G.add_node(inv, type="investor")
|
93 |
+
G.add_edge(company_node, inv)
|
94 |
+
|
95 |
+
pos = nx.spring_layout(G, seed=42)
|
96 |
+
|
97 |
+
edge_x, edge_y = [], []
|
98 |
+
for edge in G.edges():
|
99 |
+
x0, y0 = pos[edge[0]]
|
100 |
+
x1, y1 = pos[edge[1]]
|
101 |
+
edge_x.extend([x0, x1, None])
|
102 |
+
edge_y.extend([y0, y1, None])
|
103 |
+
|
104 |
+
edge_trace = go.Scatter(
|
105 |
+
x=edge_x, y=edge_y,
|
106 |
+
line=dict(width=1, color="#888"),
|
107 |
+
hoverinfo='none',
|
108 |
+
mode='lines'
|
109 |
+
)
|
110 |
+
|
111 |
+
node_x, node_y, text_labels = [], [], []
|
112 |
+
colors = []
|
113 |
+
for node, data in G.nodes(data=True):
|
114 |
+
x, y = pos[node]
|
115 |
+
node_x.append(x)
|
116 |
+
node_y.append(y)
|
117 |
+
text_labels.append(node)
|
118 |
+
colors.append("#FF5733" if data["type"] == "company" else "#1F77B4")
|
119 |
+
|
120 |
+
node_trace = go.Scatter(
|
121 |
+
x=node_x, y=node_y,
|
122 |
+
mode='markers+text',
|
123 |
+
text=text_labels,
|
124 |
+
textposition="top center",
|
125 |
+
hoverinfo='text',
|
126 |
+
marker=dict(size=15, color=colors)
|
127 |
+
)
|
128 |
+
|
129 |
+
fig = go.Figure(data=[edge_trace, node_trace],
|
130 |
+
layout=go.Layout(
|
131 |
+
title="Biotech Funding Network",
|
132 |
+
title_x=0.5,
|
133 |
+
showlegend=False,
|
134 |
+
hovermode='closest',
|
135 |
+
margin=dict(b=0, l=0, r=0, t=40)
|
136 |
+
))
|
137 |
+
return fig
|