Skip to content

Commit bc039e4

Browse files
pre-commit-ci[bot]DipayanDasgupta
authored andcommitted
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 496dc79 commit bc039e4

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

mesa/examples/basic/boltzmann_wealth_model/app.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import altair as alt
2-
31
from mesa.examples.basic.boltzmann_wealth_model.model import BoltzmannWealth
42
from mesa.mesa_logging import INFO, log_to_stderr
53
from mesa.visualization import (
@@ -67,4 +65,4 @@ def agent_portrayal(agent):
6765
model_params=model_params,
6866
name="Boltzmann Wealth Model",
6967
)
70-
page # noqa
68+
page # noqa

mesa/visualization/backends/altair_backend.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
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+
17
import warnings
28
from collections.abc import Callable
39
from 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,18 @@ 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"]
251259
# This is the corrected line:
252260
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())
256264

257265
# Extract additional parameters from kwargs
258266
title = kwargs.pop("title", "")
@@ -316,10 +324,16 @@ def draw_agents(
316324
),
317325
title="Shape",
318326
),
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+
),
320332
fill=fill_encoding,
321333
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+
),
323337
tooltip=tooltip_list,
324338
)
325339
.properties(title=title, width=chart_width, height=chart_height)
@@ -431,4 +445,4 @@ def draw_propertylayer(
431445
main_charts.append(current_chart)
432446

433447
base = alt.layer(*main_charts).resolve_scale(color="independent")
434-
return base
448+
return base

mesa/visualization/components/portrayal_components.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,4 @@ def __post_init__(self):
118118
if self.color is not None and self.colormap is not None:
119119
raise ValueError("Specify either 'color' or 'colormap', not both.")
120120
if self.color is None and self.colormap is None:
121-
raise ValueError("Specify one of 'color' or 'colormap'")
121+
raise ValueError("Specify one of 'color' or 'colormap'")

tests/test_backends.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ def test_altair_backend_draw_agents():
248248
"color": np.array(["red", "blue"]),
249249
"filled": np.array([True, True]),
250250
"stroke": np.array(["black", "black"]),
251+
"tooltip": np.array([None, None]),
251252
}
252253
ab.space_drawer.get_viz_limits = MagicMock(return_value=(0, 10, 0, 10))
253254
assert ab.draw_agents(arguments) is not None

0 commit comments

Comments
 (0)