# SegmentStatistics error in Python

**URL:** <https://discourse.slicer.org/t/segmentstatistics-error-in-python/29171>\
**Category:** Support\
**Tags:** segmentation, python\
**Created:** [April 27, 2023, 11:59am UTC](https://discourse.slicer.org/t/segmentstatistics-error-in-python/29171 "2023-04-27T11:59:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Markb](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@Markb](https://discourse.slicer.org/u/Markb)\
**Post date:** [April 27, 2023, 11:59am UTC](https://discourse.slicer.org/t/segmentstatistics-error-in-python/29171/1 "2023-04-27T11:59:00Z")

</div>

Hi. I have a bunch of segments in an RTStruct file, and a bunch of images that I want to get some statistics from. Doing this manually is a bit of a timehog, so I looked into writing a quick python script and running it using the python interactor. It’s based largely on things I found on this forum and Google, so could be very wrong, but at the moment it seems to pull through the correct segments and scalars, and generates a data table, just doesn’t populate it.

At the moment, I get an error that

```auto
if not segmentationNode.GetParentTransformNode() is None:
AttributeError: 'NoneType' object has no attribute 'GetParentTransformNode'

```

My code is below:

```auto
print("Starting scalar volume processing loop...")
import SegmentStatistics

rtstruct_node = slicer.util.getFirstNodeByName("RTSTRUCT") #get the RTStruct Node
scalar_volume_nodes = slicer.util.getNodesByClass("vtkMRMLScalarVolumeNode") #retrieve all scalar nodes

for scalar_volume_node in scalar_volume_nodes:
    if 'Dose' in scalar_volume_node.GetName(): #check if dose is in the name, as don't want to analysis the CT etc.
        scalar_volume_name = scalar_volume_node.GetName() #Get the unique name for the scalar volume
        print("Quantifying node " + scalar_volume_name)
        statistics_table_node = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLTableNode", scalar_volume_name + "_stat") #Create a new table node to hold the statistics

        for segment_index in range(rtstruct_node.GetSegmentation().GetNumberOfSegments()): #Loop through the segments and compute statistics for each segment
            segment_name = rtstruct_node.GetSegmentation().GetNthSegment(segment_index).GetName()
            segment_id = rtstruct_node.GetSegmentation().GetNthSegmentID(segment_index)
            print("Measuring segment " + segment_name + " with ID: " + segment_id)
            
            segmentStat = SegmentStatistics.SegmentStatisticsLogic()
            segmentStat.getParameterNode().SetParameter("Segmentation", segment_id)
            segmentStat.getParameterNode().SetParameter("ScalarVolume", scalar_volume_node.GetID())
            #segmentStat.getParameterNode().SetParameter("LabelmapSegmentStatisticsPlugin.enabled","False")
            #segmentStat.getParameterNode().SetParameter("ScalarVolumeSegmentStatisticsPlugin.voxel_count.enabled","False")
            segmentStat.computeStatistics()
            segmentStat.exportToTable(statistics_table_node)
            segmentStat.showTable(statistics_table_node)

```

Any help would be greatly appreciated! There aren’t many online resources (that I’ve found) and it is quite the learning curve.

---

<div class="post-metadata">

**Author:** ![pearsonm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pearsonm/32/18514_2.png) [@pearsonm](https://discourse.slicer.org/u/pearsonm)\
**Post date:** [April 27, 2023, 10:24pm UTC](https://discourse.slicer.org/t/segmentstatistics-error-in-python/29171/2 "2023-04-27T22:24:53Z")

</div>

Can you show the code where segmentationNode is defined?  
You will get this error if segmentationNode is equal to None and therefore has no attributes.

---

<div class="post-metadata">

**Author:** ![Markb](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@Markb](https://discourse.slicer.org/u/Markb)\
**Post date:** [April 28, 2023, 7:58am UTC](https://discourse.slicer.org/t/segmentstatistics-error-in-python/29171/3 "2023-04-28T07:58:45Z")

</div>

Ah, that is the entirety of the code I have, and that might be the issue. The segments are in the rtstruct file, so I thought slicer.util.getFirstNodeByName(“RTSTRUCT”) would retrieve this as the segmentation. As the print statements provided the right information back to me, I didn’t question this.

Do I need to change the first line in rtstruct\_node to something like  
`restruct_node = slicer.util.getNodesByClass("vtkMRMLSegmentationNode")`

I’m away from my computer at the moment to try this, but will give it a go later today.

Thank you
