How do I get the ID number from subject hierarchy information for a segmentation?

I want to get the ID number present in the subject hierarchy information. I want to do that to access the children segments through the ID number. Heres a picture of what I want to get using python. How can I do this?

Hi. You can do it like this:

shNode = slicer.mrmlScene.GetSubjectHierarchyNode()
segmentationItemID = shNode.GetItemByName("Segmentation")
1 Like

Thanks, that’s great. How can I do this for the children?

There is a segmentation data node, there isn’t a segment data node for each segment
You can access segments from a segmentation like this:

segmentationDataNode = shNode.GetItemDataNode(segmentationItemID)

segmentIDinPosition0 = segmentationDataNode.GetSegmentation().GetNthSegmentID(0)
segmentInPosition0 = segmentationDataNode.GetSegmentation().GetSegment(segmentIDinPosition0)

upperAirwaySegmentID = segmentationDataNode.GetSegmentation().GetSegmentIdBySegmentName('upper_airway')
upperAirwaySegment = segmentationDataNode.GetSegmentation().GetSegment(upperAirwaySegmentID)
1 Like

Is there a way of doing this by vtk ID, so that the name of the segmentation doesn’t have to be used, as it can be ambiguous?

Yes, sure, the segment ID is stored in the segmentID attribute of the subject hierarchy item:

1 Like

You may use GetItemAttribute method to get the segmentID from the vtk ID:

shNode = slicer.mrmlScene.GetSubjectHierarchyNode()
segmentID = shNode.GetItemAttribute(vtkID, "segmentID")