Skip to content

Commit 9159924

Browse files
author
bpinsard
committed
adding trim interface that keeps the original dtype storage
1 parent 039cb6b commit 9159924

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

nipype/interfaces/nipy/preprocess.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import numpy as np
66

77
from ...utils.misc import package_check
8-
from ...utils.filemanip import split_filename
8+
from ...utils.filemanip import split_filename, fname_presuffix
99

1010

1111
try:
@@ -181,3 +181,53 @@ def _list_outputs(self):
181181
outputs['out_file'] = self._out_file_path
182182
outputs['par_file'] = self._par_file_path
183183
return outputs
184+
185+
186+
class TrimInputSpec(BaseInterfaceInputSpec):
187+
in_file = File(
188+
exists=True, mandatory=True,
189+
desc="EPI image to trim")
190+
begin_index = traits.Int(
191+
0,usedefault=True,
192+
desc='first volume')
193+
end_index = traits.Int(
194+
0,usedefault=True,
195+
desc='last volume indexed as in python (and 0 for last)')
196+
out_file = File(desc='output filename')
197+
suffix = traits.Str(
198+
'_trim',usedefault=True,
199+
desc='suffix for out_file to use if no out_file provided')
200+
201+
class TrimOutputSpec(TraitedSpec):
202+
out_file = File(exists=True)
203+
204+
class Trim(BaseInterface):
205+
input_spec = TrimInputSpec
206+
output_spec = TrimOutputSpec
207+
208+
def _run_interface(self, runtime):
209+
out_file = self._list_outputs()['out_file']
210+
nii = nb.load(self.inputs.in_file)
211+
if self.inputs.end_index == 0:
212+
s = slice(self.inputs.begin_index,nii.shape[3])
213+
else:
214+
s = slice(self.inputs.begin_index,self.inputs.end_index)
215+
nii2 = nb.Nifti1Image(
216+
nii.get_data()[...,s],
217+
nii.get_affine(),
218+
nii.get_header())
219+
nb.save(nii2,out_file)
220+
return runtime
221+
222+
def _list_outputs(self):
223+
outputs = self.output_spec().get()
224+
outputs['out_file'] = self.inputs.out_file
225+
if not isdefined(outputs['out_file']):
226+
outputs['out_file'] = fname_presuffix(
227+
self.inputs.in_file,
228+
newpath=os.getcwd(),
229+
suffix=self.inputs.suffix)
230+
outputs['out_file'] = os.path.abspath(outputs['out_file'])
231+
return outputs
232+
233+

0 commit comments

Comments
 (0)