2222DUMMY_SPEED = - 1000
2323
2424
25- def _speed_to_color (speed : float , min_speed : float , max_speed : float ) -> str :
26- """Map a speed value to a color using a colormap."""
27- normalized_speed = (speed - min_speed ) / (max_speed - min_speed )
28- r , g , b = plt .cm .jet_r (normalized_speed )[:3 ] # type: ignore
25+ def _value_to_color (value : float , min_value : float , max_value : float ) -> str :
26+ """Map a scalar value to a color using a colormap."""
27+ if max_value <= min_value :
28+ normalized_value = 0.5
29+ else :
30+ normalized_value = (value - min_value ) / (max_value - min_value )
31+ normalized_value = min (max (normalized_value , 0.0 ), 1.0 )
32+ r , g , b = plt .cm .jet_r (normalized_value )[:3 ] # type: ignore
2933 return f"rgba({ r * 255 :.0f} , { g * 255 :.0f} , { b * 255 :.0f} , 0.5)"
3034
3135
@@ -85,7 +89,10 @@ def _get_geometry_traces(area: Polygon) -> Scatter:
8589
8690
8791def _get_colormap (
88- frame_data : pd .DataFrame , max_speed : float , color_mode : str
92+ frame_data : pd .DataFrame ,
93+ min_value : float ,
94+ max_value : float ,
95+ color_mode : str ,
8996) -> List [Scatter ]:
9097 """Utilize scatter plots with varying colors for each agent instead of individual shapes.
9198
@@ -97,8 +104,17 @@ def _get_colormap(
97104 "color" : frame_data ["speed" ],
98105 "colorscale" : "Jet_r" ,
99106 "colorbar" : {"title" : "Speed [m/s]" },
100- "cmin" : 0 ,
101- "cmax" : max_speed ,
107+ "cmin" : min_value ,
108+ "cmax" : max_value ,
109+ }
110+ elif color_mode == "Motivation" :
111+ marker_dict = {
112+ "size" : frame_data ["radius" ] * 2 ,
113+ "color" : frame_data ["motivation" ],
114+ "colorscale" : "Jet_r" ,
115+ "colorbar" : {"title" : "Motivation" },
116+ "cmin" : min_value ,
117+ "cmax" : max_value ,
102118 }
103119 else :
104120 colors = frame_data ["gender" ].map ({2 : "blue" , 1 : "green" })
@@ -113,7 +129,13 @@ def _get_colormap(
113129 y = frame_data ["y" ],
114130 mode = "markers" ,
115131 marker = marker_dict ,
116- text = frame_data ["speed" ] if color_mode == "speed" else frame_data ["gender" ],
132+ text = (
133+ frame_data ["speed" ]
134+ if color_mode == "Speed"
135+ else frame_data ["motivation" ]
136+ if color_mode == "Motivation"
137+ else frame_data ["gender" ]
138+ ),
117139 showlegend = False ,
118140 hoverinfo = "none" ,
119141 )
@@ -122,7 +144,10 @@ def _get_colormap(
122144
123145
124146def _get_shapes_for_frame (
125- frame_data : pd .DataFrame , min_speed : float , max_speed : float , color_mode : str
147+ frame_data : pd .DataFrame ,
148+ min_value : float ,
149+ max_value : float ,
150+ color_mode : str ,
126151) -> Tuple [Shape , Scatter , Shape ]:
127152 """Construct circles as Shapes for agents, Hover and Directions."""
128153
@@ -162,7 +187,9 @@ def create_shape(row: pd.DataFrame) -> Shape:
162187 _create_orientation_line (row , color = "rgba(255,255,255,0)" ),
163188 )
164189 if color_mode == "Speed" :
165- color = _speed_to_color (row ["speed" ], min_speed , max_speed )
190+ color = _value_to_color (row ["speed" ], min_value , max_value )
191+ elif color_mode == "Motivation" :
192+ color = _value_to_color (row ["motivation" ], min_value , max_value )
166193 else :
167194 gender_colors = {
168195 1 : "blue" , # Assuming 1 is for female
@@ -301,7 +328,14 @@ def _get_processed_frame_data(
301328 """Process frame data and ensure it matches the maximum agent count."""
302329 frame_data = data_df [data_df ["frame" ] == frame_num ]
303330 agent_count = len (frame_data )
304- dummy_agent_data = {"x" : 0 , "y" : 0 , "radius" : 0 , "speed" : DUMMY_SPEED }
331+ dummy_agent_data = {
332+ "x" : 0 ,
333+ "y" : 0 ,
334+ "radius" : 0 ,
335+ "speed" : DUMMY_SPEED ,
336+ "motivation" : 0.0 ,
337+ "gender" : 0 ,
338+ }
305339 while len (frame_data ) < max_agents :
306340 dummy_df = pd .DataFrame ([dummy_agent_data ])
307341 frame_data = pd .concat ([frame_data , dummy_df ], ignore_index = True )
@@ -326,8 +360,15 @@ def animate(
326360 """Animate a trajectory."""
327361 data_df ["radius" ] = radius
328362 frames = data_df ["frame" ].unique ()
329- min_speed = data_df ["speed" ].min ()
330- max_speed = data_df ["speed" ].max ()
363+ if color_mode == "Speed" :
364+ min_value = data_df ["speed" ].min ()
365+ max_value = data_df ["speed" ].max ()
366+ elif color_mode == "Motivation" :
367+ min_value = data_df ["motivation" ].min ()
368+ max_value = data_df ["motivation" ].max ()
369+ else :
370+ min_value = 0.0
371+ max_value = 1.0
331372 max_agents = data_df .groupby ("frame" ).size ().max ()
332373 frames = []
333374 steps = []
@@ -340,14 +381,16 @@ def animate(
340381 initial_shapes ,
341382 initial_hover_trace ,
342383 initial_arrows ,
343- ) = _get_shapes_for_frame (initial_frame_data , min_speed , max_speed , color_mode )
344- color_map_trace = _get_colormap (initial_frame_data , max_speed , color_mode )
384+ ) = _get_shapes_for_frame (initial_frame_data , min_value , max_value , color_mode )
385+ color_map_trace = _get_colormap (
386+ initial_frame_data , min_value , max_value , color_mode
387+ )
345388 for frame_num in selected_frames :
346389 frame_data , agent_count = _get_processed_frame_data (
347390 data_df , frame_num , max_agents
348391 )
349392 shapes , hover_traces , arrows = _get_shapes_for_frame (
350- frame_data , min_speed , max_speed , color_mode
393+ frame_data , min_value , max_value , color_mode
351394 )
352395 # title = f"<b>{title_note + ' | ' if title_note else ''}N: {agent_count}</b>"
353396 title = f"<b>{ title_note + ' | ' if title_note else '' } Number of Agents: { initial_agent_count } </b>"
0 commit comments