1+ """Altair-based renderer for Mesa spaces.
2+
3+ This module provides an Altair-based renderer for visualizing Mesa model spaces,
4+ agents, and property layers with interactive charting capabilities.
5+ """
6+
17import warnings
28from collections .abc import Callable
39from dataclasses import fields
@@ -201,8 +207,6 @@ def collect_agent_data(
201207
202208 return final_data
203209
204-
205-
206210 def draw_agents (
207211 self , arguments , chart_width : int = 450 , chart_height : int = 350 , ** kwargs
208212 ):
@@ -219,7 +223,8 @@ def draw_agents(
219223 "size" : arguments ["size" ][i ],
220224 "shape" : arguments ["shape" ][i ],
221225 "opacity" : arguments ["opacity" ][i ],
222- "strokeWidth" : arguments ["strokeWidth" ][i ] / 10 , # Scale for continuous domain
226+ "strokeWidth" : arguments ["strokeWidth" ][i ]
227+ / 10 , # Scale for continuous domain
223228 "original_color" : arguments ["color" ][i ],
224229 }
225230 # Add tooltip data if available
@@ -230,7 +235,11 @@ def draw_agents(
230235 # Determine fill and stroke colors
231236 if arguments ["filled" ][i ]:
232237 record ["viz_fill_color" ] = arguments ["color" ][i ]
233- record ["viz_stroke_color" ] = arguments ["stroke" ][i ] if isinstance (arguments ["stroke" ][i ], str ) else None
238+ record ["viz_stroke_color" ] = (
239+ arguments ["stroke" ][i ]
240+ if isinstance (arguments ["stroke" ][i ], str )
241+ else None
242+ )
234243 else :
235244 record ["viz_fill_color" ] = None
236245 record ["viz_stroke_color" ] = arguments ["color" ][i ]
@@ -240,19 +249,19 @@ def draw_agents(
240249 df = pd .DataFrame (records )
241250
242251 # Ensure all columns that should be numeric are, handling potential Nones
243- numeric_cols = ['x' , 'y' , ' size' , ' opacity' , ' strokeWidth' , ' original_color' ]
252+ numeric_cols = ["x" , "y" , " size" , " opacity" , " strokeWidth" , " original_color" ]
244253 for col in numeric_cols :
245254 if col in df .columns :
246- df [col ] = pd .to_numeric (df [col ], errors = 'coerce' )
247-
255+ df [col ] = pd .to_numeric (df [col ], errors = "coerce" )
248256
249257 # Get tooltip keys from the first valid record
250258 tooltip_list = ["x" , "y" ]
251- # This is the corrected line:
252259 if any (t is not None for t in arguments ["tooltip" ]):
253- first_valid_tooltip = next ((t for t in arguments ["tooltip" ] if t ), None )
254- if first_valid_tooltip :
255- tooltip_list .extend (first_valid_tooltip .keys ())
260+ first_valid_tooltip = next (
261+ (t for t in arguments ["tooltip" ] if t is not None ), None
262+ )
263+ if first_valid_tooltip is not None :
264+ tooltip_list .extend (first_valid_tooltip .keys ())
256265
257266 # Extract additional parameters from kwargs
258267 title = kwargs .pop ("title" , "" )
@@ -316,10 +325,16 @@ def draw_agents(
316325 ),
317326 title = "Shape" ,
318327 ),
319- opacity = alt .Opacity ("opacity:Q" , title = "Opacity" , scale = alt .Scale (domain = [0 , 1 ], range = [0 , 1 ])),
328+ opacity = alt .Opacity (
329+ "opacity:Q" ,
330+ title = "Opacity" ,
331+ scale = alt .Scale (domain = [0 , 1 ], range = [0 , 1 ]),
332+ ),
320333 fill = fill_encoding ,
321334 stroke = alt .Stroke ("viz_stroke_color:N" , scale = None ),
322- strokeWidth = alt .StrokeWidth ("strokeWidth:Q" , scale = alt .Scale (domain = [0 , 1 ])),
335+ strokeWidth = alt .StrokeWidth (
336+ "strokeWidth:Q" , scale = alt .Scale (domain = [0 , 1 ])
337+ ),
323338 tooltip = tooltip_list ,
324339 )
325340 .properties (title = title , width = chart_width , height = chart_height )
@@ -431,4 +446,4 @@ def draw_propertylayer(
431446 main_charts .append (current_chart )
432447
433448 base = alt .layer (* main_charts ).resolve_scale (color = "independent" )
434- return base
449+ return base
0 commit comments