mathildekappel commited on
Commit
f31db2c
Β·
1 Parent(s): 5bfb237

visual hinge and slider

Browse files
Files changed (1) hide show
  1. app.py +81 -48
app.py CHANGED
@@ -4,112 +4,145 @@ import json
4
  import plotly.graph_objects as go
5
 
6
  # -----------------------------
7
- # Data path
8
  # -----------------------------
9
- DATA_PATH = "000_hinge_0/000_hinge_0_Gen175_Pop10000_baseline_MAP-Elite-Explore_APGen_initState_max_v0/Archive_space_Object_000_JointNature_hinge_Item0_Generation_175_PopSize_10000_baseline_MAP-Elite-Explore_APGen_initState_max_v0.jsonl"
 
 
 
10
 
11
  # -----------------------------
12
- # Load trajectories
13
  # -----------------------------
14
  def load_all_trajectories(path):
15
  traj_list = []
16
- count =0
17
- nb_traj_stored = 0
18
  with open(path, "r") as f:
19
  for line in f:
20
- if count %50==0:
21
  obj = json.loads(line)
22
  traj = obj.get("traj_list")
23
- if traj is None:
24
- continue
25
- traj = [x[:3] for x in traj]
26
- traj_list.append(traj)
27
- nb_traj_stored += 1
28
  count += 1
29
- print(f"Number of trajectories stored: {nb_traj_stored}")
30
  return traj_list
31
 
32
- def generate_trajectories():
33
- trajs = load_all_trajectories(DATA_PATH)
 
34
  return {f"Trajectory {i}": traj for i, traj in enumerate(trajs)}
35
 
36
- TRAJECTORIES = generate_trajectories()
 
 
 
 
 
 
37
 
38
  # -----------------------------
39
- # Plot function
40
  # -----------------------------
41
- def update_plot(choice, progress):
 
42
  fig = go.Figure()
43
 
44
- for name, traj in TRAJECTORIES.items():
45
  traj = np.array(traj)
46
 
47
  if len(traj) == 0:
48
  continue
49
 
50
- # other trajectories
51
  if name != choice:
52
  fig.add_trace(go.Scatter3d(
53
  x=traj[:, 0],
54
  y=traj[:, 1],
55
  z=traj[:, 2],
56
  mode="lines",
57
- line=dict(width=4, color="red"),
58
- opacity=0.1,
59
  showlegend=False
60
  ))
61
 
62
- # selected trajectory (animated by slider)
63
  else:
64
- n = len(traj)
65
- k = max(2, int(n * progress))
66
- traj_partial = traj[:k]
67
 
68
  fig.add_trace(go.Scatter3d(
69
- x=traj_partial[:, 0],
70
- y=traj_partial[:, 1],
71
- z=traj_partial[:, 2],
72
  mode="lines",
73
- line=dict(width=6, color="green"),
74
  name=name
75
  ))
76
 
77
  fig.update_layout(
78
- title=f"{choice} | progress={progress:.2f}",
79
- scene=dict(
80
- xaxis_title="X",
81
- yaxis_title="Y",
82
- zaxis_title="Z"
83
- ),
84
  margin=dict(l=0, r=0, b=0, t=40)
85
  )
86
 
87
  return fig
88
 
 
89
  # -----------------------------
90
- # Gradio Blocks UI (LIVE)
91
  # -----------------------------
92
- choices = list(TRAJECTORIES.keys())
 
93
 
 
 
 
 
 
 
 
94
  with gr.Blocks() as demo:
95
 
96
- gr.Markdown("# πŸ”΄ 3D Trajectory Viewer (LIVE SLIDER)")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
  with gr.Row():
99
- traj_dropdown = gr.Dropdown(choices, label="Choose trajectory")
100
- slider = gr.Slider(0, 1, value=1, step=0.01, label="Progress")
101
 
102
- plot = gr.Plot()
103
 
104
- # live binding (IMPORTANT)
105
- def refresh(choice, progress):
106
- return update_plot(choice, progress)
107
 
108
- traj_dropdown.change(refresh, [traj_dropdown, slider], plot)
109
- slider.change(refresh, [traj_dropdown, slider], plot)
110
 
111
- # initial render
112
- demo.load(refresh, [traj_dropdown, slider], plot)
113
 
114
  if __name__ == "__main__":
115
  demo.launch()
 
4
  import plotly.graph_objects as go
5
 
6
  # -----------------------------
7
+ # DATA PATHS (tes "documents 000")
8
  # -----------------------------
9
+ DATA_PATH_1 = "000_hinge_0/000_hinge_0_Gen175_Pop10000_baseline_MAP-Elite-Explore_APGen_initState_max_v0/Archive_space_Object_000_JointNature_hinge_Item0_Generation_175_PopSize_10000_baseline_MAP-Elite-Explore_APGen_initState_max_v0.jsonl"
10
+
11
+ DATA_PATH_2 = "000_slider_0/000_slider_0_Gen175_Pop10000_baseline_MAP-Elite-Explore_APGen_initState_max_v0/Archive_space_Object_000_JointNature_slider_Item0_Generation_175_PopSize_10000_baseline_MAP-Elite-Explore_APGen_initState_max_v0.jsonl"
12
+
13
 
