# AddSegmentFromClosedSurfaceRepresentation performance

**URL:** <https://discourse.slicer.org/t/addsegmentfromclosedsurfacerepresentation-performance/30909>\
**Category:** Development\
**Tags:** segmentation, python\
**Created:** [August 1, 2023, 3:27pm UTC](https://discourse.slicer.org/t/addsegmentfromclosedsurfacerepresentation-performance/30909 "2023-08-01T15:27:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![d0rnkernsky](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/d0rnkernsky/32/67029_2.png) [@d0rnkernsky](https://discourse.slicer.org/u/d0rnkernsky)\
**Post date:** [August 1, 2023, 3:27pm UTC](https://discourse.slicer.org/t/addsegmentfromclosedsurfacerepresentation-performance/30909/1 "2023-08-01T15:27:37Z")

</div>

Hello,

I am working on a custom module where I need to add many (360) objects to an existing segmentation node. Although the code seems very simple it takes hours to finish. I am not sure what causes the slowdown. Maybe there is a way to add all objects in a batch without calling AddSegmentFromClosedSurfaceRepresentation for each object. I will greatly appreciate any help.

**My code:**

```auto
segmentationNode = slicer.mrmlScene.GetFirstNodeByName("My segmentation")
for wedge in wedges:
      wedgeID = segmentationNode.AddSegmentFromClosedSurfaceRepresentation(wedge["wedgePD"], wedge["seg_name"], wedge["seg_colour"])

```

The time to execute AddSegmentFromClosedSurfaceRepresentation increases with the number of wedges added. Below is the time to execution time for first 30 steps  
1st call to AddSegmentFromClosedSurfaceRepresentation finished in 0.009  
2nd call to AddSegmentFromClosedSurfaceRepresentation finished in 0.188  
3rd call to AddSegmentFromClosedSurfaceRepresentation finished in 0.248  
…  
10th call to AddSegmentFromClosedSurfaceRepresentation finished in 0.448  
…  
30th call to AddSegmentFromClosedSurfaceRepresentation finished in 1.101

---

<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:** [August 2, 2023, 4:30am UTC](https://discourse.slicer.org/t/addsegmentfromclosedsurfacerepresentation-performance/30909/2 "2023-08-02T04:30:13Z")

</div>

You probably have multiple representations in your segmentation. Each time you add a new segment, all the representations might need to be recomputed. You can make adding a segment take 0 seconds by setting the source (master) representation to “closed surface” and remove the “binary labelmap” representation.

---

<div class="post-metadata">

**Author:** ![d0rnkernsky](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/d0rnkernsky/32/67029_2.png) [@d0rnkernsky](https://discourse.slicer.org/u/d0rnkernsky)\
**Post date:** [August 21, 2023, 8:18pm UTC](https://discourse.slicer.org/t/addsegmentfromclosedsurfacerepresentation-performance/30909/3 "2023-08-21T20:18:23Z")

</div>

Setting the master representation as closed surface helped, but it didn’t make it instant. Right now it takes around 0.5-0.8 sec. to add one surface. I have a follow up question. How do I check how many representations I have? When I open the Segmentation module and select the active segmentation I only see one segmentation.

---

<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:** [August 25, 2023, 1:11pm UTC](https://discourse.slicer.org/t/addsegmentfromclosedsurfacerepresentation-performance/30909/4 "2023-08-25T13:11:43Z")

</div>

May be able to speed up the import if you do enclose all the import steps between a `StartModify` and an `EndModify` call.

If you only have one representation listed then you have only that representation. You can get the list of representation by calling methods od the vtkSegmentation object that is stored in the segmentation node.
