# Retrieving SegmentationStatistics from Large Datasets Using 3D Slicer API

**URL:** https://discourse.slicer.org/t/retrieving-segmentationstatistics-from-large-datasets-using-3d-slicer-api/37924
**Category:** Development
**Tags:** statistics, segmentations, segment-editor, api, segmentation, python, 3d-model
**Created:** [August 17, 2024, 6:05am UTC](https://discourse.slicer.org/t/retrieving-segmentationstatistics-from-large-datasets-using-3d-slicer-api/37924 "2024-08-17T06:05:22Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![JoshOnichino](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/joshonichino/32/77651_2.png) [@JoshOnichino](https://discourse.slicer.org/u/JoshOnichino)
#### Post date: [August 17, 2024, 6:05am UTC](https://discourse.slicer.org/t/retrieving-segmentationstatistics-from-large-datasets-using-3d-slicer-api/37924/1 "2024-08-17T06:05:22Z")

</div>

Hello! I a healthcare researcher working on medical image segmentation. I am hoping to automate the retrieval of SegmentationStatistics tables using 3D Slicer’s Python API. However, when I was developing my program, I ran into an issue with the code below.

```
volume_path = os.path.join(input_directory, filename, f'{filename}.nrrd')
segmentation_path = os.path.join(input_directory, filename, f'{filename}_corrected.seg.nrrd')

volume_node = slicer.util.loadVolume(volume_path)
segmentation_node = slicer.util.loadSegmentation(segmentation_path)

segmentStatisticsLogic = SegmentStatistics.SegmentStatisticsLogic() 
segmentStatisticsLogic.getParameterNode().SetNodeReferenceID("Segmentation", segmentation_node.GetID())
segmentStatisticsLogic.getParameterNode().SetNodeReferenceID("ScalarVolume", volume_node.GetID())

segmentStatisticsLogic.computeStatistics()

```

When I run the above, the final line of code throws an Attribute Error  
(AttributeError: ‘NoneType’ object has no attribute ‘GetParentTransformNode’). I looked into the API source code to try to find the root of the error and found that the error comes from the beginning of the computeStatistics method.

```
def computeStatistics(self):
    """Compute statistical measures for all (visible) segments"""
    self.reset()

    segmentationNode = slicer.mrmlScene.GetNodeByID(self.getParameterNode().GetParameter("Segmentation"))
    transformedSegmentationNode = None
    try:
        if not segmentationNode.GetParentTransformNode() is None:
            ...

```

It seems that the segmentationNode is None, but I am not sure why. Is there an error with the way that I loaded the files? Or is there something else I am not considering?

I am new to 3D Slicer (and segmentation, at large) so any insight into this would be a huge help! Thank you very much in advance for your time and consideration and take care.

---

<div class="post-metadata">

### Author: ![chir.set](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chir.set/32/66982_2.png) [@chir.set](https://discourse.slicer.org/u/chir.set)
#### Post date: [August 17, 2024, 8:14am UTC](https://discourse.slicer.org/t/retrieving-segmentationstatistics-from-large-datasets-using-3d-slicer-api/37924/2 "2024-08-17T08:14:15Z")

</div>

> [@JoshOnichino](#):
>
> `self.getParameterNode().GetParameter("Segmentation")`

It seems that the parameter node of your class does not have a parameter named “Segmentation”. You may be confusing with the parameter node of segmentStatisticsLogic.

---

<div class="post-metadata">

### Author: ![JoshOnichino](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/joshonichino/32/77651_2.png) [@JoshOnichino](https://discourse.slicer.org/u/JoshOnichino)
#### Post date: [August 17, 2024, 3:23pm UTC](https://discourse.slicer.org/t/retrieving-segmentationstatistics-from-large-datasets-using-3d-slicer-api/37924/3 "2024-08-17T15:23:10Z")

</div>

Thank you very much for your response! I took a dive into the source code ([Slicer/Modules/Scripted/SegmentStatistics/SegmentStatistics.py at main · Slicer/Slicer · GitHub](https://github.com/Slicer/Slicer/blob/main/Modules/Scripted/SegmentStatistics/SegmentStatistics.py#L355)) and found out that I was indeed confusing the logic.

Essentially, the error came from a mistake in the way the parameters belonging to the node were set.

Incorrect Code:

```auto
segmentStatisticsLogic.getParameterNode().SetNodeReferenceID("Segmentation", segmentation_node.GetID())
segmentStatisticsLogic.getParameterNode().SetNodeReferenceID("ScalarVolume", volume_node.GetID())

```

Correct Code:

```auto
segmentStatisticsLogic.getParameterNode().SetParameter("Segmentation", segmentation_node.GetID())            
segmentStatisticsLogic.getParameterNode().SetParameter("ScalarVolume", volume_node.GetID())

```

So, SetParameter is the correct method to use since I could not find SetNodeReferenceID as a method for SegmentStatisticsLogic in the source code linked above.

Thanks again and take care!
