Skip to content

🐛 Fix Broken Margin Behaviour#1067

Open
measty wants to merge 3 commits into
developfrom
fix-margin-bug
Open

🐛 Fix Broken Margin Behaviour#1067
measty wants to merge 3 commits into
developfrom
fix-margin-bug

Conversation

@measty
Copy link
Copy Markdown
Collaborator

@measty measty commented May 22, 2026

Fixes a bug in MultiTaskSegmentor’s WSI probability-map vertical merging when spilling intermediate probability rows to Zarr. Previously, once a head spilled to Zarr, subsequent chunks for that same head could continue accumulating in memory instead of being appended to the Zarr. At finalization, the code preferred the Zarr array and discarded the in-memory chunks, producing truncated probability maps for only the heads that spilled resulting in the shape mismatch that was being seen by me and Jiaqi.

The fix keeps hold of the opened Zarr group after the first spill and routes later chunks for that head into the same Zarr dataset. This preserves the full merged probability-map height across all heads.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 22, 2026

Codecov Report

❌ Patch coverage is 77.96610% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.77%. Comparing base (0202a7d) to head (e22ca77).

Files with missing lines Patch % Lines
tiatoolbox/models/engine/semantic_segmentor.py 76.36% 6 Missing and 7 partials ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1067      +/-   ##
===========================================
- Coverage    99.88%   99.77%   -0.12%     
===========================================
  Files           85       85              
  Lines        11625    11658      +33     
  Branches      1524     1530       +6     
===========================================
+ Hits         11612    11632      +20     
- Misses           7       13       +6     
- Partials         6       13       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@shaneahmed shaneahmed requested review from Jiaqi-Lv and shaneahmed May 26, 2026 10:16
@shaneahmed shaneahmed changed the title fix broken margin behaviour 🐛 Fix Broken Margin Behaviour May 26, 2026
@shaneahmed shaneahmed added this to the Release 2.1.1 milestone May 26, 2026
@shaneahmed shaneahmed added the bug Something isn't working label May 26, 2026
@Jiaqi-Lv
Copy link
Copy Markdown
Collaborator

Is this related to my issue with SemanticSegmentor? #1071 ?

@measty
Copy link
Copy Markdown
Collaborator Author

measty commented May 28, 2026

Is this related to my issue with SemanticSegmentor? #1071 ?

It fixed a similar-looking issue I was having when changing the margin (not the stride as in your case). What I was getting was basically the same error you saw:

curr_chunk[-overlap:] += next_chunk[:overlap]
ValueError: operands could not be broadcast together

On some values, but when varying margin not stride. I was hoping it might fix your issue too, but tried it on the same slide as in your issue and it does not. So, similar symptoms but not the same cause.

@measty
Copy link
Copy Markdown
Collaborator Author

measty commented May 28, 2026

This PR now fixes #1071 too

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes vertical probability-map merging behavior, especially when intermediate probability rows spill to Zarr during WSI segmentation, so later chunks continue appending instead of being dropped.

Changes:

  • Adds generalized vertical chunk aggregation for semantic segmentation with multi-row overlap handling.
  • Keeps and returns the opened Zarr group during multitask vertical probability spilling.
  • Adds regression tests for multi-row overlap and continuing appends after Zarr spill.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
tiatoolbox/models/engine/semantic_segmentor.py Refactors vertical merging to aggregate active overlapping chunks and store finalized segments incrementally.
tiatoolbox/models/engine/multi_task_segmentor.py Updates multitask probability spilling to retain the Zarr group for later appends.
tests/engines/test_semantic_segmentor.py Adds coverage for a row overlapping multiple following rows.
tests/engines/test_multi_task_segmentor.py Updates cache helper test and adds a regression test for post-spill appending.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +2611 to +2613
zarr_group=(
zarr_group if probabilities_zarr[idx] is not None else None
),
@Jiaqi-Lv
Copy link
Copy Markdown
Collaborator

Looks good.

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

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants