|
| 1 | +"""Image tools interfaces.""" |
| 2 | +import numpy as np |
| 3 | +import nibabel as nb |
| 4 | +from nipype.utils.filemanip import fname_presuffix |
| 5 | +from nipype import logging |
| 6 | +from nipype.interfaces.base import ( |
| 7 | + traits, TraitedSpec, BaseInterfaceInputSpec, SimpleInterface, File |
| 8 | +) |
| 9 | + |
| 10 | +LOGGER = logging.getLogger('nipype.interface') |
| 11 | + |
| 12 | + |
| 13 | +class _ExtractB0InputSpec(BaseInterfaceInputSpec): |
| 14 | + in_file = File(exists=True, mandatory=True, desc='dwi file') |
| 15 | + b0_ixs = traits.List(traits.Int, mandatory=True, |
| 16 | + desc='Index of b0s') |
| 17 | + |
| 18 | + |
| 19 | +class _ExtractB0OutputSpec(TraitedSpec): |
| 20 | + out_file = File(exists=True, desc='b0 file') |
| 21 | + |
| 22 | + |
| 23 | +class ExtractB0(SimpleInterface): |
| 24 | + """ |
| 25 | + Extract all b=0 volumes from a dwi series. |
| 26 | +
|
| 27 | + Example |
| 28 | + ------- |
| 29 | + >>> os.chdir(tmpdir) |
| 30 | + >>> extract_b0 = ExtractB0() |
| 31 | + >>> extract_b0.inputs.in_file = str(data_dir / 'dwi.nii.gz') |
| 32 | + >>> extract_b0.inputs.b0_ixs = [0, 1, 2] |
| 33 | + >>> res = extract_b0.run() # doctest: +SKIP |
| 34 | +
|
| 35 | + """ |
| 36 | + |
| 37 | + input_spec = _ExtractB0InputSpec |
| 38 | + output_spec = _ExtractB0OutputSpec |
| 39 | + |
| 40 | + def _run_interface(self, runtime): |
| 41 | + self._results['out_file'] = extract_b0( |
| 42 | + self.inputs.in_file, |
| 43 | + self.inputs.b0_ixs, |
| 44 | + newpath=runtime.cwd) |
| 45 | + return runtime |
| 46 | + |
| 47 | + |
| 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 | + |
| 66 | +class _RescaleB0InputSpec(BaseInterfaceInputSpec): |
| 67 | + in_file = File(exists=True, mandatory=True, desc='b0s file') |
| 68 | + mask_file = File(exists=True, mandatory=True, desc='mask file') |
| 69 | + |
| 70 | + |
| 71 | +class _RescaleB0OutputSpec(TraitedSpec): |
| 72 | + out_ref = File(exists=True, desc='One average b0 file') |
| 73 | + out_b0s = File(exists=True, desc='series of rescaled b0 volumes') |
| 74 | + |
| 75 | + |
| 76 | +class RescaleB0(SimpleInterface): |
| 77 | + """ |
| 78 | + Rescale the b0 volumes to deal with average signal decay over time. |
| 79 | +
|
| 80 | + Example |
| 81 | + ------- |
| 82 | + >>> os.chdir(tmpdir) |
| 83 | + >>> rescale_b0 = RescaleB0() |
| 84 | + >>> rescale_b0.inputs.in_file = str(data_dir / 'dwi.nii.gz') |
| 85 | + >>> rescale_b0.inputs.mask_file = str(data_dir / 'dwi_mask.nii.gz') |
| 86 | + >>> res = rescale_b0.run() # doctest: +SKIP |
| 87 | +
|
| 88 | + """ |
| 89 | + |
| 90 | + input_spec = _RescaleB0InputSpec |
| 91 | + output_spec = _RescaleB0OutputSpec |
| 92 | + |
| 93 | + def _run_interface(self, runtime): |
| 94 | + self._results['out_b0s'] = rescale_b0( |
| 95 | + self.inputs.in_file, |
| 96 | + self.inputs.mask_file, |
| 97 | + newpath=runtime.cwd |
| 98 | + ) |
| 99 | + self._results['out_ref'] = median( |
| 100 | + self._results['out_b0s'], |
| 101 | + newpath=runtime.cwd |
| 102 | + ) |
| 103 | + 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 |
0 commit comments