Spaces:
Sleeping
Sleeping
Update lineage_visualizer.py
Browse files- lineage_visualizer.py +65 -25
lineage_visualizer.py
CHANGED
|
@@ -1,26 +1,66 @@
|
|
|
|
|
| 1 |
# Author: Liam Grinstead
|
| 2 |
-
# Renders
|
| 3 |
-
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# lineage_visualizer.py
|
| 2 |
# Author: Liam Grinstead
|
| 3 |
+
# Renders a multi-generation SVG tree: ancestors (top), root (center), descendants (levels below)
|
| 4 |
+
|
| 5 |
+
from typing import List
|
| 6 |
+
from lineage_tracker import get_ancestors, get_descendants
|
| 7 |
+
|
| 8 |
+
def _row_positions(n: int, width: int, y: int) -> List[tuple]:
|
| 9 |
+
# return evenly spaced (x, y) for n nodes
|
| 10 |
+
if n <= 0:
|
| 11 |
+
return []
|
| 12 |
+
step = width // (n + 1)
|
| 13 |
+
return [(step * (i + 1), y) for i in range(n)]
|
| 14 |
+
|
| 15 |
+
def render_lineage_tree(agent_id: str, max_depth: int = 5) -> str:
|
| 16 |
+
width, height = 900, 600
|
| 17 |
+
margin_top = 40
|
| 18 |
+
svg = [f"<svg width='{width}' height='{height}' xmlns='http://www.w3.org/2000/svg'>"]
|
| 19 |
+
svg.append(f"<style>text{{font-family:monospace}} .node{{filter:drop-shadow(0 1px 1px rgba(0,0,0,.2))}}</style>")
|
| 20 |
+
svg.append(f"<text x='10' y='25' font-size='16'>Lineage view: {agent_id}</text>")
|
| 21 |
+
|
| 22 |
+
# Layout rows
|
| 23 |
+
root_y = 180
|
| 24 |
+
ancestor_y = 80
|
| 25 |
+
base_y = 280
|
| 26 |
+
level_gap = 70
|
| 27 |
+
|
| 28 |
+
# Ancestors row
|
| 29 |
+
ancestors = get_ancestors(agent_id) # oldest → newest
|
| 30 |
+
if ancestors:
|
| 31 |
+
anc_positions = _row_positions(len(ancestors), width, ancestor_y)
|
| 32 |
+
for (x, y), aid in zip(anc_positions, ancestors):
|
| 33 |
+
svg.append(f"<circle class='node' cx='{x}' cy='{y}' r='16' fill='#6c5ce7'/>")
|
| 34 |
+
svg.append(f"<text x='{x-30}' y='{y+34}' font-size='12'>{aid}</text>")
|
| 35 |
+
# connect ancestors to root (last ancestor to root)
|
| 36 |
+
last_x, last_y = anc_positions[-1]
|
| 37 |
+
svg.append(f"<line x1='{last_x}' y1='{last_y+16}' x2='{width//2}' y2='{root_y-16}' stroke='#444'/>")
|
| 38 |
+
else:
|
| 39 |
+
svg.append(f"<text x='10' y='{ancestor_y}' font-size='12' fill='#888'>No ancestors</text>")
|
| 40 |
+
|
| 41 |
+
# Root node
|
| 42 |
+
root_x = width // 2
|
| 43 |
+
svg.append(f"<circle class='node' cx='{root_x}' cy='{root_y}' r='20' fill='#00b894'>"
|
| 44 |
+
f"<animate attributeName='r' values='20;24;20' dur='2s' repeatCount='indefinite'/></circle>")
|
| 45 |
+
svg.append(f"<text x='{root_x-40}' y='{root_y+40}' font-size='12'>{agent_id} (root)</text>")
|
| 46 |
+
|
| 47 |
+
# Descendant levels
|
| 48 |
+
descendants = get_descendants(agent_id, depth=max_depth) # {'level_1': [...], 'level_2': [...]}
|
| 49 |
+
current_y = base_y
|
| 50 |
+
for level_idx in range(1, max_depth + 1):
|
| 51 |
+
level_key = f"level_{level_idx}"
|
| 52 |
+
nodes = descendants.get(level_key, [])
|
| 53 |
+
if not nodes:
|
| 54 |
+
break
|
| 55 |
+
positions = _row_positions(len(nodes), width, current_y)
|
| 56 |
+
svg.append(f"<text x='10' y='{current_y-26}' font-size='12' fill='#888'>Gen {level_idx}</text>")
|
| 57 |
+
for (x, y), nid in zip(positions, nodes):
|
| 58 |
+
svg.append(f"<circle class='node' cx='{x}' cy='{y}' r='14' fill='#0984e3'>"
|
| 59 |
+
f"<animate attributeName='fill' values='#0984e3;#74b9ff;#0984e3' dur='2s' repeatCount='indefinite'/></circle>")
|
| 60 |
+
svg.append(f"<text x='{x-28}' y='{y+33}' font-size='12'>{nid}</text>")
|
| 61 |
+
# draw soft link from approximate parent band to this node (visual continuity)
|
| 62 |
+
svg.append(f"<path d='M {root_x} {root_y+20} Q {(root_x+x)//2} {(root_y+y)//2} {x} {y-14}' stroke='rgba(0,0,0,.35)' fill='none'/>")
|
| 63 |
+
current_y += level_gap
|
| 64 |
+
|
| 65 |
+
svg.append("</svg>")
|
| 66 |
+
return "".join(svg)
|