# Calculate Statistics with Python takes longer then with 3D Slicer GUI

**URL:** https://discourse.slicer.org/t/calculate-statistics-with-python-takes-longer-then-with-3d-slicer-gui/34961
**Category:** Support
**Tags:** segmentation, python, scripted
**Created:** [March 18, 2024, 6:23pm UTC](https://discourse.slicer.org/t/calculate-statistics-with-python-takes-longer-then-with-3d-slicer-gui/34961 "2024-03-18T18:23:30Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![thorbenjt](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@thorbenjt](https://discourse.slicer.org/u/thorbenjt)
#### Post date: [March 18, 2024, 6:23pm UTC](https://discourse.slicer.org/t/calculate-statistics-with-python-takes-longer-then-with-3d-slicer-gui/34961/1 "2024-03-18T18:23:30Z")

</div>

Hi there,  
i am currently working on a script the automates tresholding and in addition splitting the segments into islands.  
this works just as fine as if i would do it per hand in the 3D Slicer GUI.  
The critical part is the calculation of the Statistics. As stated in the title, it takes up to 2 mins to calculate the statistics when using the code, but only 17 seconds when using ther 3D Slicer GUI.

Here is the code which is based on code snippets i found in previous discussions:  
node=slicer.util.loadVolume(filename)

```
segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode") # Creates Empty Segmentation
segmentationNode.CreateDefaultDisplayNodes() # only needed for display
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(node)
addedSegmentID = segmentationNode.GetSegmentation().AddEmptySegment("skin") #add segment

# Create segment editor to get access to effects
segmentEditorWidget = slicer.qMRMLSegmentEditorWidget() #-> [Qt] QLayout::addChildLayout: layout "" already has a parent
segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentEditorNode")
segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
segmentEditorWidget.setSegmentationNode(segmentationNode) #->[Qt] ctkSliderWidget::setSingleStep() 0 is out of bounds. 0 100 1
segmentEditorWidget.setSourceVolumeNode(node)

# Thresholding
segmentEditorWidget.setActiveEffectByName("Threshold")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("MinimumThreshold","125.00")
effect.setParameter("MaximumThreshold","255.00")
effect.self().onApply()

segmentEditorWidget.setActiveEffectByName("Islands")
effect = segmentEditorWidget.activeEffect()
effect.setParameter("MinimumSize","25")
effect.setParameter("Operation","SPLIT_ISLANDS_TO_SEGMENTS")
effect.self().onApply()
segmentEditorWidget.setActiveEffect(None)
segmentEditorWidget = None

segmentationNode = slicer.util.getNode('Segmentation')

resultsTableNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLTableNode')

import SegmentStatistics
segStatLogic = SegmentStatistics.SegmentStatisticsLogic()
segStatLogic.getParameterNode().SetParameter("Segmentation", segmentationNode.GetID())
segStatLogic.getParameterNode().SetParameter("LabelmapSegmentStatisticsPlugin.enabled","True")
segStatLogic.getParameterNode().SetParameter("ScalarVolumeSegmentStatisticsPlugin","False")
segStatLogic.getParameterNode().SetParameter("CloseSurfaceStatisticsPlugin","False")
segStatLogic.computeStatistics()
stats = segStatLogic.getStatistics()

```

This is the file i want to segment:

 ![test001](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/9/490750d96a45de627d31778d2a7cd9f680ca6eab.jpeg)

Thanks in advance for any help 🙂
