|
| 1 | +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- |
| 2 | +# vi: set ft=python sts=4 ts=4 sw=4 et: |
| 3 | +""" |
| 4 | +SynthStrip interfaces |
| 5 | +~~~~~~~~~~~~~~~~~~~~~ |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +import os.path as op |
| 10 | + |
| 11 | +import nibabel as nb |
| 12 | +from nipype.interfaces.afni import Zeropad |
| 13 | +from nipype.interfaces.base import ( |
| 14 | + BaseInterfaceInputSpec, |
| 15 | + File, |
| 16 | + SimpleInterface, |
| 17 | + TraitedSpec, |
| 18 | + traits, |
| 19 | +) |
| 20 | +from nipype.interfaces.freesurfer.base import FSCommandOpenMP, FSTraitedSpec |
| 21 | +from nipype.utils.filemanip import fname_presuffix |
| 22 | +from niworkflows.utils.images import _copyxform |
| 23 | + |
| 24 | + |
| 25 | +class FSTraitedSpecOpenMP(FSTraitedSpec): |
| 26 | + num_threads = traits.Int(desc='allows for specifying more threads', nohash=True) |
| 27 | + |
| 28 | + |
| 29 | +class _PrepareSynthStripGridInputSpec(BaseInterfaceInputSpec): |
| 30 | + input_image = File(exists=True, mandatory=True) |
| 31 | + |
| 32 | + |
| 33 | +class _PrepareSynthStripGridOutputSpec(TraitedSpec): |
| 34 | + prepared_image = File(exists=True) |
| 35 | + |
| 36 | + |
| 37 | +class PrepareSynthStripGrid(SimpleInterface): |
| 38 | + input_spec = _PrepareSynthStripGridInputSpec |
| 39 | + output_spec = _PrepareSynthStripGridOutputSpec |
| 40 | + |
| 41 | + def _run_interface(self, runtime): |
| 42 | + out_fname = fname_presuffix( |
| 43 | + self.inputs.input_image, |
| 44 | + newpath=runtime.cwd, |
| 45 | + suffix='_SynthStripGrid.nii', |
| 46 | + use_ext=False, |
| 47 | + ) |
| 48 | + self._results['prepared_image'] = out_fname |
| 49 | + |
| 50 | + # possibly downsample the image for sloppy mode. Always ensure float32 |
| 51 | + img = nb.load(self.inputs.input_image) |
| 52 | + if not img.ndim == 3: |
| 53 | + raise Exception('3D inputs are required for Synthstrip') |
| 54 | + xvoxels, yvoxels, zvoxels = img.shape |
| 55 | + |
| 56 | + def get_padding(nvoxels): |
| 57 | + extra_slices = nvoxels % 64 |
| 58 | + if extra_slices == 0: |
| 59 | + return 0 |
| 60 | + complete_64s = nvoxels // 64 |
| 61 | + return 64 * (complete_64s + 1) - nvoxels |
| 62 | + |
| 63 | + def split_padding(padding): |
| 64 | + halfpad = padding // 2 |
| 65 | + return halfpad, halfpad + halfpad % 2 |
| 66 | + |
| 67 | + spad = get_padding(zvoxels) |
| 68 | + rpad, lpad = split_padding(get_padding(xvoxels)) |
| 69 | + apad, ppad = split_padding(get_padding(yvoxels)) |
| 70 | + |
| 71 | + zeropad = Zeropad( |
| 72 | + S=spad, |
| 73 | + R=rpad, |
| 74 | + L=lpad, |
| 75 | + A=apad, |
| 76 | + P=ppad, |
| 77 | + in_files=self.inputs.input_image, |
| 78 | + out_file=out_fname, |
| 79 | + ) |
| 80 | + |
| 81 | + _ = zeropad.run() |
| 82 | + if not op.exists(out_fname): |
| 83 | + raise Exception('zeropad failed! You may need to increase the memory limit.') |
| 84 | + return runtime |
| 85 | + |
| 86 | + |
| 87 | +class _SynthStripInputSpec(FSTraitedSpecOpenMP): |
| 88 | + input_image = File(argstr='-i %s', exists=True, mandatory=True) |
| 89 | + no_csf = traits.Bool(argstr='--no-csf', desc='Exclude CSF from brain border.') |
| 90 | + border = traits.Int(argstr='-b %d', desc='Mask border threshold in mm. Default is 1.') |
| 91 | + gpu = traits.Bool(argstr='-g') |
| 92 | + out_brain = File( |
| 93 | + argstr='-o %s', |
| 94 | + name_template='%s_brain.nii.gz', |
| 95 | + name_source=['input_image'], |
| 96 | + keep_extension=False, |
| 97 | + desc='skull stripped image with corrupt sform', |
| 98 | + ) |
| 99 | + out_brain_mask = File( |
| 100 | + argstr='-m %s', |
| 101 | + name_template='%s_mask.nii.gz', |
| 102 | + name_source=['input_image'], |
| 103 | + keep_extension=False, |
| 104 | + desc='mask image with corrupt sform', |
| 105 | + ) |
| 106 | + |
| 107 | + |
| 108 | +class _SynthStripOutputSpec(TraitedSpec): |
| 109 | + out_brain = File(exists=True) |
| 110 | + out_brain_mask = File(exists=True) |
| 111 | + |
| 112 | + |
| 113 | +class SynthStrip(FSCommandOpenMP): |
| 114 | + input_spec = _SynthStripInputSpec |
| 115 | + output_spec = _SynthStripOutputSpec |
| 116 | + _cmd = 'mri_synthstrip' |
| 117 | + |
| 118 | + def _num_threads_update(self): |
| 119 | + if self.inputs.num_threads: |
| 120 | + self.inputs.environ.update({'OMP_NUM_THREADS': '1'}) |
| 121 | + |
| 122 | + |
| 123 | +class FixHeaderSynthStrip(SynthStrip): |
| 124 | + def _run_interface(self, runtime, correct_return_codes=(0,)): |
| 125 | + # Run normally |
| 126 | + runtime = super()._run_interface(runtime, correct_return_codes) |
| 127 | + |
| 128 | + outputs = self._list_outputs() |
| 129 | + if not op.exists(outputs['out_brain']): |
| 130 | + raise Exception('mri_synthstrip failed!') |
| 131 | + |
| 132 | + if outputs.get('out_brain_mask'): |
| 133 | + _copyxform(self.inputs.input_image, outputs['out_brain_mask']) |
| 134 | + |
| 135 | + _copyxform(self.inputs.input_image, outputs['out_brain']) |
| 136 | + |
| 137 | + return runtime |
| 138 | + |
| 139 | + |
| 140 | +class MockSynthStrip(SimpleInterface): |
| 141 | + input_spec = _SynthStripInputSpec |
| 142 | + output_spec = _SynthStripOutputSpec |
| 143 | + |
| 144 | + def _run_interface(self, runtime): |
| 145 | + from nipype.interfaces.fsl import BET |
| 146 | + |
| 147 | + this_bet = BET( |
| 148 | + mask=True, |
| 149 | + in_file=self.inputs.input_image, |
| 150 | + output_type='NIFTI_GZ', |
| 151 | + ) |
| 152 | + result = this_bet.run() |
| 153 | + self._results['out_brain'] = result.outputs.out_file |
| 154 | + self._results['out_brain_mask'] = result.outputs.mask_file |
| 155 | + |
| 156 | + return runtime |
0 commit comments