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

**URL:** <https://discourse.slicer.org/t/how-do-i-get-the-id-number-from-subject-hierarchy-information-for-a-segmentation/16780>\
**Category:** Development\
**Tags:** python\
**Created:** [March 26, 2021, 4:45pm UTC](https://discourse.slicer.org/t/how-do-i-get-the-id-number-from-subject-hierarchy-information-for-a-segmentation/16780 "2021-03-26T16:45:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![akshay\_pillai](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/akshay_pillai/32/8301_2.png) [@akshay\_pillai](https://discourse.slicer.org/u/akshay_pillai)\
**Post date:** [March 26, 2021, 4:45pm UTC](https://discourse.slicer.org/t/how-do-i-get-the-id-number-from-subject-hierarchy-information-for-a-segmentation/16780/1 "2021-03-26T16:45:05Z")

</div>

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?

 ![id](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/a/4a42a98c2916843fbab780c502793e83285c7aa4.png)

---

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [March 26, 2021, 5:09pm UTC](https://discourse.slicer.org/t/how-do-i-get-the-id-number-from-subject-hierarchy-information-for-a-segmentation/16780/2 "2021-03-26T17:09:21Z")

</div>

Hi. You can do it like this:

```auto
shNode = slicer.mrmlScene.GetSubjectHierarchyNode()
segmentationItemID = shNode.GetItemByName("Segmentation")

```

---

<div class="post-metadata">

**Author:** ![akshay\_pillai](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/akshay_pillai/32/8301_2.png) [@akshay\_pillai](https://discourse.slicer.org/u/akshay_pillai)\
**Post date:** [March 26, 2021, 5:12pm UTC](https://discourse.slicer.org/t/how-do-i-get-the-id-number-from-subject-hierarchy-information-for-a-segmentation/16780/3 "2021-03-26T17:12:54Z")

</div>

> [@mau\_igna\_06](#):
>
> ```auto
> shNode = slicer.mrmlScene.GetSubjectHierarchyNode()
> segmentationItemID = shNode.GetItemByName("Segmentation")
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [March 26, 2021, 5:59pm UTC](https://discourse.slicer.org/t/how-do-i-get-the-id-number-from-subject-hierarchy-information-for-a-segmentation/16780/4 "2021-03-26T17:59:31Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![koeglfryderyk](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/koeglfryderyk/32/14638_2.png) [@koeglfryderyk](https://discourse.slicer.org/u/koeglfryderyk)\
**Post date:** [May 9, 2022, 9:21pm UTC](https://discourse.slicer.org/t/how-do-i-get-the-id-number-from-subject-hierarchy-information-for-a-segmentation/16780/5 "2022-05-09T21:21:33Z")

</div>

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?

---

<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:** [May 10, 2022, 4:22am UTC](https://discourse.slicer.org/t/how-do-i-get-the-id-number-from-subject-hierarchy-information-for-a-segmentation/16780/6 "2022-05-10T04:22:50Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![Thibaut\_Faller](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/thibaut_faller/32/9663_2.png) [@Thibaut\_Faller](https://discourse.slicer.org/u/Thibaut_Faller)\
**Post date:** [August 2, 2022, 3:15pm UTC](https://discourse.slicer.org/t/how-do-i-get-the-id-number-from-subject-hierarchy-information-for-a-segmentation/16780/7 "2022-08-02T15:15:26Z")

</div>

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

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

```
