@@ -24,7 +24,6 @@ class ExtractB0(SimpleInterface):
24
24
25
25
Example
26
26
-------
27
-
28
27
>>> os.chdir(tmpdir)
29
28
>>> extract_b0 = ExtractB0()
30
29
>>> extract_b0.inputs.in_file = str(data_dir / 'dwi.nii.gz')
@@ -34,7 +33,7 @@ class ExtractB0(SimpleInterface):
34
33
"""
35
34
36
35
input_spec = _ExtractB0InputSpec
37
- output_spec = ExtractB0OutputSpec
36
+ output_spec = _ExtractB0OutputSpec
38
37
39
38
def _run_interface (self , runtime ):
40
39
self ._results ['out_file' ] = extract_b0 (
@@ -71,7 +70,7 @@ class _RescaleB0InputSpec(BaseInterfaceInputSpec):
71
70
mask_file = File (exists = True , mandatory = True , desc = 'mask file' )
72
71
73
72
74
- class RescaleB0OutputSpec (TraitedSpec ):
73
+ class _RescaleB0OutputSpec (TraitedSpec ):
75
74
out_file = File (exists = True , desc = 'b0 file' )
76
75
77
76
@@ -82,7 +81,6 @@ class RescaleB0(SimpleInterface):
82
81
83
82
Example
84
83
-------
85
-
86
84
>>> os.chdir(tmpdir)
87
85
>>> rescale_b0 = RescaleB0()
88
86
>>> rescale_b0.inputs.in_file = str(data_dir / 'dwi.nii.gz')
@@ -91,8 +89,8 @@ class RescaleB0(SimpleInterface):
91
89
92
90
"""
93
91
94
- input_spec = RescaleB0InputSpec
95
- output_spec = RescaleB0OutputSpec
92
+ input_spec = _RescaleB0InputSpec
93
+ output_spec = _RescaleB0OutputSpec
96
94
97
95
def _run_interface (self , runtime ):
98
96
self ._results ['out_file' ] = rescale_b0 (
@@ -104,7 +102,9 @@ def _run_interface(self, runtime):
104
102
105
103
def rescale_b0 (in_file , mask_file , newpath = None ):
106
104
"""
107
- Rescale the *b0* volumes from a DWI dataset."""
105
+ Rescale the input volumes using the median signal intensity
106
+ and output a median image.
107
+ """
108
108
import numpy as np
109
109
import nibabel as nib
110
110
from nipype .utils .filemanip import fname_presuffix
@@ -113,20 +113,25 @@ def rescale_b0(in_file, mask_file, newpath=None):
113
113
in_file , suffix = '_median_b0' , newpath = newpath )
114
114
115
115
img = nib .load (in_file )
116
- data = img .get_fdata (dtype = 'float32' )
116
+ if img .dataobj .ndim == 3 :
117
+ return in_file
118
+ if img .shape [- 1 ] == 1 :
119
+ nb .squeeze_image (img ).to_filename (out_file )
120
+ return out_file
117
121
122
+ data = img .get_fdata (dtype = 'float32' )
118
123
mask_img = nib .load (mask_file )
119
124
mask_data = mask_img .get_fdata (dtype = 'float32' )
120
125
121
- mean_b0_signals = data [mask_data > 0 , ...].mean (axis = 0 )
126
+ median_signal = data [mask_data > 0 , ...].median (axis = 0 )
122
127
123
- rescale_b0 = 1000 * data / mean_b0_signals
128
+ rescaled_data = 1000 * data / median_signal
124
129
125
- median_b0 = np .median (rescale_b0 , axis = - 1 )
130
+ median_data = np .median (rescaled_data , axis = - 1 )
126
131
127
132
hdr = img .header .copy ()
128
- hdr .set_data_shape (median_b0 .shape )
133
+ hdr .set_data_shape (median_data .shape )
129
134
hdr .set_xyzt_units ('mm' )
130
135
hdr .set_data_dtype (np .float32 )
131
- nib .Nifti1Image (median_b0 , img .affine , hdr ).to_filename (out_file )
136
+ nib .Nifti1Image (median_data , img .affine , hdr ).to_filename (out_file )
132
137
return out_file
0 commit comments