question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ITKReader doesn't return numpy array

See original GitHub issue

Describe the bug Currently, we use the itk.array_view_from_image() to get data array: https://github.com/Project-MONAI/MONAI/blob/dev/monai/data/image_reader.py#L316 But actually, it’s not numpy array, so following logic which depends on numpy.ndarray type will fail.

loader = LoadImage()
img, meta = loader(dicom_path)
print(type(img))

Get the output:

<class 'itk.itkPyBufferPython.NDArrayITKBase'>

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
Nic-Macommented, Sep 6, 2021

Hi @wyli ,

After some testing, I feel it’s better to also update ITK reader to use np.asarray(), because some operations of NDArrayITKBase is different from ndarray, very easy to cause unknown errors. For example:

print(type(img))
ret = np.mean(img)
print(isinstance(ret, np.ndarray))
print(ret)

img1 = np.array([1, 2, 3])
print(type(img1))
ret1 = np.mean(img1)
print(isinstance(ret1, np.ndarray))
print(ret1)

The output is:

<class 'itk.itkPyBufferPython.NDArrayITKBase'>
True
-3.4086406e-07
<class 'numpy.ndarray'>
False
2.0

The check for isinstance(mean, np.ndarray) will fail here.

What do you think?

Thanks.

1reaction
wylicommented, Sep 6, 2021

NDArrayITKBase is a subclass of ndarray, shouldn’t we enhance/fix the relevant failing logic instead of converting them to ndarray, is this a duplicate ticket of #2885?

Read more comments on GitHub >

github_iconTop Results From Across the Web

data not read completelyideal = 5734400 : actual = 2700286M ...
Hi @zivy, I am trying to read in and mhd file in pytorch, and convert it to a numpy array that is then...
Read more >
Need to transpose array obtained from itk instead of reversing ...
In ITKReader, itk.size(img) should not be reversed since it outputs correct size of an image. Instead, itk.array_view_from_image(img, keep_axes= ...
Read more >
Read numpy array image in ITK - python - Stack Overflow
I am trying to figure out how to read an numpy image array into ITK using python and I can't find a way...
Read more >
Images - | notebook.community
We have two options for converting from SimpleITK to numpy: GetArrayFromImage(): returns a copy of the image data. You can then freely modify...
Read more >
03_Image_Details
We have two options for converting from SimpleITK to numpy: GetArrayFromImage(): returns a copy of the image data. You can then freely modify...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found