Skip to content

Commit a3bdc5d

Browse files
committed
ENH: Remove redundant DIPY function
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 Set the minimum required DIPY version to 1.10.0. 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
1 parent 1359d51 commit a3bdc5d

File tree

2 files changed

+6
-76
lines changed

2 files changed

+6
-76
lines changed

mriqc/interfaces/diffusion.py

Lines changed: 5 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import nibabel as nb
2828
import numpy as np
2929
import scipy.ndimage as nd
30+
from dipy.core.gradients import gradient_table
31+
from dipy.stats.qc import find_qspace_neighbors
3032
from nipype.interfaces.base import (
3133
BaseInterfaceInputSpec as _BaseInterfaceInputSpec,
3234
)
@@ -339,7 +341,9 @@ def _run_interface(self, runtime):
339341
bvecs = np.loadtxt(self._results['out_bvec_file']).T
340342
bvals = np.loadtxt(self._results['out_bval_file'])
341343

342-
self._results['qspace_neighbors'] = _find_qspace_neighbors(bvals, bvecs)
344+
gtab = gradient_table(bvals, bvecs=bvecs)
345+
346+
self._results['qspace_neighbors'] = find_qspace_neighbors(gtab)
343347
self._results['out_bmatrix'] = np.hstack((bvecs, bvals[:, np.newaxis])).tolist()
344348

345349
return runtime
@@ -1386,80 +1390,6 @@ def get_spike_mask(
13861390
return spike_mask
13871391

13881392

1389-
def _find_qspace_neighbors(bvals: np.ndarray, bvecs: np.ndarray) -> list[tuple[int, int]]:
1390-
"""
1391-
Create a mapping of dwi volume index to its nearest neighbor in q-space.
1392-
1393-
This function implements an approximate nearest neighbor search in q-space
1394-
(excluding delta encoding). It calculates the Cartesian distance between
1395-
q-space representations of each diffusion-weighted imaging (DWI) volume
1396-
(represented by b-values and b-vectors) and identifies the closest one
1397-
(excluding the volume itself and b=0 volumes).
1398-
1399-
Parameters
1400-
----------
1401-
bvals : :obj:`~numpy.ndarray`
1402-
List of b-values.
1403-
bvecs : :obj:`~numpy.ndarray`
1404-
Table of b-vectors.
1405-
1406-
Returns
1407-
-------
1408-
:obj:`list` of :obj:`tuple`
1409-
A list of 2-tuples indicating the nearest q-space neighbor
1410-
of each dwi volume.
1411-
1412-
Examples
1413-
--------
1414-
>>> _find_qspace_neighbors(
1415-
... np.array([0, 1000, 1000, 2000]),
1416-
... np.array([
1417-
... [1, 0, 0],
1418-
... [1, 0, 0],
1419-
... [0.99, 0.0001, 0.0001],
1420-
... [1, 0, 0]
1421-
... ]),
1422-
... )
1423-
[(1, 2), (2, 1), (3, 1)]
1424-
1425-
Notes
1426-
-----
1427-
This is a copy of DIPY's code to be removed (and just imported) as soon as
1428-
a new release of DIPY is cut including
1429-
`dipy/dipy#3156 <https://github.com/dipy/dipy/pull/3156>`__.
1430-
1431-
"""
1432-
from dipy.core.geometry import cart_distance
1433-
from dipy.core.gradients import gradient_table
1434-
1435-
gtab = gradient_table(bvals, bvecs)
1436-
1437-
dwi_neighbors: list[tuple[int, int]] = []
1438-
1439-
# Only correlate the b>0 images
1440-
dwi_indices = np.flatnonzero(~gtab.b0s_mask)
1441-
1442-
# Get a pseudo-qspace value for b>0s
1443-
qvecs = np.sqrt(gtab.bvals)[:, np.newaxis] * gtab.bvecs
1444-
1445-
for dwi_index in dwi_indices:
1446-
qvec = qvecs[dwi_index]
1447-
1448-
# Calculate distance in q-space, accounting for symmetry
1449-
pos_dist = cart_distance(qvec[np.newaxis, :], qvecs)
1450-
neg_dist = cart_distance(qvec[np.newaxis, :], -qvecs)
1451-
distances = np.min(np.column_stack([pos_dist, neg_dist]), axis=1)
1452-
1453-
# Be sure we don't select the image as its own neighbor
1454-
distances[dwi_index] = np.inf
1455-
# Or a b=0
1456-
distances[gtab.b0s_mask] = np.inf
1457-
neighbor_index = np.argmin(distances)
1458-
dwi_neighbors.append((dwi_index, neighbor_index))
1459-
1460-
return dwi_neighbors
1461-
1462-
14631393
def noise_piesno(data: np.ndarray, n_channels: int = 4) -> (np.ndarray, np.ndarray):
14641394
"""
14651395
Estimates noise in raw diffusion MRI (dMRI) data using the PIESNO algorithm.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ classifiers = [
1818
]
1919
dependencies = [
2020
"acres",
21-
"dipy",
21+
"dipy >= 1.10.0",
2222
'importlib_resources; python_version < "3.9"', # jinja2 imports deprecated function removed in 2.1
2323
"markupsafe ~= 2.0.1",
2424
"matplotlib",

0 commit comments

Comments
 (0)