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   
    segStat.onApply()
    segStat.onExportToTable()
    segTbl = slicer.util.getNode('Segmentation statistics*')

This is the error:

Slicer-4.8.0/lib/Slicer-4.8/qt-scripted-modules/SegmentStatistics.py", 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())
segStatLogic.getParameterNode().SetParameter("LabelmapSegmentStatisticsPlugin.enabled","False")
segStatLogic.getParameterNode().SetParameter("ScalarVolumeSegmentStatisticsPlugin.voxel_count.enabled","False")
segStatLogic.computeStatistics()
segStatLogic.exportToTable(resultsTableNode)
segStatLogic.showTable(resultsTableNode)
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()
    slicer.mrmlScene.AddNode(resultsTableNode)
    segStatLogic.exportToTable(resultsTableNode)
    segStatLogic.showTable(resultsTableNode)

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

resultsTableNode = slicer.vtkMRMLTableNode()
slicer.mrmlScene.AddNode(resultsTableNode)

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

Regards,
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ā€)
slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(patientMask,segmentationNode)

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

//setting parameters
parameters= {}
parameters[ā€˜input Segmentationā€™] = segmentationNode.GetID()
parameters[ā€˜output modelā€™] = model.GetID()

//Runing CLi
slicer.cli.runSync(segMesh,None,parameters)

Please help.

Regards,
Saima Safdar