fix: read a background-inclusive one-hot correctly in _foreground - #115
Open
Hendrik-code wants to merge 1 commit into
Open
Hendrik-code wants to merge 1 commit into
Hendrik-code wants to merge 1 commit into
Conversation
The package carries two one-hot conventions. `collapse_onehot_to_index` documents background as implicit -- channel c is label c + 1, background voxels are all-zero. `seg_region_masks` emits one channel per distinct value for a label map, background included, and test_seg_layout.py builds the same shape. `_foreground` was written for the first. Given the second, the background channel covers exactly the voxels the foreground channels do not, so `amax > 0` is True at every voxel: measured, fraction foreground 1.0 instead of 0.5. That made `in_seg` apply the transform to the whole patch and `out_seg` to none of it, which is the precise failure the function's docstring says it exists to fix -- in the one layout it was not checked against. A background-inclusive one-hot is recognisable without guessing at values: it is multi-channel and leaves no voxel unset. A background-implicit one-hot has all-zero background voxels unless the patch is labelled edge to edge, which a real segmentation patch is not. Single-channel input is never second-guessed. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
The defect
The package carries two one-hot conventions:
collapse_onehot_to_index(gpu/fromSeg.py)cis labelc + 1, background voxels are all-zeroseg_region_masks(gpu/fromSeg.py)_foreground(gpu/contrast.py) is written for the first. Given the second, the background channel covers exactly the voxels the foreground channels do not, soamax > 0is True at every voxel:With an all-True foreground,
in_segapplies the transform to the whole patch andout_segto none of it — which is word for word the failure_foreground's own docstring says it was written to fix, in the one layout it was not checked against.unit_tests/test_seg_layout.pybuilds exactly this shape (for v in range(5)), andseg_region_masksproduces it for every single-channel label map.The fix
A background-inclusive one-hot is recognisable without guessing at label values: it is multi-channel and leaves no voxel unset, because the background channel covers whatever the foreground channels do not. A background-implicit one-hot always has all-zero background voxels unless the patch is labelled edge to edge, which a real segmentation patch is not. Single-channel input is never second-guessed.
Tests
unit_tests/test_foreground_layouts.pyasserts all four layouts give the same answer for the same anatomy — label map, background-implicit one-hot, background-inclusive one-hot, and the actual output ofseg_region_masks, since one helper feeds the other's layout. Plus an empty mask (no foreground) and a fully labelled single-channel map (all foreground, not second-guessed).Verified failing before the fix (2 failed — the two background-inclusive cases) and passing after. Full suite unchanged against baseline;
ruff check .clean.🤖 Generated with Claude Code