Skip to content

Add coho habitat pipeline with DB-side classification#56

Merged
NewGraphEnvironment merged 11 commits intomainfrom
habitat-pipeline
Mar 19, 2026
Merged

Add coho habitat pipeline with DB-side classification#56
NewGraphEnvironment merged 11 commits intomainfrom
habitat-pipeline

Conversation

@NewGraphEnvironment
Copy link
Copy Markdown
Owner

Summary

Habitat model pipeline — extract, enrich, break, classify, aggregate — all on PostgreSQL. Proves the workflow on the Byman-Ailport subbasin before scaling to full Neexdzii study area.

New functions

  • frs_edge_types() — FWA edge type lookup from bundled CSV (41 codes from GeoBC User Guide)
  • frs_col_join() — generic lookup table enrichment (channel_width, MAD, upstream_area, any key)

Enhanced functions

  • frs_classify(where=) — scope classification by SQL predicate (edge_type, waterbody_key, accessible)
  • frs_network(to=) — write results to DB working tables instead of pulling to R memory
  • frs_break_find(where=, append=) — filter points_table rows, combine multiple break sources
  • frs_point_snap(exclude_edge_types=) — configurable snap guard (only exclude subsurface 1425, not wetland connectors 1410)

Bundled data

  • parameters_habitat_thresholds.csv — bcfishpass defaults (11 species)
  • parameters_habitat_method.csv — MAD vs channel_width per watershed group
  • parameters_fresh.csv — access gradients + spawn gradient mins
  • edge_types.csv + feature_codes.csv — FWA reference tables

Vignette: Classifying Coho Habitat on the Byman-Ailport Subbasin

  • Full pipeline: network → col_join → col_generate → break (gradient + falls) → classify (accessible, spawning, rearing, lake rearing) → aggregate
  • Access barriers from gradient thresholds AND barrier falls
  • Lake rearing gap: bcfishpass scores 0 km, fresh recovers habitat
  • Scenario comparison: spawn gradient min at 0%, 0.5%, 1.0%

Issues

Test plan

  • 505 tests pass (devtools::test())
  • devtools::check() — 0 errors, 0 warnings
  • All three vignettes render from cache
  • Habitat vignette rendered from live DB

Relates to NewGraphEnvironment/sred-2025-2026#16

NewGraphEnvironment and others added 11 commits March 18, 2026 17:30
Bundled CSV from GeoBC Freshwater Atlas User Guide (2009) Table 2 —
41 edge type codes with descriptions and category groupings. Exported
helper frs_edge_types(category = "lake") for filtering. Categories:
stream, lake, wetland, river, construction, connector, subsurface,
canal, island, coastline, boundary, unknown.

Also adds feature_codes.csv from Table 3 (feature code attributes).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Scopes which rows get classified via SQL predicate. Works with ranges,
breaks, and overrides modes. Enables lake-specific rearing classification
via waterbody_key filtering and edge_type-aware habitat modelling.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Joins columns from any lookup table onto a working table via SQL
UPDATE FROM. Detects source column types from information_schema
(handles search_path for unqualified table names). Supports direct
keys, composite keys, named key mapping, and subqueries.

Used for channel_width, MAD, upstream_area, MAP — any FWA lookup
or custom model output.

Fixes #51

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…1410)

edge_type 1410 (network connector) is real wetland connectivity (204K
segments in wetlands). Only 1425 (subsurface flow, 343 segments) should
be excluded from snap candidates by default.

Adds exclude_edge_types param to frs_point_snap() — default 1425,
NULL to snap to everything.

Fixes #52
Relates to NewGraphEnvironment/bcfishpass#8

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
When to is provided, writes results to working table(s) via CREATE
TABLE AS instead of returning sf. Multi-table uses prefix + suffix
naming. Returns conn invisibly for pipe chaining. Keeps data on
PostgreSQL — no R memory bottleneck at scale.

Relates to #53

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
where filters rows from points_table (e.g. barrier_ind = TRUE).
append = TRUE inserts into existing breaks table instead of creating
— enables combining gradient barriers + falls in one breaks table.

Adds points_where param to frs_break() convenience wrapper.

Relates to #38

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
parameters_habitat_thresholds.csv — bcfishpass defaults from
smnorris/bcfishpass/parameters/example_newgraph (11 species).
parameters_habitat_method.csv — MAD vs channel_width control
per watershed group (187 WSGs).
parameters_fresh.csv — fresh-specific: access gradient thresholds
(from bcfishpass model_access_*.sql) and spawn/rear gradient mins.

data-raw/bcfishpass_params.R fetches upstream defaults from GitHub.

Fixes #54
Relates to #55

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
data-raw/example_byman_ailport.R uses frs_network(to=) + frs_col_join()
pipeline. byman_ailport.rds now includes channel_width and
channel_width_source on streams. byman_ailport_habitat.rds caches the
full habitat pipeline results for the vignette.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Full pipeline on Byman-Ailport subbasin: extract → col_join → col_generate
→ break (access gradient + barrier falls) → classify (accessible, spawning,
rearing, lake rearing) → aggregate at crossings. Scenario comparison at
0%, 0.5%, 1.0% minimum spawn gradient. Lake rearing gap vs bcfishpass.
All computation on PostgreSQL.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
dev/rag_build.R builds local ragnar DuckDB store from FWA User Guide
PDF (171 chunks, gitignored at data/rag/). Planning files updated
to reflect habitat pipeline progress.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…gnore

- Qualify read.csv as utils::read.csv in frs_edge_types()
- Document points_where param in frs_break() roxygen
- Add data/rag and figure to .Rbuildignore

R CMD check: 0 errors, 0 warnings, 1 note (.lintr hidden file)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@NewGraphEnvironment NewGraphEnvironment merged commit aac96ef into main Mar 19, 2026
1 check passed
@NewGraphEnvironment NewGraphEnvironment deleted the habitat-pipeline branch March 19, 2026 00:56
NewGraphEnvironment added a commit that referenced this pull request Apr 9, 2026
These PWF files sat in planning/active/ since March 2026 when PR #56
(coho habitat pipeline) merged but the planning files were never
archived. Workflow hygiene reset before starting issue #110.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant