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

**URL:** <https://discourse.slicer.org/t/how-to-select-an-existing-roi-in-localtreshold-of-segmenteditor-with-code/25100>\
**Category:** Support\
**Tags:** segmentation, extensions-manager, python\
**Created:** [September 5, 2022, 11:56am UTC](https://discourse.slicer.org/t/how-to-select-an-existing-roi-in-localtreshold-of-segmenteditor-with-code/25100 "2022-09-05T11:56:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![KKLOVECOCO](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kklovecoco/32/16521_2.png) [@KKLOVECOCO](https://discourse.slicer.org/u/KKLOVECOCO)\
**Post date:** [September 5, 2022, 11:56am UTC](https://discourse.slicer.org/t/how-to-select-an-existing-roi-in-localtreshold-of-segmenteditor-with-code/25100/1 "2022-09-05T11:56:23Z")

</div>

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](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#fit-markups-roi-to-volume) 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).

```auto
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](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/d/6/d660b97243ee74b821fd727dbbef62058ae996fd.png)

---

<div class="post-metadata">

**Author:** ![KKLOVECOCO](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kklovecoco/32/16521_2.png) [@KKLOVECOCO](https://discourse.slicer.org/u/KKLOVECOCO)\
**Post date:** [September 5, 2022, 11:56am UTC](https://discourse.slicer.org/t/how-to-select-an-existing-roi-in-localtreshold-of-segmenteditor-with-code/25100/2 "2022-09-05T11:56:08Z")

</div>

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](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#fit-markups-roi-to-volume) 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)

```auto
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](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/d/6/d660b97243ee74b821fd727dbbef62058ae996fd.png)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 5, 2022, 12:10pm UTC](https://discourse.slicer.org/t/how-to-select-an-existing-roi-in-localtreshold-of-segmenteditor-with-code/25100/3 "2022-09-05T12:10:13Z")

</div>

It seems that thw. ROI is not stored in MRML, but it is [directly read from the GUI](https://github.com/lassoan/SlicerSegmentEditorExtraEffects/blob/719b242faf301c899fb8ec7a448440ae54ca762c/SegmentEditorLocalThreshold/SegmentEditorLocalThresholdLib/SegmentEditorEffect.py#L366). 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!

---

<div class="post-metadata">

**Author:** ![KKLOVECOCO](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kklovecoco/32/16521_2.png) [@KKLOVECOCO](https://discourse.slicer.org/u/KKLOVECOCO)\
**Post date:** [September 5, 2022, 12:24pm UTC](https://discourse.slicer.org/t/how-to-select-an-existing-roi-in-localtreshold-of-segmenteditor-with-code/25100/4 "2022-09-05T12:24:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KKLOVECOCO](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kklovecoco/32/16521_2.png) [@KKLOVECOCO](https://discourse.slicer.org/u/KKLOVECOCO)\
**Post date:** [September 5, 2022, 3:09pm UTC](https://discourse.slicer.org/t/how-to-select-an-existing-roi-in-localtreshold-of-segmenteditor-with-code/25100/5 "2022-09-05T15:09:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chir.set](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chir.set/32/66982_2.png) [@chir.set](https://discourse.slicer.org/u/chir.set)\
**Post date:** [September 5, 2022, 7:42pm UTC](https://discourse.slicer.org/t/how-to-select-an-existing-roi-in-localtreshold-of-segmenteditor-with-code/25100/6 "2022-09-05T19:42:37Z")

</div>

> [@KKLOVECOCO](#):
>
> tell me how to define “effectWidget”

Sorry to interfere.

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

```auto
# 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.
"""

```

---

<div class="post-metadata">

**Author:** ![KKLOVECOCO](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kklovecoco/32/16521_2.png) [@KKLOVECOCO](https://discourse.slicer.org/u/KKLOVECOCO)\
**Post date:** [September 6, 2022, 5:30am UTC](https://discourse.slicer.org/t/how-to-select-an-existing-roi-in-localtreshold-of-segmenteditor-with-code/25100/7 "2022-09-06T05:30:15Z")

</div>

Final solution for the parameter setting section:

```auto
# 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

---

<div class="post-metadata">

**Author:** ![KKLOVECOCO](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kklovecoco/32/16521_2.png) [@KKLOVECOCO](https://discourse.slicer.org/u/KKLOVECOCO)\
**Post date:** [September 6, 2022, 5:32am UTC](https://discourse.slicer.org/t/how-to-select-an-existing-roi-in-localtreshold-of-segmenteditor-with-code/25100/8 "2022-09-06T05:32:31Z")

</div>

This solution is useful, thanks a lot 😄

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 6, 2022, 3:51pm UTC](https://discourse.slicer.org/t/how-to-select-an-existing-roi-in-localtreshold-of-segmenteditor-with-code/25100/9 "2022-09-06T15:51:51Z")

</div>

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