Reduce peak memory usage in clean_segmentation and _clean_frame#39
Draft
Reduce peak memory usage in clean_segmentation and _clean_frame#39
Conversation
- Add inplace=False parameter to clean_segmentation() so callers can skip the full array copy and halve peak memory usage - Remove leftover debug code (frame 13 / label 31 diagnostics) - Refactor _clean_frame() to process each label independently: del label_mask immediately after ndimage.label, use np.bincount for sizes (no per-component boolean arrays kept in memory), and create comp_mask one at a time instead of pre-storing all masks - Add tests for inplace=False/True behaviour in TestCleanSegmentation Agent-Logs-Url: https://github.com/timsmsmsm/easytrack/sessions/c64eff3d-956c-47f2-88c3-8544d3823d2d Co-authored-by: Pablo1990 <1974224+Pablo1990@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix memory issue with clean_segmentation function
Reduce peak memory usage in clean_segmentation and _clean_frame
Apr 2, 2026
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.
clean_segmentation()duplicated the entire array upfront, and_clean_frame()accumulated all component boolean masks simultaneously — together causing severe memory spikes on large(T, Z, Y, X)stacks.Changes
clean_segmentation()—inplaceparameterinplace=Falseparameter. SetTrueto skip the full array copy and halve peak memory:_clean_frame()— label-by-label processingcomponent_infodict that kept all component masks in memory at once.del label_maskimmediately afterndimage.label, compute sizes vianp.bincount(no per-component arrays), then create and apply eachcomp_maskindividually before moving on.Tests
test_inplace_false_does_not_modify_input,test_inplace_true_modifies_input, andtest_inplace_true_produces_same_result_as_copytoTestCleanSegmentation.