Skip to content

Commit

Permalink
ENH: Remove redundant DIPY function
Browse files Browse the repository at this point in the history
Remove redundant DIPY `dipy.stats.qc.find_qspace_neighbors` function:
DIPY 1.10.0 was released on Dec 12, 2024, and includes the commit where
the function at issue was added:
https://docs.dipy.org/stable/release_notes/release1.10.html
dipy/dipy#3156

This patch set will additionally fix the DIPY keyword-only argument
warning:
```
mriqc/interfaces/diffusion.py::mriqc.interfaces.diffusion._find_qspace_neighbors
  /src/mriqc/mriqc/interfaces/diffusion.py:1435:
   UserWarning: Pass ['bvecs'] as keyword args. From version 2.0.0 passing
   these as positional arguments will result in an error.
    gtab = gradient_table(bvals, bvecs)
```

raised for example in:
https://app.circleci.com/pipelines/github/nipreps/mriqc/1651/workflows/01145976-37e9-4633-a00b-3ffe27053801/jobs/10057
  • Loading branch information
jhlegarreta committed Jan 9, 2025
1 parent 1359d51 commit 964b453
Showing 1 changed file with 5 additions and 75 deletions.
80 changes: 5 additions & 75 deletions mriqc/interfaces/diffusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import nibabel as nb
import numpy as np
import scipy.ndimage as nd
from dipy.core.gradients import gradient_table
from dipy.stats.qc import find_qspace_neighbors
from nipype.interfaces.base import (
BaseInterfaceInputSpec as _BaseInterfaceInputSpec,
)
Expand Down Expand Up @@ -339,7 +341,9 @@ def _run_interface(self, runtime):
bvecs = np.loadtxt(self._results['out_bvec_file']).T
bvals = np.loadtxt(self._results['out_bval_file'])

self._results['qspace_neighbors'] = _find_qspace_neighbors(bvals, bvecs)
gtab = gradient_table(bvals, bvecs=bvecs)

self._results['qspace_neighbors'] = find_qspace_neighbors(gtab)
self._results['out_bmatrix'] = np.hstack((bvecs, bvals[:, np.newaxis])).tolist()

return runtime
Expand Down Expand Up @@ -1386,80 +1390,6 @@ def get_spike_mask(
return spike_mask


def _find_qspace_neighbors(bvals: np.ndarray, bvecs: np.ndarray) -> list[tuple[int, int]]:
"""
Create a mapping of dwi volume index to its nearest neighbor in q-space.
This function implements an approximate nearest neighbor search in q-space
(excluding delta encoding). It calculates the Cartesian distance between
q-space representations of each diffusion-weighted imaging (DWI) volume
(represented by b-values and b-vectors) and identifies the closest one
(excluding the volume itself and b=0 volumes).
Parameters
----------
bvals : :obj:`~numpy.ndarray`
List of b-values.
bvecs : :obj:`~numpy.ndarray`
Table of b-vectors.
Returns
-------
:obj:`list` of :obj:`tuple`
A list of 2-tuples indicating the nearest q-space neighbor
of each dwi volume.
Examples
--------
>>> _find_qspace_neighbors(
... np.array([0, 1000, 1000, 2000]),
... np.array([
... [1, 0, 0],
... [1, 0, 0],
... [0.99, 0.0001, 0.0001],
... [1, 0, 0]
... ]),
... )
[(1, 2), (2, 1), (3, 1)]
Notes
-----
This is a copy of DIPY's code to be removed (and just imported) as soon as
a new release of DIPY is cut including
`dipy/dipy#3156 <https://github.com/dipy/dipy/pull/3156>`__.
"""
from dipy.core.geometry import cart_distance
from dipy.core.gradients import gradient_table

gtab = gradient_table(bvals, bvecs)

dwi_neighbors: list[tuple[int, int]] = []

# Only correlate the b>0 images
dwi_indices = np.flatnonzero(~gtab.b0s_mask)

# Get a pseudo-qspace value for b>0s
qvecs = np.sqrt(gtab.bvals)[:, np.newaxis] * gtab.bvecs

for dwi_index in dwi_indices:
qvec = qvecs[dwi_index]

# Calculate distance in q-space, accounting for symmetry
pos_dist = cart_distance(qvec[np.newaxis, :], qvecs)
neg_dist = cart_distance(qvec[np.newaxis, :], -qvecs)
distances = np.min(np.column_stack([pos_dist, neg_dist]), axis=1)

# Be sure we don't select the image as its own neighbor
distances[dwi_index] = np.inf
# Or a b=0
distances[gtab.b0s_mask] = np.inf
neighbor_index = np.argmin(distances)
dwi_neighbors.append((dwi_index, neighbor_index))

return dwi_neighbors


def noise_piesno(data: np.ndarray, n_channels: int = 4) -> (np.ndarray, np.ndarray):
"""
Estimates noise in raw diffusion MRI (dMRI) data using the PIESNO algorithm.
Expand Down

0 comments on commit 964b453

Please sign in to comment.