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
if not segmentationNode.GetParentTransformNode() is None:
AttributeError: 'NoneType' object has no attribute 'GetParentTransformNode'
My code is below:
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.