Skip to content

Reduce peak memory usage in clean_segmentation and _clean_frame#39

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-memory-issue-clean-segmentation
Draft

Reduce peak memory usage in clean_segmentation and _clean_frame#39
Copilot wants to merge 2 commits intomainfrom
copilot/fix-memory-issue-clean-segmentation

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 2, 2026

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()inplace parameter

  • Adds inplace=False parameter. Set True to skip the full array copy and halve peak memory:
    # default: safe copy (existing behaviour)
    cleaned = clean_segmentation(seg)
    
    # memory-constrained: modify in place
    cleaned = clean_segmentation(seg, inplace=True)
  • Removes leftover debug code that printed frame-13 / label-31 diagnostics on every call.

_clean_frame() — label-by-label processing

  • Drops the component_info dict that kept all component masks in memory at once.
  • Processes one label at a time: del label_mask immediately after ndimage.label, compute sizes via np.bincount (no per-component arrays), then create and apply each comp_mask individually before moving on.

Tests

  • Adds test_inplace_false_does_not_modify_input, test_inplace_true_modifies_input, and test_inplace_true_produces_same_result_as_copy to TestCleanSegmentation.

Copilot AI linked an issue Apr 2, 2026 that may be closed by this pull request
- 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
Copilot AI requested a review from Pablo1990 April 2, 2026 14:47
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.

Memory issue with clean_segmentation

2 participants