Skip to content

fix: node distance interpolation for flipped/single reaches - #215

Merged
jameshgrn merged 5 commits into
mainfrom
fix/v17c-004-node-distances
Mar 30, 2026
Merged

fix: node distance interpolation for flipped/single reaches#215
jameshgrn merged 5 commits into
mainfrom
fix/v17c-004-node-distances

Conversation

@jameshgrn

@jameshgrn jameshgrn commented Mar 30, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Fix inverted node distances on 660 flow-corrected reaches (offset reversal for stale v17b node dist_out)
  • Fix single-node reach placement from upstream edge to centroid (25,732 nodes)
  • Sync v17c_flow_corrections table: add 31 missing flipped IDs, remove 7 oscillation-reverted false positives (636 → 660)
  • Add 3 node distance columns (hydro_dist_out, hydro_dist_hw, dist_out_dijkstra) to NetCDF export spec and schema migration
  • Fix flow_verification.py::rebuild_derived_attrs missing flipped_reach_ids and update_node_columns call
  • Fix 3 pre-existing test failures (stale is_mainstem_edge, missing topology_suspect column)
  • Update release notes: 5 → 8 node-level v17c variables
  • Organize export files into versioned directories with version-tagged filenames and fresh SHA256 checksums

Validation

  • 0 negative distances across 5 columns, 11.1M nodes
  • 0 NULLs across 8 propagated node columns
  • 0 intra-reach monotonicity violations (660 flipped + 248K normal)
  • 716 tests pass, 163 skipped, 0 failed (full suite)
  • ruff check + format clean
  • All 54 export files (0.0.2 + 0.0.3 + 0.0.4) version-tagged and checksum-verified

Export file organization

Reorganized data/exports/ into versioned directories with version-tagged filenames:

data/exports/
├── 0.0.2/netcdf/   6 files  *_v17c_0.0.2.nc         (6/6 SHA256 OK)
├── 0.0.3/          24 files  *_v17c_0.0.3.*          (24/24 SHA256 OK)
│   ├── netcdf/     6 NC
│   ├── gpkg/       6 GPKG
│   └── parquet/   12 Parquet
└── 0.0.4/          24 files  *_v17c_0.0.4.*          (24/24 SHA256 OK)
    ├── netcdf/     6 NC
    ├── gpkg/       6 GPKG
    └── parquet/   12 Parquet

Updated /preflight skill check #9 (export version tagging) to enforce versioned directory + filename convention.

Test plan

  • 5 new interpolation regression tests (normal/flipped/single-node/isolation/no-negatives)
  • Full test suite: 716 passed
  • DB preflight checks 1–5 pass (node ordering, monotonicity, propagation, ghost exclusion, hydro_dist_hw freshness)
  • Previously-failing tests now pass (column_order, flow_direction, flow_verification)
  • 0.0.4 exports: 0 negative distances, 0 NULLs across 6 regions
  • All 54 export files version-tagged with fresh SHA256 checksums verified

Three node-level interpolation bugs fixed:
- Flow-corrected reaches (636) had inverted distances because offset
  used stale v17b node dist_out without reversing direction
- Single-node reaches (25,732) placed node at upstream edge instead
  of centroid (reach_length / 2)
- Node distance columns (hydro_dist_out, hydro_dist_hw,
  dist_out_dijkstra) missing from export spec and schema migration

Also fixes flow_verification.py rebuild_derived_attrs:
- Pass flipped_reach_ids to save_to_duckdb for correct node interpolation
- Add missing update_node_columns call after save
- Update stale docstring to match post-refactor compute order

Test fixture cleanup:
- Rename is_mainstem_edge -> is_mainstem in test DB
- Add topology_suspect/topology_approved to reach_topology fixtures
- Match test fixture column order to production schema
- Add 5 interpolation regression tests (normal/flipped/single/no-neg)

Standalone fix script: scripts/maintenance/fix_node_distances.py
fix_node_distances.py now imports propagate_reach_to_nodes from
output.py instead of duplicating the offset/CTE SQL.
@jameshgrn

Copy link
Copy Markdown
Collaborator Author

Code Review Summary

Findings: 0 P1, 0 P2, 1 P3, 3 P4

P3 — Fixed

  • Dry-run display off-by-one (fix_node_distances.py:105): row[:6] included node_id, shifting all variable assignments. Fixed to unpack all 7 columns with _nid discard. Display-only — no data impact.

P4 — Informational (no action)

  1. db_path parameter unused in rebuild_derived_attrs (pre-existing)
  2. SQL IN clause via string interpolation — pragmatic for DuckDB with 800+ IDs from trusted source
  3. Good dedup: single interpolation source in output.py, maintenance script imports it

Verification

  • 23/23 output tests pass, 46/46 flow tests pass
  • ruff check + format clean
  • Interpolation math spot-checked (flipped offset, hydro_dist_hw, hydro_dist_out)
  • flow_verification.py compute order matches v17c_pipeline.py

Fix pushed as f7f7550.

v17c_flow_corrections had 31 missing flipped reach IDs (topology
differs from v17b but not logged by section-level flow correction)
and 7 false positives (oscillation-reverted reaches still marked
as flipped). Synced table to 660 IDs and reran node propagation.
Result: 0 monotonicity inversions across 11.1M nodes.

Also fixed import path: project root instead of src/ to avoid
relative import beyond top-level package error.
@jameshgrn
jameshgrn merged commit eb0716b into main Mar 30, 2026
@jameshgrn
jameshgrn deleted the fix/v17c-004-node-distances branch March 30, 2026 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant