|
27 | 27 | import nibabel as nb
|
28 | 28 | import numpy as np
|
29 | 29 | import scipy.ndimage as nd
|
| 30 | +from dipy.core.gradients import gradient_table |
| 31 | +from dipy.stats.qc import find_qspace_neighbors |
30 | 32 | from nipype.interfaces.base import (
|
31 | 33 | BaseInterfaceInputSpec as _BaseInterfaceInputSpec,
|
32 | 34 | )
|
@@ -339,7 +341,9 @@ def _run_interface(self, runtime):
|
339 | 341 | bvecs = np.loadtxt(self._results['out_bvec_file']).T
|
340 | 342 | bvals = np.loadtxt(self._results['out_bval_file'])
|
341 | 343 |
|
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) |
343 | 347 | self._results['out_bmatrix'] = np.hstack((bvecs, bvals[:, np.newaxis])).tolist()
|
344 | 348 |
|
345 | 349 | return runtime
|
@@ -1386,80 +1390,6 @@ def get_spike_mask(
|
1386 | 1390 | return spike_mask
|
1387 | 1391 |
|
1388 | 1392 |
|
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 |
| - |
1463 | 1393 | def noise_piesno(data: np.ndarray, n_channels: int = 4) -> (np.ndarray, np.ndarray):
|
1464 | 1394 | """
|
1465 | 1395 | Estimates noise in raw diffusion MRI (dMRI) data using the PIESNO algorithm.
|
|
0 commit comments