dipy / dipy

DIPY is the paragon 3D/4D+ imaging library in Python. Contains generic methods for spatial normalization, signal processing, machine learning, statistical analysis and visualization of medical images. Additionally, it contains specialized methods for computational anatomy including diffusion, perfusion and structural imaging.
https://dipy.org
Other
703 stars 437 forks source link

Error in patch2self for single-slice data #2469

Open m13slash9 opened 2 years ago

m13slash9 commented 2 years ago

Description

When calling a patch2self denoising routine for single-slice data, a np.pad fails (presumably in the 2nd dismension)

----> 1 diff_denoised_data = patch2self(diff_data, gradient_table.bvals, model='ols', shift_intensity=True, clip_negative_vals=False, b0_threshold=25)

C:\ProgramData\Anaconda3\envs\dipy_env\lib\site-packages\dipy\denoise\patch2self.py in patch2self(data, bvals, patch_radius, model, b0_threshold, out_dtype, alpha, verbose, b0_denoising, clip_negative_vals, shift_intensity)
    282 
    283     else:
--> 284         train_b0 = _extract_3d_patches(np.pad(data_b0s, ((patch_radius[0],
    285                                               patch_radius[0]),
    286                                               (patch_radius[1],

<__array_function__ internals> in pad(*args, **kwargs)

C:\ProgramData\Anaconda3\envs\dipy_env\lib\site-packages\numpy\lib\arraypad.py in pad(array, pad_width, mode, **kwargs)
    741 
    742     # Broadcast to shape (array.ndim, 2)
--> 743     pad_width = _as_pairs(pad_width, array.ndim, as_index=True)
    744 
    745     if callable(mode):

C:\ProgramData\Anaconda3\envs\dipy_env\lib\site-packages\numpy\lib\arraypad.py in _as_pairs(x, ndim, as_index)
    516     # Converting the array with `tolist` seems to improve performance
    517     # when iterating and indexing the result (see usage in `pad`)
--> 518     return np.broadcast_to(x, (ndim, 2)).tolist()
    519 
    520 

<__array_function__ internals> in broadcast_to(*args, **kwargs)

C:\ProgramData\Anaconda3\envs\dipy_env\lib\site-packages\numpy\lib\stride_tricks.py in broadcast_to(array, shape, subok)
    409            [1, 2, 3]])
    410     """
--> 411     return _broadcast_to(array, shape, subok=subok, readonly=True)
    412 
    413 

C:\ProgramData\Anaconda3\envs\dipy_env\lib\site-packages\numpy\lib\stride_tricks.py in _broadcast_to(array, shape, subok, readonly)
    346                          'negative')
    347     extras = []
--> 348     it = np.nditer(
    349         (array,), flags=['multi_index', 'refs_ok', 'zerosize_ok'] + extras,
    350         op_flags=['readonly'], itershape=shape, order='C')

ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (4,2)  and requested shape (2,2)

Way to reproduce

For the traceback above the diff_data.shape is (94, 124, 1, 13)

and

gradient_table.bvals.shape is (13,)

then diff_denoised_data = patch2self(diff_data, gradient_table.bvals, model='ols', shift_intensity=True, clip_negative_vals=False, b0_threshold=25) results in the traceback above.

Attempting diff_denoised_data = patch2self(np.concatenate((diff_data,diff_data,diff_data),2), gradient_table.bvals, model='ols', shift_intensity=True, clip_negative_vals=False, b0_threshold=25) works with no errors

Environment

Windows-10-10.0.19043-SP0 Python 3.9.7 | packaged by conda-forge | (default, Sep 29 2021, 19:15:42) [MSC v.1916 64 bit (AMD64)] dipy 1.4.1 NumPy 1.21.3 SciPy 1.7.1 Nibabel 3.2.1 H5py 3.4.0

skoudoro commented 2 years ago

Thank you for pointing that out @m13slash9.

@ShreyasFadnavis will give you feedback ASAP.

ShreyasFadnavis commented 2 years ago

Hi @m13slash9 -- Thanks for pointing this out, I had not thought of this edge case! Your solution : diff_denoised_data = patch2self(np.concatenate((diff_data,diff_data,diff_data),2), gradient_table.bvals, model='ols', shift_intensity=True, clip_negative_vals=False, b0_threshold=25)

should work as a good workaround without hampering the performance!

@skoudoro : Following up with a PR to fix this.

skoudoro commented 2 years ago

Great! Thanks @ShreyasFadnavis !