ITK icon indicating copy to clipboard operation
ITK copied to clipboard

xarray from ITK image fails with downcast error

Open PranjalSahu opened this issue 2 years ago • 3 comments

Description

Steps to Reproduce

filepath = 'atlas_fc_nii.gz'
img1 = itk.imread(filepath, itk.F)
x_img = itk.xarray_from_image(img1)

Expected behavior

Actual behavior

85 metadata = dict(l_image)
     86 ignore_keys = {"direction", "origin", "spacing"}
     87 for key in metadata:

File ~/anaconda3/envs/ethicon/lib/python3.8/site-packages/itk/itkImagePython.py:7271, in itkImageBase3.__getitem__(self, key)
   7269         return np.flip(itk.array_from_matrix(self.GetDirection()), axis=None)
   7270     else:
-> 7271         return self.GetMetaDataDictionary()[key]
   7272 else:
   7273     return itk.array_view_from_image(self).__getitem__(key)

File ~/anaconda3/envs/ethicon/lib/python3.8/site-packages/itk/ITKCommonBasePython.py:851, in itkMetaDataDictionary.__getitem__(self, key)
    849 import itk
    850 obj = self.Get(key)
--> 851 return itk.down_cast(obj).GetMetaDataObjectValue()

File ~/anaconda3/envs/ethicon/lib/python3.8/site-packages/itk/support/extras.py:1844, in down_cast(obj)
   1841         except Exception:
   1842             # fail silently for now
   1843             pass
-> 1844     raise RuntimeError(f"Can't downcast to a specialization of {class_name}")
   1845 else:
   1846     return t.cast(obj)

RuntimeError: Can't downcast to a specialization of MetaDataObject

Reproducibility

Sample file: atlas_fc.nii.gz

Versions

5.3rc4.post2 If the commit number is required, run $ git rev-parse --short HEAD. -->

Environment

Additional Information

PranjalSahu avatar Aug 02 '22 17:08 PranjalSahu

@PranjalSahu this is a Matrix object for the Nifti transformation? Could support be added similar to https://github.com/InsightSoftwareConsortium/ITK/pull/2270 ?

thewtex avatar Aug 09 '22 13:08 thewtex

@thewtex yes that's what I discovered. It is failing for the key qto_xyz which is 4x4 Matrix. After adding wrapping for Matrix it could proceed but failed at another step. I will check and update here.

PranjalSahu avatar Aug 09 '22 14:08 PranjalSahu

Matrix with 4x4 is failing currently. Looking to resolve this issue https://github.com/InsightSoftwareConsortium/ITK/issues/3540 .

PranjalSahu avatar Aug 11 '22 15:08 PranjalSahu

Will re-open if some new keys are discovered that are failing.

PranjalSahu avatar Aug 18 '22 20:08 PranjalSahu