Spaces:
Sleeping
Sleeping
Create visualization.py
Browse files- genesis/visualization.py +62 -0
genesis/visualization.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# genesis/visualization.py
|
2 |
+
import networkx as nx
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import io
|
5 |
+
import base64
|
6 |
+
|
7 |
+
def _graph_to_image_bytes(G, layout_func=nx.spring_layout):
|
8 |
+
"""Helper: Convert NetworkX graph to PNG bytes."""
|
9 |
+
plt.figure(figsize=(8, 6))
|
10 |
+
pos = layout_func(G)
|
11 |
+
nx.draw_networkx_nodes(G, pos, node_size=1500, node_color="#34d399", alpha=0.9)
|
12 |
+
nx.draw_networkx_edges(G, pos, width=2, alpha=0.5, edge_color="#6b7280")
|
13 |
+
nx.draw_networkx_labels(G, pos, font_size=10, font_family="sans-serif", font_color="white")
|
14 |
+
plt.axis("off")
|
15 |
+
|
16 |
+
buf = io.BytesIO()
|
17 |
+
plt.savefig(buf, format="png", bbox_inches="tight")
|
18 |
+
plt.close()
|
19 |
+
buf.seek(0)
|
20 |
+
return buf
|
21 |
+
|
22 |
+
def generate_pathway_graph(entities, relationships):
|
23 |
+
"""
|
24 |
+
Create a pathway relationship graph for synthetic biology concepts.
|
25 |
+
entities: list of strings
|
26 |
+
relationships: list of dicts {source, target, type}
|
27 |
+
Returns: file path to PNG
|
28 |
+
"""
|
29 |
+
G = nx.DiGraph()
|
30 |
+
for entity in entities:
|
31 |
+
G.add_node(entity)
|
32 |
+
|
33 |
+
for rel in relationships:
|
34 |
+
G.add_edge(rel["source"], rel["target"], label=rel["type"])
|
35 |
+
|
36 |
+
buf = _graph_to_image_bytes(G)
|
37 |
+
file_path = "/tmp/pathway_graph.png"
|
38 |
+
with open(file_path, "wb") as f:
|
39 |
+
f.write(buf.read())
|
40 |
+
return file_path
|
41 |
+
|
42 |
+
def generate_funding_network(companies):
|
43 |
+
"""
|
44 |
+
Create a funding network graph.
|
45 |
+
companies: list of dicts {name, investors}
|
46 |
+
Returns: file path to PNG
|
47 |
+
"""
|
48 |
+
G = nx.Graph()
|
49 |
+
for comp in companies:
|
50 |
+
company_node = f"π’ {comp['name']}"
|
51 |
+
G.add_node(company_node)
|
52 |
+
investors = [i.strip() for i in comp["investors"].split(",")]
|
53 |
+
for inv in investors:
|
54 |
+
inv_node = f"π° {inv}"
|
55 |
+
G.add_node(inv_node)
|
56 |
+
G.add_edge(company_node, inv_node)
|
57 |
+
|
58 |
+
buf = _graph_to_image_bytes(G, layout_func=nx.kamada_kawai_layout)
|
59 |
+
file_path = "/tmp/funding_network.png"
|
60 |
+
with open(file_path, "wb") as f:
|
61 |
+
f.write(buf.read())
|
62 |
+
return file_path
|