How to use Segment Editor effects from python script?

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;

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;

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;

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.

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.


threshold

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

Yusuf Utku Gül

If UI simulation is an acceptable solution for you, you can get some insight here. 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.

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

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

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

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.

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:

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.