# How to save .seg.nrrd with script?

**URL:** <https://discourse.slicer.org/t/how-to-save-seg-nrrd-with-script/4614>\
**Category:** Support\
**Tags:** segmentation, python\
**Created:** [November 1, 2018, 6:05pm UTC](https://discourse.slicer.org/t/how-to-save-seg-nrrd-with-script/4614 "2018-11-01T18:05:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kayarre](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kayarre/32/3606_2.png) [@kayarre](https://discourse.slicer.org/u/kayarre)\
**Post date:** [November 1, 2018, 6:05pm UTC](https://discourse.slicer.org/t/how-to-save-seg-nrrd-with-script/4614/1 "2018-11-01T18:05:56Z")

</div>

Hi,  
I am trying to save a segmentation node using the `slicer.util.SaveNode` where the node is a `vtkMRMLSegmentationNode` but I get this weird error even though it appears to save the segmentation. it might be a bug somewhere else, but I can’t quite figure it out:

```auto
Saved node case4_ventricle in /home/sansomk/caseFiles/mri/VWI_proj/case4/image_vols/case4_ventricle.seg.nrrd
Warning: In /work/Stable/Slicer-4100/Libs/MRML/Core/vtkMRMLSegmentationNode.cxx, line 599
vtkMRMLSegmentationNode (0x86c6270): GenerateMergedLabelmap: Segment not found: vtkMRMLSegmentationNode1

Bad table range: [0, -1]

GetOutputImageData: Lookup table exists but empty!

Bad table range: [0, -1]

GetOutputImageData: Lookup table exists but empty!

error: [/home/sansomk/tools/Slicer-4.10.0-linux-amd64/bin/SlicerApp-real] exit abnormally - Report the problem.

```

so the problem is that it fails on:  
`slicer.vtkSlicerSegmentationsModuleLogic.ExportSegmentsToLabelmapNode(segmentationNode, segmentIDs, maskVolumeNode, inputVolumeNode)`

I think maybe the problem is that segmentsIDs should be a array of each segment string…?

so this was the solution:

```auto
segmentIDs = vtk.vtkStringArray()
for segID in range(segmentationNode.GetSegmentation().GetNumberOfSegments()):
    segmentIDs.InsertNextValue(segmentationNode.GetSegmentation().GetNthSegmentID(segID))

```

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [November 3, 2018, 1:33am UTC](https://discourse.slicer.org/t/how-to-save-seg-nrrd-with-script/4614/2 "2018-11-03T01:33:56Z")

</div>

Thanks for reporting the problem.

Using Slicer (revision [3699716](https://github.com/Slicer/Slicer/commit/36997169adf0e323015fb368aeb3ea9ecdb2e793)), after loading `MRHead` sample data and creating two segments, then using `slicer.util.saveNode`, I am not able to reproduce the problem.

Could you provide more details about the workflow leading to this error ?

Which exact version of Slicer are you using ?

```auto
>>> slicer.util.saveNode(slicer.mrmlScene.GetNodeByID("vtkMRMLSegmentationNode1"), "/tmp/foo.seg.nrrd")
True

```

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/d/c/dc5d364db94c00de59fd3f4c8980b8ceb9a47512.png)

Looking at the header of the saved `nrrd` file also confirms the segments are saved:

```auto
$ head -n 30 /tmp/foo.seg.nrrd
NRRD0005
# Complete NRRD file format specification at:
# http://teem.sourceforge.net/nrrd/format.html
type: unsigned char
dimension: 4
space: right-anterior-superior
sizes: 2 74 1 116
space directions: none (0,-1,0) (-0,0,-1) (1.2999954223632799,-0,0)
kinds: list domain domain domain
encoding: gzip
space origin: (-86.6448974609375,23.928604125976989,-10.214302062987983)
measurement frame: (1,0,0) (0,1,0) (0,0,1)
Segment0_Color:=0.501961 0.682353 0.501961
Segment0_ColorAutoGenerated:=1
Segment0_Extent:=0 52 0 0 76 115
Segment0_ID:=Segment_1
Segment0_Name:=Segment_1
Segment0_NameAutoGenerated:=1
Segment0_Tags:=TerminologyEntry:Segmentation category and type - 3D Slicer General Anatomy list~SRT^T-D0050^Tissue~SRT^T-D0050^Tissue~^^~Anatomic codes - DICOM master list~^^~^^|
Segment1_Color:=0.945098 0.839216 0.568627
Segment1_ColorAutoGenerated:=1
Segment1_Extent:=3 73 0 0 0 65
Segment1_ID:=Segment_2
Segment1_Name:=Segment_2
Segment1_NameAutoGenerated:=1
Segment1_Tags:=TerminologyEntry:Segmentation category and type - 3D Slicer General Anatomy list~SRT^T-D0050^Tissue~SRT^T-D0050^Tissue~^^~Anatomic codes - DICOM master list~^^~^^|
Segmentation_ContainedRepresentationNames:=Binary labelmap|Closed surface|
Segmentation_ConversionParameters:=Compute surface normals|1|Compute surface normals. 1 (default) = surface normals are computed. 0 = surface normals are not computed (slightly faster but produces less smooth surface display).&Crop to reference image geometry|0|Crop the model to the extent of reference geometry. 0 (default) = created labelmap will contain the entire model. 1 = created labelmap extent will be within reference image extent.&Decimation factor|0.0|Desired reduction in the total number of polygons. Range: 0.0 (no decimation) to 1.0 (as much simplification as possible). Value of 0.8 typically reduces data set size by 80% without losing too much details.&Fractional labelmap oversampling factor|1|Determines the oversampling of the reference image geometry. All segments are oversampled with the same value (value of 1 means no oversampling).&Oversampling factor|1|Determines the oversampling of the reference image geometry. If it's a number, then all segments are oversampled with the same value (value of 1 means no oversampling). If it has the value "A", then automatic oversampling is calculated.&Reference image geometry|0;0;1.29999542236328;-86.6448974609375;-1;0;0;133.928604125977;0;-1;0;116.785697937012;0;0;0;1;0;255;0;255;0;129;|Image geometry description string determining the geometry of the labelmap that is created in course of conversion. Can be copied from a volume, using the button.&Smoothing factor|0.5|Smoothing factor. Range: 0.0 (no smoothing) to 1.0 (strong smoothing).&Threshold fraction|0.5|Determines the threshold that the closed surface is created at as a fractional value between 0 and 1.&
Segmentation_MasterRepresentation:=Binary labelmap
Segmentation_ReferenceImageExtentOffset:=110 127 0

```

---

<div class="post-metadata">

**Author:** ![kayarre](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/kayarre/32/3606_2.png) [@kayarre](https://discourse.slicer.org/u/kayarre)\
**Post date:** [November 5, 2018, 11:36pm UTC](https://discourse.slicer.org/t/how-to-save-seg-nrrd-with-script/4614/3 "2018-11-05T23:36:39Z")

</div>

Version 4.10

What I determined is that I was passing the segmentationNode instead of a list of the segments that make up the segmentationNode.  
Does that make sense? I am using a modified version of the slicer case iterator module.

---

<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:** [November 6, 2018, 2:43am UTC](https://discourse.slicer.org/t/how-to-save-seg-nrrd-with-script/4614/4 "2018-11-06T02:43:58Z")

</div>

You can use `slicer.vtkSlicerSegmentationsModuleLogic.ExportSegmentsToLabelmapNode` method if you want to export selected segments.

To export all segments, use `slicer.vtkSlicerSegmentationsModuleLogic.ExportAllSegmentsToLabelmapNode` method.

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [June 13, 2023, 3:19am UTC](https://discourse.slicer.org/t/how-to-save-seg-nrrd-with-script/4614/5 "2023-06-13T03:19:42Z")

</div>

> [@lassoan](#):
>
> `sModuleLogic.ExportAllSegmentsToLabelmapNode` method.

Hi Andras,  
I am getting the following do not understand why?

[VTK] SetAndObserveColorToDisplayNode failed: invalid Colors module logic.

[VTK] SetAndObserveColorToDisplayNode failed: invalid Colors module logic.

[VTK] Generic Warning: In /work/Preview/Slicer-0/Libs/MRML/Core/vtkDataFileFormatHelper.cxx, line 237

[VTK] vtkDataFileFormatHelper::GetFileExtensionFromFormatString: please update deprecated extension-only format specifier to ‘File format name (.ext)’ format! Current format string: .nrrd

[VTK] Generic Warning: In /work/Preview/Slicer-0/Libs/MRML/Core/vtkDataFileFormatHelper.cxx, line 237

[VTK] vtkDataFileFormatHelper::GetFileExtensionFromFormatString: please update deprecated extension-only format specifier to ‘File format name (.ext)’ format! Current format string: .nrrd

[VTK] RequestInformation: No LookupTable was set but number of components in input doesn’t match OutputFormat, therefore input can’t be passed through!

regards,  
Saima
