# Cloning a SegmentationNode segment in Python

**URL:** <https://discourse.slicer.org/t/cloning-a-segmentationnode-segment-in-python/29035>\
**Category:** Development\
**Tags:** segmentation\
**Created:** [April 21, 2023, 12:11am UTC](https://discourse.slicer.org/t/cloning-a-segmentationnode-segment-in-python/29035 "2023-04-21T00:11:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pearsonm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pearsonm/32/18514_2.png) [@pearsonm](https://discourse.slicer.org/u/pearsonm)\
**Post date:** [April 21, 2023, 12:11am UTC](https://discourse.slicer.org/t/cloning-a-segmentationnode-segment-in-python/29035/1 "2023-04-21T00:11:28Z")

</div>

I am working on a module where I make a make a binary labelmap on a 2d view with the scissors rectangle tool. The next step is to cut the rectangle into 3 regions for further processing.

My first approach was to add 3 segments with AddEmptySegment and then use SetExtent to resize the rectangles. This is not being reflected in the segmentation node so I am obviously missing something.

I worked out that I can do this manually by right-clicking on the mask in Subject Hierarchy in the Data module and selecting Clone. I can then rename the segment and modify Extent.

Is it possible to perform the clone from Python? I looked at the SubjectHierarchy code but could not work it out.

---

<div class="post-metadata">

**Author:** ![pearsonm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pearsonm/32/18514_2.png) [@pearsonm](https://discourse.slicer.org/u/pearsonm)\
**Post date:** [April 21, 2023, 1:19am UTC](https://discourse.slicer.org/t/cloning-a-segmentationnode-segment-in-python/29035/2 "2023-04-21T01:19:22Z")

</div>

I worked it out, if the original mask is (62, 71, 69, 102, 0, 11) the upper region is

```auto
slicer.mrmlScene.GetNodeByID("vtkMRMLSegmentationNode1").GetSegmentation().AddEmptySegment("upper")
s0 = slicer.mrmlScene.GetNodeByID("vtkMRMLSegmentationNode1").GetSegmentation().GetNthSegment(0)
s1 = slicer.mrmlScene.GetNodeByID("vtkMRMLSegmentationNode1").GetSegmentation().GetNthSegment(1)
s1.DeepCopy(s0)
slicer.mrmlScene.GetNodeByID("vtkMRMLSegmentationNode1").GetSegmentation().GetNthSegment(1).SetName("upper")
slicer.mrmlScene.GetNodeByID("vtkMRMLSegmentationNode1").GetSegmentation().GetNthSegment(1).GetRepresentation("Binary labelmap").SetExtent([62, 71, 69, 80, 0, 11])

```

---

<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:** [May 4, 2023, 8:51am UTC](https://discourse.slicer.org/t/cloning-a-segmentationnode-segment-in-python/29035/3 "2023-05-04T08:51:10Z")

</div>

You can clone an item via code like this

`clonedItemID = slicer.modules.subjecthierarchy.logic().CloneSubjectHierarchyItem(shNode, itemID)`

---

<div class="post-metadata">

**Author:** ![pearsonm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pearsonm/32/18514_2.png) [@pearsonm](https://discourse.slicer.org/u/pearsonm)\
**Post date:** [May 4, 2023, 10:39am UTC](https://discourse.slicer.org/t/cloning-a-segmentationnode-segment-in-python/29035/4 "2023-05-04T10:39:37Z")

</div>

Thanks, that is a much easier way to do it.

---

<div class="post-metadata">

**Author:** ![pearsonm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pearsonm/32/18514_2.png) [@pearsonm](https://discourse.slicer.org/u/pearsonm)\
**Post date:** [May 17, 2023, 4:23am UTC](https://discourse.slicer.org/t/cloning-a-segmentationnode-segment-in-python/29035/5 "2023-05-17T04:23:29Z")

</div>

I found a problem using the suggested method on a segmentation. If you load sample data and create a segmentation with Segment Editor the clone is not valid

```auto
import SampleData
node = SampleData.SampleDataLogic().downloadMRHead()

```

Switch to Segment Editor and create a segmentation

```auto
segmentationNode = slicer.mrmlScene.GetNodeByID("vtkMRMLSegmentationNode1")
shNode = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(slicer.mrmlScene)
segmentationID = shNode.GetItemByDataNode(segmentationNode)
segmentID = shNode.GetItemChildWithName(segmentationID, "Segment_1")
clonedItemID = slicer.modules.subjecthierarchy.logic().CloneSubjectHierarchyItem(shNode, segmentID)

```

The result is Segment\_1 Copy but it is not a valid segment

 ![step2](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/b/5bae7474aa03e3e3d6986f8a23dddfdb48f68f8c.png)

If I right-click on Segment\_1 and choose Clone then the clone is a valid copy. Should the 2 methods be equivalent?
