1
+ from pathlib import Path
1
2
import numpy as np
2
3
import nibabel as nb
3
4
from nipype .utils .filemanip import fname_presuffix
@@ -31,11 +32,7 @@ def extract_b0(in_file, b0_ixs, out_path=None):
31
32
"""
32
33
if out_path is None :
33
34
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 )
39
36
40
37
img = nb .load (in_file )
41
38
data = img .get_fdata (dtype = "float32" )
@@ -78,11 +75,7 @@ def rescale_b0(in_file, mask_file, out_path=None):
78
75
"""
79
76
if out_path is None :
80
77
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 )
86
79
87
80
img = nb .load (in_file )
88
81
if img .dataobj .ndim == 3 :
@@ -124,11 +117,7 @@ def median(in_file, out_path=None):
124
117
"""
125
118
if out_path is None :
126
119
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 )
132
121
133
122
img = nb .load (in_file )
134
123
if img .dataobj .ndim == 3 :
@@ -164,8 +153,9 @@ def average_images(images, out_path=None):
164
153
Examples
165
154
--------
166
155
>>> 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)
169
159
>>> assert os.path.isfile(out_path)
170
160
"""
171
161
from nilearn .image import mean_img
@@ -187,6 +177,13 @@ def get_list_data(file_list, dtype=np.float32):
187
177
----------
188
178
file_list : str
189
179
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]
190
187
"""
191
188
return nb .concat_images ([nb .load (fname ) for fname in file_list ]).get_fdata (dtype = dtype )
192
189
@@ -210,8 +207,32 @@ def match_transforms(dwi_files, transforms, b0_ixs):
210
207
211
208
Returns
212
209
-------
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)
215
236
"""
216
237
num_dwis = len (dwi_files )
217
238
num_transforms = len (transforms )
@@ -246,6 +267,13 @@ def save_4d_to_3d(in_file):
246
267
-------
247
268
out_files : list
248
269
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]
249
277
"""
250
278
files_3d = nb .four_to_three (nb .load (in_file ))
251
279
out_files = []
@@ -273,6 +301,16 @@ def prune_b0s_from_dwis(in_files, b0_ixs):
273
301
-------
274
302
out_files : list
275
303
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)
276
314
"""
277
315
if in_files [0 ].endswith ("_warped.nii.gz" ):
278
316
out_files = [
@@ -310,6 +348,14 @@ def save_3d_to_4d(in_files):
310
348
-------
311
349
out_file : str
312
350
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]
313
359
"""
314
360
img_4d = nb .funcs .concat_images ([nb .load (img_3d ) for img_3d in in_files ])
315
361
out_file = fname_presuffix (in_files [0 ], suffix = "_merged" )
0 commit comments