Skip to content

frs_cluster: apply bridge_gradient and bridge_distance along path #153

@NewGraphEnvironment

Description

@NewGraphEnvironment

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:

  1. Cluster rearing with ST_ClusterDBSCAN
  2. Find cluster minimums (most downstream point per cluster, ordered by wscode)
  3. fwa_downstreamtrace from each minimum, mainstem only (blue_line_key = watershed_key)
  4. Cap at bridge_distance, row_number sequentially
  5. Find first segment with gradient >= bridge_gradient
  6. Find nearest spawning (label_connect = TRUE)
  7. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions