Skip to content

Commit 6877fa1

Browse files
committed
update out_path
1 parent 199a496 commit 6877fa1

File tree

2 files changed

+68
-63
lines changed

2 files changed

+68
-63
lines changed

dmriprep/interfaces/images.py

Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
"""Image tools interfaces."""
2-
import numpy as np
3-
import nibabel as nb
4-
from nipype.utils.filemanip import fname_presuffix
52
from nipype import logging
63
from nipype.interfaces.base import (
74
traits, TraitedSpec, BaseInterfaceInputSpec, SimpleInterface, File
85
)
96

7+
from dmriprep.utils.images import extract_b0, rescale_b0, median
8+
109
LOGGER = logging.getLogger('nipype.interface')
1110

1211

@@ -45,24 +44,6 @@ def _run_interface(self, runtime):
4544
return runtime
4645

4746

48-
def extract_b0(in_file, b0_ixs, newpath=None):
49-
"""Extract the *b0* volumes from a DWI dataset."""
50-
out_file = fname_presuffix(
51-
in_file, suffix='_b0', newpath=newpath)
52-
53-
img = nb.load(in_file)
54-
data = img.get_fdata(dtype='float32')
55-
56-
b0 = data[..., b0_ixs]
57-
58-
hdr = img.header.copy()
59-
hdr.set_data_shape(b0.shape)
60-
hdr.set_xyzt_units('mm')
61-
hdr.set_data_dtype(np.float32)
62-
nb.Nifti1Image(b0, img.affine, hdr).to_filename(out_file)
63-
return out_file
64-
65-
6647
class _RescaleB0InputSpec(BaseInterfaceInputSpec):
6748
in_file = File(exists=True, mandatory=True, desc='b0s file')
6849
mask_file = File(exists=True, mandatory=True, desc='mask file')
@@ -101,45 +82,3 @@ def _run_interface(self, runtime):
10182
newpath=runtime.cwd
10283
)
10384
return runtime
104-
105-
106-
def rescale_b0(in_file, mask_file, newpath=None):
107-
"""Rescale the input volumes using the median signal intensity."""
108-
out_file = fname_presuffix(
109-
in_file, suffix='_rescaled_b0', newpath=newpath)
110-
111-
img = nb.load(in_file)
112-
if img.dataobj.ndim == 3:
113-
return in_file
114-
115-
data = img.get_fdata(dtype='float32')
116-
mask_img = nb.load(mask_file)
117-
mask_data = mask_img.get_fdata(dtype='float32')
118-
119-
median_signal = np.median(data[mask_data > 0, ...], axis=0)
120-
rescaled_data = 1000 * data / median_signal
121-
hdr = img.header.copy()
122-
nb.Nifti1Image(rescaled_data, img.affine, hdr).to_filename(out_file)
123-
return out_file
124-
125-
126-
def median(in_file, newpath=None):
127-
"""Average a 4D dataset across the last dimension using median."""
128-
out_file = fname_presuffix(
129-
in_file, suffix='_b0ref', newpath=newpath)
130-
131-
img = nb.load(in_file)
132-
if img.dataobj.ndim == 3:
133-
return in_file
134-
if img.shape[-1] == 1:
135-
nb.squeeze_image(img).to_filename(out_file)
136-
return out_file
137-
138-
median_data = np.median(img.get_fdata(dtype='float32'),
139-
axis=-1)
140-
141-
hdr = img.header.copy()
142-
hdr.set_xyzt_units('mm')
143-
hdr.set_data_dtype(np.float32)
144-
nb.Nifti1Image(median_data, img.affine, hdr).to_filename(out_file)
145-
return out_file

dmriprep/utils/images.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import numpy as np
2+
import nibabel as nb
3+
from nipype.utils.filemanip import fname_presuffix
4+
5+
6+
def extract_b0(in_file, b0_ixs, out_path=None):
7+
"""Extract the *b0* volumes from a DWI dataset."""
8+
if out_path is None:
9+
out_path = fname_presuffix(
10+
in_file, suffix='_b0', use_ext=True)
11+
12+
img = nb.load(in_file)
13+
data = img.get_fdata(dtype='float32')
14+
15+
b0 = data[..., b0_ixs]
16+
17+
hdr = img.header.copy()
18+
hdr.set_data_shape(b0.shape)
19+
hdr.set_xyzt_units('mm')
20+
hdr.set_data_dtype(np.float32)
21+
nb.Nifti1Image(b0, img.affine, hdr).to_filename(out_path)
22+
return out_path
23+
24+
25+
def rescale_b0(in_file, mask_file, out_path=None):
26+
"""Rescale the input volumes using the median signal intensity."""
27+
if out_path is None:
28+
out_path = fname_presuffix(
29+
in_file, suffix='_rescaled_b0', use_ext=True)
30+
31+
img = nb.load(in_file)
32+
if img.dataobj.ndim == 3:
33+
return in_file
34+
35+
data = img.get_fdata(dtype='float32')
36+
mask_img = nb.load(mask_file)
37+
mask_data = mask_img.get_fdata(dtype='float32')
38+
39+
median_signal = np.median(data[mask_data > 0, ...], axis=0)
40+
rescaled_data = 1000 * data / median_signal
41+
hdr = img.header.copy()
42+
nb.Nifti1Image(rescaled_data, img.affine, hdr).to_filename(out_path)
43+
return out_path
44+
45+
46+
def median(in_file, out_path=None):
47+
"""Average a 4D dataset across the last dimension using median."""
48+
if out_path is None:
49+
out_path = fname_presuffix(
50+
in_file, suffix='_b0ref', use_ext=True)
51+
52+
img = nb.load(in_file)
53+
if img.dataobj.ndim == 3:
54+
return in_file
55+
if img.shape[-1] == 1:
56+
nb.squeeze_image(img).to_filename(out_path)
57+
return out_path
58+
59+
median_data = np.median(img.get_fdata(dtype='float32'),
60+
axis=-1)
61+
62+
hdr = img.header.copy()
63+
hdr.set_xyzt_units('mm')
64+
hdr.set_data_dtype(np.float32)
65+
nb.Nifti1Image(median_data, img.affine, hdr).to_filename(out_path)
66+
return out_path

0 commit comments

Comments
 (0)