# Finding corresponding segments in segmentations

**URL:** <https://discourse.slicer.org/t/finding-corresponding-segments-in-segmentations/4055>\
**Category:** Support\
**Tags:** segmentation\
**Created:** [September 10, 2018, 8:23pm UTC](https://discourse.slicer.org/t/finding-corresponding-segments-in-segmentations/4055 "2018-09-10T20:23:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![phcerdan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/phcerdan/32/1559_2.png) [@phcerdan](https://discourse.slicer.org/u/phcerdan)\
**Post date:** [September 10, 2018, 8:23pm UTC](https://discourse.slicer.org/t/finding-corresponding-segments-in-segmentations/4055/1 "2018-09-10T20:23:19Z")

</div>

Hi, I haven’t found a way to change the `segmentId` on a `SegmentationNode`.  
I can change the name of a segment, but not the ID.

```auto
segmentationNode.GetSegmentation().GetSegment('a_segment_id').SetName('segment_name')

```

Just wondering if I am missing some API, or it is just not possible.

Thanks!

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [September 10, 2018, 8:38pm UTC](https://discourse.slicer.org/t/finding-corresponding-segments-in-segmentations/4055/2 "2018-09-10T20:38:21Z")

</div>

I know there are calls from the widget:

```python
a=slicer.modules.segmenteditor.widgetRepresentation().self().editor
a.setSegmentationNodeID("words")
# or
a.setCurrentSegmentId("words")
```

---

<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:** [September 10, 2018, 8:42pm UTC](https://discourse.slicer.org/t/finding-corresponding-segments-in-segmentations/4055/3 "2018-09-10T20:42:02Z")

</div>

Why do you want to change the segment ID? Those are just identifiers that do not show up on the UI, they are just strings to uniquely identify the segments. The human readable identifier of the segments are their names. If you really want to, you can [add a segment with an ID specified](https://github.com/Slicer/Slicer/blob/master/Libs/vtkSegmentationCore/vtkSegmentation.h#L204).

@jamesobutler As far as I understand he wants to set the ID for the segment, not the selected segment in Segment Editor.

---

<div class="post-metadata">

**Author:** ![phcerdan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/phcerdan/32/1559_2.png) [@phcerdan](https://discourse.slicer.org/u/phcerdan)\
**Post date:** [September 10, 2018, 9:01pm UTC](https://discourse.slicer.org/t/finding-corresponding-segments-in-segmentations/4055/4 "2018-09-10T21:01:49Z")

</div>

My logic was using `segmentId` to compare segments (with same ID) between different subjects (each one represented with a SegmentationNode).

Creating a segmentation from a label map always give numerical segmentIds, usually `1, 2, ...`. However, when getting a segmentation from a model, the segmentId is the name of the model…

I wanted to change the segmentId generated from the model to be `1`, for easy comparison with label maps.

But I can change my logic to use the segment name instead of ID, which makes more sense. 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:** [September 10, 2018, 9:07pm UTC](https://discourse.slicer.org/t/finding-corresponding-segments-in-segmentations/4055/5 "2018-09-10T21:07:46Z")

</div>

Segment name could work for sure, however if you want to find corresponding segments, then I’d suggest making use of the [terminologies feature](https://www.slicer.org/wiki/Documentation/Nightly/Modules/Terminologies).  
Once you select a terminology for a segment, it fills a [tag](https://github.com/Slicer/Slicer/blob/master/Libs/vtkSegmentationCore/vtkSegment.h#L52) in the segment. You can then compare the values of these tags. The advantage of using terminologies is that it relies on a standard dictionary, which is unambiguous, and there is no room for human error when inputing the name for example.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 10, 2018, 9:44pm UTC](https://discourse.slicer.org/t/finding-corresponding-segments-in-segmentations/4055/6 "2018-09-10T21:44:35Z")

</div>

Changing segment ID of a segment that is already added to a segmentation node would cause many issues. When you create a segment but it is not yet added to the segmentation then you can still set the ID. However, for the described purpose teeminology seems to be a much better choice.

---

<div class="post-metadata">

**Author:** ![phcerdan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/phcerdan/32/1559_2.png) [@phcerdan](https://discourse.slicer.org/u/phcerdan)\
**Post date:** [September 10, 2018, 10:02pm UTC](https://discourse.slicer.org/t/finding-corresponding-segments-in-segmentations/4055/7 "2018-09-10T22:02:47Z")

</div>

That sounds good! However, in my testing, it seems that not all the public members of vtkSegment are wrapped in python. Especially missing [GetTags](https://github.com/Slicer/Slicer/blob/master/Libs/vtkSegmentationCore/vtkSegment.h#L107) which uses a `std::map` as input.

```auto
slicer.mrmlScene.GetNodeByID('vtkMRMLSegmentationNode1').GetSegmentation().GetSegment('1')

```

Only `GetTag(std::string, std::string&)` is available, and it has a string reference, so it might be usable with `s_ref = vtk.vtk.reference()` for the second argument.

I am going to use names meanwhile, hopefully, will be enough for my needs. Thanks for the input, I will update if I find something else.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 11, 2018, 12:45am UTC](https://discourse.slicer.org/t/finding-corresponding-segments-in-segmentations/4055/8 "2018-09-11T00:45:39Z")

</div>

In Slicer you often find multiple interfaces for accessing the same features: some of them are more convenient to use from C++, while others are more accessible from Python.

Segment GetTag()/SetTag() is usable from Python, see for example [here](https://github.com/lassoan/SlicerSegmentEditorExtraEffects/blob/master/SegmentEditorSurfaceCut/SegmentEditorSurfaceCutLib/SegmentEditorEffect.py). You can find example of how to retrieve terminology for a segment [here](https://github.com/Slicer/Slicer/blob/0f339cc1d8074c4ded2f3c3f54da8f2bdb3836c6/Modules/Loadable/Segmentations/Widgets/qMRMLSegmentsTableView.cxx#L1245-L1315).

---

<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:** [September 11, 2018, 12:50pm UTC](https://discourse.slicer.org/t/finding-corresponding-segments-in-segmentations/4055/9 "2018-09-11T12:50:31Z")

</div>

If you suggest specific python-accessible functions I can add them for you if this is the issue that prevents using terminologies.
