dft_rast_classify(): classify factor input from its raw codes (#91) - #94
Open
NewGraphEnvironment wants to merge 6 commits into
Open
NewGraphEnvironment wants to merge 6 commits into
NewGraphEnvironment wants to merge 6 commits into
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PRhUJsuKABLfpBGktPoiBN
terra::unique() returns a factor's labels, not its codes, so a raster that was already a factor (a published RAT raster, or one re-classified) matched no code in class_table and came back with no levels or colours. Strip the levels on a copy with strip_copy() after the remap step, where only an unremapped factor needs it; is.factor(x)[1] keeps a stack on the pre-existing layer-1 path instead of erroring. BULK classified_2017.tif: file-backed peak unchanged (1.05 GiB); an in-memory unremapped factor pays one copy (5.47 vs 4.20 GiB). Fixes #91 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PRhUJsuKABLfpBGktPoiBN
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PRhUJsuKABLfpBGktPoiBN
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PRhUJsuKABLfpBGktPoiBN
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PRhUJsuKABLfpBGktPoiBN
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PRhUJsuKABLfpBGktPoiBN
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
dft_rast_classify()returned a factor with no levels and no colours when given a raster that was already a factor, for example a published floodplain raster with a RAT, or a raster re-classified. The cause:terra::unique()returns a factor's labels, not its codes. The fix strips the levels on a copy (strip_copy(), which already existed) before reading the codes. The caller's raster is untouched.terra::classify()already reads raw codes, so a remap that matches pays nothing extra. A remap that matches nothing had the same bug onmain, and now works.is.factor(x)[1]: the plan review and code-check round 1 each found, independently, that a bareis.factor(x)errors on a multi-layer stack.mainclassified a stack's layer 1 without error, and that behaviour is kept.Scale check (BULK
classified_2017.tif, 169M cells, published RAT,/usr/bin/time -l)The one cost is an in-memory factor with no matching remap: one full extra copy. No R-level terra call reads a factor's codes without a copy (
activeCat<-andlevels<-bothdeepcopy()).Related Issues
Test plan
devtools::test(): 1344 pass / 0 fail / 15 skip (network and bfast, opt-in)main(9 assertions) and pass on the branch: re-classify, file-backed RAT with its own labels and palette, remap on a factor (matched and unmatched), caller unmodified, active category ≠ 1, 2-layer stacks[1]turns the stack test red/code-check: 3 rounds (the stack error, then Clean, Clean). Round 3 checked every NEWS claim against a copy ofmain.Notes
Planning record:
planning/archive/2026-09-issue-91-classify-factor-input/, which includes the plan review and the three review rounds.🤖 Generated with Claude Code
https://claude.ai/code/session_01PRhUJsuKABLfpBGktPoiBN