Skip to content

Commit 4b72f3f

Browse files
committed
refactor: more ruff fixes
1 parent 3fed272 commit 4b72f3f

File tree

12 files changed

+31
-24
lines changed

12 files changed

+31
-24
lines changed

examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
def compute_gini(model):
66
agent_wealths = [agent.wealth for agent in model.agents]
77
x = sorted(agent_wealths)
8-
N = model.num_agents
9-
B = sum(xi * (N - i) for i, xi in enumerate(x)) / (N * sum(x))
8+
N = model.num_agents # noqa: N806
9+
B = sum(xi * (N - i) for i, xi in enumerate(x)) / (N * sum(x)) # noqa: N806
1010
return 1 + (1 / N) - 2 * B
1111

1212

@@ -45,7 +45,7 @@ def step(self):
4545
self.datacollector.collect(self)
4646

4747
def run_model(self, n):
48-
for i in range(n):
48+
for _ in range(n):
4949
self.step()
5050

5151

examples/boltzmann_wealth_model_network/boltzmann_wealth_model_network/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from .model import BoltzmannWealthModelNetwork
44

55

6-
def network_portrayal(G):
6+
def network_portrayal(G): # noqa: N803
77
# The model ensures there is 0 or 1 agent per node
88

99
portrayal = {}

examples/el_farol/el_farol.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
" for m in memory_sizes\n",
2525
"]\n",
2626
"for model in models:\n",
27-
" for i in range(100):\n",
27+
" for _ in range(100):\n",
2828
" model.step()"
2929
]
3030
},
@@ -99,7 +99,7 @@
9999
" for ns in num_strategies_list\n",
100100
"]\n",
101101
"for model in models:\n",
102-
" for i in range(100):\n",
102+
" for _ in range(100):\n",
103103
" model.step()"
104104
]
105105
},

examples/el_farol/el_farol/model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ def __init__(
1010
crowd_threshold=60,
1111
num_strategies=10,
1212
memory_size=10,
13-
N=100,
13+
num_agents=100,
1414
):
1515
super().__init__()
1616
self.running = True
17-
self.num_agents = N
17+
self.num_agents = num_agents
1818

1919
# Initialize the previous attendance randomly so the agents have a history
2020
# to work with from the start.

examples/shape_example/shape_example/model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ def __init__(self, model, heading=(1, 0)):
1010

1111

1212
class ShapeExample(mesa.Model):
13-
def __init__(self, N=2, width=20, height=10):
13+
def __init__(self, num_agents=2, width=20, height=10):
1414
super().__init__()
15-
self.N = N # num of agents
15+
self.num_agents = num_agents # num of agents
1616
self.headings = ((1, 0), (0, 1), (-1, 0), (0, -1)) # tuples are fast
1717
self.grid = OrthogonalMooreGrid((width, height), torus=True, random=self.random)
1818

1919
self.make_walker_agents()
2020
self.running = True
2121

2222
def make_walker_agents(self):
23-
for _ in range(self.N):
23+
for _ in range(self.num_agents):
2424
x = self.random.randrange(self.grid.dimensions[0])
2525
y = self.random.randrange(self.grid.dimensions[1])
2626
cell = self.grid[(x, y)]

gis/agents_and_networks/src/space/road_network.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class RoadNetwork:
1919

2020
def __init__(self, lines: gpd.GeoSeries):
2121
segmented_lines = gpd.GeoDataFrame(geometry=segmented(lines))
22-
G = momepy.gdf_to_nx(segmented_lines, approach="primal", length="length")
22+
G = momepy.gdf_to_nx(segmented_lines, approach="primal", length="length") # noqa: N806
2323
self.nx_graph = G.subgraph(max(nx.connected_components(G), key=len))
2424
self.crs = lines.crs
2525

