Error message may need rephrasing

Hi! I found that doing this:

DisplacementFieldNumpy = np.ones(
    (3, 4, 100, 100))
PyBuffer = itk.PyBuffer[VectorImageType]
DisplacementField = PyBuffer.GetImageFromArray(DisplacementFieldNumpy)


RuntimeError: Size mismatch of image and Buffer.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "<string>", line 17, in __PYTHON_EL_eval
File "/home/edgar/.local/lib/python3.10/site-packages/itk/", line 11790, in GetImageFromArray
imageView = itkPyBufferIVF33.GetImageViewFromArray(ndarr, is_vector, need_contiguous=False)
File "/home/edgar/.local/lib/python3.10/site-packages/itk/", line 11760, in GetImageViewFromArray
imgview = itkPyBufferIVF33._GetImageViewFromArray(ndarr, ndarr.shape[::-1], 1)
SystemError: <built-in function itkPyBufferIVF33__GetImageViewFromArray> returned a result with an exception set

If the first line is changed to

DisplacementFieldNumpy = np.ones(
    (len(vti.dimensions), nz, 100, 100), dtype=np.float32)

the rest works.

The message should probably be one of those which tell people that their data type is wrong.

Python 3.10.10
Numpy 1.24.2

From pip:
itk 5.3.0
itk-core 5.3.0
itk-elastix 0.15.0
itk-filtering 5.3.0
itk-io 5.3.0
itk-meshtopolydata 0.10.0
itk-minimalpathextraction 1.2.4
itk-numerics 5.3.0
itk-registration 5.3.0
itk-segmentation 5.3.0
itk-strain 0.3.6
itk-tubetk 1.3.4
itkwidgets 0.32.5
numpy 1.24.2

Thank you for ITK!

Hi @edgar ,

Thanks for the feedback!

Yes, interpreting that error message requires understanding that VectorImageType requested does not match the NumPy type passed. F (float) here corresponds to np.float32. D (double) corresponds to np.float64, which is the default for np.ones.

More information can be found here:

We can also call itk.image_from_array instead of PyBuffer directly. In this case, this would be:

DisplacementFieldNumpy = np.ones(
    (3, 4, 100, 100))
DisplacementField = itk.image_from_array(DisplacementFieldNumpy, ttype=VectorImageType)

Hope this helps,