Commit Β·
f31db2c
1
Parent(s): 5bfb237
visual hinge and slider
Browse files
app.py
CHANGED
|
@@ -4,112 +4,145 @@ import json
|
|
| 4 |
import plotly.graph_objects as go
|
| 5 |
|
| 6 |
# -----------------------------
|
| 7 |
-
#
|
| 8 |
# -----------------------------
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# -----------------------------
|
| 12 |
-
#
|
| 13 |
# -----------------------------
|
| 14 |
def load_all_trajectories(path):
|
| 15 |
traj_list = []
|
| 16 |
-
count =0
|
| 17 |
-
|
| 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 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
count += 1
|
| 29 |
-
|
| 30 |
return traj_list
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
return {f"Trajectory {i}": traj for i, traj in enumerate(trajs)}
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# -----------------------------
|
| 39 |
-
#
|
| 40 |
# -----------------------------
|
| 41 |
-
def
|
|
|
|
| 42 |
fig = go.Figure()
|
| 43 |
|
| 44 |
-
for name, traj in
|
| 45 |
traj = np.array(traj)
|
| 46 |
|
| 47 |
if len(traj) == 0:
|
| 48 |
continue
|
| 49 |
|
| 50 |
-
#
|
| 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="
|
| 58 |
-
opacity=0.
|
| 59 |
showlegend=False
|
| 60 |
))
|
| 61 |
|
| 62 |
-
# selected
|
| 63 |
else:
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
traj_partial = traj[:k]
|
| 67 |
|
| 68 |
fig.add_trace(go.Scatter3d(
|
| 69 |
-
x=
|
| 70 |
-
y=
|
| 71 |
-
z=
|
| 72 |
mode="lines",
|
| 73 |
-
line=dict(width=6, color="
|
| 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 |
-
#
|
| 91 |
# -----------------------------
|
| 92 |
-
|
|
|
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
with gr.Blocks() as demo:
|
| 95 |
|
| 96 |
-
gr.Markdown("# π΄
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
with gr.Row():
|
| 99 |
-
|
| 100 |
-
|
| 101 |
|
| 102 |
-
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
return update_plot(choice, progress)
|
| 107 |
|
| 108 |
-
|
| 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()
|