vtk complex array

Hey,

i’m new to vtk. i try to make a vtk image that contains a vtk complex array to create a nifti file with complex values in python. i used vtk.util.numpy_to_vtk to create an array, i know the function don’t support complex numbers and i was wondering if there is a way to fill the array with complex nubmers.

this is the code i’m currently using:

import numpy as np
from vtk.util import numpy_support

array = np.concatenate((np.real(numpy_array), np.imag(numpy_array)), axis=3)
stacked_array = array.reshape(-1, array.shape[-1])
vtk_array = numpy_support.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()

Thanks,
Shay

See vtkNIFTIImageWriter documentation: Images of type float or double that have 2 components will automatically be written as complex values. So, you need to convert your 3D array of complex numbers to a 4D scalar array before calling numpy_to_vtk.

Thanks for the quick reply. i forgot to mention that my numpy array is a 4D array since it’s a vector with complex components. I tried to make it 5D array as you suggested, but i don’t know how to set the number of the vector components in the vtk_image (I guess.vtk_image.SetDimensions only set the spatial dimensions)

Thanks

It has to be a 4D array (x, y, z, c). VTK can only deal with volumes up to 4 dimensions.