How to select an existing ROI in LocalTreshold of SegmentEditor with code?

Operating system:windows 10
Slicer version: 5.0.3

Hi, all,
I just started learning slicer+jupyter and recently I have a problem that I can’t solve, I would appreciate if someone tell me:

Before activating LocalTreshold(from extensions module named “SegmentEditorExtraEffects”), I followed the link to fit ROI to volume.

Then, I have set the basic parameters as shown, but how can I set the ROI with code?
"effect.setParameter(“ROI”,)" doesn’t seem to work (: sampleID/name/vtkMRMLAnnotationROINode).

segmentEditorWidget.setActiveEffectByName("Local Threshold")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("MinimumThreshold",str(40000))
effect.setParameter("MaximumThreshold",str(200000))
effect.setParameter("MinimumDiameterMm",str(4))
#How to select a ROI?

Snipaste_2022-09-05_13-21-42

Hi, all,
I just started learning slicer+jupyter and recently I have a problem that I can’t solve, I would appreciate if someone tell me:

Before activating the effect of LocalThreshold (an effect of SegmentEditorExtraEffects), I followed the link to fit an existing ROI to volume, the ROI is smaller than the volume. Then,I have set the basic parameters as shown, how can I set the ROI with code?
"effect.setParameter(“ROI”,)" seems to have no effect (: sampleID/name/vtkMRMLAnnotationROINode)

segmentEditorWidget.setActiveEffectByName("Local Threshold")
effect = segmentEditorWidget.activeEffect()
# You can change parameters by calling: effect.setParameter("MyParameterName", someValue)
effect.setParameter("MinimumThreshold",str(40000))
effect.setParameter("MaximumThreshold",str(200000))
effect.setParameter("MinimumDiameterMm",str(4))
#How to selcet ROI?

Snipaste_2022-09-05_13-21-42

It seems that thw. ROI is not stored in MRML, but it is directly read from the GUI. This is not ideal, but it happens for extensions that are still somewhat experimental, being in the the “SegmentEditorExtraEffects” extension.

Until this is fixed (by adding a MRML node reference), you may access the effect’s GUI and set the ROI node calling effectWidget.roiSelector.setCurrentNode(SomeROI).

@Sunderlandkyl when you find some time could you update the Local Threshold effect to store the ROI in a node reference and post here when it is done? Thank you!

1 Like

Sincerely thank you for your reply, I am very excited, I will be back with the results in a while! :grin:

1 Like

I’m ashamed to be back…begging you to tell me how to define “effectWidget”. :smiling_face_with_tear:

Sorry to interfere.

This code snippet may give you enough insight for a workaround.

# The segment editor module must have been shown at least once.
mainWindow = slicer.util.mainWindow()
mainWindow.moduleSelector().selectModule('SegmentEditor')
# Select an effect.
widgetEditor = slicer.modules.SegmentEditorWidget.editor
widgetEditor.setActiveEffectByName("Local Threshold")
activeEffect = widgetEditor.activeEffect()
# Get the ROI combobox.
roiSelector = activeEffect.optionsFrame().children()[14]

"""
'activeEffect.optionsFrame().children()' lists all widgets of an effect.

If a widget has a name you can access it as such :
activeEffect.optionsFrame().findChild(qt.QPushButton, "SegmentEditorEffectApply")

If a widget does not have a name, guess and count its index sequentially.
An MRML node reference for this effect would be nicer of course.
"""
1 Like

Final solution for the parameter setting section:

# Run segmentation
segmentEditorWidget.setActiveEffectByName("Local Threshold")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("MinimumThreshold",str(40000))
effect.setParameter("MaximumThreshold",str(200000))
effect.setParameter("MinimumDiameterMm",str(4))
roiSelector = effect.optionsFrame().children()[14]
roiSelector.setCurrentNode(roiNode)

Thanks to @chir.set

This solution is useful, thanks a lot :smile:

In the meantime, @Sunderlandkyl made the ROI node accessible via a node reference (available in the Extensions Manager from tomorrow).