Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RadQy Dimension Error with Diffusion Weighted Imaging #45

Open
kyle-sullivan315 opened this issue Jan 8, 2025 · 3 comments
Open

RadQy Dimension Error with Diffusion Weighted Imaging #45

kyle-sullivan315 opened this issue Jan 8, 2025 · 3 comments
Assignees

Comments

@kyle-sullivan315
Copy link

Hello, my team and I are attempting to run radqy on our BIDS dataset (nii.gz - compressed nifti files). So far the tool is able to process our anatomical scans, but we are hitting an issue with our diffusion weighted imaging (dwi) scans. For dwi scans with just one volume, there is no issue. But for our dwi scans with more than one volume, we get the following error:

Traceback (most recent call last):
File ".../radqy-0.1.5/bin/radqy", line 8, in
sys.exit(run_cli())
File ".../radqy-0.1.5/lib/python3.10/site-packages/radqy/cli.py", line 44, in run_cli
main(args)
File ".../radqy-0.1.5/lib/python3.10/site-packages/radqy/radqy.py", line 616, in main
s = IQM(v, name, total_participants, participant_index, subject_type, total_tags, fname_outdir, save_masks_flag,functions, sample_size, scan_type)
File ".../radqy-0.1.5/lib/python3.10/site-packages/radqy/radqy.py", line 452, in init
self.save_image(participant, I, j, folder)
File ".../radqy-0.1.5/lib/python3.10/site-packages/radqy/radqy.py", line 482, in save_image
plt.imsave(image_path, I, cmap=cm.Greys_r)
File ".../radqy-0.1.5/lib/python3.10/site-packages/matplotlib/pyplot.py", line 2604, in imsave
matplotlib.image.imsave(fname, arr, **kwargs)
File ".../radqy-0.1.5/lib/python3.10/site-packages/matplotlib/image.py", line 1636, in imsave
rgba = sm.to_rgba(arr, bytes=True)
File ".../radqy-0.1.5/lib/python3.10/site-packages/matplotlib/cm.py", line 399, in to_rgba
raise ValueError("Third dimension must be 3 or 4")
ValueError: Third dimension must be 3 or 4

It appears to be an issue creating the .pngs where the dimensions of the nifti files are not compatible with RGB or RGB-A format, but again, only when the scan has multiple volumes. Investigating the shape of the scans using a python tool called nibabel gives the following:
single volume scan shape: (140, 140, 22)
multiple volume (5) scan shape: (160, 160, 22, 5)

Have you encountered this error before? Do you know of a work around or fix? Thanks.

@Amir-Reza-Sadri
Copy link
Collaborator

Thank you for using RadQy and reporting this issue! The error occurs because plt.imsave does not handle 4D data (e.g., multi-volume scans). To resolve this, you can update the save_image function to process each volume and slice individually. Here’s the fixed code:

def save_image(self, participant, I, index, folder):
participant_dir = folder / participant
participant_dir.mkdir(parents=True, exist_ok=True)

if I.ndim == 4:  # Multi-volume (4D)
    for vol_idx in range(I.shape[-1]):
        for slice_idx in range(I.shape[2]):
            slice_image = I[:, :, slice_idx, vol_idx]
            filename = f"{participant}_vol{vol_idx}_slice{slice_idx}.png"
            plt.imsave(participant_dir / filename, slice_image, cmap=cm.Greys_r)
elif I.ndim == 3:  # Single volume (3D)
    for slice_idx in range(I.shape[2]):
        slice_image = I[:, :, slice_idx]
        filename = f"{participant}_slice{slice_idx}.png"
        plt.imsave(participant_dir / filename, slice_image, cmap=cm.Greys_r)
elif I.ndim == 2:  # Single slice (2D)
    filename = f"{participant}_slice{index}.png"
    plt.imsave(participant_dir / filename, I, cmap=cm.Greys_r)
else:
    raise ValueError(f"Unsupported image shape: {I.shape}.")

@kyle-sullivan315
Copy link
Author

Thank you for the solution. This update appears to have worked. Unfortunately, we have run into another error stemming from the shape of the DWI images. Since the error is saying that the function is expecting 2D arrays, I'd guess that the DWI images have 3D arrays that are not compatible.

The error message is as follows:

Traceback (most recent call last):
File "/group/tools/Anaconda/Anaconda3-2024.06-1/envs/radqy-0.1.5/bin/radqy", line 8, in
sys.exit(run_cli())
File "/group/tools/Anaconda/Anaconda3-2024.06-1/envs/radqy-0.1.5/lib/python3.10/site-packages/radqy/cli.py", line 44, in run_cli
main(args)
File "/group/tools/Anaconda/Anaconda3-2024.06-1/envs/radqy-0.1.5/lib/python3.10/site-packages/radqy/radqy.py", line 631, in main
s = IQM(v, name, total_participants, participant_index, subject_type, total_tags, fname_outdir, save_masks_flag,functions, sample_size, scan_type)
File "/group/tools/Anaconda/Anaconda3-2024.06-1/envs/radqy-0.1.5/lib/python3.10/site-packages/radqy/radqy.py", line 461, in init
name, measure = func(F, B, c, f, b)
File "/group/tools/Anaconda/Anaconda3-2024.06-1/envs/radqy-0.1.5/lib/python3.10/site-packages/radqy/radqy.py", line 88, in func5
I_hat = conv2(F, filt, mode='same')
File "/group/tools/Anaconda/Anaconda3-2024.06-1/envs/radqy-0.1.5/lib/python3.10/site-packages/scipy/signal/_signaltools.py", line 1733, in convolve2d
raise ValueError('convolve2d inputs must both be 2-D arrays')
ValueError: convolve2d inputs must both be 2-D arrays

Any help in solving this issue would be greatly appreciated.

@kyle-sullivan315
Copy link
Author

Apologies, did not mean to close the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants