From 22ef063d06eeb74ffcee8210c2dc6f4e0b9bb668 Mon Sep 17 00:00:00 2001 From: Satrajit Ghosh Date: Fri, 31 May 2013 18:13:54 -0400 Subject: [PATCH 1/3] fix: spm_realign code to handle 3d files without registering to mean --- nipype/interfaces/spm/preprocess.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/nipype/interfaces/spm/preprocess.py b/nipype/interfaces/spm/preprocess.py index 73549c191f..1c82ebd162 100644 --- a/nipype/interfaces/spm/preprocess.py +++ b/nipype/interfaces/spm/preprocess.py @@ -120,7 +120,8 @@ class RealignInputSpec(SPMCommandInputSpec): desc='gaussian smoothing kernel width') separation = traits.Range(low=0.0, field='eoptions.sep', desc='sampling separation in mm') - register_to_mean = traits.Bool(field='eoptions.rtm', + register_to_mean = traits.Bool(True, field='eoptions.rtm', + mandatory=True, usedefault=True, desc='Indicate whether realignment is done to the mean image') weight_img = File(exists=True, field='eoptions.weight', desc='filename of weighting image') @@ -213,14 +214,26 @@ def _list_outputs(self): outputs['mean_image'] = fname_presuffix(first_image, prefix='mean') if resliced_all: + reg_to_mean = self.inputs.register_to_mean outputs['realigned_files'] = [] - for imgf in filename_to_list(self.inputs.in_files): + for idx, imgf in enumerate(filename_to_list(self.inputs.in_files)): realigned_run = [] - if isinstance(imgf,list): - for inner_imgf in filename_to_list(imgf): - realigned_run.append(fname_presuffix(inner_imgf, prefix=self.inputs.out_prefix)) + if isinstance(imgf, list): + for i, inner_imgf in enumerate(filename_to_list(imgf)): + newfile = fname_presuffix(inner_imgf, + prefix=self.inputs.out_prefix) + if os.path.exists(newfile): + realigned_run.append(newfile) + continue + if idx==0 and i == 0 and not reg_to_mean and \ + func_is_3d(inner_imgf): + realigned_run.append(fname_presuffix(inner_imgf, + prefix='')) else: - realigned_run = fname_presuffix(imgf, prefix=self.inputs.out_prefix) + realigned_run = fname_presuffix(imgf, + prefix=self.inputs.out_prefix) + if idx==0 and not reg_to_mean and func_is_3d(imgf): + realigned_run = fname_presuffix(imgf, prefix='') outputs['realigned_files'].append(realigned_run) return outputs From 4271c909f55c3aff8d10bf72060c49b665e05b88 Mon Sep 17 00:00:00 2001 From: Satrajit Ghosh Date: Fri, 31 May 2013 20:43:08 -0400 Subject: [PATCH 2/3] fix: spm_realign always reslices to the first volume even if register_to_mean rtm is set to True --- nipype/interfaces/spm/preprocess.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/nipype/interfaces/spm/preprocess.py b/nipype/interfaces/spm/preprocess.py index 1c82ebd162..2d54d73bda 100644 --- a/nipype/interfaces/spm/preprocess.py +++ b/nipype/interfaces/spm/preprocess.py @@ -225,14 +225,13 @@ def _list_outputs(self): if os.path.exists(newfile): realigned_run.append(newfile) continue - if idx==0 and i == 0 and not reg_to_mean and \ - func_is_3d(inner_imgf): + if idx==0 and i == 0 and func_is_3d(inner_imgf): realigned_run.append(fname_presuffix(inner_imgf, prefix='')) else: realigned_run = fname_presuffix(imgf, prefix=self.inputs.out_prefix) - if idx==0 and not reg_to_mean and func_is_3d(imgf): + if idx==0 and func_is_3d(imgf): realigned_run = fname_presuffix(imgf, prefix='') outputs['realigned_files'].append(realigned_run) return outputs From 123247fb62626654c9a06d7300fab8632972ee4a Mon Sep 17 00:00:00 2001 From: Satrajit Ghosh Date: Wed, 3 Jul 2013 09:19:24 -0400 Subject: [PATCH 3/3] sty: many pep8 fixes --- CHANGES | 1 + nipype/interfaces/spm/preprocess.py | 225 +++++++++++++++++----------- 2 files changed, 140 insertions(+), 86 deletions(-) diff --git a/CHANGES b/CHANGES index ffe2052d44..51270cd353 100644 --- a/CHANGES +++ b/CHANGES @@ -1,6 +1,7 @@ Next release ============ +* FIX: Deals properly with 3d files in SPM Realign Release 0.8.0 (May 8, 2013) =========================== diff --git a/nipype/interfaces/spm/preprocess.py b/nipype/interfaces/spm/preprocess.py index 0995b39106..e97b3a4aa0 100644 --- a/nipype/interfaces/spm/preprocess.py +++ b/nipype/interfaces/spm/preprocess.py @@ -27,20 +27,22 @@ from nipype.utils.filemanip import (fname_presuffix, filename_to_list, list_to_filename, split_filename) + class SliceTimingInputSpec(SPMCommandInputSpec): - in_files = InputMultiPath(traits.Either(traits.List(File(exists=True)),File(exists=True)), field='scans', + in_files = InputMultiPath(traits.Either(traits.List(File(exists=True)), + File(exists=True)), field='scans', desc='list of filenames to apply slice timing', mandatory=True, copyfile=False) num_slices = traits.Int(field='nslices', desc='number of slices in a volume', mandatory=True) time_repetition = traits.Float(field='tr', - desc='time between volume acquisitions ' \ - '(start to start time)', + desc=('time between volume acquisitions ' + '(start to start time)'), mandatory=True) time_acquisition = traits.Float(field='ta', - desc='time of volume acquisition. usually ' \ - 'calculated as TR-(TR/num_slices)', + desc=('time of volume acquisition. usually ' + 'calculated as TR-(TR/num_slices)'), mandatory=True) slice_order = traits.List(traits.Int(), field='so', desc='1-based order in which slices are acquired', @@ -49,10 +51,13 @@ class SliceTimingInputSpec(SPMCommandInputSpec): desc='1-based Number of the reference slice', mandatory=True) out_prefix = traits.String('a', field='prefix', usedefault=True, - desc = 'slicetimed output prefix') + desc='slicetimed output prefix') + class SliceTimingOutputSpec(TraitedSpec): - timecorrected_files = OutputMultiPath(File(exist=True, desc='slice time corrected files')) + timecorrected_files = OutputMultiPath(File(exist=True, + desc='slice time corrected files')) + class SliceTiming(SPMCommand): """Use spm to perform slice timing correction. @@ -93,11 +98,10 @@ def _list_outputs(self): outputs = self._outputs().get() outputs['timecorrected_files'] = [] - filelist = filename_to_list(self.inputs.in_files) for f in filelist: run = [] - if isinstance(f,list): + if isinstance(f, list): for inner_f in filename_to_list(f): run.append(fname_presuffix(inner_f, prefix=self.inputs.out_prefix)) @@ -109,8 +113,10 @@ def _list_outputs(self): class RealignInputSpec(SPMCommandInputSpec): - in_files = InputMultiPath(traits.Either(traits.List(File(exists=True)),File(exists=True)), field='data', mandatory=True, - desc='list of filenames to realign', copyfile=True) + in_files = InputMultiPath(traits.Either(traits.List(File(exists=True)), + File(exists=True)), field='data', + mandatory=True, copyfile=True, + desc='list of filenames to realign') jobtype = traits.Enum('estwrite', 'estimate', 'write', desc='one of: estimate, write, estwrite', usedefault=True) @@ -124,7 +130,7 @@ class RealignInputSpec(SPMCommandInputSpec): mandatory=True, usedefault=True, desc='Indicate whether realignment is done to the mean image') weight_img = File(exists=True, field='eoptions.weight', - desc='filename of weighting image') + desc='filename of weighting image') interp = traits.Range(low=0, high=7, field='eoptions.interp', desc='degree of b-spline used for interpolation') wrap = traits.List(traits.Int(), minlen=3, maxlen=3, @@ -141,15 +147,18 @@ class RealignInputSpec(SPMCommandInputSpec): write_mask = traits.Bool(field='roptions.mask', desc='True/False mask output image') out_prefix = traits.String('r', field='prefix', usedefault=True, - desc = 'realigned output prefix') + desc='realigned output prefix') class RealignOutputSpec(TraitedSpec): mean_image = File(exists=True, desc='Mean image file from the realignment') - realigned_files = OutputMultiPath(traits.Either(traits.List(File(exists=True)),File(exists=True)), desc='Realigned files') + realigned_files = OutputMultiPath(traits.Either(traits.List(File(exists=True)), + File(exists=True)), + desc='Realigned files') realignment_parameters = OutputMultiPath(File(exists=True), desc='Estimated translation and rotation parameters') + class Realign(SPMCommand): """Use spm_realign for estimating within modality rigid body alignment @@ -185,16 +194,16 @@ def _parse_inputs(self): """validate spm realign options if set to None ignore """ einputs = super(Realign, self)._parse_inputs() - return [{'%s' % (self.inputs.jobtype):einputs[0]}] + return [{'%s' % (self.inputs.jobtype): einputs[0]}] def _list_outputs(self): outputs = self._outputs().get() - resliced_all = self.inputs.write_which[0] > 0 + resliced_all = self.inputs.write_which[0] > 0 resliced_mean = self.inputs.write_which[1] > 0 if isdefined(self.inputs.in_files): outputs['realignment_parameters'] = [] for imgf in self.inputs.in_files: - if isinstance(imgf,list): + if isinstance(imgf, list): tmp_imgf = imgf[0] else: tmp_imgf = imgf @@ -202,8 +211,8 @@ def _list_outputs(self): prefix='rp_', suffix='.txt', use_ext=False)) - if not isinstance(imgf,list) and func_is_3d(imgf): - break; + if not isinstance(imgf, list) and func_is_3d(imgf): + break if self.inputs.jobtype == "write" or self.inputs.jobtype == "estwrite": if isinstance(self.inputs.in_files[0], list): first_image = self.inputs.in_files[0][0] @@ -212,12 +221,8 @@ def _list_outputs(self): if resliced_mean: outputs['mean_image'] = fname_presuffix(first_image, prefix='mean') - - index_start = 0 - if self.inputs.write_which[0]<2: - index_start = 1 + if resliced_all: - reg_to_mean = self.inputs.register_to_mean outputs['realigned_files'] = [] for idx, imgf in enumerate(filename_to_list(self.inputs.in_files)): realigned_run = [] @@ -228,13 +233,14 @@ def _list_outputs(self): if os.path.exists(newfile): realigned_run.append(newfile) continue - if idx==0 and i == 0 and func_is_3d(inner_imgf): + if (idx == 0) and (i == 0) and \ + func_is_3d(inner_imgf): realigned_run.append(fname_presuffix(inner_imgf, prefix='')) else: realigned_run = fname_presuffix(imgf, prefix=self.inputs.out_prefix) - if idx==0 and func_is_3d(imgf): + if (idx == 0) and func_is_3d(imgf): realigned_run = fname_presuffix(imgf, prefix='') outputs['realigned_files'].append(realigned_run) return outputs @@ -272,13 +278,14 @@ class CoregisterInputSpec(SPMCommandInputSpec): write_mask = traits.Bool(field='roptions.mask', desc='True/False mask output image') out_prefix = traits.String('r', field='roptions.prefix', usedefault=True, - desc = 'coregistered output prefix') + desc='coregistered output prefix') class CoregisterOutputSpec(TraitedSpec): coregistered_source = OutputMultiPath(File(exists=True), desc='Coregistered source files') - coregistered_files = OutputMultiPath(File(exists=True), desc='Coregistered other files') + coregistered_files = OutputMultiPath(File(exists=True), + desc='Coregistered other files') class Coregister(SPMCommand): @@ -320,12 +327,12 @@ def _format_arg(self, opt, spec, val): def _parse_inputs(self): """validate spm coregister options if set to None ignore """ - if self.inputs.jobtype == "write": + if self.inputs.jobtype == "write": einputs = super(Coregister, self)._parse_inputs(skip=('jobtype', 'apply_to_files')) else: einputs = super(Coregister, self)._parse_inputs(skip=('jobtype')) jobtype = self.inputs.jobtype - return [{'%s' % (jobtype):einputs[0]}] + return [{'%s' % (jobtype): einputs[0]}] def _list_outputs(self): outputs = self._outputs().get() @@ -346,6 +353,7 @@ def _list_outputs(self): return outputs + class NormalizeInputSpec(SPMCommandInputSpec): template = File(exists=True, field='eoptions.template', desc='template file to normalize to', @@ -381,15 +389,20 @@ class NormalizeInputSpec(SPMCommandInputSpec): desc='the amount of the regularization for the nonlinear part of the normalization (opt)') write_preserve = traits.Bool(field='roptions.preserve', desc='True/False warped images are modulated (opt,)') - write_bounding_box = traits.List(traits.List(traits.Float(), minlen=3, maxlen=3), field='roptions.bb', minlen=2, maxlen=2, desc='3x2-element list of lists (opt)') - write_voxel_sizes = traits.List(traits.Float(), field='roptions.vox', minlen=3, maxlen=3, desc='3-element list (opt)') + write_bounding_box = traits.List(traits.List(traits.Float(), minlen=3, + maxlen=3), + field='roptions.bb', minlen=2, maxlen=2, + desc='3x2-element list of lists (opt)') + write_voxel_sizes = traits.List(traits.Float(), field='roptions.vox', + minlen=3, maxlen=3, + desc='3-element list (opt)') write_interp = traits.Range(low=0, hign=7, field='roptions.interp', desc='degree of b-spline used for interpolation') write_wrap = traits.List(traits.Int(), field='roptions.wrap', - desc='Check if interpolation should wrap in [x,y,z] - list of bools (opt)') - + desc=('Check if interpolation should wrap in [x,y,z] ' + '- list of bools (opt)')) out_prefix = traits.String('w', field='roptions.prefix', usedefault=True, - desc = 'normalized output prefix') + desc='normalized output prefix') class NormalizeOutputSpec(TraitedSpec): @@ -397,6 +410,7 @@ class NormalizeOutputSpec(TraitedSpec): normalized_source = OutputMultiPath(File(exists=True), desc='Normalized source files') normalized_files = OutputMultiPath(File(exists=True), desc='Normalized other files') + class Normalize(SPMCommand): """use spm_normalise for warping an image to a template @@ -447,7 +461,7 @@ def _parse_inputs(self): if not isdefined(self.inputs.apply_to_files): if isdefined(self.inputs.source): einputs[0]['subj']['resample'] = scans_for_fname(self.inputs.source) - return [{'%s' % (jobtype):einputs[0]}] + return [{'%s' % (jobtype): einputs[0]}] def _list_outputs(self): outputs = self._outputs().get() @@ -476,6 +490,7 @@ def _list_outputs(self): return outputs + class SegmentInputSpec(SPMCommandInputSpec): data = InputMultiPath(File(exists=True), field='data', desc='one scan per subject', copyfile=False, mandatory=True) @@ -524,8 +539,9 @@ class SegmentInputSpec(SPMCommandInputSpec): warp_frequency_cutoff = traits.Float(field='opts.warpco', desc='Cutoff of DCT bases') bias_regularization = traits.Enum(0, 0.00001, 0.0001, 0.001, 0.01, 0.1, 1, 10, field='opts.biasreg', desc='no(0) - extremely heavy (10)') - bias_fwhm = traits.Enum(30, 40, 50, 60, 70 , 80, 90, 100, 110, 120, 130, 'Inf', field='opts.biasfwhm', - desc='FWHM of Gaussian smoothness of bias') + bias_fwhm = traits.Enum(30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, + 'Inf', field='opts.biasfwhm', + desc='FWHM of Gaussian smoothness of bias') sampling_distance = traits.Float(field='opts.samp', desc='Sampling distance on data for parameter estimation') mask_image = File(exists=True, field='opts.msk', @@ -542,10 +558,14 @@ class SegmentOutputSpec(TraitedSpec): native_csf_image = File(desc='native space csf probability map') normalized_csf_image = File(desc='normalized csf probability map') modulated_csf_image = File(desc='modulated, normalized csf probability map') - modulated_input_image = File(deprecated='0.10', new_name='bias_corrected_image', desc='bias-corrected version of input image') - bias_corrected_image = File(desc='bias-corrected version of input image') + modulated_input_image = File(deprecated='0.10', + new_name='bias_corrected_image', + desc='bias-corrected version of input image') + bias_corrected_image = File(desc='bias-corrected version of input image') transformation_mat = File(exists=True, desc='Normalization transformation') - inverse_transformation_mat = File(exists=True, desc='Inverse normalization info') + inverse_transformation_mat = File(exists=True, + desc='Inverse normalization info') + class Segment(SPMCommand): """use spm_segment to separate structural images into different @@ -571,7 +591,7 @@ class Segment(SPMCommand): def _format_arg(self, opt, spec, val): """Convert input to appropriate format for spm """ - clean_masks_dict = {'no':0, 'light':1, 'thorough':2} + clean_masks_dict = {'no': 0, 'light': 1, 'thorough': 2} if opt in ['data', 'tissue_prob_maps']: if isinstance(val, list): @@ -590,16 +610,19 @@ def _list_outputs(self): outputs = self._outputs().get() f = self.inputs.data[0] - for tidx,tissue in enumerate(['gm', 'wm', 'csf']): - outtype = '%s_output_type'%tissue + for tidx, tissue in enumerate(['gm', 'wm', 'csf']): + outtype = '%s_output_type' % tissue if isdefined(getattr(self.inputs, outtype)): - for idx,(image,prefix) in enumerate([('modulated','mw'), - ('normalized','w'), - ('native','')]): + for idx, (image, prefix) in enumerate([('modulated', 'mw'), + ('normalized', 'w'), + ('native', '')]): if getattr(self.inputs, outtype)[idx]: - outfield = '%s_%s_image'%(image,tissue) - outputs[outfield] = fname_presuffix(f, prefix='%sc%d'%(prefix,tidx+1)) - if isdefined(self.inputs.save_bias_corrected) and self.inputs.save_bias_corrected: + outfield = '%s_%s_image' % (image, tissue) + outputs[outfield] = fname_presuffix(f, + prefix='%sc%d' % (prefix, + tidx+1)) + if isdefined(self.inputs.save_bias_corrected) and \ + self.inputs.save_bias_corrected: outputs['bias_corrected_image'] = fname_presuffix(f, prefix='m') t_mat = fname_presuffix(f, suffix='_seg_sn.mat', use_ext=False) outputs['transformation_mat'] = t_mat @@ -607,6 +630,7 @@ def _list_outputs(self): outputs['inverse_transformation_mat'] = invt_mat return outputs + class NewSegmentInputSpec(SPMCommandInputSpec): channel_files = InputMultiPath(File(exists=True), desc="A list of files to be segmented", @@ -635,6 +659,7 @@ class NewSegmentInputSpec(SPMCommandInputSpec): write_deformation_fields = traits.List(traits.Bool(), minlen=2, maxlen=2, field='warp.write', desc="Which deformation fields to write:[Inverse, Forward]") + class NewSegmentOutputSpec(TraitedSpec): native_class_images = traits.List(traits.List(File(exists=True)), desc='native space probability maps') dartel_input_images = traits.List(traits.List(File(exists=True)), desc='dartel imported class images') @@ -646,6 +671,7 @@ class NewSegmentOutputSpec(TraitedSpec): forward_deformation_field = OutputMultiPath(File(exists=True)) inverse_deformation_field = OutputMultiPath(File(exists=True)) + class NewSegment(SPMCommand): """Use spm_preproc8 (New Segment) to separate structural images into different tissue classes. Supports multiple modalities. @@ -737,16 +763,16 @@ def _list_outputs(self): if isdefined(self.inputs.tissues): for i, tissue in enumerate(self.inputs.tissues): if tissue[2][0]: - outputs['native_class_images'][i].append(os.path.join(pth,"c%d%s.nii"%(i+1, base))) + outputs['native_class_images'][i].append(os.path.join(pth, "c%d%s.nii" % (i+1, base))) if tissue[2][1]: - outputs['dartel_input_images'][i].append(os.path.join(pth,"rc%d%s.nii"%(i+1, base))) + outputs['dartel_input_images'][i].append(os.path.join(pth, "rc%d%s.nii" % (i+1, base))) if tissue[3][0]: - outputs['normalized_class_images'][i].append(os.path.join(pth,"wc%d%s.nii"%(i+1, base))) + outputs['normalized_class_images'][i].append(os.path.join(pth, "wc%d%s.nii" % (i+1, base))) if tissue[3][1]: - outputs['modulated_class_images'][i].append(os.path.join(pth,"mwc%d%s.nii"%(i+1, base))) + outputs['modulated_class_images'][i].append(os.path.join(pth, "mwc%d%s.nii" % (i+1, base))) else: for i in range(n_classes): - outputs['native_class_images'][i].append(os.path.join(pth,"c%d%s.nii"%(i+1, base))) + outputs['native_class_images'][i].append(os.path.join(pth, "c%d%s.nii" % (i+1, base))) outputs['transformation_mat'].append(os.path.join(pth, "%s_seg8.mat" % base)) if isdefined(self.inputs.write_deformation_fields): @@ -762,18 +788,27 @@ def _list_outputs(self): outputs['bias_field_images'].append(os.path.join(pth, "BiasField_%s.nii" % (base))) return outputs + class SmoothInputSpec(SPMCommandInputSpec): - in_files = InputMultiPath(File(exists=True), field='data', desc='list of files to smooth', mandatory=True, copyfile=False) - fwhm = traits.Either(traits.List(traits.Float(), minlen=3, maxlen=3), traits.Float(), field='fwhm', desc='3-list of fwhm for each dimension (opt)') - data_type = traits.Int(field='dtype', desc='Data type of the output images (opt)') - implicit_masking = traits.Bool(field='im', desc='A mask implied by a particular voxel value') + in_files = InputMultiPath(File(exists=True), field='data', + desc='list of files to smooth', + mandatory=True, copyfile=False) + fwhm = traits.Either(traits.List(traits.Float(), minlen=3, maxlen=3), + traits.Float(), field='fwhm', + desc='3-list of fwhm for each dimension (opt)') + data_type = traits.Int(field='dtype', + desc='Data type of the output images (opt)') + implicit_masking = traits.Bool(field='im', + desc=('A mask implied by a particular ' + 'voxel value')) out_prefix = traits.String('s', field='prefix', usedefault=True, - desc = 'smoothed output prefix') + desc='smoothed output prefix') class SmoothOutputSpec(TraitedSpec): smoothed_files = OutputMultiPath(File(exists=True), desc='smoothed files') + class Smooth(SPMCommand): """Use spm_smooth for 3D Gaussian smoothing of image volumes. @@ -824,33 +859,42 @@ class DARTELInputSpec(SPMCommandInputSpec): field='warp.settings.template', desc='Prefix for template') regularization_form = traits.Enum('Linear', 'Membrane', 'Bending', - field = 'warp.settings.rform', + field='warp.settings.rform', desc='Form of regularization energy term') - iteration_parameters = traits.List(traits.Tuple(traits.Range(1,10), traits.Tuple(traits.Float, traits.Float, traits.Float), - traits.Enum(1,2,4,8,16,32,64,128,256,512), - traits.Enum(0,0.5,1,2,4,8,16,32)), + iteration_parameters = traits.List(traits.Tuple(traits.Range(1, 10), + traits.Tuple(traits.Float, + traits.Float, + traits.Float), + traits.Enum(1, 2, 4, 8, 16, + 32, 64, 128, + 256, 512), + traits.Enum(0, 0.5, 1, 2, 4, + 8, 16, 32)), minlen=3, maxlen=12, - field = 'warp.settings.param', + field='warp.settings.param', desc="""List of tuples for each iteration - Inner iterations - Regularization parameters - Time points for deformation model - smoothing parameter """) - optimization_parameters = traits.Tuple(traits.Float, traits.Range(1,8), traits.Range(1,8), - field = 'warp.settings.optim', + optimization_parameters = traits.Tuple(traits.Float, traits.Range(1, 8), + traits.Range(1, 8), + field='warp.settings.optim', desc="""Optimization settings a tuple - LM regularization - cycles of multigrid solver - relaxation iterations """) + class DARTELOutputSpec(TraitedSpec): final_template_file = File(exists=True, desc='final DARTEL template') template_files = traits.List(File(exists=True), desc='Templates from different stages of iteration') dartel_flow_fields = traits.List(File(exists=True), desc='DARTEL flow fields') + class DARTEL(SPMCommand): """Use spm DARTEL to create a template and flow fields @@ -877,7 +921,7 @@ def _format_arg(self, opt, spec, val): if opt in ['image_files']: return scans_for_fnames(val, keep4d=True, separate_sessions=True) elif opt == 'regularization_form': - mapper = {'Linear':0, 'Membrane':1, 'Bending':2} + mapper = {'Linear': 0, 'Membrane': 1, 'Bending': 2} return mapper[val] elif opt == 'iteration_parameters': params = [] @@ -902,14 +946,14 @@ def _list_outputs(self): outputs = self._outputs().get() outputs['template_files'] = [] for i in range(6): - outputs['template_files'].append(os.path.realpath('%s_%d.nii'%(self.inputs.template_prefix, i+1))) - outputs['final_template_file'] = os.path.realpath('%s_6.nii'%self.inputs.template_prefix) + outputs['template_files'].append(os.path.realpath('%s_%d.nii' % (self.inputs.template_prefix, i+1))) + outputs['final_template_file'] = os.path.realpath('%s_6.nii' % self.inputs.template_prefix) outputs['dartel_flow_fields'] = [] for filename in self.inputs.image_files[0]: pth, base, ext = split_filename(filename) - outputs['dartel_flow_fields'].append(os.path.realpath('u_%s_%s%s'%(base, - self.inputs.template_prefix, - ext))) + outputs['dartel_flow_fields'].append(os.path.realpath('u_%s_%s%s' % (base, + self.inputs.template_prefix, + ext))) return outputs @@ -938,10 +982,12 @@ class DARTELNorm2MNIInputSpec(SPMCommandInputSpec): traits.Float(), field='mni_norm.fwhm', desc='3-list of fwhm for each dimension') + class DARTELNorm2MNIOutputSpec(TraitedSpec): normalized_files = OutputMultiPath(File(exists=True), desc='Normalized files in MNI space') normalization_parameter_file = File(exists=True, desc='Transform parameters to MNI space') + class DARTELNorm2MNI(SPMCommand): """Use spm DARTEL to normalize data to MNI space @@ -997,9 +1043,9 @@ def _list_outputs(self): prefix = 's' + prefix for filename in self.inputs.apply_to_files: pth, base, ext = split_filename(filename) - outputs['normalized_files'].append(os.path.realpath('%s%s%s'%(prefix, - base, - ext))) + outputs['normalized_files'].append(os.path.realpath('%s%s%s' % (prefix, + base, + ext))) return outputs @@ -1015,15 +1061,16 @@ class CreateWarpedInputSpec(SPMCommandInputSpec): copyfile=False, mandatory=True) iterations = traits.Range(low=0, high=9, - desc="The number of iterations: log2(number of time steps)", - field='crt_warped.K', - ) + desc=("The number of iterations: log2(number of " + "time steps)"), + field='crt_warped.K') interp = traits.Range(low=0, high=7, field='crt_warped.interp', desc='degree of b-spline used for interpolation') + class CreateWarpedOutputSpec(TraitedSpec): - warped_files = traits.List( - File(exists=True, desc='final warped files')) + warped_files = traits.List(File(exists=True, desc='final warped files')) + class CreateWarped(SPMCommand): """Apply a flow field estimated by DARTEL to create warped images @@ -1062,14 +1109,16 @@ def _list_outputs(self): outputs['warped_files'] = [] for filename in self.inputs.image_files: pth, base, ext = split_filename(filename) - outputs['warped_files'].append(os.path.realpath('w%s%s'%(base, - ext))) + outputs['warped_files'].append(os.path.realpath('w%s%s' % (base, + ext))) return outputs + class ApplyDeformationFieldInputSpec(SPMCommandInputSpec): in_files = InputMultiPath(File(exists=True), mandatory=True, field='fnames') - deformation_field = File(exists=True, mandatory=True, field='comp{1}.def' ) - reference_volume = File(exists=True, mandatory=True, field='comp{2}.id.space') + deformation_field = File(exists=True, mandatory=True, field='comp{1}.def') + reference_volume = File(exists=True, mandatory=True, + field='comp{2}.id.space') interp = traits.Range(low=0, high=7, field='interp', desc='degree of b-spline used for interpolation') @@ -1077,6 +1126,7 @@ class ApplyDeformationFieldInputSpec(SPMCommandInputSpec): class ApplyDeformationFieldOutputSpec(TraitedSpec): out_files = OutputMultiPath(File(exists=True)) + class ApplyDeformations(SPMCommand): input_spec = ApplyDeformationFieldInputSpec output_spec = ApplyDeformationFieldOutputSpec @@ -1103,9 +1153,10 @@ def _list_outputs(self): outputs['out_files'] = [] for filename in self.inputs.in_files: _, fname = os.path.split(filename) - outputs['out_files'].append(os.path.realpath('w%s'%fname)) + outputs['out_files'].append(os.path.realpath('w%s' % fname)) return outputs + class VBMSegmentInputSpec(SPMCommandInputSpec): in_files = InputMultiPath( @@ -1211,6 +1262,7 @@ class VBMSegmentInputSpec(SPMCommandInputSpec): field='estwrite.output.warps', desc='forward and inverse field') + class VBMSegmentOuputSpec(TraitedSpec): native_class_images = traits.List(traits.List(File(exists=True)), @@ -1240,6 +1292,7 @@ class VBMSegmentOuputSpec(TraitedSpec): jacobian_determinant_images = OutputMultiPath(File(exists=True)) + class VBMSegment(SPMCommand): """Use VBM8 toolbox to separate structural images into different