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
+
1
7
import warnings
2
8
from collections .abc import Callable
3
9
from dataclasses import fields
@@ -201,8 +207,6 @@ def collect_agent_data(
201
207
202
208
return final_data
203
209
204
-
205
-
206
210
def draw_agents (
207
211
self , arguments , chart_width : int = 450 , chart_height : int = 350 , ** kwargs
208
212
):
@@ -219,7 +223,8 @@ def draw_agents(
219
223
"size" : arguments ["size" ][i ],
220
224
"shape" : arguments ["shape" ][i ],
221
225
"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
223
228
"original_color" : arguments ["color" ][i ],
224
229
}
225
230
# Add tooltip data if available
@@ -230,7 +235,11 @@ def draw_agents(
230
235
# Determine fill and stroke colors
231
236
if arguments ["filled" ][i ]:
232
237
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
+ )
234
243
else :
235
244
record ["viz_fill_color" ] = None
236
245
record ["viz_stroke_color" ] = arguments ["color" ][i ]
@@ -240,19 +249,18 @@ def draw_agents(
240
249
df = pd .DataFrame (records )
241
250
242
251
# 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" ]
244
253
for col in numeric_cols :
245
254
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" )
248
256
249
257
# Get tooltip keys from the first valid record
250
258
tooltip_list = ["x" , "y" ]
251
259
# This is the corrected line:
252
260
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 ())
261
+ first_valid_tooltip = next ((t for t in arguments ["tooltip" ] if t ), None )
262
+ if first_valid_tooltip :
263
+ tooltip_list .extend (first_valid_tooltip .keys ())
256
264
257
265
# Extract additional parameters from kwargs
258
266
title = kwargs .pop ("title" , "" )
@@ -316,10 +324,16 @@ def draw_agents(
316
324
),
317
325
title = "Shape" ,
318
326
),
319
- opacity = alt .Opacity ("opacity:Q" , title = "Opacity" , scale = alt .Scale (domain = [0 , 1 ], range = [0 , 1 ])),
327
+ opacity = alt .Opacity (
328
+ "opacity:Q" ,
329
+ title = "Opacity" ,
330
+ scale = alt .Scale (domain = [0 , 1 ], range = [0 , 1 ]),
331
+ ),
320
332
fill = fill_encoding ,
321
333
stroke = alt .Stroke ("viz_stroke_color:N" , scale = None ),
322
- strokeWidth = alt .StrokeWidth ("strokeWidth:Q" , scale = alt .Scale (domain = [0 , 1 ])),
334
+ strokeWidth = alt .StrokeWidth (
335
+ "strokeWidth:Q" , scale = alt .Scale (domain = [0 , 1 ])
336
+ ),
323
337
tooltip = tooltip_list ,
324
338
)
325
339
.properties (title = title , width = chart_width , height = chart_height )
@@ -431,4 +445,4 @@ def draw_propertylayer(
431
445
main_charts .append (current_chart )
432
446
433
447
base = alt .layer (* main_charts ).resolve_scale (color = "independent" )
434
- return base
448
+ return base
0 commit comments