Repository navigation
Expand file tree
/
Copy pathtest_change_detection.py
More file actions
382 lines (314 loc) · 13.3 KB
/
Copy pathtest_change_detection.py
File metadata and controls
382 lines (314 loc) · 13.3 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
"""Tests for openpois.conflation.change_detection (same-entity rule).
Synthetic pairs only: a ghost and an Overture row 3 m apart, both ``Cafe``,
scored with the production weights (0.20 distance / 0.40 name / 0.40 type,
``min_shadow_match_score`` 0.70, ``min_prior_name_match_score`` 70). The
rule under test (2026-09-24): demote an Overture-only POI only when OSM
closed the *same* business, with the six guards from
``.claude/plans/location-report-propagation.md``.
"""
from __future__ import annotations
from math import cos, radians
import geopandas as gpd
import numpy as np
import pandas as pd
import pytest
from shapely.geometry import Point, box
from openpois.conflation.change_detection import (
apply_current_survivor_filter,
apply_shadow_match,
filter_ghosts_by_age,
find_shadow_matches,
)
LON, LAT = -122.335, 47.608
M_PER_DEG_LAT = 111_320.0
NOW = pd.Timestamp("2026-09-24", tz = "UTC")
PRODUCTION = dict(
min_match_score = 0.70,
max_radius_m = 200.0,
default_radius_m = 100.0,
distance_weight = 0.20,
name_weight = 0.40,
type_weight = 0.40,
identifier_weight = 0.0,
min_prior_name_match_score = 70.0,
)
def _pt(dx_m: float = 0.0, dy_m: float = 0.0) -> Point:
"""A point offset from (LON, LAT) by metres east / north."""
return Point(
LON + dx_m / (M_PER_DEG_LAT * cos(radians(LAT))),
LAT + dy_m / M_PER_DEG_LAT,
)
def _ghosts(rows: list[dict]) -> gpd.GeoDataFrame:
defaults = {
"event_type": "hard_delete",
"event_timestamp": NOW - pd.Timedelta(days = 365),
"prior_name": None,
"prior_brand": None,
"new_name": None,
"shared_label": "Cafe",
"geometry": _pt(),
}
full = []
for i, row in enumerate(rows):
r = {**defaults, **row}
r.setdefault("ghost_id", f"node/{i + 1}/v2:{r['event_type']}")
full.append(r)
df = pd.DataFrame(full)
df["event_timestamp"] = pd.to_datetime(df["event_timestamp"], utc = True)
return gpd.GeoDataFrame(df, geometry = "geometry", crs = "EPSG:4326")
def _overture(rows: list[dict]) -> gpd.GeoDataFrame:
defaults = {
"name": None,
"brand": None,
"shared_label": "Cafe",
"geometry": _pt(3.0, 0.0),
}
df = pd.DataFrame([{**defaults, **row} for row in rows])
return gpd.GeoDataFrame(df, geometry = "geometry", crs = "EPSG:4326")
def _match(ov: gpd.GeoDataFrame, gh: gpd.GeoDataFrame, **overrides):
return find_shadow_matches(ov, gh, **{**PRODUCTION, **overrides})
# --- Same-entity name gate --------------------------------------------------
@pytest.mark.parametrize(
"ghost_name, ghost_brand, ov_name, ov_brand, expect",
[
("Joe's Cafe", None, "Joe's Cafe", None, True), # same name
("Walgreens", None, "Walgreens Pharmacy", None, True), # subset
("Starbucks", None, "Pike Place Roastery", "Starbucks", True), # brand
("Joe's Coffee House", None, "Joe's Cafe", None, False), # different
(None, None, "Joe's Cafe", None, False), # unnamed ghost
("Random Books", None, "Joe's Cafe", None, False), # unrelated
("La Madelaine Bakery", None, "la Madeleine", None, True), # accents
],
ids = [
"same-name", "walgreens-subset", "starbucks-brand-only",
"joes-coffee-house-vs-cafe", "unnamed-ghost", "unrelated",
"la-madeleine-normalised",
],
)
def test_name_gate_decides_the_match(
ghost_name, ghost_brand, ov_name, ov_brand, expect,
):
gh = _ghosts([{"prior_name": ghost_name, "prior_brand": ghost_brand}])
ov = _overture([{"name": ov_name, "brand": ov_brand}])
matches = _match(ov, gh)
assert (len(matches) == 1) is expect
if expect:
assert matches.iloc[0]["composite_score"] >= 0.70
assert matches.iloc[0]["distance_m"] < 5
def test_identical_names_are_no_longer_dropped_as_same_entity():
"""The pre-2026-09-24 second-stage subset/superset drop is gone: an
identical name is now the strongest reason to demote, not a reason to
skip."""
gh = _ghosts([{"prior_name": "Blue Harbor Bank"}])
ov = _overture([{"name": "Blue Harbor Bank"}])
assert len(_match(ov, gh)) == 1
def test_unnamed_overture_row_never_matches_even_on_brand():
gh = _ghosts([{"prior_name": "Starbucks", "prior_brand": "Starbucks"}])
ov = _overture([{"name": None, "brand": "Starbucks"}])
assert _match(ov, gh).empty
def test_unnamed_ghost_with_brand_never_matches():
gh = _ghosts([{"prior_name": None, "prior_brand": "Starbucks"}])
ov = _overture([{"name": "Starbucks"}])
assert _match(ov, gh).empty
def test_shared_generic_token_alone_is_not_a_match():
""""Plaza Pharmacy" / "CVS Pharmacy" score 80 on the raw token-set ratio
purely through "pharmacy"; the category-token rule strips it."""
gh = _ghosts([{"prior_name": "Plaza Pharmacy", "shared_label": "Pharmacy"}])
ov = _overture([{"name": "CVS Pharmacy", "shared_label": "Pharmacy"}])
assert _match(ov, gh).empty
def test_type_mismatch_still_blocks_a_same_name_pair():
gh = _ghosts([{"prior_name": "Joe's Cafe", "shared_label": "Bank"}])
ov = _overture([{"name": "Joe's Cafe"}])
assert _match(ov, gh).empty
# --- Rename direction -------------------------------------------------------
def _rename_ghost():
return _ghosts([{
"event_type": "substantial_rename",
"prior_name": "Plaza Pharmacy",
"new_name": "CVS Pharmacy",
"shared_label": "Pharmacy",
}])
def test_rename_already_carried_by_overture_is_skipped():
ov = _overture([{"name": "CVS Pharmacy", "shared_label": "Pharmacy"}])
assert _match(ov, _rename_ghost()).empty
def test_rename_demotes_when_overture_still_shows_the_prior_name():
ov = _overture([{"name": "Plaza Pharmacy", "shared_label": "Pharmacy"}])
assert len(_match(ov, _rename_ghost())) == 1
def test_rename_guard_only_applies_to_substantial_rename_ghosts():
"""A lifecycle ghost keeps its name, so new_name == prior_name; the
guard must not turn every named lifecycle ghost into a skip."""
gh = _ghosts([{
"event_type": "lifecycle_prefix_added",
"prior_name": "Joe's Cafe",
"new_name": "Joe's Cafe",
}])
ov = _overture([{"name": "Joe's Cafe"}])
assert len(_match(ov, gh)) == 1
def test_ghosts_without_new_name_column_still_match():
gh = _ghosts([{"prior_name": "Joe's Cafe"}]).drop(columns = ["new_name"])
ov = _overture([{"name": "Joe's Cafe"}])
assert len(_match(ov, gh)) == 1
# --- Current-OSM-survivor filter -------------------------------------------
def _snapshot(tmp_path, name = "Blue Harbor Bank", offset_m = 120.0):
"""A full-snapshot GeoParquet with one named building way whose
centroid is ``offset_m`` north of the Overture point, plus an unnamed
node."""
c = _pt(0.0, offset_m)
dlat = 0.0001
dlon = 0.0002
way = box(c.x - dlon, c.y - dlat, c.x + dlon, c.y + dlat)
gdf = gpd.GeoDataFrame(
{
"source": ["osm", "osm"],
"osm_id": [10, 11],
"osm_type": ["way", "node"],
"name": [name, None],
},
geometry = [way, _pt(500.0, 0.0)],
crs = "EPSG:4326",
)
path = tmp_path / "osm_snapshot.parquet"
gdf.to_parquet(path)
return path
def _one_match():
return pd.DataFrame({
"osm_idx": [0], "overture_idx": [0],
"composite_score": [0.95], "distance_m": [3.0],
})
def test_survivor_way_centroid_120m_away_suppresses_at_150m(tmp_path):
ov = _overture([{"name": "Blue Harbor Bank", "shared_label": "Bank"}])
kept, n_dropped = apply_current_survivor_filter(
_one_match(), ov,
snapshot_path = _snapshot(tmp_path),
radius_m = 150.0, name_similarity_threshold = 70.0,
verbose = False,
)
assert n_dropped == 1
assert kept.empty
def test_survivor_way_centroid_120m_away_is_missed_at_50m(tmp_path):
"""The pre-2026-09-24 radius: documents why it was widened."""
ov = _overture([{"name": "Blue Harbor Bank", "shared_label": "Bank"}])
kept, n_dropped = apply_current_survivor_filter(
_one_match(), ov,
snapshot_path = _snapshot(tmp_path),
radius_m = 50.0, name_similarity_threshold = 70.0,
verbose = False,
)
assert n_dropped == 0
assert len(kept) == 1
def test_survivor_names_are_normalised_before_comparison(tmp_path):
ov = _overture([{"name": "Blue Harbor Bank, Inc.", "shared_label": "Bank"}])
kept, n_dropped = apply_current_survivor_filter(
_one_match(), ov,
snapshot_path = _snapshot(tmp_path, name = "Blue Harbór Bank LLC"),
radius_m = 150.0, name_similarity_threshold = 70.0,
verbose = False,
)
assert n_dropped == 1
def test_survivor_with_a_different_name_does_not_suppress(tmp_path):
ov = _overture([{"name": "Blue Harbor Bank", "shared_label": "Bank"}])
kept, n_dropped = apply_current_survivor_filter(
_one_match(), ov,
snapshot_path = _snapshot(tmp_path, name = "Mendocino Masonic Hall"),
radius_m = 150.0, name_similarity_threshold = 70.0,
verbose = False,
)
assert n_dropped == 0
# --- Ghost age --------------------------------------------------------------
def test_filter_ghosts_by_age_drops_a_four_year_old_ghost():
gh = _ghosts([
{"prior_name": "Old", "event_timestamp": NOW - pd.Timedelta(days = 4 * 365)},
{"prior_name": "Recent", "event_timestamp": NOW - pd.Timedelta(days = 365)},
{"prior_name": "Undated", "event_timestamp": pd.NaT},
])
kept = filter_ghosts_by_age(gh, 3, now = NOW)
assert list(kept["prior_name"]) == ["Recent", "Undated"]
# Disabled filter keeps everything.
assert len(filter_ghosts_by_age(gh, None, now = NOW)) == 3
assert len(filter_ghosts_by_age(gh, 0, now = NOW)) == 3
# --- End to end through apply_shadow_match ---------------------------------
def _baseline(tmp_path, rows: list[dict]):
defaults = {
"unified_id": None, "source": "overture", "shared_label": "Cafe",
"name": None, "brand": None,
"conf_mean": 0.8, "conf_lower": 0.6, "conf_upper": 0.9,
"geometry": _pt(3.0, 0.0),
}
full = []
for i, row in enumerate(rows):
r = {**defaults, **row}
r["unified_id"] = r["unified_id"] or f"ov{i}"
full.append(r)
gdf = gpd.GeoDataFrame(pd.DataFrame(full), geometry = "geometry",
crs = "EPSG:4326")
path = tmp_path / "conflated_baseline.parquet"
gdf.to_parquet(path)
return path
def _run(tmp_path, ghosts, baseline_rows, **overrides):
ghosts_path = tmp_path / "ghosts.parquet"
ghosts.to_parquet(ghosts_path)
params = tmp_path / "fitted_params.csv"
params.write_text("param_name,mean\nlambda_0,0.1\n")
out = tmp_path / "conflated_cd.parquet"
kwargs = {
**{k: v for k, v in PRODUCTION.items()},
"default_delta": 0.141,
"survivor_filter": {"enabled": False},
"verbose": False,
}
kwargs.update(overrides)
summary = apply_shadow_match(
_baseline(tmp_path, baseline_rows), ghosts_path, params, out, **kwargs,
)
return summary, gpd.read_parquet(out)
def test_apply_shadow_match_demotes_a_recent_same_name_ghost(tmp_path):
gh = _ghosts([{"prior_name": "Joe's Cafe"}])
summary, out = _run(
tmp_path, gh, [{"name": "Joe's Cafe"}], max_ghost_age_years = 3,
)
assert summary["n_shadow_matches"] == 1
assert bool(out["shadow_matched"].iloc[0])
assert out["conf_mean"].iloc[0] == pytest.approx(0.8 * 0.141)
assert out["shadow_event_type"].iloc[0] == "hard_delete"
def test_apply_shadow_match_drops_a_four_year_old_ghost(tmp_path):
gh = _ghosts([{
"prior_name": "Joe's Cafe",
"event_timestamp": pd.Timestamp.now(tz = "UTC") - pd.Timedelta(days = 4 * 365),
}])
summary, out = _run(
tmp_path, gh, [{"name": "Joe's Cafe"}], max_ghost_age_years = 3,
)
assert summary["n_ghosts"] == 0
assert summary["n_shadow_matches"] == 0
assert not out["shadow_matched"].iloc[0]
assert out["conf_mean"].iloc[0] == pytest.approx(0.8)
def test_apply_shadow_match_uses_full_snapshot_for_survivors(tmp_path):
gh = _ghosts([{"prior_name": "Blue Harbor Bank", "shared_label": "Bank"}])
summary, out = _run(
tmp_path, gh,
[{"name": "Blue Harbor Bank", "shared_label": "Bank"}],
full_snapshot_path = _snapshot(tmp_path),
rated_snapshot_path = None,
survivor_filter = {
"enabled": True, "radius_m": 150, "name_similarity_threshold": 70,
"use_full_snapshot": True,
},
)
assert summary["n_survivor_dropped"] == 1
assert summary["n_shadow_matches"] == 0
assert out["conf_mean"].iloc[0] == pytest.approx(0.8)
def test_apply_shadow_match_falls_back_to_rated_snapshot(tmp_path):
"""use_full_snapshot false → the rated snapshot is the candidate set."""
gh = _ghosts([{"prior_name": "Blue Harbor Bank", "shared_label": "Bank"}])
rated = _snapshot(tmp_path)
summary, _ = _run(
tmp_path, gh,
[{"name": "Blue Harbor Bank", "shared_label": "Bank"}],
full_snapshot_path = None,
rated_snapshot_path = rated,
survivor_filter = {
"enabled": True, "radius_m": 150, "name_similarity_threshold": 70,
"use_full_snapshot": False,
},
)
assert summary["n_survivor_dropped"] == 1