Hi all,
I’m wondering how to script some simple steps from the Segment Editor. I know that there are a few recipes from Andras and others in this forum, which are great, thanks for the continuous support.
In one of the recipes, there is a good example for thresholding:
Now, the effect I wanna use programmatically is “Logical operators”, specifically I wanna create a hull, i.e. one segment that sums up all the other segments (in other words, a logical_or of all segments contained in the segmentation node), which is easy to automate. The steps I need to do are:
Create a new segment (via node_seg.GetSegmentation().AddSegment(‘hull’))
Copy the first segment into ‘hull’
Add all other segments into ‘hull’.
For this, and other simple steps… Is there a documentation on all the parameters of all effects.
I was trying something like:
It would be nice to create separate documentation for these - contributions are welcome!
@jcfr, do you have any suggestions for documenting segment editor effect parameters? It would be nice if documentation would be available at the same place for C++ and Python effects. I could create a new markdown file in developer_guide and describe parameters of the effects there, but it would be a bit nicer if the description could be in each effect’s .py or .h file. Could ReadTheDocs fetch documentation from segment editor effect source files (currently Developer guide does not seem to be able to get anything from there).
Until separate documentation is written, then open source code is the documentation. You can find the relevant file by Google search. For example first hit when searching for github slicer Logical operators takes you to SegmentEditorLogicalEffect.py. For all Segment Editor effects, setMRMLDefaults method contains the list of parameters that the effect uses. If there are constants then they are typically defined at the bottom of the file.
Hi Andras,
few weeks ago, I found an alternative way to solve my problem, but now I had to come back to using segment editor effects, in more detail. This time I managed to solve a few problems, one of them was how to use the “logical operators” effect programmatically. You already provide a lot of examples for scripted effects here, but there is no example for the logical effects. Actually it’s simple, but I needed to sift through the source code a bit. Maybe this example will help others to save some time.
# Run segment editor effects from script
# Prerequisites 1/2:
# Create segment editor widget and node to get scripted access to effects
segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentEditorNode")
segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
# Prerequisites 2/2:
# nvol ... the reference volume node for the segmentation
# nseg ... the segmentation node
# 'tgtseg' is the name of the target segment, i.e. the region that is being edited
# 'srcseg' is the name of the source segment, i.e. the region from which information is being "transferred" by the logical operator
# Setting up the nodes to be edited
segmentEditorWidget.setSegmentationNode(nseg)
segmentEditorWidget.setMasterVolumeNode(nvol)
# Set overwrite mode: 0/1/2 -> overwrite all/visible/none
segmentEditorNode.SetOverwriteMode(2) # i.e. "allow overlap" in UI
# Get the segment IDs
segid_tgt = nseg.GetSegmentation().GetSegmentIdBySegmentName('tgtseg')
segid_src = nseg.GetSegmentation().GetSegmentIdBySegmentName('srcseg')
# Running the effect:
# The logical operation is set via strings
# 'COPY', 'UNION', 'INTERSECT', 'SUBTRACT', 'INVERT', 'CLEAR', 'FILL'
# (see: https://apidocs.slicer.org/master/SegmentEditorLogicalEffect_8py_source.html)
segmentEditorNode.SetSelectedSegmentID(segid_tgt)
segmentEditorWidget.setActiveEffectByName("Logical operators")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("Operation","COPY") # change the operation here
effect.setParameter("ModifierSegmentID",segid_src)
effect.self().onApply()