# Programatically save Overlapping Segments

**URL:** <https://discourse.slicer.org/t/programatically-save-overlapping-segments/38512>\
**Category:** Development\
**Created:** [September 24, 2024, 11:27am UTC](https://discourse.slicer.org/t/programatically-save-overlapping-segments/38512 "2024-09-24T11:27:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tito21](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tito21/32/78012_2.png) [@tito21](https://discourse.slicer.org/u/tito21)\
**Post date:** [September 24, 2024, 11:27am UTC](https://discourse.slicer.org/t/programatically-save-overlapping-segments/38512/1 "2024-09-24T11:27:48Z")

</div>

Hi, I am using Slicer as part of a workflow that includes segmentation of overlapping regions. The segmentation needs to be saved programmatically.

When I save as a `.nrrd` file with ` slicer.modules.segmentations.logic().ExportSegmentsBinaryLabelmapRepresentationToFiles(segmentationNode, labelmapVolumeNode, referenceVolumeNode)` the overlapping segments get flatten. However, when I save the whole scene by clicking on the save icon, the segmentation gets exported as a `.seg.nrrd` 4D non overlapping volumes. What is the command to save the segmentation as a 4D volume?

Thanks in advance

---

<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 24, 2024, 11:35am UTC](https://discourse.slicer.org/t/programatically-save-overlapping-segments/38512/2 "2024-09-24T11:35:50Z")

</div>

What is the difference between the `.seg.nrrd` and the 4D volume you need? The `.seg.nrrd` file is basically a 4D nrrd with some extra metadata in the header.

---

<div class="post-metadata">

**Author:** ![tito21](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tito21/32/78012_2.png) [@tito21](https://discourse.slicer.org/u/tito21)\
**Post date:** [September 24, 2024, 11:43am UTC](https://discourse.slicer.org/t/programatically-save-overlapping-segments/38512/3 "2024-09-24T11:43:46Z")

</div>

Thanks for your prompt reply.

The `seg.nrrd` file is what I need, but I need a way to generate it programmatically. When I save the segmentation with the command above, I get only a 3D array, but I need the 4D volume you get by saving the file manually.

---

<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 24, 2024, 11:52am UTC](https://discourse.slicer.org/t/programatically-save-overlapping-segments/38512/4 "2024-09-24T11:52:25Z")

</div>

Ah, I see. Here’s how I’d do it:

```auto
s = getNode('vtkMRMLSegmentationNode1')
ss = s.GetStorageNode()
ss.SetFileName(r'd:/segmentation.seg.nrrd')
ss.WriteData(s)

```

(I found the corresponding example in the [script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#save-a-node-to-file), it says the same thing)

---

<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 24, 2024, 2:01pm UTC](https://discourse.slicer.org/t/programatically-save-overlapping-segments/38512/5 "2024-09-24T14:01:21Z")

</div>

You can also use the `saveNode` convenience function (from the script repository, too):

```python
success = slicer.util.saveNode(node, filename)

```

---

<div class="post-metadata">

**Author:** ![tito21](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tito21/32/78012_2.png) [@tito21](https://discourse.slicer.org/u/tito21)\
**Post date:** [September 24, 2024, 2:04pm UTC](https://discourse.slicer.org/t/programatically-save-overlapping-segments/38512/6 "2024-09-24T14:04:20Z")

</div>

This solved my problem.

Thanks for the help

Here is the code I use

```python
def exportLabelmap():

    filepath = outputPath + "/label.seg.nrrd"
    segmentationNode = getNode('vtkMRMLSegmentationNode1')
    storageNode = segmentationNode.CreateDefaultStorageNode()
    storageNode.SetFileName(filepath)
    storageNode.WriteData(segmentationNode)

    slicer.util.delayDisplay("Segmentation saved to " + filepath)

shortcut = qt.QShortcut(slicer.util.mainWindow())
shortcut.setKey(qt.QKeySequence("Ctrl+Shift+s"))
shortcut.connect( "activated()", exportLabelmap)

```

---

<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 24, 2024, 3:55pm UTC](https://discourse.slicer.org/t/programatically-save-overlapping-segments/38512/7 "2024-09-24T15:55:14Z")

</div>

Thanks for sharing the code that worked for you.

> [@tito21](#):
>
> `segmentationNode = getNode('vtkMRMLSegmentationNode1')`

Hardcoding an automatically generated ID would be fragile (e.g., you may create a second segmentation and delete the first one; then you will not find any node by the ID `vtkMRMLSegmentationNode1`). Instead, you can get the first segmentation node from the scene like this (from the [script repository](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#export-labelmap-node-from-segmentation-node)):

```python
segmentationNode = slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLSegmentationNode")

```
