Change segment name in python

Hi!

I’m making a module where I go through the connected components in the image (by clicking previous/next) and give them a label according to the button a user has pressed. For example, the user goes through the segmentations and assigns the correct organ to the segmentation by clicking on one of the three buttons: Liver, Brain or Other.

I want to now change the name of the segmentation according to the button that they have pressed. How do I get the segment ID that the segment editor is currently on?

I know you can get an ID or name of a segment like this:
segment = self.segmentation_node.GetSegment(segment_ID)

but I don’t know the ID/name. I just want to get the ID of the current segment that is selected in the segment editor.

Any advice will be appreciated! Thanks!

The currently selected segment is a property of the segment editor node, rather than the segmentation itself. Conceptually, the segmentation node holds the segmentation itself, while a segment editor node modifies the segmentation in a segmentation node. With this separation, it would be possible for a segmentation to be being modified by two independent segment editors, for example. So, what you need is the segment editor node that the user is selecting the current segment with. Then, you can get the segment ID of the currently selected segment and change its name with something like

newSegmentName = 'Liver'
currentSegmentID = mySegmentEditorNode.GetSelectedSegmentID()
segmentationNode = mySegmentEditorNode.GetSegmentationNode()
currentSegment = segmentationNode.GetSegmentation().GetSegment(currentSegmentID)
currentSegment.SetName(newSegmentName)

Thanks for helping me out!

At the moment I have this:

segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentEditorNode")
currentSegmentID = segmentEditorNode.GetSelectedSegmentID()
segmentationNode = segmentEditorNode.GetSegmentationNode()
currentSegment = segmentationNode.GetSegmentation().GetSegment(currentSegmentID)

But I get this error:

File "C:/Users/.../CheckSeg.py", line 306, in confirm_label_clicked
    currentSegment = segmentationNode.GetSegmentation().GetSegment(currentSegmentID)
AttributeError: 'NoneType' object has no attribute 'GetSegmentation'

What am I doing wrong?

You have created a new segment editor node, which has not been associated with a segmentation yet. Instead, what you want to do is access the segment editor node which is associated with the Slicer GUI. You can do this via

segmentEditorNode = slicer.modules.SegmentEditorWidget.editor.mrmlSegmentEditorNode()

I also learned while investigating this that as an alternative path, you can get the current segment ID and segmentation node directly from the segment editor widget via:

currentSegmentID = slicer.modules.SegmentEditorWidget.editor.currentSegmentID() 
segmentationNode = slicer.modules.SegmentEditorWidget.editor.segmentationNode()

Either way should work equivalently.

Thanks!! This worked for me :slight_smile:

1 Like