# reading segmentation file from Slicer (NRRD) with multiple segments with SimpleITK shows Exception

**URL:** <https://discourse.itk.org/t/reading-segmentation-file-from-slicer-nrrd-with-multiple-segments-with-simpleitk-shows-exception/808>\
**Category:** Engineering\
**Tags:** simpleitk, python\
**Created:** [March 29, 2018, 7:03pm UTC](https://discourse.itk.org/t/reading-segmentation-file-from-slicer-nrrd-with-multiple-segments-with-simpleitk-shows-exception/808 "2018-03-29T19:03:58Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Diego\_C](https://discourse.itk.org/user_avatar/discourse.itk.org/diego_c/32/279_2.png) [@Diego\_C](https://discourse.itk.org/u/Diego_C)\
**Post date:** [March 29, 2018, 7:03pm UTC](https://discourse.itk.org/t/reading-segmentation-file-from-slicer-nrrd-with-multiple-segments-with-simpleitk-shows-exception/808/1 "2018-03-29T19:03:58Z")

</div>

Hello!

I segmented two prostate lesions in Slicer and saved the segmentation as NRRD.

 ![image](https://discourse.itk.org/uploads/default/original/1X/043a7c45aede2e2dd12841888e77619feb3d7124.jpg)

I am trying to read this file using SimpleITK but I am getting the following error message:

```auto
File "/Users/odin/anaconda/lib/python2.7/site-packages/SimpleITK/SimpleITK.py", line 8332, in ReadImage
    return _SimpleITK.ReadImage(*args)
RuntimeError: Exception thrown in SimpleITK ReadImage: /scratch/dashboards/SimpleITK-OSX10.6-intel-pkg/SimpleITK/Code/IO/src/sitkImageReaderBase.cxx:285:
sitk::ERROR: Only Complex image with float and double are supported!
```

However I can perfectly read segmentation files with **one** segment. Is this a limitation of the current version?  
When looking at the NRRD headers:

```auto
dimension = 4
sizes= [2, 41, 80, 9]
kinds=['complex', 'domain', 'domain', 'domain']
```

So I know I have my two segments there (first index of sizes gives the number of segments) and that actually matches with the data array.  
Does anyone know about this problem?

Thanks,

Diego

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [March 30, 2018, 2:42pm UTC](https://discourse.itk.org/t/reading-segmentation-file-from-slicer-nrrd-with-multiple-segments-with-simpleitk-shows-exception/808/2 "2018-03-30T14:42:45Z")

</div>

Hello Diego,

How was this image created? I’m guessing the “type” is some thing like “unsigned short”. Seems not appropriate to save a multi-segmentation image with a “complex type”.

Technically in C++ [std::complex\_short](https://stackoverflow.com/questions/11108743/why-does-c-mandate-that-complex-only-be-instantiated-for-float-double-or-lon) has undefined behavior. So SimpleITK only support std::complex of float and double as the error message says.

Perhaps the NRRD author (@glk) or a Slicer developer (@fedorov @lassoan) could tell us if that is a normal file type or not.

---

<div class="post-metadata">

**Author:** ![fedorov](https://discourse.itk.org/user_avatar/discourse.itk.org/fedorov/32/207_2.png) [@fedorov](https://discourse.itk.org/u/fedorov)\
**Post date:** [March 30, 2018, 2:52pm UTC](https://discourse.itk.org/t/reading-segmentation-file-from-slicer-nrrd-with-multiple-segments-with-simpleitk-shows-exception/808/3 "2018-03-30T14:52:10Z")

</div>

This is another (in addition to multivolumes, and perhaps other adaptations) Slicer-specific use of NRRD. @lassoan is the man with the answers.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [March 30, 2018, 2:53pm UTC](https://discourse.itk.org/t/reading-segmentation-file-from-slicer-nrrd-with-multiple-segments-with-simpleitk-shows-exception/808/4 "2018-03-30T14:53:18Z")

</div>

Why was complex chosen over some type of vector?

---

<div class="post-metadata">

**Author:** ![fedorov](https://discourse.itk.org/user_avatar/discourse.itk.org/fedorov/32/207_2.png) [@fedorov](https://discourse.itk.org/u/fedorov)\
**Post date:** [March 30, 2018, 2:54pm UTC](https://discourse.itk.org/t/reading-segmentation-file-from-slicer-nrrd-with-multiple-segments-with-simpleitk-shows-exception/808/5 "2018-03-30T14:54:14Z")

</div>

I don’t know the background, but I am sure Andras will have the answer.

---

<div class="post-metadata">

**Author:** ![fedorov](https://discourse.itk.org/user_avatar/discourse.itk.org/fedorov/32/207_2.png) [@fedorov](https://discourse.itk.org/u/fedorov)\
**Post date:** [March 30, 2018, 2:55pm UTC](https://discourse.itk.org/t/reading-segmentation-file-from-slicer-nrrd-with-multiple-segments-with-simpleitk-shows-exception/808/6 "2018-03-30T14:55:57Z")

</div>

@Diego_C to solve your immediate problem, you can export individual segments as label maps using Segmentations module of Slicer, and save those in separate 3-d files that ITK will know how to deal with.

You can even export both of the segments into a single label map 3d file, since they do not overlap.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [March 30, 2018, 2:58pm UTC](https://discourse.itk.org/t/reading-segmentation-file-from-slicer-nrrd-with-multiple-segments-with-simpleitk-shows-exception/808/7 "2018-03-30T14:58:08Z")

</div>

A potential work around is to specify the pixel type when reading the image:

```auto
sitk.ReadImage("complex_short.nrrd", outputPixelType=sitkVectorUInt16)

```

I have not tried it, but I expect it to work.

---

<div class="post-metadata">

**Author:** ![Diego\_C](https://discourse.itk.org/user_avatar/discourse.itk.org/diego_c/32/279_2.png) [@Diego\_C](https://discourse.itk.org/u/Diego_C)\
**Post date:** [March 30, 2018, 3:05pm UTC](https://discourse.itk.org/t/reading-segmentation-file-from-slicer-nrrd-with-multiple-segments-with-simpleitk-shows-exception/808/8 "2018-03-30T15:05:00Z")

</div>

Hi Bradley,

I don’t know. These are multi-segment segmentations without any custom code, just saved directly from Slicer3D.

Diego

---

<div class="post-metadata">

**Author:** ![Diego\_C](https://discourse.itk.org/user_avatar/discourse.itk.org/diego_c/32/279_2.png) [@Diego\_C](https://discourse.itk.org/u/Diego_C)\
**Post date:** [March 30, 2018, 3:12pm UTC](https://discourse.itk.org/t/reading-segmentation-file-from-slicer-nrrd-with-multiple-segments-with-simpleitk-shows-exception/808/9 "2018-03-30T15:12:26Z")

</div>

Hi Andrey!

I totally get the workaround you propose it makes sense since having separate nrrd files for each segment will give SimpleITK easily recognizable nrrd headers. However I can’t implement it! I have about 900+ segmentations already from 3 different radiologists… so I couldn’t possibly do this by hand.

Last night I develop code that reads these multi-segment segmentations which in nrrd terms means a 4-dimensional array where the first index indicates the segment s (like this [s, x, y, z])

And generates a SimpleITK image with for each segment. I made sure that the metadata for each image was preserved so each image has its origin, spacing, direction, etc.

I wonder if there is a way for me to contribute this code ??  
multi-volume nrrd (like in this case) seems to be a current limitation for sitk.ReadImage

Diego

---

<div class="post-metadata">

**Author:** ![Diego\_C](https://discourse.itk.org/user_avatar/discourse.itk.org/diego_c/32/279_2.png) [@Diego\_C](https://discourse.itk.org/u/Diego_C)\
**Post date:** [March 30, 2018, 3:13pm UTC](https://discourse.itk.org/t/reading-segmentation-file-from-slicer-nrrd-with-multiple-segments-with-simpleitk-shows-exception/808/10 "2018-03-30T15:13:51Z")

</div>

Thanks Bradley I will give it a try and let you know. I am away from my computer for Easter Friday (typing all of this on my phone) will check this weekend.

---

<div class="post-metadata">

**Author:** ![fedorov](https://discourse.itk.org/user_avatar/discourse.itk.org/fedorov/32/207_2.png) [@fedorov](https://discourse.itk.org/u/fedorov)\
**Post date:** [March 30, 2018, 3:59pm UTC](https://discourse.itk.org/t/reading-segmentation-file-from-slicer-nrrd-with-multiple-segments-with-simpleitk-shows-exception/808/12 "2018-03-30T15:59:29Z")

</div>

@Diego_C your explanation makes total sense, I didn’t realize the scale is so big!

I wonder if anything would work differently if you replaced `complex` with `vector` or something else in the NRRD header …

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)\
**Post date:** [March 30, 2018, 4:31pm UTC](https://discourse.itk.org/t/reading-segmentation-file-from-slicer-nrrd-with-multiple-segments-with-simpleitk-shows-exception/808/13 "2018-03-30T16:31:31Z")

</div>

When Slicer saves segmentation (4D volume) to file then it uses vtkTeemNRRDWriter, which unfortunately tries to be smart and guess the axis type from the number of vector components - for 2 components it assumes that it is a complex number ([https://github.com/Slicer/Slicer/blob/master/Libs/vtkTeem/vtkTeemNRRDWriter.cxx#L90](https://github.com/Slicer/Slicer/blob/master/Libs/vtkTeem/vtkTeemNRRDWriter.cxx#L90)). I’ll fix this in Slicer - it’ll always use “vector” as axis kind.

---

<div class="post-metadata">

**Author:** ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)\
**Post date:** [March 30, 2018, 5:06pm UTC](https://discourse.itk.org/t/reading-segmentation-file-from-slicer-nrrd-with-multiple-segments-with-simpleitk-shows-exception/808/14 "2018-03-30T17:06:43Z")

</div>

I am pondering if SimpleITK should automatically load complex of integers as plain vector images with a warning, or the produces error should just suggest adding the “outputPixelType” parameter.

How long has Slicer been producing this type of image?

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)\
**Post date:** [March 30, 2018, 6:21pm UTC](https://discourse.itk.org/t/reading-segmentation-file-from-slicer-nrrd-with-multiple-segments-with-simpleitk-shows-exception/808/15 "2018-03-30T18:21:52Z")

</div>

> How long has Slicer been producing this type of image?

For about a year. The problem occurs only when there are two (or few other) components. Also, many people kept using the old editor and simple labelmap format for a while. So, probably the number of “complex” files are quite low. However, it would be still useful to make SimpleITK be able to load any kinds of axis.

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [March 30, 2018, 7:30pm UTC](https://discourse.itk.org/t/reading-segmentation-file-from-slicer-nrrd-with-multiple-segments-with-simpleitk-shows-exception/808/16 "2018-03-30T19:30:54Z")

</div>

The problem is that SimpleITK supports complex type, but that image is not a kind of complex type SimpleITK supports (and appropriately gives an error message). Segmentations module should always write vector or list for the first dimension.

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)\
**Post date:** [March 30, 2018, 10:57pm UTC](https://discourse.itk.org/t/reading-segmentation-file-from-slicer-nrrd-with-multiple-segments-with-simpleitk-shows-exception/808/17 "2018-03-30T22:57:03Z")

</div>

> [@dzenanz](#):
>
> Segmentations module should always write vector or list for the first dimension.

Yes, I completely agree. I’ve committed a fix, which will be included in tomorrow’s nightly build:  
[https://github.com/Slicer/Slicer/commit/a5cb03fec66823d8ddf5e4f5942d988895781833](https://github.com/Slicer/Slicer/commit/a5cb03fec66823d8ddf5e4f5942d988895781833)

Existing segmentation file that have two segments (and thus the incorrect “complex” kind) can be fixed by loading the file into Slicer and saving it.

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)\
**Post date:** [March 30, 2018, 11:02pm UTC](https://discourse.itk.org/t/reading-segmentation-file-from-slicer-nrrd-with-multiple-segments-with-simpleitk-shows-exception/808/18 "2018-03-30T23:02:10Z")

</div>

> [@dzenanz](#):
>
> The problem is that SimpleITK supports complex type, but that image is not a kind of complex type SimpleITK supports

If SimpleITK can only support complex with float components then when a complex image with int components is read then you can convert the values to float. However, such images should be very rare, so supporting this may not worth the implementation, testing, and maintenance effort.

---

<div class="post-metadata">

**Author:** ![Diego\_C](https://discourse.itk.org/user_avatar/discourse.itk.org/diego_c/32/279_2.png) [@Diego\_C](https://discourse.itk.org/u/Diego_C)\
**Post date:** [March 31, 2018, 2:40am UTC](https://discourse.itk.org/t/reading-segmentation-file-from-slicer-nrrd-with-multiple-segments-with-simpleitk-shows-exception/808/19 "2018-03-31T02:40:16Z")

</div>

> [@lassoan](#):
>
> Existing segmentation file that have two segments (and thus the incorrect “complex” kind) can be fixed by loading the file into Slicer and saving it.

Thank you @lassoan for your help. I have 900+ segmentations by 3 radiologist at the moment so doing this change in the headers by loading each file in Slicer is not feasible. However, as I mentioned before I wrote some code that handles these multi-segmentations correctly and generates a SimpleITK image for each. I would be happy to contribute this code as a work around for people with the same problem.

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.itk.org/user_avatar/discourse.itk.org/lassoan/32/27_2.png) [@lassoan](https://discourse.itk.org/u/lassoan)\
**Post date:** [March 31, 2018, 2:45am UTC](https://discourse.itk.org/t/reading-segmentation-file-from-slicer-nrrd-with-multiple-segments-with-simpleitk-shows-exception/808/20 "2018-03-31T02:45:49Z")

</div>

What does the code do? Changes kind from “complex” to “list” in the nrrd file header?

---

<div class="post-metadata">

**Author:** ![Diego\_C](https://discourse.itk.org/user_avatar/discourse.itk.org/diego_c/32/279_2.png) [@Diego\_C](https://discourse.itk.org/u/Diego_C)\
**Post date:** [March 31, 2018, 2:51am UTC](https://discourse.itk.org/t/reading-segmentation-file-from-slicer-nrrd-with-multiple-segments-with-simpleitk-shows-exception/808/21 "2018-03-31T02:51:00Z")

</div>

Actually that was going to be my next script while you guys and gals update Slicer 😄.

No, this code loads the multi-segment segmentation NRRD using pynrrd and then for each volume creates a SimpleITK image observing the appropriate metadata.

[Next page](https://discourse.itk.org/t/reading-segmentation-file-from-slicer-nrrd-with-multiple-segments-with-simpleitk-shows-exception/808.md?page=2)
