-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
270 lines (217 loc) · 7.09 KB
/
conftest.py
File metadata and controls
270 lines (217 loc) · 7.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
"""
Shared pytest fixtures for Predator-Prey CA test suite.
"""
import pytest
import numpy as np
from dataclasses import dataclass
from typing import Tuple
# =============================================================================
# Minimal Config for Testing (avoids importing full config module)
# =============================================================================
@dataclass
class MinimalConfig:
"""Minimal configuration for fast test simulations."""
grid_size: int = 10
densities: Tuple[float, float] = (0.3, 0.15)
grid_sizes: Tuple[int, ...] = (5, 10)
prey_birth: float = 0.2
prey_death: float = 0.05
predator_birth: float = 0.8
predator_death: float = 0.05
critical_prey_birth: float = 0.2
critical_prey_death: float = 0.097
prey_death_range: Tuple[float, float] = (0.05, 0.15)
n_prey_death: int = 3
n_replicates: int = 2
warmup_steps: int = 5
measurement_steps: int = 10
evolve_sd: float = 0.05
evolve_min: float = 0.01
evolve_max: float = 0.15
directed_hunting: bool = False
save_timeseries: bool = False
timeseries_subsample: int = 2
collect_pcf: bool = False
pcf_sample_rate: float = 0.0
pcf_max_distance: float = 5.0
pcf_n_bins: int = 10
min_density_for_analysis: float = 0.01
n_jobs: int = 1
def get_prey_deaths(self) -> np.ndarray:
return np.linspace(
self.prey_death_range[0], self.prey_death_range[1], self.n_prey_death
)
def get_warmup_steps(self, L: int) -> int:
return self.warmup_steps
def get_measurement_steps(self, L: int) -> int:
return self.measurement_steps
# =============================================================================
# Grid Fixtures
# =============================================================================
@pytest.fixture
def empty_grid_10x10():
"""10x10 grid with no species."""
return np.zeros((10, 10), dtype=np.int32)
@pytest.fixture
def prey_only_grid_10x10():
"""10x10 grid with only prey (species 1) in a known pattern."""
grid = np.zeros((10, 10), dtype=np.int32)
grid[2:5, 2:5] = 1 # 3x3 block of prey = 9 cells
return grid
@pytest.fixture
def predator_only_grid_10x10():
"""10x10 grid with only predators (species 2)."""
grid = np.zeros((10, 10), dtype=np.int32)
grid[0, 0] = 2
grid[0, 9] = 2
grid[9, 0] = 2
grid[9, 9] = 2 # 4 predators in corners
return grid
@pytest.fixture
def mixed_grid_10x10():
"""10x10 grid with both prey and predators."""
grid = np.zeros((10, 10), dtype=np.int32)
# Prey cluster
grid[1:4, 1:4] = 1 # 9 prey
# Predator cluster
grid[6:8, 6:8] = 2 # 4 predators
return grid
@pytest.fixture
def single_cluster_grid():
"""Grid with exactly one connected cluster of prey."""
grid = np.zeros((5, 5), dtype=np.int32)
grid[1, 1] = 1
grid[1, 2] = 1
grid[2, 1] = 1
grid[2, 2] = 1 # 2x2 block = 4 connected cells
return grid
@pytest.fixture
def two_cluster_grid():
"""Grid with two separate prey clusters (no periodic connection)."""
grid = np.zeros((10, 10), dtype=np.int32)
# Cluster 1: top-left corner
grid[0, 0] = 1
grid[0, 1] = 1
grid[1, 0] = 1 # 3 cells
# Cluster 2: center (far enough to avoid periodic Moore connection)
grid[4, 4] = 1
grid[4, 5] = 1
grid[5, 4] = 1
grid[5, 5] = 1 # 4 cells
return grid
@pytest.fixture
def periodic_cluster_grid():
"""Grid where prey connect via periodic boundary."""
grid = np.zeros((5, 5), dtype=np.int32)
grid[0, 0] = 1 # Top-left
grid[4, 0] = 1 # Bottom-left (connects to top-left via periodic)
grid[0, 4] = 1 # Top-right (connects to top-left via periodic)
return grid
@pytest.fixture
def checkerboard_grid():
"""Alternating pattern - many small clusters."""
grid = np.zeros((6, 6), dtype=np.int32)
for i in range(6):
for j in range(6):
if (i + j) % 2 == 0:
grid[i, j] = 1
return grid
# =============================================================================
# Config Fixtures
# =============================================================================
@pytest.fixture
def minimal_config():
"""Minimal config for fast test runs."""
return MinimalConfig()
@pytest.fixture
def minimal_config_with_pcf():
"""Config with PCF collection enabled."""
return MinimalConfig(collect_pcf=True, pcf_sample_rate=1.0)
@pytest.fixture
def minimal_config_with_timeseries():
"""Config with time series collection enabled."""
return MinimalConfig(save_timeseries=True)
@pytest.fixture
def minimal_config_directed():
"""Config with directed hunting enabled."""
return MinimalConfig(directed_hunting=True)
# =============================================================================
# Model Fixtures
# =============================================================================
@pytest.fixture
def pp_model_small():
"""Small PP model for quick tests."""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))
from models.CA import PP
return PP(
rows=10,
cols=10,
densities=(0.3, 0.15),
neighborhood="moore",
seed=42,
directed_hunting=False,
)
@pytest.fixture
def pp_model_with_evolution():
"""PP model with evolution enabled."""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))
from models.CA import PP
model = PP(
rows=10,
cols=10,
densities=(0.3, 0.15),
neighborhood="moore",
seed=42,
)
model.evolve("prey_death", sd=0.05, min_val=0.01, max_val=0.15)
return model
# =============================================================================
# Utility Fixtures
# =============================================================================
@pytest.fixture
def temp_output_dir(tmp_path):
"""Temporary directory for test outputs."""
output_dir = tmp_path / "test_results"
output_dir.mkdir()
return output_dir
@pytest.fixture
def sample_results():
"""Sample simulation results for I/O testing."""
return [
{
"prey_birth": 0.2,
"prey_death": 0.05,
"predator_birth": 0.8,
"predator_death": 0.1,
"grid_size": 10,
"seed": 42,
"prey_mean": 25.5,
"prey_std": 3.2,
"pred_mean": 12.1,
"pred_std": 2.5,
"prey_survived": True,
"pred_survived": True,
"prey_cluster_sizes": [10, 5, 3],
"pred_cluster_sizes": [8, 4],
},
{
"prey_birth": 0.2,
"prey_death": 0.10,
"predator_birth": 0.8,
"predator_death": 0.1,
"grid_size": 10,
"seed": 43,
"prey_mean": 20.0,
"prey_std": 4.0,
"pred_mean": 15.0,
"pred_std": 3.0,
"prey_survived": True,
"pred_survived": True,
"prey_cluster_sizes": [12, 8],
"pred_cluster_sizes": [10, 5],
},
]