Skip to content

Commit

Permalink
Add useful hover information
Browse files Browse the repository at this point in the history
- Add first frame
- Add number of agents in the frame
- Add id, position to hover infos of agents
  • Loading branch information
chraibi committed Oct 5, 2023
1 parent 21b85bf commit e0dae5d
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions notebooks/animation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
"filename = \"./corner_roundrobin_1.sqlite\"\n",
"p1 = Polygon([(0, 0), (10, 0), (10, 10), (0, 10)])\n",
"p2 = Polygon([(10, 4), (20, 4), (20, 6), (10, 6)])\n",
"p1 = Polygon([(-3, 6), (-3, -3), (9, -3), (9, 0), (0, 0), (0, 6)])\n",
"area = GeometryCollection(p1.union(p2))\n",
"area = GeometryCollection(p1)\n",
"walkable_Area = pedpy.WalkableArea(area.geoms[0])"
]
},
Expand Down Expand Up @@ -91,10 +93,12 @@
"def get_geometry_traces(area):\n",
" geometry_traces = []\n",
" x, y = area.geoms[0].exterior.xy\n",
" geometry_traces.append(go.Scatter(x=np.array(x), y=np.array(y), mode='lines', line={\"color\": \"grey\"}))\n",
" geometry_traces.append(go.Scatter(x=np.array(x), y=np.array(y), mode='lines', line={\"color\": \"grey\"}, showlegend=False, name='Exterior', hoverinfo='name',\n",
"))\n",
" for inner in area.geoms[0].interiors:\n",
" xi,yi = zip(*inner.coords[:])\n",
" geometry_traces.append(go.Scatter(x=np.array(xi), y=np.array(yi), mode='lines', line={\"color\": \"grey\"}))\n",
" geometry_traces.append(go.Scatter(x=np.array(xi), y=np.array(yi), mode='lines', line={\"color\": \"grey\"},showlegend=False, name='Obstacle', hoverinfo='name',\n",
"))\n",
" return geometry_traces"
]
},
Expand All @@ -106,6 +110,15 @@
"source": [
"def get_shapes_for_frame(frame_data, min_speed, max_speed, midpoint):\n",
" def create_shape(row):\n",
" hover_trace = go.Scatter(\n",
" x=[row['x']],\n",
" y=[row['y']],\n",
" text=[f\"ID: {row['id']}, Pos({row['x']:.2f},{row['y']:.2f})\"],\n",
" mode='markers',\n",
" marker=dict(size=1, opacity=1),\n",
" hoverinfo=\"text\",\n",
" showlegend=False\n",
" )\n",
" if row[\"speed\"] == -1000: # Check for dummy speed\n",
" return go.layout.Shape(\n",
" type=\"circle\",\n",
Expand All @@ -117,7 +130,7 @@
" y1=row['y'] + row['radius'],\n",
" line=dict(width=0),\n",
" fillcolor=\"rgba(255,255,255,0)\" # Transparent fill\n",
" )\n",
" ), hover_trace\n",
" color = speed_to_color(row[\"speed\"], min_speed, max_speed, midpoint)\n",
" return go.layout.Shape(\n",
" type=\"circle\",\n",
Expand All @@ -129,10 +142,12 @@
" y1=row['y'] + row['radius'],\n",
" line_color=color,\n",
" fillcolor=color\n",
" )\n",
" ), hover_trace\n",
"\n",
" shapes = frame_data.apply(create_shape, axis=1).tolist()\n",
" return shapes"
" results = frame_data.apply(create_shape, axis=1).tolist()\n",
" shapes = [res[0] for res in results]\n",
" hover_traces = [res[1] for res in results]\n",
" return shapes, hover_traces"
]
},
{
Expand All @@ -141,9 +156,9 @@
"metadata": {},
"outputs": [],
"source": [
"def create_fig(geometry_traces, frames, steps, area_bounds, width=800, height=800):\n",
"def create_fig(initial_agent_count, initial_shapes, initial_hover_trace, geometry_traces, hover_traces, frames, steps, area_bounds, width=800, height=800):\n",
" minx, miny, maxx, maxy = area_bounds \n",
" fig = go.Figure(data=geometry_traces, frames=frames)\n",
" fig = go.Figure(data=geometry_traces+hover_traces+initial_hover_trace, frames=frames, layout=go.Layout(shapes=initial_shapes, title=f\"<b>Number of Agents: {initial_agent_count}</b>\",title_x=0.5))\n",
" fig.update_layout(\n",
" updatemenus=[{\n",
" 'buttons': [{\n",
Expand Down Expand Up @@ -213,16 +228,22 @@
" steps = []\n",
" unique_frames = data_df['frame'].unique()\n",
" selected_frames = unique_frames[::50]\n",
" for frame_num in selected_frames:\n",
" geometry_traces = get_geometry_traces(area)\n",
" initial_frame_data = data_df[data_df['frame'] == data_df['frame'].min()]\n",
" initial_agent_count = len(initial_frame_data)\n",
" initial_shapes, initial_hovers = get_shapes_for_frame(initial_frame_data, min_speed, max_speed, midpoint)\n",
" for frame_num in selected_frames[1:]:\n",
" frame_data = data_df[data_df['frame'] == frame_num]\n",
" agent_count = len(frame_data)\n",
" while len(frame_data) < max_agents:\n",
" dummy_df = pd.DataFrame([dummy_agent_data])\n",
" frame_data = pd.concat([frame_data, dummy_df], ignore_index=True)\n",
" \n",
" shapes = get_shapes_for_frame(frame_data, min_speed, max_speed, midpoint)\n",
" shapes, hover_traces = get_shapes_for_frame(frame_data, min_speed, max_speed, midpoint)\n",
" frame = go.Frame(\n",
" data=geometry_traces + hover_traces,\n",
" name=str(frame_num),\n",
" layout=go.Layout(shapes=shapes)\n",
" layout=go.Layout(shapes=shapes,title=f\"<b>Number of Agents: {agent_count}</b>\",title_x=0.5)\n",
" )\n",
" frames.append(frame)\n",
" step = {\n",
Expand All @@ -239,8 +260,8 @@
" } \n",
" steps.append(step)\n",
"\n",
" geometry_traces = get_geometry_traces(area)\n",
" return create_fig(geometry_traces, frames, steps, area.bounds, width=800, height=800)"
" \n",
" return create_fig(initial_agent_count, initial_shapes, initial_hovers, geometry_traces, hover_traces, frames, steps, area.bounds, width=800, height=800)"
]
},
{
Expand Down

0 comments on commit e0dae5d

Please sign in to comment.