Refactor _cluster_upstream: trace downstream from cluster to find spawning
Problem
_cluster_upstream uses boolean FWA_Upstream — "does spawning exist anywhere upstream?" No path gradient. CH rearing +6% vs bcfishpass because rearing above a 5% gradient with spawning further above passes the boolean but shouldn't.
Boolean FWA_Upstream can't do path gradient because it returns ALL upstream segments including every tributary. row_number ordering doesn't give a linear path.
What bcfishpass does (Phase 3 — rearing above spawning)
Traces DOWNSTREAM from each rearing cluster (toward the mouth). Linear — one path, no branching. Walk segment by segment, stop at first gradient >= bridge_gradient or distance >= bridge_distance. If spawning found before stop → connected.
Same pattern as _cluster_downstream but starting from the rearing cluster.
Fix
Replace _cluster_upstream internals with fwa_downstreamtrace from each cluster minimum:
- Cluster rearing with ST_ClusterDBSCAN
- Find cluster minimums (most downstream point per cluster, ordered by wscode)
- fwa_downstreamtrace from each minimum, mainstem only (blue_line_key = watershed_key)
- Cap at bridge_distance, row_number sequentially
- Find first segment with gradient >= bridge_gradient
- Find nearest spawning (label_connect = TRUE)
- Keep cluster if spawning row_number < barrier row_number
Same params (bridge_gradient, bridge_distance). Same interface. _both stays as OR — both checks now path-constrained.
How to test
devtools::install_local("/Users/airvine/Projects/repo/link")
Rscript /Users/airvine/Projects/repo/link/data-raw/compare_bcfishpass.R ADMS
# No regression: BT rearing ~-0.3%, CH ~+2%, CO ~-1%
Rscript /Users/airvine/Projects/repo/link/data-raw/compare_bcfishpass.R BULK
# CH rearing < +5% (currently +6.0%)
Branch, PWF, checkboxes on commits. Test locally before push.
Versions
Refactor _cluster_upstream: trace downstream from cluster to find spawning
Problem
_cluster_upstream uses boolean FWA_Upstream — "does spawning exist anywhere upstream?" No path gradient. CH rearing +6% vs bcfishpass because rearing above a 5% gradient with spawning further above passes the boolean but shouldn't.
Boolean FWA_Upstream can't do path gradient because it returns ALL upstream segments including every tributary. row_number ordering doesn't give a linear path.
What bcfishpass does (Phase 3 — rearing above spawning)
Traces DOWNSTREAM from each rearing cluster (toward the mouth). Linear — one path, no branching. Walk segment by segment, stop at first gradient >= bridge_gradient or distance >= bridge_distance. If spawning found before stop → connected.
Same pattern as _cluster_downstream but starting from the rearing cluster.
Fix
Replace _cluster_upstream internals with fwa_downstreamtrace from each cluster minimum:
Same params (bridge_gradient, bridge_distance). Same interface. _both stays as OR — both checks now path-constrained.
How to test
Branch, PWF, checkboxes on commits. Test locally before push.
Versions