14
  # -----------------------------
15
+ # LOAD
16
  # -----------------------------
17
  def load_all_trajectories(path):
18
  traj_list = []
19
+ count = 0
20
+
21
  with open(path, "r") as f:
22
  for line in f:
23
+ if count % 50 == 0:
24
  obj = json.loads(line)
25
  traj = obj.get("traj_list")
26
+
27
+ if traj is not None:
28
+ traj = [x[:3] for x in traj]
29
+ traj_list.append(traj)
30
+
31
  count += 1
32
+
33
  return traj_list
34
 
35
+
36
+ def build_dict(path):
37
+ trajs = load_all_trajectories(path)
38
  return {f"Trajectory {i}": traj for i, traj in enumerate(trajs)}
39
 
40
+
41
+ TRAJ_1 = build_dict(DATA_PATH_1)
42
+ TRAJ_2 = build_dict(DATA_PATH_2)
43
+
44
+ choices_1 = list(TRAJ_1.keys())
45
+ choices_2 = list(TRAJ_2.keys())
46
+
47
 
48
  # -----------------------------
49
+ # PLOT FUNCTION (generic)
50
  # -----------------------------
51
+ def plot_traj(trajs_dict, choice, progress, title):
52
+
53
  fig = go.Figure()
54
 
55
+ for name, traj in trajs_dict.items():
56
  traj = np.array(traj)
57
 
58
  if len(traj) == 0:
59
  continue
60
 
61
+ # background
62
  if name != choice:
63
  fig.add_trace(go.Scatter3d(
64
  x=traj[:, 0],
65
  y=traj[:, 1],
66
  z=traj[:, 2],
67
  mode="lines",
68
+ line=dict(width=4, color="green"),
69
+ opacity=0.05,
70
  showlegend=False
71
  ))
72
 
73
+ # selected (progressive)
74
  else:
75
+ k = max(2, int(len(traj) * progress))
76
+ traj_part = traj[:k]
 
77
 
78
  fig.add_trace(go.Scatter3d(
79
+ x=traj_part[:, 0],
80
+ y=traj_part[:, 1],
81
+ z=traj_part[:, 2],
82
  mode="lines",
83
+ line=dict(width=6, color="red"),
84
  name=name
85
  ))
86
 
87
  fig.update_layout(
88
+ title=f"{title} | {choice} | progress={progress:.2f}",
89
+ scene=dict(xaxis_title="X", yaxis_title="Y", zaxis_title="Z"),
 
 
 
 
90
  margin=dict(l=0, r=0, b=0, t=40)
91
  )
92
 
93
  return fig
94
 
95
+
96
  # -----------------------------
97
+ # WRAPPERS
98
  # -----------------------------
99
+ def update_1(choice, progress):
100
+ return plot_traj(TRAJ_1, choice, progress, "Action primitive 000_hinge_0")
101
 
102
+ def update_2(choice, progress):
103
+ return plot_traj(TRAJ_2, choice, progress, "Action primitive 000_slider_0")
104
+
105
+
106
+ # -----------------------------
107
+ # UI
108
+ # -----------------------------
109
  with gr.Blocks() as demo:
110
 
111
+ gr.Markdown("# πŸ”΄ Trajectory Explorer - 2 Documents (000 / 000)")
112
+
113
+ # -------------------------
114
+ # DOCUMENT 1
115
+ # -------------------------
116
+ gr.Markdown("## πŸ“„ Document 000 - A")
117
+
118
+ with gr.Row():
119
+ dd1 = gr.Dropdown(choices_1, label="Trajectory A")
120
+ sl1 = gr.Slider(0, 1, value=1, step=0.01, label="Progress A")
121
+
122
+ plot1 = gr.Plot()
123
+
124
+ dd1.change(update_1, [dd1, sl1], plot1)
125
+ sl1.change(update_1, [dd1, sl1], plot1)
126
+
127
+ demo.load(update_1, [dd1, sl1], plot1)
128
+
129
+
130
+ # -------------------------
131
+ # DOCUMENT 2
132
+ # -------------------------
133
+ gr.Markdown("## πŸ“„ Document 000 - B")
134
 
135
  with gr.Row():
136
+ dd2 = gr.Dropdown(choices_2, label="Trajectory B")
137
+ sl2 = gr.Slider(0, 1, value=1, step=0.01, label="Progress B")
138
 
139
+ plot2 = gr.Plot()
140
 
141
+ dd2.change(update_2, [dd2, sl2], plot2)
142
+ sl2.change(update_2, [dd2, sl2], plot2)
 
143
 
144
+ demo.load(update_2, [dd2, sl2], plot2)
 
145
 
 
 
146
 
147
  if __name__ == "__main__":
148
  demo.launch()