-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_format_observations.py
More file actions
469 lines (421 loc) · 18.1 KB
/
test_format_observations.py
File metadata and controls
469 lines (421 loc) · 18.1 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# -------------------------------------------------------------
# Copyright (c) Henry Spatial Analysis. All rights reserved.
# Licensed under the MIT License. See LICENSE in project root for information.
# -------------------------------------------------------------
"""
Unit tests for openpois.osm.format_observations.
"""
from __future__ import annotations
import pandas as pd
import pyarrow as pa
import pyarrow.parquet as pq
import pytest
from openpois.osm.format_observations import (
format_observations_duckdb,
format_observations_window,
)
VERSIONS_SCHEMA = pa.schema([
("id", pa.int64()),
("version", pa.int64()),
("changeset", pa.int64()),
("timestamp", pa.string()),
("user", pa.string()),
("uid", pa.int64()),
("type", pa.string()),
])
CHANGES_SCHEMA = pa.schema([
("key", pa.string()),
("value", pa.string()),
("change", pa.string()),
("id", pa.int64()),
("version", pa.int64()),
("type", pa.string()),
])
def _make_versions(rows):
return pa.table(
{f.name: [r.get(f.name) for r in rows] for f in VERSIONS_SCHEMA},
schema=VERSIONS_SCHEMA,
)
def _make_changes(rows):
return pa.table(
{f.name: [r.get(f.name) for r in rows] for f in CHANGES_SCHEMA},
schema=CHANGES_SCHEMA,
)
def _synthetic_inputs():
"""
Three POIs with name tags. Each has an Added, then a Changed on the
tag_key, so each should yield 2 observations (the second with changed=1).
"""
versions = []
changes = []
for elem_id in [100, 200, 300]:
for ver in [1, 2]:
versions.append({
"id": elem_id, "version": ver, "changeset": 1000 + ver,
"timestamp": f"2024-01-{ver:02d}T00:00:00+00:00",
"user": "u", "uid": 1, "type": "node",
})
changes.append({
"key": "name", "value": f"n{elem_id}.v{ver}",
"change": "Added" if ver == 1 else "Changed",
"id": elem_id, "version": ver, "type": "node",
})
changes.append({
"key": "amenity", "value": "cafe",
"change": "Added" if ver == 1 else "Changed",
"id": elem_id, "version": ver, "type": "node",
})
return versions, changes
def _write_parquets(tmp_path, versions, changes):
v_path = tmp_path / "versions.parquet"
c_path = tmp_path / "changes.parquet"
pq.write_table(_make_versions(versions), v_path)
pq.write_table(_make_changes(changes), c_path)
return v_path, c_path
class TestFormatObservationsDuckdb:
"""``format_observations_duckdb`` is the production entry point for the
OSM history → observations pipeline; these tests pin its row count,
column set, and state-machine semantics."""
def test_synthetic_inputs_produce_expected_rows(self, tmp_path):
versions, changes = _synthetic_inputs()
v_path, c_path = _write_parquets(tmp_path, versions, changes)
out_path = tmp_path / "obs.parquet"
total = format_observations_duckdb(
changes_path = c_path,
versions_path = v_path,
output_path = out_path,
tag_key = "name",
keep_keys = ["amenity"],
verbose = False,
)
# Two observations per POI (Added + Changed), three POIs
assert total == 6
out = pd.read_parquet(out_path)
assert len(out) == 6
assert set(out["id"]) == {100, 200, 300}
assert set(out["version"]) == {1, 2}
assert out["changed"].sum() == 6
# Expected output columns
expected = {
"id", "osm_type", "version", "changeset", "obs_timestamp",
"last_obs_timestamp", "last_tag_timestamp", "user", "last_tag_user",
"tag_value", "last_tag_value", "changed", "deleted",
"amenity", "amenity_last_value", "tag_key",
}
assert set(out.columns) == expected
assert set(out["osm_type"]) == {"node"}
# last_tag_value reflects the PRE-update state: None on v1, v1's value on v2.
out = out.sort_values(["id", "version"]).reset_index(drop = True)
for poi_id in [100, 200, 300]:
rows = out[out["id"] == poi_id].reset_index(drop = True)
assert pd.isna(rows.loc[0, "last_tag_value"])
assert rows.loc[1, "last_tag_value"] == f"n{poi_id}.v1"
assert rows.loc[0, "tag_value"] == f"n{poi_id}.v1"
assert rows.loc[1, "tag_value"] == f"n{poi_id}.v2"
def test_tag_state_machine(self, tmp_path):
"""Added → Changed → visible=false → visible=true sequence.
The re-added version should restore ``tag_value`` to the last SET
value (``"bar"``), matching the original state-machine semantics.
"""
versions = []
changes = []
seq = [
("Added", "foo", None, None),
("Changed", "bar", None, None),
(None, None, "false", "Added"),
(None, None, "true", "Changed"),
]
for ver, (tag_ch, tag_val, vis_val, vis_ch) in enumerate(seq, start = 1):
versions.append({
"id": 42, "version": ver, "changeset": 1000 + ver,
"timestamp": f"2024-01-{ver:02d}T00:00:00+00:00",
"user": "u", "uid": 1, "type": "node",
})
if tag_ch is not None:
changes.append({
"key": "name", "value": tag_val, "change": tag_ch,
"id": 42, "version": ver, "type": "node",
})
if vis_ch is not None:
changes.append({
"key": "visible", "value": vis_val, "change": vis_ch,
"id": 42, "version": ver, "type": "node",
})
v_path, c_path = _write_parquets(tmp_path, versions, changes)
out_path = tmp_path / "obs.parquet"
format_observations_duckdb(
changes_path = c_path,
versions_path = v_path,
output_path = out_path,
tag_key = "name",
keep_keys = [],
verbose = False,
)
out = pd.read_parquet(out_path).sort_values("version").reset_index(drop = True)
assert list(out["version"]) == [1, 2, 3, 4]
assert list(out["tag_value"].fillna("")) == ["foo", "bar", "", "bar"]
assert list(out["changed"]) == [1, 1, 1, 1]
def test_keep_key_stickiness(self, tmp_path):
"""``{k}_last_value`` must persist across versions that don't touch ``k``."""
versions = []
changes = []
seq = [
("Added", "foo", "Added", "restaurant"),
("Changed", "foo2", None, None),
("Changed", "foo3", "Changed", "bar"),
]
for ver, (tag_ch, tag_val, kk_ch, kk_val) in enumerate(seq, start = 1):
versions.append({
"id": 7, "version": ver, "changeset": 1000 + ver,
"timestamp": f"2024-02-{ver:02d}T00:00:00+00:00",
"user": "u", "uid": 1, "type": "node",
})
changes.append({
"key": "name", "value": tag_val, "change": tag_ch,
"id": 7, "version": ver, "type": "node",
})
if kk_ch is not None:
changes.append({
"key": "amenity", "value": kk_val, "change": kk_ch,
"id": 7, "version": ver, "type": "node",
})
v_path, c_path = _write_parquets(tmp_path, versions, changes)
out_path = tmp_path / "obs.parquet"
format_observations_duckdb(
changes_path = c_path,
versions_path = v_path,
output_path = out_path,
tag_key = "name",
keep_keys = ["amenity"],
verbose = False,
)
out = pd.read_parquet(out_path).sort_values("version").reset_index(drop = True)
amenities = list(out["amenity"].fillna(""))
lasts = list(out["amenity_last_value"].fillna(""))
assert amenities == ["restaurant", "restaurant", "bar"]
# v1: pre-change was None; v2: no change → last stays empty;
# v3: last = "restaurant".
assert lasts == ["", "", "restaurant"]
def test_pre_addition_rows_are_gated_out(self, tmp_path):
"""Versions before the tag_key is first Added must NOT be emitted.
The ``add_to_list`` gate only opens once ``tag_key`` is Added; a POI
carrying only a keep_key in early versions yields no observations until
the name appears.
"""
versions = []
changes = []
# v1, v2: only amenity present (no name yet). v3: name Added. v4: name Changed.
seq = [
(None, None, "Added", "shop"), # v1: no name
(None, None, "Changed", "shop2"), # v2: still no name
("Added", "cafe", None, None), # v3: name appears
("Changed", "cafe2", None, None), # v4: name changes
]
for ver, (tag_ch, tag_val, kk_ch, kk_val) in enumerate(seq, start = 1):
versions.append({
"id": 55, "version": ver, "changeset": 3000 + ver,
"timestamp": f"2024-04-{ver:02d}T00:00:00+00:00",
"user": "u", "uid": 1, "type": "node",
})
if tag_ch is not None:
changes.append({
"key": "name", "value": tag_val, "change": tag_ch,
"id": 55, "version": ver, "type": "node",
})
if kk_ch is not None:
changes.append({
"key": "amenity", "value": kk_val, "change": kk_ch,
"id": 55, "version": ver, "type": "node",
})
v_path, c_path = _write_parquets(tmp_path, versions, changes)
out_path = tmp_path / "obs.parquet"
total = format_observations_duckdb(
changes_path = c_path, versions_path = v_path, output_path = out_path,
tag_key = "name", keep_keys = ["amenity"], verbose = False,
)
# Only v3 (Added) and v4 (Changed) survive the gate.
assert total == 2
out = pd.read_parquet(out_path).sort_values("version").reset_index(drop = True)
assert list(out["version"]) == [3, 4]
assert list(out["tag_value"]) == ["cafe", "cafe2"]
def test_multi_cycle_delete_readd(self, tmp_path):
"""Two delete→re-add cycles restore tag_value each time."""
versions = []
changes = []
seq = [
("Added", "foo", None, None), # v1
(None, None, "false", "Added"), # v2 delete
(None, None, "true", "Changed"),# v3 re-add → restore "foo"
("Changed", "baz", None, None), # v4 rename
(None, None, "false", "Added"), # v5 delete
(None, None, "true", "Changed"),# v6 re-add → restore "baz"
]
for ver, (tag_ch, tag_val, vis_val, vis_ch) in enumerate(seq, start = 1):
versions.append({
"id": 71, "version": ver, "changeset": 4000 + ver,
"timestamp": f"2024-05-{ver:02d}T00:00:00+00:00",
"user": "u", "uid": 1, "type": "node",
})
if tag_ch is not None:
changes.append({
"key": "name", "value": tag_val, "change": tag_ch,
"id": 71, "version": ver, "type": "node",
})
if vis_ch is not None:
changes.append({
"key": "visible", "value": vis_val, "change": vis_ch,
"id": 71, "version": ver, "type": "node",
})
v_path, c_path = _write_parquets(tmp_path, versions, changes)
out_path = tmp_path / "obs.parquet"
format_observations_duckdb(
changes_path = c_path, versions_path = v_path, output_path = out_path,
tag_key = "name", keep_keys = [], verbose = False,
)
out = pd.read_parquet(out_path).sort_values("version").reset_index(drop = True)
assert list(out["version"]) == [1, 2, 3, 4, 5, 6]
assert list(out["tag_value"].fillna("")) == ["foo", "", "foo", "baz", "", "baz"]
assert list(out["changed"]) == [1, 1, 1, 1, 1, 1]
def test_left_join_null_inheritance(self, tmp_path):
"""Versions with no relevant changes (LEFT-JOIN produces NULLs) should
inherit prior state without crashing."""
versions = []
changes = []
for ver in [1, 2, 3]:
versions.append({
"id": 99, "version": ver, "changeset": 2000 + ver,
"timestamp": f"2024-03-{ver:02d}T00:00:00+00:00",
"user": "u", "uid": 1, "type": "node",
})
# Only v1 has tag/keep-key changes; v2 and v3 have no rows at all.
changes.append({
"key": "name", "value": "cafe", "change": "Added",
"id": 99, "version": 1, "type": "node",
})
changes.append({
"key": "amenity", "value": "cafe", "change": "Added",
"id": 99, "version": 1, "type": "node",
})
v_path, c_path = _write_parquets(tmp_path, versions, changes)
out_path = tmp_path / "obs.parquet"
total = format_observations_duckdb(
changes_path = c_path,
versions_path = v_path,
output_path = out_path,
tag_key = "name",
keep_keys = ["amenity"],
verbose = False,
)
assert total == 3
out = pd.read_parquet(out_path).sort_values("version").reset_index(drop = True)
assert list(out["tag_value"].fillna("")) == ["cafe", "cafe", "cafe"]
assert list(out["amenity"].fillna("")) == ["cafe", "cafe", "cafe"]
assert list(out["changed"]) == [1, 0, 0]
# Stage 2b golden test: DuckDB window functions must reproduce the per-POI
# state machine byte-for-byte on every fixture scenario. -------------------->
def _versions_changes_from_seq(elem_id, seq, keep_key = None):
"""Build (versions, changes) from a per-version sequence.
Each ``seq`` entry is ``(tag_change, tag_value, other_change, other_value)``
where ``other`` is a visibility toggle (``visible``) or a keep-key change,
depending on ``keep_key``.
"""
versions, changes = [], []
for ver, (tag_ch, tag_val, oth_ch, oth_val) in enumerate(seq, start = 1):
versions.append({
"id": elem_id, "version": ver, "changeset": 1000 + ver,
"timestamp": f"2024-06-{ver:02d}T00:00:00+00:00",
"user": f"u{ver}", "uid": ver, "type": "node",
})
if tag_ch is not None:
changes.append({
"key": "name", "value": tag_val, "change": tag_ch,
"id": elem_id, "version": ver, "type": "node",
})
if oth_ch is not None:
key = keep_key if keep_key else "visible"
changes.append({
"key": key, "value": oth_val, "change": oth_ch,
"id": elem_id, "version": ver, "type": "node",
})
return versions, changes
def _scenario_synthetic():
v, c = _synthetic_inputs()
return v, c, "name", ["amenity"]
def _scenario_delete_readd():
v, c = _versions_changes_from_seq(42, [
("Added", "foo", None, None),
("Changed", "bar", None, None),
(None, None, "Added", "false"),
(None, None, "Changed", "true"),
])
return v, c, "name", []
def _scenario_multi_cycle():
v, c = _versions_changes_from_seq(71, [
("Added", "foo", None, None),
(None, None, "Added", "false"),
(None, None, "Changed", "true"),
("Changed", "baz", None, None),
(None, None, "Added", "false"),
(None, None, "Changed", "true"),
])
return v, c, "name", []
def _scenario_keep_sticky():
v, c = _versions_changes_from_seq(7, [
("Added", "foo", "Added", "restaurant"),
("Changed", "foo2", None, None),
("Changed", "foo3", "Changed", "bar"),
], keep_key = "amenity")
return v, c, "name", ["amenity"]
def _scenario_pre_addition():
v, c = _versions_changes_from_seq(55, [
(None, None, "Added", "shop"),
(None, None, "Changed", "shop2"),
("Added", "cafe", None, None),
("Changed", "cafe2", None, None),
], keep_key = "amenity")
return v, c, "name", ["amenity"]
def _scenario_multi_poi():
"""Two POIs interleaved + a keep-key delete, to exercise partitioning."""
v1, c1 = _versions_changes_from_seq(1, [
("Added", "a", "Added", "cafe"),
("Changed", "a2", "Deleted", "cafe"),
], keep_key = "amenity")
v2, c2 = _versions_changes_from_seq(2, [
("Added", "b", None, None),
(None, None, "Added", "false"),
])
return v1 + v2, c1 + c2, "name", ["amenity"]
_SCENARIOS = {
"synthetic": _scenario_synthetic,
"delete_readd": _scenario_delete_readd,
"multi_cycle": _scenario_multi_cycle,
"keep_sticky": _scenario_keep_sticky,
"pre_addition": _scenario_pre_addition,
"multi_poi": _scenario_multi_poi,
}
@pytest.mark.parametrize("scenario", list(_SCENARIOS))
@pytest.mark.parametrize("num_buckets", [1, 3])
def test_window_matches_state_machine(tmp_path, scenario, num_buckets):
"""The Stage 2b window-function build must equal the state-machine build,
both single-pass and bucketed (bucketing must never split a POI's rows)."""
versions, changes, tag_key, keep_keys = _SCENARIOS[scenario]()
v_path, c_path = _write_parquets(tmp_path, versions, changes)
sm_path = tmp_path / "state_machine.parquet"
win_path = tmp_path / "window.parquet"
format_observations_duckdb(
c_path, v_path, sm_path, tag_key, keep_keys, verbose = False,
)
format_observations_window(
c_path, v_path, win_path, tag_key, keep_keys,
num_buckets = num_buckets, verbose = False,
)
sort_cols = ["osm_type", "id", "version"]
sm = (
pd.read_parquet(sm_path).sort_values(sort_cols).reset_index(drop = True)
)
win = (
pd.read_parquet(win_path).sort_values(sort_cols).reset_index(drop = True)
)
sm = sm[sorted(sm.columns)]
win = win[sorted(win.columns)]
pd.testing.assert_frame_equal(sm, win, check_dtype = False)