Skip to content

Commit 4d4cb90

Browse files
quaquelEwoutH
authored andcommitted
ruff fixes in visualization (#2867)
1 parent 268dc8b commit 4d4cb90

File tree

8 files changed

+70
-44
lines changed

8 files changed

+70
-44
lines changed

mesa/visualization/mpl_space_drawing.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def draw_space(
205205
206206
"""
207207
if ax is None:
208-
fig, ax = plt.subplots()
208+
_, ax = plt.subplots()
209209

210210
# https://stackoverflow.com/questions/67524641/convert-multiple-isinstance-checks-to-structural-pattern-matching
211211
match space:
@@ -443,7 +443,7 @@ def draw_orthogonal_grid(
443443
444444
"""
445445
if ax is None:
446-
fig, ax = plt.subplots()
446+
_, ax = plt.subplots()
447447

448448
# gather agent data
449449
s_default = (180 / max(space.width, space.height)) ** 2
@@ -488,7 +488,7 @@ def draw_hex_grid(
488488
"size", "marker", "zorder", alpha, linewidths, and edgecolors. Other field are ignored and will result in a user warning.
489489
"""
490490
if ax is None:
491-
fig, ax = plt.subplots()
491+
_, ax = plt.subplots()
492492

493493
# gather data
494494
s_default = (180 / max(space.width, space.height)) ** 2
@@ -573,7 +573,7 @@ def draw_network(
573573
574574
"""
575575
if ax is None:
576-
fig, ax = plt.subplots()
576+
_, ax = plt.subplots()
577577
if layout_kwargs is None:
578578
layout_kwargs = {"seed": 0}
579579

@@ -642,7 +642,7 @@ def draw_continuous_space(
642642
643643
"""
644644
if ax is None:
645-
fig, ax = plt.subplots()
645+
_, ax = plt.subplots()
646646

647647
# space related setup
648648
width = space.x_max - space.x_min
@@ -694,7 +694,7 @@ def draw_voronoi_grid(
694694
695695
"""
696696
if ax is None:
697-
fig, ax = plt.subplots()
697+
_, ax = plt.subplots()
698698

699699
x_list = [i[0] for i in space.centroids_coordinates]
700700
y_list = [i[1] for i in space.centroids_coordinates]

mesa/visualization/space_drawers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def draw_matplotlib(self, ax=None, **space_kwargs):
101101
}
102102

103103
if ax is None:
104-
fig, ax = plt.subplots(**fig_kwargs)
104+
_, ax = plt.subplots(**fig_kwargs)
105105

106106
# gridline styling kwargs
107107
line_kwargs = {
@@ -282,7 +282,7 @@ def draw_matplotlib(self, ax=None, **space_kwargs):
282282
}
283283

284284
if ax is None:
285-
fig, ax = plt.subplots(**fig_kwargs)
285+
_, ax = plt.subplots(**fig_kwargs)
286286

287287
line_kwargs = {
288288
"color": "black",
@@ -414,7 +414,7 @@ def draw_matplotlib(self, ax=None, **space_kwargs):
414414
The modified axes object.
415415
"""
416416
if ax is None:
417-
fig, ax = plt.subplots()
417+
_, ax = plt.subplots()
418418

419419
ax.set_axis_off()
420420
ax.set_xlim(self.viz_xmin, self.viz_xmax)
@@ -554,7 +554,7 @@ def draw_matplotlib(self, ax=None, **space_kwargs):
554554
The modified axes object
555555
"""
556556
if ax is None:
557-
fig, ax = plt.subplots()
557+
_, ax = plt.subplots()
558558

559559
border_style = "solid" if not self.space.torus else (0, (5, 10))
560560
spine_kwargs = {"linewidth": 1.5, "color": "black", "linestyle": border_style}
@@ -726,7 +726,7 @@ def draw_matplotlib(self, ax=None, **space_kwargs):
726726
The modified axes object
727727
"""
728728
if ax is None:
729-
fig, ax = plt.subplots()
729+
_, ax = plt.subplots()
730730

731731
final_segments, clip_box = self._get_clipped_segments()
732732

tests/test_components_matplotlib.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def my_portrayal(agent):
6969
agent = Agent(model)
7070
grid.move_to_empty(agent)
7171

72-
fig, ax = plt.subplots()
72+
_, ax = plt.subplots()
7373
draw_space(grid, my_portrayal, ax=ax)
7474

7575
# draw space for voroinoi
@@ -80,7 +80,7 @@ def my_portrayal(agent):
8080
agent = CellAgent(model)
8181
agent.cell = grid.select_random_empty_cell()
8282

83-
fig, ax = plt.subplots()
83+
_, ax = plt.subplots()
8484
draw_space(grid, my_portrayal, ax=ax)
8585

8686
# draw orthogonal grid
@@ -89,7 +89,7 @@ def my_portrayal(agent):
8989
for _ in range(10):
9090
agent = CellAgent(model)
9191
agent.cell = grid.select_random_empty_cell()
92-
fig, ax = plt.subplots()
92+
_, ax = plt.subplots()
9393
draw_space(grid, my_portrayal, ax=ax)
9494

9595
# draw network
@@ -104,7 +104,7 @@ def my_portrayal(agent):
104104
agent = Agent(model)
105105
pos = agent.random.randint(0, len(graph.nodes) - 1)
106106
grid.place_agent(agent, pos)
107-
fig, ax = plt.subplots()
107+
_, ax = plt.subplots()
108108
draw_space(grid, my_portrayal, ax=ax)
109109

110110
# draw continuous space
@@ -116,7 +116,7 @@ def my_portrayal(agent):
116116
agent = Agent(model)
117117
space.place_agent(agent, (x, y))
118118

119-
fig, ax = plt.subplots()
119+
_, ax = plt.subplots()
120120
draw_space(space, my_portrayal, ax=ax)
121121

122122

@@ -128,7 +128,7 @@ def test_draw_hex_grid():
128128
agent = Agent(model)
129129
grid.move_to_empty(agent)
130130

131-
fig, ax = plt.subplots()
131+
_, ax = plt.subplots()
132132
draw_hex_grid(grid, agent_portrayal, ax)
133133

134134
model = Model(seed=42)
@@ -137,7 +137,7 @@ def test_draw_hex_grid():
137137
agent = CellAgent(model)
138138
agent.cell = grid.select_random_empty_cell()
139139

140-
fig, ax = plt.subplots()
140+
_, ax = plt.subplots()
141141
draw_hex_grid(grid, agent_portrayal, ax)
142142

143143

@@ -152,7 +152,7 @@ def test_draw_voronoi_grid():
152152
agent = CellAgent(model)
153153
agent.cell = grid.select_random_empty_cell()
154154

155-
fig, ax = plt.subplots()
155+
_, ax = plt.subplots()
156156
draw_voronoi_grid(grid, agent_portrayal, ax)
157157

158158

@@ -164,7 +164,7 @@ def test_draw_orthogonal_grid():
164164
agent = Agent(model)
165165
grid.move_to_empty(agent)
166166

167-
fig, ax = plt.subplots()
167+
_, ax = plt.subplots()
168168
draw_orthogonal_grid(grid, agent_portrayal, ax)
169169

170170
model = Model(seed=42)
@@ -173,7 +173,7 @@ def test_draw_orthogonal_grid():
173173
agent = CellAgent(model)
174174
agent.cell = grid.select_random_empty_cell()
175175

176-
fig, ax = plt.subplots()
176+
_, ax = plt.subplots()
177177
draw_orthogonal_grid(grid, agent_portrayal, ax)
178178

179179

@@ -187,7 +187,7 @@ def test_draw_continuous_space():
187187
agent = Agent(model)
188188
space.place_agent(agent, (x, y))
189189

190-
fig, ax = plt.subplots()
190+
_, ax = plt.subplots()
191191
draw_continuous_space(space, agent_portrayal, ax)
192192

193193

@@ -205,7 +205,7 @@ def test_draw_network():
205205
pos = agent.random.randint(0, len(graph.nodes) - 1)
206206
grid.place_agent(agent, pos)
207207

208-
fig, ax = plt.subplots()
208+
_, ax = plt.subplots()
209209
draw_network(grid, agent_portrayal, ax)
210210

211211
model = Model(seed=42)
@@ -214,7 +214,7 @@ def test_draw_network():
214214
agent = CellAgent(model)
215215
agent.cell = grid.select_random_empty_cell()
216216

217-
fig, ax = plt.subplots()
217+
_, ax = plt.subplots()
218218
draw_network(grid, agent_portrayal, ax)
219219

220220

@@ -224,12 +224,12 @@ def test_draw_property_layers():
224224
grid = SingleGrid(10, 10, torus=True)
225225
grid.add_property_layer(PropertyLayer("test", grid.width, grid.height, 0))
226226

227-
fig, ax = plt.subplots()
227+
_, ax = plt.subplots()
228228
draw_property_layers(grid, {"test": {"colormap": "viridis", "colorbar": True}}, ax)
229229

230230
model = Model(seed=42)
231231
grid = OrthogonalMooreGrid((10, 10), torus=True, random=model.random, capacity=1)
232232
grid.create_property_layer("test", 0.0)
233233

234-
fig, ax = plt.subplots()
234+
_, ax = plt.subplots()
235235
draw_property_layers(grid, {"test": {"colormap": "viridis", "colorbar": True}}, ax)

tests/test_discrete_space.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import copy
44
import pickle
55
import random
6+
import re
67

78
import networkx as nx
89
import numpy as np
@@ -909,7 +910,9 @@ def test_property_layer_errors():
909910

910911
# Test adding a PropertyLayer with an existing name
911912
grid.add_property_layer(elevation)
912-
with pytest.raises(ValueError, match="Property layer elevation already exists."):
913+
with pytest.raises(
914+
ValueError, match=re.escape("Property layer elevation already exists.")
915+
):
913916
grid.add_property_layer(elevation)
914917

915918
# test adding a layer with different dimensions than space
@@ -918,7 +921,9 @@ def test_property_layer_errors():
918921
elevation = PropertyLayer("elevation", (10, 10), default_value=0.0)
919922
with pytest.raises(
920923
ValueError,
921-
match="Dimensions of property layer do not match the dimensions of the grid",
924+
match=re.escape(
925+
"Dimensions of property layer do not match the dimensions of the grid"
926+
),
922927
):
923928
grid.add_property_layer(elevation)
924929

tests/test_portrayal_components.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Tests for the portrayal components in Mesa visualization."""
22

3+
import re
34
from dataclasses import is_dataclass
45

56
import pytest
@@ -63,7 +64,8 @@ def test_agent_portrayal_style_update_non_existent_attribute():
6364
"""Test updating a non-existent attribute raises AttributeError."""
6465
style = AgentPortrayalStyle()
6566
with pytest.raises(
66-
AttributeError, match="'AgentPortrayalStyle' object has no attribute 'shape'"
67+
AttributeError,
68+
match=re.escape("'AgentPortrayalStyle' object has no attribute 'shape'"),
6769
):
6870
style.update(("shape", "triangle"))
6971

@@ -128,12 +130,14 @@ def test_property_layer_style_custom_initialization_with_color():
128130
def test_property_layer_style_post_init_both_color_and_colormap_error():
129131
"""Test error when both color and colormap are specified."""
130132
with pytest.raises(
131-
ValueError, match="Specify either 'color' or 'colormap', not both."
133+
ValueError, match=re.escape("Specify either 'color' or 'colormap', not both.")
132134
):
133135
PropertyLayerStyle(colormap="viridis", color="red")
134136

135137

136138
def test_property_layer_style_post_init_neither_color_nor_colormap_error():
137139
"""Test error when neither color nor colormap is specified."""
138-
with pytest.raises(ValueError, match="Specify one of 'color' or 'colormap'"):
140+
with pytest.raises(
141+
ValueError, match=re.escape("Specify one of 'color' or 'colormap'")
142+
):
139143
PropertyLayerStyle()

tests/test_solara_viz.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Test Solara visualizations."""
22

3+
import re
34
import unittest
45

56
import ipyvuetify as vw
@@ -254,23 +255,27 @@ def __init__(self, **kwargs):
254255
_check_model_params(ModelWithKwargs.__init__, {})
255256

256257
# Test invalid parameter name raises ValueError
257-
with pytest.raises(ValueError, match="Invalid model parameter: invalid_param"):
258+
with pytest.raises(
259+
ValueError, match=re.escape("Invalid model parameter: invalid_param")
260+
):
258261
_check_model_params(
259262
ModelWithOptionalParams.__init__, {"required_param": 1, "invalid_param": 2}
260263
)
261264

262265
# Test missing required parameter raises ValueError
263-
with pytest.raises(ValueError, match="Missing required model parameter: param2"):
266+
with pytest.raises(
267+
ValueError, match=re.escape("Missing required model parameter: param2")
268+
):
264269
_check_model_params(ModelWithOnlyRequired.__init__, {"param1": 1})
265270

266271
# Test passing extra parameters raises ValueError
267-
with pytest.raises(ValueError, match="Invalid model parameter: extra"):
272+
with pytest.raises(ValueError, match=re.escape("Invalid model parameter: extra")):
268273
_check_model_params(
269274
ModelWithOnlyRequired.__init__, {"param1": 1, "param2": 2, "extra": 3}
270275
)
271276

272277
# Test empty params dict raises ValueError if required params
273-
with pytest.raises(ValueError, match="Missing required model parameter"):
278+
with pytest.raises(ValueError, match=re.escape("Missing required model parameter")):
274279
_check_model_params(ModelWithOnlyRequired.__init__, {})
275280

276281

@@ -295,7 +300,7 @@ def __init__(self, param1):
295300
handle_error=False,
296301
)
297302

298-
with pytest.raises(ValueError, match="Missing required model parameter"):
303+
with pytest.raises(ValueError, match=re.escape("Missing required model parameter")):
299304
solara.render(
300305
ModelCreator(
301306
solara.reactive(ModelWithRequiredParam(param1="mock")), user_params={}
@@ -316,6 +321,8 @@ def __init__(self, param1, *args):
316321

317322
with pytest.raises(
318323
ValueError,
319-
match="Mesa's visualization requires the use of keyword arguments to ensure the parameters are passed to Solara correctly. Please ensure all model parameters are of form param=value",
324+
match=re.escape(
325+
"Mesa's visualization requires the use of keyword arguments to ensure the parameters are passed to Solara correctly. Please ensure all model parameters are of form param=value"
326+
),
320327
):
321328
_check_model_params(ModelWithArgsOnly.__init__, model_params)

0 commit comments

Comments
 (0)