-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from josephmje/enh/skullstrip
ENH: Update image utility output path behaviour
- Loading branch information
Showing
2 changed files
with
109 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import nibabel as nb | ||
import numpy as np | ||
from nipype.utils.filemanip import fname_presuffix | ||
|
||
|
||
def extract_b0(in_file, b0_ixs, out_path=None): | ||
"""Extract the *b0* volumes from a DWI dataset.""" | ||
if out_path is None: | ||
out_path = fname_presuffix( | ||
in_file, suffix='_b0', use_ext=True) | ||
|
||
img = nb.load(in_file) | ||
data = img.get_fdata() | ||
|
||
b0 = data[..., b0_ixs] | ||
|
||
hdr = img.header.copy() | ||
hdr.set_data_shape(b0.shape) | ||
hdr.set_xyzt_units('mm') | ||
nb.Nifti1Image(b0.astype(hdr.get_data_dtype()), img.affine, hdr).to_filename(out_path) | ||
return out_path | ||
|
||
|
||
def rescale_b0(in_file, mask_file, out_path=None): | ||
"""Rescale the input volumes using the median signal intensity.""" | ||
if out_path is None: | ||
out_path = fname_presuffix( | ||
in_file, suffix='_rescaled', use_ext=True) | ||
|
||
img = nb.load(in_file) | ||
if img.dataobj.ndim == 3: | ||
return in_file | ||
|
||
data = img.get_fdata() | ||
mask_img = nb.load(mask_file) | ||
mask_data = mask_img.get_fdata() | ||
|
||
median_signal = np.median(data[mask_data > 0, ...], axis=0) | ||
rescaled_data = 1000 * data / median_signal | ||
hdr = img.header.copy() | ||
nb.Nifti1Image( | ||
rescaled_data.astype(hdr.get_data_dtype()), img.affine, hdr | ||
).to_filename(out_path) | ||
return out_path | ||
|
||
|
||
def median(in_file, dtype=None, out_path=None): | ||
"""Average a 4D dataset across the last dimension using median.""" | ||
if out_path is None: | ||
out_path = fname_presuffix( | ||
in_file, suffix='_b0ref', use_ext=True) | ||
|
||
img = nb.load(in_file) | ||
if img.dataobj.ndim == 3: | ||
return in_file | ||
if img.shape[-1] == 1: | ||
nb.squeeze_image(img).to_filename(out_path) | ||
return out_path | ||
|
||
median_data = np.median(img.get_fdata(dtype=dtype), | ||
axis=-1) | ||
|
||
hdr = img.header.copy() | ||
hdr.set_xyzt_units('mm') | ||
if dtype is not None: | ||
hdr.set_data_dtype(dtype) | ||
else: | ||
dtype = hdr.get_data_dtype() | ||
nb.Nifti1Image(median_data.astype(dtype), img.affine, hdr).to_filename(out_path) | ||
return out_path |