# Loading terminology file when editing a segmentation

**URL:** <https://discourse.slicer.org/t/loading-terminology-file-when-editing-a-segmentation/23061>\
**Category:** Support\
**Tags:** segmentation\
**Created:** [April 20, 2022, 9:01pm UTC](https://discourse.slicer.org/t/loading-terminology-file-when-editing-a-segmentation/23061 "2022-04-20T21:01:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DeepaKrishnaswamy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/deepakrishnaswamy/32/8913_2.png) [@DeepaKrishnaswamy](https://discourse.slicer.org/u/DeepaKrishnaswamy)\
**Post date:** [April 20, 2022, 9:01pm UTC](https://discourse.slicer.org/t/loading-terminology-file-when-editing-a-segmentation/23061/1 "2022-04-20T21:01:07Z")

</div>

Hi,

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.

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/e/8e8cb6c1dc9618c0aec401dae6dc2b7ef55b1e30.png)

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.

![Screenshot 2022-04-20 165636](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/4/0493dde77e52339800a87ca6d3cf354642916aa4.png)

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.

Thanks!

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [April 22, 2022, 11:52am UTC](https://discourse.slicer.org/t/loading-terminology-file-when-editing-a-segmentation/23061/2 "2022-04-22T11:52:02Z")

</div>

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

```auto
  #------------------------------------------------------------------------------
  @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:

```auto
      # 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))

```

---

<div class="post-metadata">

**Author:** ![DeepaKrishnaswamy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/deepakrishnaswamy/32/8913_2.png) [@DeepaKrishnaswamy](https://discourse.slicer.org/u/DeepaKrishnaswamy)\
**Post date:** [May 6, 2022, 8:31pm UTC](https://discourse.slicer.org/t/loading-terminology-file-when-editing-a-segmentation/23061/3 "2022-05-06T20:31:27Z")

</div>

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.

Thank you for your help!
