Skip to content

fix: keep in_seg's redistribution inside the segmentation - #111

Open
Hendrik-code wants to merge 1 commit into
mainfrom
hm/fix-redistribute-dc-offset
Open

Hendrik-code wants to merge 1 commit into
mainfrom
hm/fix-redistribute-dc-offset

Conversation

@Hendrik-code

Copy link
Copy Markdown
Collaborator

The defect

On the in_seg branch, to_add is written only inside the labels and is exactly zero everywhere else:

if in_seg_bool:
    to_add[l_mask] += ... * (2 * torch.rand(1) - 1)

It was then normalised with a min-max rescale:

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 picks up -2 * to_add_min / range, which is non-zero whenever to_add_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: 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:

before:  max |delta| outside the segmentation = a constant, non-zero offset
after:   max |delta| outside the segmentation = 0.0
         max |delta| inside  the segmentation = 2.0   (unchanged peak)

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; the in_seg=0 whole-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

`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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant