Using segmentstatistics module in python script

Dear all,
How can I call and use segmentstatistics module in a simple way. I tried this code but I get an error

    volSeg  =  slicer.util.getNode('Segmentation')
    volInput =  self.inputSelector.currentNode() 

    segStat = slicer.modules.segmentstatistics.widgetRepresentation().self()
    segStat.grayscaleNode = volInput
    segStat.labelNode  = volSeg   
    segTbl = slicer.util.getNode('Segmentation statistics*')

This is the error:

Slicer-4.8.0/lib/Slicer-4.8/qt-scripted-modules/", line 176, in onApply
self.logic.getParameterNode().SetParameter("Segmentation", self.segmentationSelector.currentNode().GetID()) AttributeError: 'NoneType' object has no attribute 'GetID'

When you need to use a Slicer module from another module, you must almost always use the module’s logic directly, instead of manipulating the other module’s user interface.

In general, you can find example of how to use the module logic in module tests.

For example, you can use Segment statistics module like this:

segmentationNode  =  slicer.util.getNode('Segmentation')
masterVolumeNode =  slicer.util.getNode('MRHead')
resultsTableNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLTableNode')

import SegmentStatistics
segStatLogic = SegmentStatistics.SegmentStatisticsLogic()
segStatLogic.getParameterNode().SetParameter("Segmentation", segmentationNode.GetID())
segStatLogic.getParameterNode().SetParameter("ScalarVolume", masterVolumeNode.GetID())
1 Like

Thanks a lot, that was helpful. I didn’t know about the testing modules. The code above works for me with these modifications:

    resultsTableNode = slicer.vtkMRMLTableNode()

Creates a node and adds to the scene, similarly to your version:

resultsTableNode = slicer.vtkMRMLTableNode()

However, AddNewNodeByClass has the advantage that it is shorter and it creates nodes taking into account default node properties that are defined in the scene (you can override default properties for any nodes, such as default colors, file save formats, etc).

Hi Andras,
Could you please tell me how to do it for segment mesher. what parameters need to be set for segment mesher to run it using script.

Thank you

Saima Safdar

Hi Andras,
I am writing a scriopt for segment mesher . how to use it using scripting. I have tried below. Please guide. I do not know the complete list of parameters to set for segment mesher.

Creating importing segmentation node
[success, patientMask] = slicer.util.loadLabelVolume(‘D:/Boston Data/Patient Mask Label.nrrd’, returnNode = True)
segmentationNode = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLSegmentationNode”)

segMesh = slicer.modules.segmentmesher
//creating output model node
model = slicer.vtkMRMLModelNode()

//setting parameters
parameters= {}
parameters[‘input Segmentation’] = segmentationNode.GetID()
parameters[‘output model’] = model.GetID()

//Runing CLi

Please help.

Saima Safdar