# Using segmentstatistics module in python script

**URL:** https://discourse.slicer.org/t/using-segmentstatistics-module-in-python-script/2140
**Category:** Development
**Created:** [February 21, 2018, 2:29pm UTC](https://discourse.slicer.org/t/using-segmentstatistics-module-in-python-script/2140 "2018-02-21T14:29:18Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![brhoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/brhoom/32/1228_2.png) [@brhoom](https://discourse.slicer.org/u/brhoom)
#### Post date: [February 21, 2018, 2:29pm UTC](https://discourse.slicer.org/t/using-segmentstatistics-module-in-python-script/2140/1 "2018-02-21T14:29:18Z")

</div>

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'
```

---

<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: [February 21, 2018, 5:34pm UTC](https://discourse.slicer.org/t/using-segmentstatistics-module-in-python-script/2140/2 "2018-02-21T17:34:25Z")

</div>

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](https://github.com/Slicer/Slicer/blob/69358596167cbc99d39acfa98a5cfa2a1e4042dd/Modules/Scripted/SegmentStatistics/SegmentStatistics.py#L639-L857).

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)
```

---

<div class="post-metadata">

### Author: ![brhoom](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/brhoom/32/1228_2.png) [@brhoom](https://discourse.slicer.org/u/brhoom)
#### Post date: [February 22, 2018, 8:59pm UTC](https://discourse.slicer.org/t/using-segmentstatistics-module-in-python-script/2140/3 "2018-02-22T20:59:09Z")

</div>

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)
```

---

<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: [February 22, 2018, 9:17pm UTC](https://discourse.slicer.org/t/using-segmentstatistics-module-in-python-script/2140/4 "2018-02-22T21:17:25Z")

</div>

> [@lassoan](#):
>
> resultsTableNode = slicer.mrmlScene.AddNewNodeByClass(‘vtkMRMLTableNode’)

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](http://apidocs.slicer.org/master/classvtkMRMLScene.html#ae302c5ed4aabb2910bc35dcc9aa2513f) that are defined in the scene (you can override default properties for any nodes, such as default colors, file save formats, etc).

---

<div class="post-metadata">

### Author: ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)
#### Post date: [October 26, 2018, 12:26pm UTC](https://discourse.slicer.org/t/using-segmentstatistics-module-in-python-script/2140/5 "2018-10-26T12:26:03Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)
#### Post date: [October 30, 2018, 5:18am UTC](https://discourse.slicer.org/t/using-segmentstatistics-module-in-python-script/2140/6 "2018-10-30T05:18:22Z")

</div>

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
