0

I generate a 4D complex numpy array for example:

numpy_array = np.tile(np.array([3, 4, 0], dtype=np.complex64), (100,100,100,1))

i want to save the numpy array as a nifti file

I tried using vtk and SimpleITK, but both of them don't support complex numbers (only vectors which are real numbers) It seems that only nibabel support complex vectors and i manage to save the file, but i can only load it with nibabel, when i try to load it with ITK-SNAP or with Slicer it doesn't open. I know that ITK-SNAP can open complex vector of nifti files because i already have those files saved with another script using matlab

import numpy as np
import nibabel as nib
import SimpleITK as sitk
import vtk
from vtk.util.numpy_support import numpy_to_vtk

numpy_array = np.tile(np.array([3, 4, 0], dtype=np.complex64), (100,100,100,1))

nibabel save try:

image = nib.Nifti2Image(numpy_array, affine=np.eye(4))
nib.save(image , r'file_name.nii')

SimpleITK save try:

image = sitk.GetImageFromArray(numpy_array)
sitk.writeimage(image, r'file_name.nii')

vtk save try:

array = np.concatenate((np.real(numpy_array), np.imag(numpy_array)), axis=3)
stacked_array = array.reshape(-1, array.shape[-1])
vtk_array = numpy_to_vtk(stacked_array, deep=True,                 
array_type=vtk.VTK_FLOAT)
vtk_image = vtk.vtkImageData()
vtk_image.SetDimensions(numpy_array.shape[0], numpy_array.shape[1], 
numpy_array.shape[2])
vtk_image.GetPointData().SetScalars(vtk_array)
writer = vtk.vtkNIFTIImageWriter()
writer.SetFileName(file_name)
writer.SetInputData(vtk_image)
writer.Write()

nibabel output:

nibabel creates a nifti file with vector but with other programs like ITK-SNAP it doens't open

ITK-SNAP error:

Error: Unsupported or missing image file format. ITK-SNAP failed to create an 
ImageIO object for the image 'file_name.nii' using format ''.

SimpleITK error:

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\SimpleITK\SimpleITK.py", line 3366, in _get_sitk_vector_pixelid
    return _np_sitk[numpy_array_type.dtype]
KeyError: dtype('complex64')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\ProgramData\Anaconda3\lib\site-packages\SimpleITK\SimpleITK.py", line 3431, in GetImageFromArray
    id = _get_sitk_vector_pixelid( z )
  File "C:\ProgramData\Anaconda3\lib\site-packages\SimpleITK\SimpleITK.py", line 3371, in _get_sitk_vector_pixelid
raise TypeError('dtype: {0} is not supported.'.format(numpy_array_type.dtype))
TypeError: dtype: complex64 is not supported.

vtk output:

vtk creates a vector nifti file but with 6 components instead of 3 (consider the imaginary part as components too), i saw in the documentation of numpy_to_vtk that it doesn't support copmlex arrays, maybe someone know about a workaround.

Shay Levi
  • 21
  • 5

1 Answers1

1

The SimpleITK 1.2 release supports writing 4D complex images. The missing feature is support for complex number in stk.GetImageFromArray, but it's already added to the development branch and your code works if you a compiled version of the current SimpleITK master branch or wait for the 1.3 release scheduled for October 2019.

In the mean time you can convert the complex number components to SimpleITK separately like this:

image = sitk.RealAndImaginaryToComplexImageFilter(sitk.GetImageFromArray(numpy_array.real), 
                                                  sitk.GetImageFromArray(numpy_array.imag)) 
sitk.WriteImage(image, r'file_name.nii')
blowekamp
  • 1,401
  • 7
  • 7
  • i guess you meant using 'sitk.RealAndImaginaryToComplex' i tried using that method, but still got an error: 'RuntimeError: Exception thrown in SimpleITK RealAndImaginaryToComplex: c:\miniconda3\envs\bld\conda-bld\simpleitk_1563387058023\work\code\common\include\sitkMemberFunctionFactory.hxx:196: sitk::ERROR: Pixel type: vector of 64-bit float is not supported in 3D byclass itk::simple::RealAndImaginaryToComplexImageFilter' – Shay Levi Sep 15 '19 at 07:28
  • @ShayLevi unfortunately, SimpleITK does not support *vector* complex pixels. – Dave Chen Sep 17 '19 at 15:24