Skip to content

Commit abf5186

Browse files
author
dPys
committed
[DOC] Add doctests for all functions using HARDI data
1 parent eb43043 commit abf5186

File tree

2 files changed

+65
-19
lines changed

2 files changed

+65
-19
lines changed

dmriprep/data/tests/dwi_b0.nii.gz

0 Bytes
Binary file not shown.

dmriprep/utils/images.py

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from pathlib import Path
12
import numpy as np
23
import nibabel as nb
34
from nipype.utils.filemanip import fname_presuffix
@@ -31,11 +32,7 @@ def extract_b0(in_file, b0_ixs, out_path=None):
3132
"""
3233
if out_path is None:
3334
out_path = fname_presuffix(
34-
in_file, suffix="_b0"
35-
)
36-
else:
37-
out_path = fname_presuffix(in_file, suffix="_b0",
38-
newpath=out_path)
35+
in_file, suffix='_b0', use_ext=True)
3936

4037
img = nb.load(in_file)
4138
data = img.get_fdata(dtype="float32")
@@ -78,11 +75,7 @@ def rescale_b0(in_file, mask_file, out_path=None):
7875
"""
7976
if out_path is None:
8077
out_path = fname_presuffix(
81-
in_file, suffix="_rescaled_b0"
82-
)
83-
else:
84-
out_path = fname_presuffix(in_file, suffix="_rescaled_b0",
85-
newpath=out_path)
78+
in_file, suffix='_rescaled', use_ext=True)
8679

8780
img = nb.load(in_file)
8881
if img.dataobj.ndim == 3:
@@ -124,11 +117,7 @@ def median(in_file, out_path=None):
124117
"""
125118
if out_path is None:
126119
out_path = fname_presuffix(
127-
in_file, suffix="_b0ref"
128-
)
129-
else:
130-
out_path = fname_presuffix(in_file, suffix="_b0ref",
131-
newpath=out_path)
120+
in_file, suffix='_b0ref', use_ext=True)
132121

133122
img = nb.load(in_file)
134123
if img.dataobj.ndim == 3:
@@ -164,8 +153,9 @@ def average_images(images, out_path=None):
164153
Examples
165154
--------
166155
>>> os.chdir(tmpdir)
167-
>>> in_file = str(data_dir / 'dwi_b0.nii.gz')
168-
>>> out_path = average_images(in_file)
156+
>>> in_file = str(dipy_datadir / "HARDI193.nii.gz")
157+
>>> out_files = save_4d_to_3d(in_file)
158+
>>> out_path = average_images(out_files)
169159
>>> assert os.path.isfile(out_path)
170160
"""
171161
from nilearn.image import mean_img
@@ -187,6 +177,13 @@ def get_list_data(file_list, dtype=np.float32):
187177
----------
188178
file_list : str
189179
A list of file paths to 3D NIFTI images.
180+
181+
Examples
182+
--------
183+
>>> os.chdir(tmpdir)
184+
>>> in_file = str(dipy_datadir / "HARDI193.nii.gz")
185+
>>> out_files = save_4d_to_3d(in_file)
186+
>>> assert len(out_files) == get_list_data(out_files).shape[-1]
190187
"""
191188
return nb.concat_images([nb.load(fname) for fname in file_list]).get_fdata(dtype=dtype)
192189

@@ -210,8 +207,32 @@ def match_transforms(dwi_files, transforms, b0_ixs):
210207
211208
Returns
212209
-------
213-
out_path : str
214-
A 3D NIFTI file averaged along the 4th dimension.
210+
nearest_affines : list
211+
A list of affine file paths that correspond to each of the split
212+
dwi volumes.
213+
214+
Examples
215+
--------
216+
>>> os.chdir(tmpdir)
217+
>>> from dmriprep.utils.vectors import DiffusionGradientTable
218+
>>> dwi_file = str(dipy_datadir / "HARDI193.nii.gz")
219+
>>> check = DiffusionGradientTable(
220+
... dwi_file=dwi_file,
221+
... bvecs=str(dipy_datadir / "HARDI193.bvec"),
222+
... bvals=str(dipy_datadir / "HARDI193.bval"))
223+
>>> check.generate_rasb()
224+
>>> # Conform to the orientation of the image:
225+
>>> affines = np.zeros((check.gradients.shape[0], 4, 4))
226+
>>> transforms = []
227+
>>> for ii, aff in enumerate(affines):
228+
... aff_file = f'aff_{ii}.npy'
229+
... np.save(aff_file, aff)
230+
... transforms.append(aff_file)
231+
>>> dwi_files = save_4d_to_3d(dwi_file)
232+
>>> b0_ixs = np.where((check.bvals) <= 50)[0].tolist()[:2]
233+
>>> nearest_affines = match_transforms(dwi_files, transforms, b0_ixs)
234+
>>> assert sum([os.path.isfile(i) for i in nearest_affines]) == len(nearest_affines)
235+
>>> assert len(nearest_affines) == len(dwi_files)
215236
"""
216237
num_dwis = len(dwi_files)
217238
num_transforms = len(transforms)
@@ -246,6 +267,13 @@ def save_4d_to_3d(in_file):
246267
-------
247268
out_files : list
248269
A list of file paths to 3d NIFTI images.
270+
271+
Examples
272+
--------
273+
>>> os.chdir(tmpdir)
274+
>>> in_file = str(dipy_datadir / "HARDI193.nii.gz")
275+
>>> out_files = save_4d_to_3d(in_file)
276+
>>> assert len(out_files) == nb.load(in_file).shape[-1]
249277
"""
250278
files_3d = nb.four_to_three(nb.load(in_file))
251279
out_files = []
@@ -273,6 +301,16 @@ def prune_b0s_from_dwis(in_files, b0_ixs):
273301
-------
274302
out_files : list
275303
A list of file paths to 3d NIFTI images.
304+
305+
Examples
306+
--------
307+
>>> os.chdir(tmpdir)
308+
>>> b0_ixs = np.where(np.loadtxt(str(dipy_datadir / "HARDI193.bval")) <= 50)[0].tolist()[:2]
309+
>>> in_file = str(dipy_datadir / "HARDI193.nii.gz")
310+
>>> threeD_files = save_4d_to_3d(in_file)
311+
>>> out_files = prune_b0s_from_dwis(threeD_files, b0_ixs)
312+
>>> assert sum([os.path.isfile(i) for i in out_files]) == len(out_files)
313+
>>> assert len(out_files) == len(threeD_files) - len(b0_ixs)
276314
"""
277315
if in_files[0].endswith("_warped.nii.gz"):
278316
out_files = [
@@ -310,6 +348,14 @@ def save_3d_to_4d(in_files):
310348
-------
311349
out_file : str
312350
A file path to a 4d NIFTI image of concatenated 3D volumes.
351+
352+
Examples
353+
--------
354+
>>> os.chdir(tmpdir)
355+
>>> in_file = str(dipy_datadir / "HARDI193.nii.gz")
356+
>>> threeD_files = save_4d_to_3d(in_file)
357+
>>> out_file = save_3d_to_4d(threeD_files)
358+
>>> assert len(threeD_files) == nb.load(out_file).shape[-1]
313359
"""
314360
img_4d = nb.funcs.concat_images([nb.load(img_3d) for img_3d in in_files])
315361
out_file = fname_presuffix(in_files[0], suffix="_merged")

0 commit comments

Comments
 (0)