# Adding new segments from multiple numpy arrays

**URL:** <https://discourse.slicer.org/t/adding-new-segments-from-multiple-numpy-arrays/38257>\
**Category:** Development\
**Tags:** segmentation, segmentations, segment-editor\
**Created:** [September 6, 2024, 10:16am UTC](https://discourse.slicer.org/t/adding-new-segments-from-multiple-numpy-arrays/38257 "2024-09-06T10:16:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![roboted](https://avatars.discourse-cdn.com/v4/letter/r/6a8cbe/32.png) [@roboted](https://discourse.slicer.org/u/roboted)\
**Post date:** [September 6, 2024, 10:16am UTC](https://discourse.slicer.org/t/adding-new-segments-from-multiple-numpy-arrays/38257/1 "2024-09-06T10:16:27Z")

</div>

Hello,

I am working on importing numpy arrays into a segmentation so that they are shown in the segment editor as new segments.

What I have now after seeing some examples is:

```auto
label_map_volume = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLabelMapVolumeNode")
slicer.util.updateVolumeFromArray(label_map_volume, segmentation_mask)
slicer.modules.segmentations.logic().ImportLabelmapToSegmentationNode(label_map_volume, segmentation_node)
slicer.util.updateSegmentBinaryLabelmapFromArray(
            segmentation_mask, 
            segmentation_node, 
            label_map_volume.GetName(), 
            self.volume_node)
slicer.mrmlScene.RemoveNode(label_map_volume)

```

Where `segmentation_node` is the numpy array with the new segment to be added to the segmentation and self.volume\_node.

The problem is that every segment adds to memory about as much as the volume size, as if the whole array is saved and shown, so after ~20 to 30 segments Slicer slows down. I considered using a single array to store all the segmentations because overlapping is not a problem, but I do not know if Slicer works more efficiently than this under the hood and this approach is not optimal. I assume this is not a memory leak because the label map node is deleted once the segmentation has been updated. What is the best option from here?

Thank you for the help.

---

<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 6, 2024, 10:51am UTC](https://discourse.slicer.org/t/adding-new-segments-from-multiple-numpy-arrays/38257/2 "2024-09-06T10:51:00Z")

</div>

Slicer is in fact more efficient than storing the entire extent as many times as the number of segments. Originally, each segment only took as much space as its actual content. Later, a “layer” mechanism was introduced to make this even more efficient, however, I am not super familiar with the implementation of this part. I assume the numpy importer takes the simplest approach possible, and the effective extents and layers are not considered. I’d try to call `segmentation_node.GetSegmentation().CollapseBinaryLabelmaps()` and see if it solves the issue.

---

<div class="post-metadata">

**Author:** ![roboted](https://avatars.discourse-cdn.com/v4/letter/r/6a8cbe/32.png) [@roboted](https://discourse.slicer.org/u/roboted)\
**Post date:** [September 6, 2024, 11:20am UTC](https://discourse.slicer.org/t/adding-new-segments-from-multiple-numpy-arrays/38257/3 "2024-09-06T11:20:42Z")

</div>

This seems to solve the issue. Now only a very small amount of memory is taken for each new segment. Thank you!
