Skip to content

Commit 46ceaf6

Browse files
committed
Fixed ruff errors
1 parent cd90f94 commit 46ceaf6

File tree

1 file changed

+37
-23
lines changed

1 file changed

+37
-23
lines changed

cellular_automata/von_neumann.py

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,17 @@
1515
"""
1616

1717
import numpy as np
18-
from typing import Set, Tuple, Dict, Optional
1918

2019

2120
def create_random_grid(
22-
rows: int, columns: int, alive_probability: float, seed: Optional[int] = None
21+
rows: int, columns: int, alive_probability: float, seed: int | None = None
2322
) -> np.ndarray:
2423
"""
2524
Create initial grid with randomly distributed alive cells.
2625
2726
Args:
2827
rows: Number of grid rows
29-
columns: Number of grid columns
28+
columns: Number of grid columns
3029
alive_probability: Probability (0.0-1.0) of each cell being initially alive
3130
seed: Random seed for reproducibility
3231
@@ -60,7 +59,9 @@ def create_random_grid(
6059
raise ValueError("alive_probability must be between 0.0 and 1.0")
6160

6261
rng = np.random.default_rng(seed)
63-
alive_cells = (rng.random((rows, columns)) < alive_probability).astype(np.uint8)
62+
alive_cells = (rng.random((rows, columns)) < alive_probability).astype(
63+
np.uint8
64+
)
6465
return alive_cells
6566

6667

@@ -88,11 +89,13 @@ def count_von_neumann_neighbors(
8889
>>> counts = count_von_neumann_neighbors(mask, use_wraparound=False)
8990
>>> int(counts[1, 1]) # center cell has 0 neighbors (all adjacent are 0)
9091
0
91-
>>> int(counts[0, 1]) # top middle has 3 neighbors (down, left, right are 1)
92+
>>> int(counts[0, 1]) # top middle has 3 neighbors (down, left, right)
9293
3
9394
9495
>>> mask_simple = np.array([[1, 1], [1, 0]], dtype=np.uint8)
95-
>>> counts_simple = count_von_neumann_neighbors(mask_simple, use_wraparound=False)
96+
>>> counts_simple = count_von_neumann_neighbors(
97+
... mask_simple, use_wraparound=False
98+
... )
9699
>>> int(counts_simple[0, 0]) # top-left has 2 neighbors (right and down)
97100
2
98101
@@ -115,23 +118,25 @@ def count_von_neumann_neighbors(
115118
down_neighbors = np.roll(alive_mask, 1, axis=0)
116119
left_neighbors = np.roll(alive_mask, -1, axis=1)
117120
right_neighbors = np.roll(alive_mask, 1, axis=1)
118-
neighbor_counts = up_neighbors + down_neighbors + left_neighbors + right_neighbors
121+
neighbor_counts = (
122+
up_neighbors + down_neighbors + left_neighbors + right_neighbors
123+
)
119124
else:
120125
# Manually count neighbors without wraparound
121126
for r in range(rows):
122127
for c in range(cols):
123128
count = 0
124129
# Check up
125-
if r > 0 and alive_mask[r-1, c]:
130+
if r > 0 and alive_mask[r - 1, c]:
126131
count += 1
127132
# Check down
128-
if r < rows-1 and alive_mask[r+1, c]:
133+
if r < rows - 1 and alive_mask[r + 1, c]:
129134
count += 1
130135
# Check left
131-
if c > 0 and alive_mask[r, c-1]:
136+
if c > 0 and alive_mask[r, c - 1]:
132137
count += 1
133138
# Check right
134-
if c < cols-1 and alive_mask[r, c+1]:
139+
if c < cols - 1 and alive_mask[r, c + 1]:
135140
count += 1
136141
neighbor_counts[r, c] = count
137142

@@ -140,8 +145,8 @@ def count_von_neumann_neighbors(
140145

141146
def apply_cellular_automaton_rules(
142147
current_ages: np.ndarray,
143-
birth_neighbor_counts: Set[int],
144-
survival_neighbor_counts: Set[int],
148+
birth_neighbor_counts: set[int],
149+
survival_neighbor_counts: set[int],
145150
maximum_age: int = 5,
146151
use_wraparound: bool = True,
147152
) -> np.ndarray:
@@ -168,22 +173,25 @@ def apply_cellular_automaton_rules(
168173
Examples:
169174
>>> ages = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]], dtype=np.uint8)
170175
>>> new_ages = apply_cellular_automaton_rules(
171-
... ages, birth_neighbor_counts={2},
176+
... ages, birth_neighbor_counts={2},
172177
... survival_neighbor_counts={2, 3}, use_wraparound=False
173178
... )
174-
>>> bool(new_ages[0, 0] > 0) # corner should be born (2 neighbors: right and down)
179+
>>> # corner should be born (2 neighbors: right and down)
180+
>>> bool(new_ages[0, 0] > 0)
175181
True
176182
177183
>>> # Test aging of dead cells
178184
>>> dead_aging = np.array([[2, 0, 0]], dtype=np.uint8) # age 2, no survival
179185
>>> result = apply_cellular_automaton_rules(
180-
... dead_aging, birth_neighbor_counts=set(),
186+
... dead_aging, birth_neighbor_counts=set(),
181187
... survival_neighbor_counts=set(), maximum_age=3
182188
... )
183189
>>> bool(result[0, 0] == 3) # should age from 2 to 3
184190
True
185191
186-
>>> apply_cellular_automaton_rules(np.array([1, 2]), {1}, {1}) # doctest: +IGNORE_EXCEPTION_DETAIL
192+
>>> apply_cellular_automaton_rules(
193+
... np.array([1, 2]), {1}, {1}
194+
... ) # doctest: +IGNORE_EXCEPTION_DETAIL
187195
Traceback (most recent call last):
188196
ValueError: current_ages must be a 2D array
189197
"""
@@ -198,8 +206,12 @@ def apply_cellular_automaton_rules(
198206
)
199207

200208
# Determine which cells are born or survive
201-
birth_mask = (~alive_cells_mask) & np.isin(neighbor_counts, list(birth_neighbor_counts))
202-
survival_mask = alive_cells_mask & np.isin(neighbor_counts, list(survival_neighbor_counts))
209+
birth_mask = (~alive_cells_mask) & np.isin(
210+
neighbor_counts, list(birth_neighbor_counts)
211+
)
212+
survival_mask = alive_cells_mask & np.isin(
213+
neighbor_counts, list(survival_neighbor_counts)
214+
)
203215

204216
new_ages = current_ages.copy()
205217

@@ -223,11 +235,11 @@ def simulate_von_neumann_cellular_automaton(
223235
grid_rows: int = 20,
224236
grid_columns: int = 40,
225237
initial_alive_probability: float = 0.25,
226-
birth_rules: Set[int] = None,
227-
survival_rules: Set[int] = None,
238+
birth_rules: set[int] | None = None,
239+
survival_rules: set[int] | None = None,
228240
maximum_cell_age: int = 5,
229241
generations: int = 100,
230-
random_seed: Optional[int] = None,
242+
random_seed: int | None = None,
231243
use_wraparound_edges: bool = True,
232244
) -> list[np.ndarray]:
233245
"""
@@ -262,7 +274,9 @@ def simulate_von_neumann_cellular_automaton(
262274
>>> all(grid.shape == (5, 5) for grid in result)
263275
True
264276
265-
>>> simulate_von_neumann_cellular_automaton(generations=0) # doctest: +IGNORE_EXCEPTION_DETAIL
277+
>>> simulate_von_neumann_cellular_automaton(
278+
... generations=0
279+
... ) # doctest: +IGNORE_EXCEPTION_DETAIL
266280
Traceback (most recent call last):
267281
ValueError: generations must be positive
268282
"""

0 commit comments

Comments
 (0)