|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Apply the change-detection penalty to a baseline conflated dataset. |
| 4 | +
|
| 5 | +Reads: |
| 6 | + - the baseline ``conflated.parquet`` (no change detection) |
| 7 | + - ``ghosts.parquet`` (from ``scripts/conflation/build_ghosts.py``) |
| 8 | + - ``fitted_params.csv`` for the active ``model_output`` version |
| 9 | +
|
| 10 | +Writes a new conflated parquet (suffix ``_cd`` by default) whose |
| 11 | +unmatched-Overture rows have had ``conf_mean`` re-weighted by |
| 12 | +``δ_group`` for any spatial+name+taxonomy match against a ghost. Audit |
| 13 | +columns are appended (``shadow_*`` + ``original_conf_mean``) so the |
| 14 | +demoted rows can be inspected by hand. |
| 15 | +
|
| 16 | +Usage: |
| 17 | + python scripts/conflation/apply_change_detection.py \ |
| 18 | + --baseline-suffix=baseline --output-suffix=cd [--test] |
| 19 | +
|
| 20 | +Both ``--baseline-suffix`` and ``--output-suffix`` are inserted into |
| 21 | +the conflated filename before ``.parquet`` (e.g. ``conflated_cd.parquet``). |
| 22 | +""" |
| 23 | +from __future__ import annotations |
| 24 | + |
| 25 | +import argparse |
| 26 | +import time |
| 27 | +from pathlib import Path |
| 28 | + |
| 29 | +from config_versioned import Config |
| 30 | + |
| 31 | +from openpois.conflation.change_detection import apply_shadow_match |
| 32 | + |
| 33 | + |
| 34 | +def _suffixed_path(base_path: Path, suffix: str | None) -> Path: |
| 35 | + """Insert ``suffix`` before the parquet extension.""" |
| 36 | + if not suffix: |
| 37 | + return base_path |
| 38 | + return base_path.with_name( |
| 39 | + f"{base_path.stem}_{suffix}{base_path.suffix}" |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +def main() -> None: |
| 44 | + parser = argparse.ArgumentParser( |
| 45 | + description = ( |
| 46 | + "Apply change-detection penalty to a baseline conflated " |
| 47 | + "dataset using OSM-history-derived ghost POIs." |
| 48 | + ) |
| 49 | + ) |
| 50 | + parser.add_argument( |
| 51 | + "--baseline-suffix", |
| 52 | + default = "baseline", |
| 53 | + help = ( |
| 54 | + "Suffix inserted into the input parquet filename " |
| 55 | + "(default: 'baseline' → conflated_baseline.parquet). " |
| 56 | + "Pass an empty string to read conflated.parquet directly." |
| 57 | + ), |
| 58 | + ) |
| 59 | + parser.add_argument( |
| 60 | + "--output-suffix", |
| 61 | + default = "cd", |
| 62 | + help = ( |
| 63 | + "Suffix inserted into the output parquet filename " |
| 64 | + "(default: 'cd' → conflated_cd.parquet)." |
| 65 | + ), |
| 66 | + ) |
| 67 | + parser.add_argument( |
| 68 | + "--test", |
| 69 | + action = "store_true", |
| 70 | + help = ( |
| 71 | + "Restrict ghosts to the configured conflation.test_bbox. " |
| 72 | + "Use when the baseline was produced with --test." |
| 73 | + ), |
| 74 | + ) |
| 75 | + parser.add_argument( |
| 76 | + "--min-prior-name-score", |
| 77 | + type = float, |
| 78 | + default = None, |
| 79 | + help = ( |
| 80 | + "Override config's min_prior_name_match_score. Higher " |
| 81 | + "values require a stricter Overture-name vs ghost-prior-" |
| 82 | + "name token_set_ratio match before a penalty fires. " |
| 83 | + "Default config value implements decision rule A " |
| 84 | + "(name-match required)." |
| 85 | + ), |
| 86 | + ) |
| 87 | + parser.add_argument( |
| 88 | + "--no-survivor-filter", |
| 89 | + action = "store_true", |
| 90 | + help = ( |
| 91 | + "Disable the current-OSM-survivor post-filter for this " |
| 92 | + "run. Used for ablation against the vetted set." |
| 93 | + ), |
| 94 | + ) |
| 95 | + args = parser.parse_args() |
| 96 | + |
| 97 | + config = Config("~/repos/openpois/config.yaml") |
| 98 | + |
| 99 | + conflated_base = config.get_file_path("conflation", "conflated") |
| 100 | + baseline_path = _suffixed_path( |
| 101 | + conflated_base, args.baseline_suffix, |
| 102 | + ) |
| 103 | + output_path = _suffixed_path( |
| 104 | + conflated_base, args.output_suffix, |
| 105 | + ) |
| 106 | + ghosts_path = config.get_file_path("ghost_osm", "ghosts") |
| 107 | + |
| 108 | + model_dir = Path(config.get_dir_path("model_output")) |
| 109 | + fitted_params_path = model_dir / config.get( |
| 110 | + "directories", "model_output", "files", "fitted_params", |
| 111 | + ) |
| 112 | + |
| 113 | + cd_cfg = config.get("conflation", "change_detection") |
| 114 | + min_match_score = float(cd_cfg["min_shadow_match_score"]) |
| 115 | + default_delta = float(cd_cfg["default_delta"]) |
| 116 | + min_prior_name_match_score = float( |
| 117 | + cd_cfg.get("min_prior_name_match_score", 0) |
| 118 | + ) |
| 119 | + if args.min_prior_name_score is not None: |
| 120 | + min_prior_name_match_score = float(args.min_prior_name_score) |
| 121 | + |
| 122 | + survivor_filter = cd_cfg.get("suppress_if_current_survivor") or {} |
| 123 | + if args.no_survivor_filter: |
| 124 | + survivor_filter = dict(survivor_filter) |
| 125 | + survivor_filter["enabled"] = False |
| 126 | + print("Current-OSM-survivor filter disabled for this run.") |
| 127 | + |
| 128 | + max_radius_m = float(config.get("conflation", "max_radius_m")) |
| 129 | + default_radius_m = float( |
| 130 | + config.get("conflation", "default_radius_m") |
| 131 | + ) |
| 132 | + distance_weight = float(config.get("conflation", "distance_weight")) |
| 133 | + name_weight = float(config.get("conflation", "name_weight")) |
| 134 | + type_weight = float(config.get("conflation", "type_weight")) |
| 135 | + identifier_weight = float( |
| 136 | + config.get("conflation", "identifier_weight") |
| 137 | + ) |
| 138 | + |
| 139 | + # R1 needs the rated snapshot; no other auxiliary inputs are |
| 140 | + # needed by the simplified pipeline. |
| 141 | + rated_snapshot_path = config.get_file_path( |
| 142 | + "snapshot_osm", "rated_snapshot", |
| 143 | + ) |
| 144 | + |
| 145 | + test_bbox = ( |
| 146 | + config.get("conflation", "test_bbox") if args.test else None |
| 147 | + ) |
| 148 | + |
| 149 | + print(f"Baseline: {baseline_path}") |
| 150 | + print(f"Ghosts: {ghosts_path}") |
| 151 | + print(f"Fitted params: {fitted_params_path}") |
| 152 | + print(f"Output: {output_path}") |
| 153 | + print(f"Rated snapshot (survivor filter): {rated_snapshot_path}") |
| 154 | + print( |
| 155 | + f"min_match_score={min_match_score} " |
| 156 | + f"max_radius_m={max_radius_m} " |
| 157 | + f"default_delta={default_delta} " |
| 158 | + f"min_prior_name_match_score={min_prior_name_match_score}" |
| 159 | + ) |
| 160 | + if args.test: |
| 161 | + print(f"Test bbox: {test_bbox}") |
| 162 | + |
| 163 | + t0 = time.time() |
| 164 | + summary = apply_shadow_match( |
| 165 | + conflated_path = baseline_path, |
| 166 | + ghosts_path = ghosts_path, |
| 167 | + fitted_params_path = fitted_params_path, |
| 168 | + output_path = output_path, |
| 169 | + min_match_score = min_match_score, |
| 170 | + max_radius_m = max_radius_m, |
| 171 | + default_radius_m = default_radius_m, |
| 172 | + distance_weight = distance_weight, |
| 173 | + name_weight = name_weight, |
| 174 | + type_weight = type_weight, |
| 175 | + identifier_weight = identifier_weight, |
| 176 | + default_delta = default_delta, |
| 177 | + test_bbox = test_bbox, |
| 178 | + rated_snapshot_path = rated_snapshot_path, |
| 179 | + survivor_filter = survivor_filter, |
| 180 | + min_prior_name_match_score = min_prior_name_match_score, |
| 181 | + ) |
| 182 | + elapsed = time.time() - t0 |
| 183 | + |
| 184 | + print(f"\nApplied change-detection in {elapsed:.0f}s") |
| 185 | + print(f" Total conflated rows: {summary['n_total']:,}") |
| 186 | + print( |
| 187 | + f" Unmatched Overture rows: " |
| 188 | + f"{summary['n_unmatched_overture']:,}" |
| 189 | + ) |
| 190 | + print(f" Ghosts considered: {summary['n_ghosts']:,}") |
| 191 | + print( |
| 192 | + f" Shadow matches (final): " |
| 193 | + f"{summary['n_shadow_matches']:,}" |
| 194 | + ) |
| 195 | + print( |
| 196 | + f" Dropped by survivor filter: " |
| 197 | + f"{summary['n_survivor_dropped']}" |
| 198 | + ) |
| 199 | + print( |
| 200 | + f" Mean penalty factor (Δ/old): " |
| 201 | + f"{summary['mean_penalty_factor']:.4f}" |
| 202 | + ) |
| 203 | + print(f" Output: {output_path}") |
| 204 | + |
| 205 | + |
| 206 | +if __name__ == "__main__": |
| 207 | + main() |
0 commit comments