fix: keep in_seg's redistribution inside the segmentation - #111
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
`aug_redistribute_seg` builds a `to_add` field that is exactly zero outside the
labels on the `in_seg` branch, then normalised it with
img += 2 * (to_add - to_add.min()) / (to_add.max() - to_add.min() + 1e-6)
A min-max rescale moves zero: a voxel where nothing was redistributed picked up
`-2 * min / range`, non-zero whenever min < 0 -- and it always is, because the
per-label scale is `2 * rand - 1` on [-1, 1]. So `in_seg` restricted where the
redistribution was computed but not where it landed, and the whole patch got a
uniform DC offset.
Scaling by the largest magnitude keeps the same peak amplitude of 2 and maps
zero to zero. Measured on a 16-cube with a central label: the largest change
outside the segmentation goes from a constant offset to exactly 0.0, while
inside it still reaches 2.0.
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
On the
in_segbranch,to_addis written only inside the labels and is exactly zero everywhere else:It was then normalised with a min-max rescale:
A min-max rescale moves zero. A voxel where nothing was redistributed picks up
-2 * to_add_min / range, which is non-zero wheneverto_add_min < 0— and it always is, because the per-label scale is2 * rand - 1on[-1, 1].So
in_segrestricted where the redistribution was computed but not where it landed: the whole patch, background included, got a uniform DC offset. That is the opposite of what the parameter promises.Measured
16-cube volume, central 8-cube label,
in_seg=1.0, comparing against the same image after the function's own[0, 1]normalisation:The fix
Scale by the largest magnitude instead:
2 * to_add / (to_add.abs().max() + 1e-6). Same peak amplitude of 2, and zero maps to zero, so an untouched voxel stays untouched.Tests
unit_tests/test_redistribute_in_seg.py: nothing changes outside the labels across five seeds; something still changes inside them (the guard against "fixing" this by disabling the transform); the peak amplitude is unchanged; thein_seg=0whole-image branch still redistributes; and a constant patch stays finite.Verified failing before the fix (2 failed) and passing after. Full suite unchanged against baseline;
ruff check .clean.🤖 Generated with Claude Code