@@ -70,7 +70,7 @@ def __init__(self, campus, lines, output_dir) -> None:
7070
self._path_cache_result = f"{output_dir}/{campus}_path_cache_result.pkl"
7171
try:
7272
with open(self._path_cache_result, "rb") as cached_result:
73-
self._path_select_cache = pickle.load(cached_result)
73+
self._path_select_cache = pickle.load(cached_result) # noqa: S301
7474
except FileNotFoundError:
7575
self._path_select_cache = {}
7676

gis/agents_and_networks/src/space/utils.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def get_coord_matrix(
2222
def get_affine_transform(
2323
from_coord: np.ndarray, to_coord: np.ndarray
2424
) -> tuple[float, float, float, float, float, float]:
25-
A, res, rank, s = np.linalg.lstsq(from_coord, to_coord, rcond=None)
25+
A, res, rank, s = np.linalg.lstsq(from_coord, to_coord, rcond=None) # noqa: N806
2626

2727
np.testing.assert_array_almost_equal(res, np.zeros_like(res), decimal=15)
2828
np.testing.assert_array_almost_equal(A[:, 2], np.array([0.0, 0.0, 1.0]), decimal=15)
@@ -57,7 +57,7 @@ def _segmented(linestring: LineString) -> list[LineString]:
5757
# reference: https://gis.stackexchange.com/questions/367228/using-shapely-interpolate-to-evenly-re-sample-points-on-a-linestring-geodatafram
5858
def redistribute_vertices(geom, distance):
5959
if isinstance(geom, LineString):
60-
if (num_vert := int(round(geom.length / distance))) == 0:
60+
if (num_vert := round(geom.length / distance)) == 0:
6161
num_vert = 1
6262
return LineString(
6363
[
@@ -78,9 +78,13 @@ class UnitTransformer:
7878
_degree2meter: pyproj.Transformer
7979
_meter2degree: pyproj.Transformer
8080

81-
def __init__(
82-
self, degree_crs=pyproj.CRS("EPSG:4326"), meter_crs=pyproj.CRS("EPSG:3857")
83-
):
81+
def __init__(self, degree_crs: pyproj.CRS | None, meter_crs: pyproj.CRS | None):
82+
if degree_crs is None:
83+
degree_crs = pyproj.CRS("EPSG:4326")
84+
85+
if meter_crs is None:
86+
meter_crs = pyproj.CRS("EPSG:3857")
87+
8488
self._degree2meter = pyproj.Transformer.from_crs(
8589
degree_crs, meter_crs, always_xy=True
8690
)

gis/geo_schelling/model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
def get_largest_connected_components(gdf):
1313
"""Get the largest connected component of a GeoDataFrame."""
1414
# create spatial weights matrix
15-
W = libpysal.weights.Queen.from_dataframe(
15+
w = libpysal.weights.Queen.from_dataframe(
1616
gdf, use_index=True, silence_warnings=True
1717
)
1818
# get component labels
19-
gdf["component"] = W.component_labels
19+
gdf["component"] = w.component_labels
2020
# get the largest component
2121
largest_component = gdf["component"].value_counts().idxmax()
2222
# subset the GeoDataFrame

gis/geo_schelling_points/geo_schelling_points/model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
def get_largest_connected_components(gdf):
1616
"""Get the largest connected component of a GeoDataFrame."""
1717
# create spatial weights matrix
18-
W = libpysal.weights.Queen.from_dataframe(
18+
w = libpysal.weights.Queen.from_dataframe(
1919
gdf, use_index=True, silence_warnings=True
2020
)
2121
# get component labels
22-
gdf["component"] = W.component_labels
22+
gdf["component"] = w.component_labels
2323
# get the largest component
2424
largest_component = gdf["component"].value_counts().idxmax()
2525
# subset the GeoDataFrame

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
requires = ["hatchling"]
33
build-backend = "hatchling.build"
44

5+
[tool.hatch.build.targets.wheel]
6+
packages = ["examples", "gis", "rl"]
7+
58
[project]
69
name = "mesa-models"
710
description = "Importable Mesa models."

0 commit comments

Comments
 (0)