Ignore class_values entries absent from the input array#2
Merged
Conversation
clean_array raised KeyError when class_values named a class that does not occur in the array, but only when smooth_edge_size > 0. The smooth path in smooth_edges_to_codes looked up value_to_code[cv_] unguarded, while value_to_code is built solely from np.unique(array). The two sibling call sites already guarded this (multiclean.py target_codes filters on membership; the background loop uses .get), so this was a missed case rather than intentional strictness. Filter target_class_values against the values present in the array once at the entry point, which covers all three call sites and skips the wasted morphological opening for a class whose mask is all zeros. background_class_values is derived as set(all) - set(target), so it is unaffected. Add parametrized sweeps over dtype x smooth_edge_size x min_island_size x class_values. The original point tests missed this because the bug lived in one corner of that grid. Verified by reverting the fix: 121 of the new cases fail without it. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Problem
clean_arrayraisedKeyErrorwhenclass_valuesnamed a class that does not occur in the input array — but only whensmooth_edge_size > 0:The smooth path in
smooth_edges_to_codesdid an unguardedvalue_to_code[cv_], andvalue_to_codeis built solely fromnp.unique(array). The two sibling call sites already guarded this —multiclean.pyfilterstarget_codeson membership, and the background loop uses.getwith aNonecheck — so this was a missed case, not intentional strictness.This mainly affects tiled processing, where one fixed class list is reused across tiles whose contents vary. The failure depends on the data, so it surfaces intermittently.
Fix
Filter
target_class_valuesagainst the values present in the array once at the entry point. That covers all three call sites and skips the wastedcv2.morphologyExcall for a class whose mask is entirely zeros.background_class_valuesis derived asset(all) - set(target), so it is unaffected.Tests
Added parametrized sweeps over
dtype×smooth_edge_size×min_island_size×class_values. The existing point tests missed this because the bug lived in one corner of that grid, so the new tests assert invariants across it:Verified by reverting the fix: 121 of the new cases fail without it. Suite is 322 passed in ~1.7s.
🤖 Generated with Claude Code