# How to use Segment Editor effects from python script?

**URL:** <https://discourse.slicer.org/t/how-to-use-segment-editor-effects-from-python-script/20815>\
**Category:** Support\
**Tags:** segmentation, python\
**Created:** [November 28, 2021, 12:54pm UTC](https://discourse.slicer.org/t/how-to-use-segment-editor-effects-from-python-script/20815 "2021-11-28T12:54:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![utkgl](https://avatars.discourse-cdn.com/v4/letter/u/c68b51/32.png) [@utkgl](https://discourse.slicer.org/u/utkgl)\
**Post date:** [November 28, 2021, 12:54pm UTC](https://discourse.slicer.org/t/how-to-use-segment-editor-effects-from-python-script/20815/1 "2021-11-28T12:54:49Z")

</div>

Hi!  
I am trying to automatize some series of lung segmentation steps on 3D Slicer with using python scripts.  
Currently I can resample my file using;

```auto
volumeNode = slicer.util.loadVolume(file)
emptyVolumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLScalarVolumeNode", "output volume")
slicer.util.mainWindow().moduleSelector().selectModule('ResampleScalarVolume')
              
params = {}
params["InputVolume"] = volumeNode
params["OutputVolume"] = emptyVolumeNode
params["outputPixelSpacing"] = (2,2,2)
              
resampleModule = slicer.modules.resamplescalarvolume
cliNode = slicer.cli.runSync(resampleModule,None,params)

```

Or add empty segmentations using;

```auto
slicer.util.mainWindow().moduleSelector().selectModule('Segmentations')
segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
segmentationNode.CreateDefaultDisplayNodes()
segmentationNode. SetReferenceImageGeometryParameterFromVolumeNode(emptyVolumeNode)
leftSegmentID = segmentationNode.GetSegmentation().AddEmptySegment("left lung")
rightSegmentID = segmentationNode.GetSegmentation().AddEmptySegment("right lung")
tracheaSegmentID = segmentationNode.GetSegmentation().AddEmptySegment("trachea")	

```

Also in Segment Editor, the “Smoothing” section I can use the smoothing tool like this;

```auto
segmentEditorWidget.setActiveEffectByName("Smoothing") 
effect = segmentEditorWidget.activeEffect() 
effect.setParameter("SmoothingMethod", "CLOSING") 
effect.setParameter("KernelSizeMm", 12) 
effect.self().onApply()

```

However I am struggling to use “Threshold” and “Grow from seeds” parts. In Threshold part I want it to click the “Use for masking” button and in the Grow from seeds part I want it to first click “Initialize” then “Apply” button.

```auto
segmentEditorWidget.setActiveEffectByName("Threshold")
 effect = segmentEditorWidget.activeEffect()
effect.setParameter("MinimumThreshold","-1024")
effect.setParameter("MaximumThreshold","-500")
effect.self().onApply()

```

For example this code sets correct threshold values but clicks “apply” instead of “use for masking” button.

 ![grow from seeds](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/9/d/9d782b8be473b062afd86b19f82c510f20787542.jpeg)  
 ![threshold](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/6/7675e36d7ca93b0ea9aa0ed2b77062a17cee6008.png)

I researched the script repository but wasn’t able to find anything. I would appreciate any help. Thanks!

Yusuf Utku Gül

---

<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:** [November 28, 2021, 1:34pm UTC](https://discourse.slicer.org/t/how-to-use-segment-editor-effects-from-python-script/20815/2 "2021-11-28T13:34:32Z")

</div>

If UI simulation is an acceptable solution for you, you can get some insight [here](https://github.com/vmtk/SlicerExtension-VMTK/blob/6aeec24352244ed2bd9313605c981f8bff1a7215/FiducialCenterlineExtraction/FiducialCenterlineExtraction.py#L736). Basically, you can dig through the effect UI and locate the widgets unexposed by the effect’s API, and interact next by the widget’s API.

---

<div class="post-metadata">

**Author:** ![utkgl](https://avatars.discourse-cdn.com/v4/letter/u/c68b51/32.png) [@utkgl](https://discourse.slicer.org/u/utkgl)\
**Post date:** [November 28, 2021, 8:20pm UTC](https://discourse.slicer.org/t/how-to-use-segment-editor-effects-from-python-script/20815/3 "2021-11-28T20:20:42Z")

</div>

Thank you for the response, I am looking into it right now.

---

<div class="post-metadata">

**Author:** ![qiqi5210](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/qiqi5210/32/15920_2.png) [@qiqi5210](https://discourse.slicer.org/u/qiqi5210)\
**Post date:** [November 29, 2021, 12:51am UTC](https://discourse.slicer.org/t/how-to-use-segment-editor-effects-from-python-script/20815/4 "2021-11-29T00:51:03Z")

</div>

The initialization button for seed growth can be set as follows:

```auto
segmentEditorWidget.setActiveEffectByName("Grow form seeds")
effect = segmentEditorWidget.activeEffect()
effect.self().onPreview()

```

---

<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:** [November 30, 2021, 12:10am UTC](https://discourse.slicer.org/t/how-to-use-segment-editor-effects-from-python-script/20815/5 "2021-11-30T00:10:49Z")

</div>

> [@chir.set](#):
>
> If UI simulation is an acceptable solution for you

Simulating button clicks would make the code very fragile. All the necessary API should be available to run effects without simulating GUI events, but if it turns out that anything is missing then we can expose more functions.

There are full examples of how to perform Thresholding and Grow from seeds from Python scripts in the [script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#how-to-run-segment-editor-effects-from-a-script).

There are also some nice modules that do this, using a simple, wizard-like interface, for example the LungCTAnalyzer extension by @rbumm, which can automatically segment lungs and trachea from a few clicks that the user is guided through:

[![](https://img.youtube.com/vi/fpLxm7uAvZQ/maxresdefault.jpg "Automated lung CT segmentation and analysis for COVID-19 assessment") ](https://www.youtube.com/watch?v=fpLxm7uAvZQ)

Source code: [GitHub - rbumm/SlicerLungCTAnalyzer: This is a 3D Slicer extension for segmentation and spatial reconstruction of infiltrated, collapsed, and emphysematous areas in lung CT.](https://github.com/rbumm/SlicerLungCTAnalyzer)
