MIC-DKFZ / napari-nifti

Apache License 2.0
4 stars 1 forks source link

Fail to load 4D MRI images #2

Closed acorbat closed 4 weeks ago

acorbat commented 1 month ago

Hi,

I have 4D MRI images (3D for space and an extra dimension for echoes). I am able to load the images when using napari-nifti v0.0.12 but it fails to load them when using napari-nifti v0.0.14.

I'm pasting a clipped part of the error below in case it helps, but I think the problem lies within how MedVol deals with shapes.

The shape of the image is (6, 248, 248, 248).

File ~/micromamba/envs/ccm_pipe/lib/python3.12/site-packages/npe2/io_utils.py:172, in _read(paths=['/...ct/1_magnitude/rotated_trimmed_Mag_nR042836w0.nii'], stack=False, plugin_name='napari-nifti', return_reader=True, _pm=<npe2._plugin_manager.PluginManager object>)
    167     read_func = rdr.exec(
    168         kwargs={"path": paths, "stack": stack, "_registry": _pm.commands}
    169     )
    170     if read_func is not None:
    171         # if the reader function raises an exception here, we don't try to catch it
--> 172         if layer_data := read_func(paths, stack=stack):
        paths = ['/data/MRI_Project/1_magnitude/rotated_trimmed_Mag_nR042836w0.nii']
        read_func = <function reader_function at 0x7a46b7a9bd80>
        stack = False
    173             return (layer_data, rdr) if return_reader else layer_data
    175 if plugin_name:

File ~/micromamba/envs/ccm_pipe/lib/python3.12/site-packages/npe2/manifest/contributions/_readers.py:59, in ReaderContribution.exec.<locals>.npe1_compat(paths=['/1_magnitude/rotated_trimmed_Mag_nR042836w0.nii'], stack=False)
     56 @wraps(callable_)
     57 def npe1_compat(paths, *, stack):
     58     path = v2_to_v1(paths, stack)
---> 59     return callable_(path)
        path = '/data/MRI_Project/1_magnitude/rotated_trimmed_Mag_nR042836w0.nii'
        callable_ = <function reader_function at 0x7a46b7ad0900>

File ~/micromamba/envs/ccm_pipe/lib/python3.12/site-packages/napari_nifti/_reader.py:56, in reader_function(path='1_magnitude/rotated_trimmed_Mag_nR042836w0.nii')
     54 paths = [path] if isinstance(path, str) else path
     55 # load all files
---> 56 image_data_list = [MedVol(_path) for _path in paths]
        paths = ['/data/MRI_Project/1_magnitude/rotated_trimmed_Mag_nR042836w0.nii']
     57 # Convert to LayerData tuples
     58 layer_data = [(image_data.array, {"affine": image_data.affine, 
     59                                   "metadata": {"spacing": image_data.spacing, "origin": image_data.origin, "direction": image_data.direction, "header": image_data.header}}, "image")
     60               for image_data in image_data_list]

File <string>:9, in __init__(self=MedVol(array=array([[[[ 4.89159,  1.3615 , ..., ....76747, -11.68615]), direction=None, header=None), array='/1_magnitude/rotated_trimmed_Mag_nR042836w0.nii', spacing=None, origin=None, direction=None, header=None, copy=None)

File ~/micromamba/envs/ccm_pipe/lib/python3.12/site-packages/medvol/medvol.py:22, in MedVol.__post_init__(self=MedVol(array=array([[[[ 4.89159,  1.3615 , ..., ....76747, -11.68615]), direction=None, header=None))
     19     raise ValueError("Array must be a 3D numpy array or a filepath string")
     21 if isinstance(self.array, str):
---> 22     self._load(self.array)

     24 # Validate spacing: Must be None or an array-like with shape (3,)
     25 if self.spacing is not None:

File ~/micromamba/envs/ccm_pipe/lib/python3.12/site-packages/medvol/medvol.py:81, in MedVol._load(self=MedVol(array=array([[[[ 4.89159,  1.3615 , ..., ....76747, -11.68615]), direction=None, header=None), filepath='/1_magnitude/rotated_trimmed_Mag_nR042836w0.nii')
     79 self.spacing = np.array(image_sitk.GetSpacing()[::-1])
     80 self.origin = np.array(image_sitk.GetOrigin()[::-1])
---> 81 self.direction = np.array(image_sitk.GetDirection()[::-1]).reshape(3, 3)
     82 self.header = {key: image_sitk.GetMetaData(key) for key in image_sitk.GetMetaDataKeys()}

ValueError: cannot reshape array of size 16 into shape (3,3)
Karol-G commented 1 month ago

Hey,

I changed the backend for loading the images. The new backend is "better" (loads the affine correctly, which was wrong before), but it can currently only handle 3D data. To be honest, I forgot that people might want to load 2D or 4D images as well. I will take care of this today or next week. Until then please use the old version. Sorry for that issue.

Best regards, Karol

Karol-G commented 4 weeks ago

Hey,

can you upgrade both the medvol and napari-nifti package and check again? It should work now.

pip install -U medvol
pip install -U napari-nifti

Best regards, Karol

acorbat commented 4 weeks ago

ok, I tried it out with

napari-nifti      0.0.16
MedVol            0.0.12

and although it opens the images with the right number of channels and sizes, they don't look right.

image

Maybe the affine transform failed?

Thank you for looking into this.

Karol-G commented 4 weeks ago

Hmmm, would you be okay to send me the image so I can better debug? You can write me at karol.gotkowski [at] dkfz.de

Karol-G commented 4 weeks ago

The issue was resolved via mail. It is likely that the image was displayed correctly and that the direction matrix of the image has some issues. Feel free to open the issue again if there are still problems with the image or if it turns out that my assumption is not correct :)