1
- import nibabel as nb
1
+ """Utilities to handle images."""
2
2
import numpy as np
3
+ import nibabel as nb
3
4
from nipype .utils .filemanip import fname_presuffix
4
5
5
6
6
7
def extract_b0 (in_file , b0_ixs , out_path = None ):
7
8
"""Extract the *b0* volumes from a DWI dataset."""
8
9
if out_path is None :
9
- out_path = fname_presuffix (
10
- in_file , suffix = '_b0' , use_ext = True )
10
+ out_path = fname_presuffix (in_file , suffix = '_b0' )
11
11
12
12
img = nb .load (in_file )
13
- data = img .get_fdata ()
14
-
15
- b0 = data [..., b0_ixs ]
13
+ bzeros = np .squeeze (np .asanyarray (img .dataobj )[..., b0_ixs ])
16
14
17
15
hdr = img .header .copy ()
18
- hdr .set_data_shape (b0 .shape )
16
+ hdr .set_data_shape (bzeros .shape )
19
17
hdr .set_xyzt_units ('mm' )
20
- nb .Nifti1Image (b0 . astype ( hdr . get_data_dtype ()) , img .affine , hdr ).to_filename (out_path )
18
+ nb .Nifti1Image (bzeros , img .affine , hdr ).to_filename (out_path )
21
19
return out_path
22
20
23
21
@@ -27,24 +25,25 @@ def rescale_b0(in_file, mask_file, out_path=None):
27
25
out_path = fname_presuffix (
28
26
in_file , suffix = '_rescaled' , use_ext = True )
29
27
30
- img = nb .load (in_file )
28
+ img = nb .squeeze_image ( nb . load (in_file ) )
31
29
if img .dataobj .ndim == 3 :
32
- return in_file
30
+ return in_file , [ 1.0 ]
33
31
32
+ mask_data = nb .load (mask_file ).get_fdata () > 0
33
+
34
+ dtype = img .get_data_dtype ()
34
35
data = img .get_fdata ()
35
- mask_img = nb .load (mask_file )
36
- mask_data = mask_img .get_fdata ()
37
36
38
- median_signal = np .median (data [mask_data > 0 , ...], axis = 0 )
39
- rescaled_data = 1000 * data / median_signal
40
- hdr = img .header .copy ()
41
- nb .Nifti1Image (
42
- rescaled_data .astype (hdr .get_data_dtype ()), img .affine , hdr
43
- ).to_filename (out_path )
44
- return out_path
37
+ median_signal = np .median (data [mask_data , ...], axis = 0 )
38
+ # Normalize to the first volume
39
+ signal_drift = median_signal [0 ] / median_signal
40
+ data /= signal_drift
45
41
42
+ nb .Nifti1Image (data .astype (dtype ), img .affine , img .header ).to_filename (out_path )
43
+ return out_path , signal_drift .tolist ()
46
44
47
- def median (in_file , dtype = None , out_path = None ):
45
+
46
+ def median (in_file , out_path = None ):
48
47
"""Average a 4D dataset across the last dimension using median."""
49
48
if out_path is None :
50
49
out_path = fname_presuffix (
@@ -57,14 +56,8 @@ def median(in_file, dtype=None, out_path=None):
57
56
nb .squeeze_image (img ).to_filename (out_path )
58
57
return out_path
59
58
60
- median_data = np . median ( img .get_fdata ( dtype = dtype ),
61
- axis = - 1 )
59
+ dtype = img .get_data_dtype ()
60
+ median_data = np . median ( img . get_fdata (), axis = - 1 )
62
61
63
- hdr = img .header .copy ()
64
- hdr .set_xyzt_units ('mm' )
65
- if dtype is not None :
66
- hdr .set_data_dtype (dtype )
67
- else :
68
- dtype = hdr .get_data_dtype ()
69
- nb .Nifti1Image (median_data .astype (dtype ), img .affine , hdr ).to_filename (out_path )
62
+ nb .Nifti1Image (median_data .astype (dtype ), img .affine , img .header ).to_filename (out_path )
70
63
return out_path
0 commit comments