I am working on a module that uses a terminology file to define regions/colors. When I first add a segment to a node, the terminology file I’ve specified is automatically chosen.
I then draw a segment and save the file. When I load the file back in, and go to add an another segment, the terminology file is not longer the one that was automatically set. Instead, it defaults to the name of the segmentation node, and I have to select the terminology file from the dropdown menu.
Is there a way for the terminology file I want to be automatically chosen with the additional segment? I did see that once I select the file I want, any additional segment I add automatically uses that file.
This is a use case that is not perfectly handled as it is in the latest Slicer. I had to do the same (if I understand your problem correctly), and decided to do this workaround, as I didn’t have time starting to fix the terminology feature (which is quite complex so I expect it a relatively large work if we need to consider and test every use case):
#------------------------------------------------------------------------------
@vtk.calldata_type(vtk.VTK_STRING)
def onSegmentAdded(self, caller, eventId, callData=None):
"""
Called when a segment is added.
Set custom terminology context to new segment
:param VTKObject caller: The VTKObject which initiated the callback (the segmentation node)
:param int eventId: ID of the event which was called.
Note: The event argument seems to be always 'NoEvent' regardless which event is observed
:param int callData: Event data passed along with the callback, containing the segment ID
:return void:
"""
del eventId # to eliminate PyLint complaint about an unused argument
segmentationNode = caller
segmentID = callData
segment = segmentationNode.GetSegmentation().GetSegment(segmentID)
segment.SetTag(slicer.vtkSegment.GetTerminologyEntryTagName(),
'Custom segmentation category and type~Custom^95759010^Segmentation~^^~^^~Anatomic codes - DICOM master list~^^~^^') # This you'll need to edit to match the metadata of your terminology
And of course you have to set up observation:
# Observe segment added event so that the terminology context can be set instead of the default
self.segmentationNodeObserverTags.append(segmentationNode.AddObserver(
slicer.vtkSegmentation.SegmentAdded, self.moduleWidget.onSegmentAdded))
I actually realized that the issue is when I load a file that contains a segment, and not with adding an additional segment. Now when I load the file, for each existing segment I assign the terminology entry I want. When I go to add a new segment, the terminology entry I want is automatically selected.