-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_chunking.py
More file actions
248 lines (221 loc) · 8.47 KB
/
test_chunking.py
File metadata and controls
248 lines (221 loc) · 8.47 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
"""Tests for openpois.conflation.chunking."""
from __future__ import annotations
import numpy as np
import pytest
from shapely.geometry import Point
from openpois.conflation.chunking import (
ChunkSpec,
assign_primary_chunk,
bbox_mask,
compute_chunks,
extract_centroids_lonlat,
)
def _random_lonlat(n: int, seed: int) -> np.ndarray:
"""Generate a uniformly-distributed (n, 2) lonlat array over CONUS."""
rng = np.random.default_rng(seed)
lon = rng.uniform(-124.0, -67.0, size = n)
lat = rng.uniform(25.0, 49.0, size = n)
return np.column_stack([lon, lat])
class TestComputeChunks:
def test_single_chunk_when_small(self):
"""N <= target produces exactly one chunk."""
centroids = _random_lonlat(50, seed = 0)
chunks = compute_chunks(
osm_centroids_lonlat = centroids,
overture_centroids_lonlat = np.zeros((0, 2)),
chunk_target_pois = 100,
buffer_m = 200.0,
)
assert len(chunks) == 1
assert chunks[0].chunk_id == 0
def test_target_count_respected(self):
"""No chunk's pooled count exceeds 2× target (recursion halts
when a split leaves one side empty, which is the only way a
chunk can exceed target; allow slack)."""
osm = _random_lonlat(5_000, seed = 1)
ov = _random_lonlat(5_000, seed = 2)
target = 1_000
chunks = compute_chunks(
osm_centroids_lonlat = osm,
overture_centroids_lonlat = ov,
chunk_target_pois = target,
buffer_m = 200.0,
)
pooled = np.vstack([osm, ov])
primary = assign_primary_chunk(pooled, chunks)
counts = np.bincount(primary, minlength = len(chunks))
assert counts.max() <= 2 * target
# Roughly 10 chunks expected for 10,000 points and target 1,000
assert 6 <= len(chunks) <= 20
def test_tile_coverage_no_gaps_no_overlaps(self):
"""Every centroid lands in exactly one chunk."""
osm = _random_lonlat(3_000, seed = 3)
ov = _random_lonlat(2_000, seed = 4)
chunks = compute_chunks(
osm_centroids_lonlat = osm,
overture_centroids_lonlat = ov,
chunk_target_pois = 500,
buffer_m = 200.0,
)
pooled = np.vstack([osm, ov])
primary = assign_primary_chunk(pooled, chunks)
assert (primary >= 0).all()
assert primary.max() < len(chunks)
# Sum of per-chunk counts equals N
counts = np.bincount(primary, minlength = len(chunks))
assert counts.sum() == len(pooled)
def test_determinism(self):
"""Same inputs produce identical chunk layouts."""
osm = _random_lonlat(2_000, seed = 5)
ov = _random_lonlat(1_500, seed = 6)
c1 = compute_chunks(osm, ov, 500, 200.0)
c2 = compute_chunks(osm, ov, 500, 200.0)
assert len(c1) == len(c2)
for a, b in zip(c1, c2):
assert a == b
def test_duplicate_centroids(self):
"""All-identical centroids collapse to a single chunk (recursion
halts immediately when median split produces an empty side)."""
n = 500
centroids = np.full((n, 2), [-122.0, 47.0])
chunks = compute_chunks(
osm_centroids_lonlat = centroids,
overture_centroids_lonlat = np.zeros((0, 2)),
chunk_target_pois = 100,
buffer_m = 200.0,
)
# Single chunk even though count > target (degenerate split)
assert len(chunks) == 1
def test_empty_raises(self):
with pytest.raises(ValueError):
compute_chunks(
osm_centroids_lonlat = np.zeros((0, 2)),
overture_centroids_lonlat = np.zeros((0, 2)),
chunk_target_pois = 100,
buffer_m = 200.0,
)
def test_buffered_bbox_larger_than_core(self):
centroids = _random_lonlat(200, seed = 7)
chunks = compute_chunks(
osm_centroids_lonlat = centroids,
overture_centroids_lonlat = np.zeros((0, 2)),
chunk_target_pois = 50,
buffer_m = 500.0,
)
for c in chunks:
assert c.buffered_bbox[0] < c.core_bbox[0]
assert c.buffered_bbox[1] < c.core_bbox[1]
assert c.buffered_bbox[2] > c.core_bbox[2]
assert c.buffered_bbox[3] > c.core_bbox[3]
def test_buffer_scales_with_metres(self):
"""Buffer widths in degrees should roughly equal
``buffer_m / (~111_139 m/deg)`` at the chunk's latitude."""
centroids = _random_lonlat(200, seed = 8)
chunks_100 = compute_chunks(
centroids, np.zeros((0, 2)), 50, 100.0,
)
chunks_500 = compute_chunks(
centroids, np.zeros((0, 2)), 50, 500.0,
)
# Core bboxes identical; buffered bboxes scale linearly
assert len(chunks_100) == len(chunks_500)
for c_small, c_big in zip(chunks_100, chunks_500):
assert c_small.core_bbox == c_big.core_bbox
small_width = c_small.buffered_bbox[2] - c_small.core_bbox[2]
big_width = c_big.buffered_bbox[2] - c_big.core_bbox[2]
assert big_width == pytest.approx(
small_width * 5.0, rel = 0.01,
)
class TestAssignPrimaryChunk:
def test_boundary_point_assigned_once(self):
"""A point on a chunk boundary lands in exactly one chunk."""
osm = _random_lonlat(1_000, seed = 9)
chunks = compute_chunks(
osm, np.zeros((0, 2)), 200, 200.0,
)
primary = assign_primary_chunk(osm, chunks)
assert (primary >= 0).all()
assert len(np.unique(primary)) <= len(chunks)
def test_global_max_point_claimed(self):
"""Point at global xmax / ymax is claimed by a rightmost /
topmost chunk."""
centroids = np.array(
[
[-122.0, 47.0],
[-121.0, 48.0], # global max corner
[-121.5, 47.5],
]
)
chunks = compute_chunks(
centroids, np.zeros((0, 2)), 1, 200.0,
)
primary = assign_primary_chunk(centroids, chunks)
assert (primary >= 0).all()
# All three points must be claimed
assert (primary != -1).sum() == 3
def test_gap_raises(self):
"""Extraneous points outside the chunks' global bbox raise."""
centroids = _random_lonlat(200, seed = 10)
chunks = compute_chunks(
centroids, np.zeros((0, 2)), 50, 200.0,
)
# Add a point outside the global bbox
stray = np.array([[180.0, 90.0]])
with pytest.raises(
ValueError, match = "did not fall into any",
):
assign_primary_chunk(
np.vstack([centroids, stray]), chunks,
)
def test_empty_input(self):
chunks = compute_chunks(
_random_lonlat(100, seed = 11),
np.zeros((0, 2)), 50, 200.0,
)
result = assign_primary_chunk(np.zeros((0, 2)), chunks)
assert len(result) == 0
class TestBboxMask:
def test_in_and_out(self):
centroids = np.array(
[
[-122.5, 47.5], # in
[-120.0, 48.0], # out
[-122.0, 47.6], # in
]
)
bbox = (-123.0, 47.0, -121.0, 48.0)
mask = bbox_mask(centroids, bbox)
assert mask.tolist() == [True, False, True]
def test_inclusive_edges(self):
centroids = np.array([[-122.0, 47.0]])
# Point on the lower-left corner
mask = bbox_mask(
centroids, (-122.0, 47.0, -121.0, 48.0)
)
assert mask[0]
def test_empty(self):
mask = bbox_mask(
np.zeros((0, 2)), (-1.0, -1.0, 1.0, 1.0),
)
assert len(mask) == 0
class TestExtractCentroids:
def test_points(self):
geoms = np.array(
[Point(-122.5, 47.5), Point(-121.0, 48.0)]
)
result = extract_centroids_lonlat(geoms)
assert result.shape == (2, 2)
assert result[0, 0] == pytest.approx(-122.5)
assert result[0, 1] == pytest.approx(47.5)
def test_empty(self):
result = extract_centroids_lonlat(np.array([]))
assert result.shape == (0, 2)
class TestChunkSpec:
def test_frozen(self):
spec = ChunkSpec(
chunk_id = 0,
core_bbox = (0.0, 0.0, 1.0, 1.0),
buffered_bbox = (-0.1, -0.1, 1.1, 1.1),
)
with pytest.raises(Exception):
spec.chunk_id = 1 # frozen dataclass