From 8dedf25b196754443c5a8bd44961ecbcb0872ca9 Mon Sep 17 00:00:00 2001
From: crides <zhuhaoqing@live.cn>
Date: Mon, 10 Apr 2023 17:14:32 -0500
Subject: [PATCH] analysis_tree: add `visualize` method

The `visualize` method uses the `networkx` to graph the nodes as a tree.
Each node in the tree is currently shown with its ID and start time

closes one half of #19
---
 verse/analysis/analysis_tree.py | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/verse/analysis/analysis_tree.py b/verse/analysis/analysis_tree.py
index 2e861aea..147b98f4 100644
--- a/verse/analysis/analysis_tree.py
+++ b/verse/analysis/analysis_tree.py
@@ -7,6 +7,9 @@ import numpy.typing as nptyp, numpy as np, portion
 
 from verse.analysis.dryvr import _EPSILON
 
+import networkx as nx
+import matplotlib.pyplot as plt
+
 TraceType = nptyp.NDArray[np.float_]
 
 class AnalysisTreeNode:
@@ -226,3 +229,14 @@ class AnalysisTree:
             total_len = len(other_tree[min_agents[0]])
             # bloat and containment
             return all(other_tree[aid][i][j] in this_tree[aid][i][j].apply(lambda x: x.replace(lower=lambda v: v - tol, upper=lambda v: v + tol)) for aid in other_agents for i in range(total_len) for j in range(cont_num))
+
+    def visualize(self):
+        G = nx.Graph()
+        for node in self.nodes:
+            G.add_node(node.id,time=(node.id,node.start_time))
+            for child in node.child:
+                G.add_node(child.id,time=(child.id,child.start_time))
+                G.add_edge(node.id, child.id)
+        labels = nx.get_node_attributes(G, 'time') 
+        nx.draw_planar(G,labels=labels)
+        plt.show()
-- 
GitLab