Skip to content

fix(ai): keep auto segment mode from crashing on a viewport without a segmentation - #2953

Open
TFRadicalImaging wants to merge 1 commit into
cornerstonejs:mainfrom
TFRadicalImaging:fix/onnx-auto-segment-without-active-segmentation
Open

TFRadicalImaging wants to merge 1 commit into
cornerstonejs:mainfrom
TFRadicalImaging:fix/onnx-auto-segment-without-active-segmentation

Conversation

@TFRadicalImaging

@TFRadicalImaging TFRadicalImaging commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Context

LabelmapSlicePropagationTool re-points its ONNXSegmentationController at whichever viewport becomes active: an application calls setToolConfiguration with a new sourceViewportId, onSetToolConfiguration runs _init, and _init calls segmentAI.initViewport(viewport). In a multi-viewport layout the newly active viewport often has no segmentation of its own. Two separate failures follow from that switch.

1. The render listener throws. With auto segment mode on, the IMAGE_RENDERED listener reads the active segmentation of the viewport and takes segmentationId from it without a check:

const segmentation =
  cornerstoneTools.segmentation.activeSegmentation.getActiveSegmentation(viewport.id);
const segmentIndex =
  cornerstoneTools.segmentation.segmentIndex.getActiveSegmentIndex(
    segmentation.segmentationId   // segmentation is undefined here
  );
TypeError: Cannot read properties of undefined (reading 'segmentationId')
    at ONNXSegmentationController.viewportRenderedListener
    at triggerEvent
    at Array.forEach (<anonymous>)
    at ContextPoolRenderingEngine._renderFlaggedViewports

It throws on every render of that viewport while Labelmap Assist is on.

2. The decoder previews into a viewport that has no preview. This one was hidden behind the first. initViewport resets currentImage but keeps randomPoints, the world points gathered from the previous viewport's segmentation. initViewport then calls tryLoad(), and when the encode of the new image finishes, tryLoad rebuilds this.points from those stale points and runs the decoder. createLabelmap calls this.tool.addPreview(viewport.element) on the new viewport, which has no segmentation to preview into, gets undefined back, and throws:

TypeError: Cannot destructure property 'previewSegmentIndex' of 'preview' as it is undefined.
    at ONNXSegmentationController.createLabelmap
    at ONNXSegmentationController.decode
    at async ONNXSegmentationController.runDecode

This one needs a real prediction on the first viewport (paint a slice, scroll so points are collected), then a click on another viewport. On a viewport that does have its own segmentation, the same stale points would produce a preview seeded from the other viewport.

Changes & Results

  • viewportRenderedListener: when the viewport has no active segmentation, return before the propagation step. There is nothing to propagate from. The existing non-acquisition-plane branch right above it already returns the same way.
  • initViewport: clear randomPoints together with currentImage, so points gathered on one viewport are never decoded against another.

Before: with Labelmap Assist on, clicking a second viewport that has no segmentation throws from the render listener. After a prediction on the first viewport, it also throws from the decoder.
After: the switch is silent. Assist keeps predicting on the first viewport. Once the second viewport gets a segmentation of its own, Assist predicts there too, because the early return leaves currentImage untouched and the next render goes through normally.

Testing

New packages/ai/src/ONNXSegmentationController.test.ts:

  • rendering a viewport with no active segmentation in auto segment mode does not throw;
  • initViewport does not carry the previous viewport's propagation points over.

pnpm jest in packages/ai: 2 suites / 5 tests pass. Negative check: with the source change reverted, both new tests fail (TypeError for the first).

Manual, in an OHIF-based viewer with a multi-viewport cine MR layout (@cornerstonejs/ai 5.10.9 with this change applied to its dist):

  1. Add a labelmap segmentation on viewport 1, paint two segments, turn on Labelmap Assist, click viewport 2 → before: failure 1; after: no error.
  2. Same, but paint a slice and scroll to get a prediction on viewport 1 first, then click viewport 2 → before: failures 1 and 2; with only the render-listener guard: failure 2; after: no error.
  3. After that, add a segmentation on viewport 2, paint, scroll → a prediction appears on viewport 2 with no error.

Checklist

PR

  • My Pull Request title is descriptive, accurate and follows the semantic-release format and guidelines.

Code

  • My code has been well-documented (function documentation, inline comments, etc.)

Public Documentation Updates

  • The documentation page has been updated as necessary for any public API additions or removals.

Tested Environment

  • "OS: macOS 15"
  • "Node version: 22.17.1"
  • "Browser: Chrome 151"

Summary by CodeRabbit

  • Bug Fixes
    • Switching viewports now clears leftover segmentation points, preventing data from a previous viewport from affecting the new one.
    • Auto-segmentation no longer attempts to propagate points when the active viewport has no segmentation, avoiding rendering errors.

… segmentation

LabelmapSlicePropagationTool re-points its ONNXSegmentationController at
whichever viewport becomes active. With auto segment mode on, the next
render of that viewport read segmentationId off the active segmentation,
which is undefined when the viewport has none, and threw from the
IMAGE_RENDERED listener.

With that fixed, a second failure surfaced on the same switch: initViewport
kept the propagation points gathered on the previous viewport, so once the
new image was encoded the decoder ran them against a viewport whose
labelmap tool had no preview to create, and createLabelmap threw on the
undefined preview.

Skip propagation where there is no active segmentation, and drop the
previous viewport's points when the controller moves to another one.
@coderabbitai

coderabbitai Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 99d2ca13-564a-4238-8735-55203263862a

📥 Commits

Reviewing files that changed from the base of the PR and between 7dbbb5f and 0738e39.

📒 Files selected for processing (2)
  • packages/ai/src/ONNXSegmentationController.test.ts
  • packages/ai/src/ONNXSegmentationController.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.


📝 Walkthrough

Walkthrough

The controller now clears leftover propagation points when it initializes a viewport. In auto-segment mode, it returns from the render listener when the active viewport has no active segmentation. Tests cover both behaviors.

Changes

Viewport safeguards

Layer / File(s) Summary
Reset propagation state and guard auto-segment rendering
packages/ai/src/ONNXSegmentationController.ts, packages/ai/src/ONNXSegmentationController.test.ts
initViewport clears randomPoints. The render listener returns when no active segmentation exists. Tests cover both cases.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: wayfarer3130

Merge Risk: ⚪ Minimal · up to 0738e

The change prevents stale propagation points from crossing viewports and avoids auto-segmentation errors when no segmentation exists. No actionable merge-blocking risk remains.

Security Architecture Review

Security architecture risk: 🔵 Low · up to 0738e

The change reduces stale segmentation reuse and avoids rendering failures without expanding access or authority. Risk remains low because safe completion of predictions already running during viewport switches is not fully established.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The evidenced mutable-state scope is propagation inputs and segmentation previews associated with viewports sharing one controller. Broader tenant, service, or environment exposure is not established by this source slice.

Trust Boundaries and Controls

  • observed — The new guard requires an active segmentation for the selected viewport before reading its segment identity and scanning propagation inputs. This is a state-validity prerequisite, not an authentication or authorization control.

Resilience and Maintainability Implications

  • inferred — A pre-existing ownership weakness remains: decode validates image identity before asynchronous inference, then creates a preview using the current mutable viewport and tool without a post-inference ownership check. Switching during inference can therefore redirect completion. The inspected changes do not introduce this behavior, and detached-preview cleanup was not established.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main fix and follows the repository's semantic-release format.
Description check ✅ Passed The description is complete and relevant. It explains the two failures, the code changes, test coverage, manual validation, and checklist status.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Autopilot is currently an internal CodeRabbit preview.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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