|
7 | 7 | from nipype.utils.filemanip import fname_presuffix
|
8 | 8 | from nipype.interfaces.base import (
|
9 | 9 | traits, TraitedSpec, BaseInterfaceInputSpec, File,
|
10 |
| - SimpleInterface |
| 10 | + SimpleInterface, OutputMultiPath, InputMultiPath |
11 | 11 | )
|
12 | 12 |
|
13 | 13 | IFLOGGER = logging.getLogger('nipype.interface')
|
@@ -89,3 +89,67 @@ def _run_interface(self, runtime):
|
89 | 89 | maskimg.to_filename(self._results['out_mask'])
|
90 | 90 |
|
91 | 91 | return runtime
|
| 92 | + |
| 93 | +class _Save4Dto3DInputSpec(BaseInterfaceInputSpec): |
| 94 | + in_file = File(exists=True, mandatory=True, desc='input 4d image') |
| 95 | + |
| 96 | + |
| 97 | +class _Save4Dto3DOutputSpec(TraitedSpec): |
| 98 | + out_files = OutputMultiPath(File(exists=True), |
| 99 | + desc='output list of 3d images') |
| 100 | + |
| 101 | + |
| 102 | +class Save4Dto3D(SimpleInterface): |
| 103 | + """Split a 4D dataset along the last dimension |
| 104 | + into multiple 3D volumes.""" |
| 105 | + |
| 106 | + input_spec = _Save4Dto3DInputSpec |
| 107 | + output_spec = _Save4Dto3DOutputSpec |
| 108 | + |
| 109 | + def _run_interface(self, runtime): |
| 110 | + filenii = nb.load(self.inputs.in_file) |
| 111 | + if len(filenii.shape) != 4: |
| 112 | + raise RuntimeError('Input image (%s) is not 4D.' % filenii) |
| 113 | + |
| 114 | + files_3d = nb.four_to_three(filenii) |
| 115 | + out_files = [] |
| 116 | + for i, file_3d in enumerate(files_3d): |
| 117 | + out_file = fname_presuffix(in_file, suffix="_tmp_{}".format(i)) |
| 118 | + file_3d.to_filename(out_file) |
| 119 | + out_files.append(out_file) |
| 120 | + |
| 121 | + self._results['out_files'] = out_files |
| 122 | + return runtime |
| 123 | + |
| 124 | + |
| 125 | +class _Save3Dto4DInputSpec(BaseInterfaceInputSpec): |
| 126 | + in_files = InputMultiPath(File(exists=True, mandatory=True, |
| 127 | + desc='input list of 3d images')) |
| 128 | + |
| 129 | + |
| 130 | +class _Save3Dto4DOutputSpec(TraitedSpec): |
| 131 | + out_file = File(exists=True, desc='output list of 3d images') |
| 132 | + |
| 133 | + |
| 134 | +class Save3Dto4D(SimpleInterface): |
| 135 | + """Merge a list of 3D volumes along the last dimension into a single |
| 136 | + 4D image.""" |
| 137 | + |
| 138 | + input_spec = _Save4Dto3DInputSpec |
| 139 | + output_spec = _Save4Dto3DOutputSpec |
| 140 | + |
| 141 | + def _run_interface(self, runtime): |
| 142 | + nii_list = [] |
| 143 | + for i, f in enumerate(self.inputs.in_files): |
| 144 | + filenii = nb.load(f) |
| 145 | + filenii = nb.squeeze_image(filenii) |
| 146 | + if len(filenii.shape) != 3: |
| 147 | + raise RuntimeError('Input image (%s) is not 3D.' % f) |
| 148 | + else: |
| 149 | + nii_list.append(filenii) |
| 150 | + img_4d = nb.funcs.concat_images(nii_list) |
| 151 | + out_file = fname_presuffix(self.inputs.in_files[0], suffix="_merged") |
| 152 | + img_4d.to_filename(out_file) |
| 153 | + |
| 154 | + self._results['out_file'] = out_file |
| 155 | + return runtime |
0 commit comments