Retrieving SegmentationStatistics from Large Datasets Using 3D Slicer API

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.

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.

2 Likes

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

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

Correct Code:

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!