Permalink
Browse files

fix: resting works with no filter regressors, added spm conn workflow…

… and workflow to import files into conn (but this depends on a matlab file that needs to be in the path)
  • Loading branch information...
1 parent eac1aa8 commit 59b5ca7bf238beb7bdcc3112944a8b335790826a @akeshavan akeshavan committed May 22, 2012
@@ -12,6 +12,7 @@
import workflow12
import workflow13
import synced_corr_display_h5
+import workflow14
from .base import (list_workflows, configure_workflow, run_workflow,
display_workflow_info)
@@ -2,7 +2,7 @@
import nipype.algorithms.rapidart as ra # rapid artifact detection
from nipype.interfaces.fsl.utils import EPIDeWarp
from nipype.workflows.smri.freesurfer.utils import create_getmask_flow
-from .modular_nodes import create_mod_smooth, mod_realign, mod_filter
+from .modular_nodes import create_mod_smooth, mod_realign, mod_filter, mod_regressor
import nipype.pipeline.engine as pe
import nipype.interfaces.utility as util
@@ -87,9 +87,14 @@ def try_import(fname):
temp = np.zeros(a.shape)
temp[1:, :] = np.diff(a, axis=0)
out = np.hstack((out, temp))
-
- np.savetxt(filter_file, out)
- return filter_file
+ if out is not None:
+ np.savetxt(filter_file, out)
+ return filter_file
+ else:
+ filter_file = os.path.abspath("empty_file.txt")
+ a = open(filter_file,'w')
+ a.close()
+ return filter_file
def create_prep(name='preproc'):
@@ -586,9 +591,13 @@ def create_rest_prep(name='preproc',fieldmap=False):
'art_outliers'])
# regress out noise
- remove_noise = pe.MapNode(fsl.FilterRegressor(filter_all=True),
- name='regress_nuisance',
- iterfield=['design_file','in_file'])
+ remove_noise = pe.MapNode(util.Function(input_names=["in_file","design_file","mask"],
+ output_names=["out_file"],function=mod_regressor),
+ name='regress_nuisance',iterfield=["in_file","design_file"])
+
+ #pe.MapNode(fsl.FilterRegressor(filter_all=True),
+ # name='regress_nuisance',
+ # iterfield=['design_file','in_file'])
# bandpass filter
#bandpass_filter = pe.MapNode(fsl.TemporalFilter(),
@@ -0,0 +1,71 @@
+import os
+from nipype.interfaces.base import traits, InputMultiPath
+from nipype.interfaces.matlab import MatlabCommand
+import scipy.io as sio
+from nipype.interfaces.base import TraitedSpec, BaseInterface, BaseInterfaceInputSpec
+import shutil
+
+class ConnImportInputSpec(BaseInterfaceInputSpec):
+ functional_files = InputMultiPath(desc="preprocessed functional files")
+ structural_files = InputMultiPath(desc="structural files")
+ csf_mask = InputMultiPath(desc="csf mask files")
+ white_mask = InputMultiPath(desc="white matter mask files")
+ grey_mask = InputMultiPath(desc="grey matter mask files")
+ tr = traits.Float()
+ n_subjects = traits.Int()
+ realignment_parameters = InputMultiPath( desc='realignment parameters')
+ outliers = InputMultiPath( desc='art outliers')
+ norm_components = InputMultiPath(desc='art norm components')
+ project_name = traits.Str(desc="name of the project")
+ script = traits.Str()
+
+class ConnImportOutputSpec(TraitedSpec):
+ conn_inputs = traits.File()
+ conn_batch = traits.File()
+ conn_directory = traits.Directory()
+
+class ConnImport(BaseInterface):
+ input_spec = ConnImportInputSpec
+ output_spec = ConnImportOutputSpec
+
+ def _run_interface(self, runtime):
+ def islist(i):
+ if not isinstance(i,list):
+ i = [str(i)]
+ return i
+ else:
+ I = []
+ for l in i:
+ if not l.endswith('.par'):
+ I.append(str(l))
+ else:
+ shutil.copy2(l,l+'.txt')
+ I.append(l+'.txt')
+ return I
+
+ info = {}
+ info["functional_files"] = islist(self.inputs.functional_files)
+ info["structural_files"] = islist(self.inputs.structural_files)
+ info["csf_mask"] = islist(self.inputs.csf_mask)
+ info["white_mask"] = islist(self.inputs.white_mask)
+ info["grey_mask"] = islist(self.inputs.grey_mask)
+ info["TR"] = float(self.inputs.tr)
+ info["realignment_parameters"] = islist(self.inputs.realignment_parameters)
+ info["outliers"] = islist(self.inputs.outliers)
+ info["norm_components"] = islist(self.inputs.norm_components)
+ info["filename"] = '%s/conn_%s.mat'%(os.getcwd(),self.inputs.project_name)
+ info["n_subjects"] = int(self.inputs.n_subjects)
+ conn_inputs = os.path.abspath('inputs_to_conn.mat')
+ sio.savemat(conn_inputs, {"in":info})
+ print "saved conn_inputs.mat file"
+ script="""load %s; batch=bips_load_conn(in); conn_batch(batch)"""%conn_inputs
+ mlab = MatlabCommand(script=script, mfile=True)
+ result = mlab.run()
+ return result.runtime
+
+ def _list_outputs(self):
+ outputs = self._outputs().get()
+ outputs['conn_inputs'] = os.path.abspath('inputs_to_conn.mat')
+ outputs['conn_batch'] = '%s/conn_%s.mat'%(os.getcwd(),self.inputs.project_name)
+ outputs['conn_directory'] = '%s/conn_%s'%(os.getcwd(),self.inputs.project_name)
+ return outputs
@@ -414,4 +414,17 @@ def mod_filter(in_file,algorithm,lowpass_freq, highpass_freq,tr):
out_img = nib.Nifti1Image(Filtered_data,nib.load(in_file).get_affine())
out_img.to_filename(out_file)
- return out_file
+ return out_file
+
+def mod_regressor(design_file,in_file,mask):
+ import nipype.interfaces.fsl as fsl
+ if "empty_file.txt" in design_file:
+ return in_file
+ else:
+ reg = fsl.FilterRegressor(filter_all=True)
+ reg.inputs.in_file = in_file
+ reg.inputs.design_file = design_file
+ reg.inputs.mask = mask
+ res = reg.run()
+ out_file = res.outputs.out_file
+ return out_file
@@ -196,9 +196,7 @@ def create_spm_preproc(name='preproc'):
poplist = lambda x: x.pop()
#realign = pe.Node(spm.Realign(), name='realign')
- sym_func = pe.Node(niu.Function(input_names=['in_file'],output_names=['out_link'],function=do_symlink),name='func_symlink')
-
- sym_struct = sym_func.clone(name='sym_struct')
+ sym_func = pe.Node(niu.Function(input_names=['in_file'],output_names=['out_link'],function=do_symlink),name='func_symlink')
realign = pe.Node(niu.Function(input_names=['node','in_file','tr','do_slicetime','sliceorder'],
output_names=['out_file','par_file'],
@@ -231,6 +229,7 @@ def create_spm_preproc(name='preproc'):
smooth = pe.Node(spm.Smooth(), name='smooth')
normalize = pe.Node(spm.Normalize(jobtype='write'),name='normalize')
+ normalize_struct = normalize.clone('normalize_struct')
segment = pe.Node(spm.Segment(csf_output_type=[True,True,False],
gm_output_type=[True,True,False],
wm_output_type=[True,True,False]),name='segment')
@@ -266,13 +265,15 @@ def create_spm_preproc(name='preproc'):
workflow.connect(convert2nii,'out_file',segment,'data')
workflow.connect(segment, 'transformation_mat', normalize, 'parameter_file')
+ workflow.connect(segment, 'transformation_mat', normalize_struct, 'parameter_file')
+ workflow.connect(convert2nii,'out_file',normalize_struct, 'apply_to_files')
workflow.connect(realign,'out_file',normalize, 'apply_to_files')
#normalize.inputs.template='/software/spm8/templates/EPI.nii'
workflow.connect(normalize,'normalized_files',smooth,'in_files')
#workflow.connect(realign, 'realigned_files', smooth, 'in_files')
artdetect = pe.Node(ra.ArtifactDetect(mask_type='file',
- parameter_source='SPM',
+ parameter_source='FSL',
use_differences=[True,False],
use_norm=True,
save_plot=True),
@@ -298,13 +299,15 @@ def create_spm_preproc(name='preproc'):
'outlier_files',
'outlier_stats',
'outlier_plots',
+ 'norm_components',
'mod_csf',
'unmod_csf',
'mod_wm',
'unmod_wm',
'mod_gm',
'unmod_gm',
- 'mean'
+ 'mean',
+ 'normalized_struct'
]),
name="outputspec")
workflow.connect([
@@ -315,7 +318,8 @@ def create_spm_preproc(name='preproc'):
(smooth, outputnode, [('smoothed_files', 'smoothed_files')]),
(artdetect, outputnode,[('outlier_files', 'outlier_files'),
('statistic_files','outlier_stats'),
- ('plot_files','outlier_plots')])
+ ('plot_files','outlier_plots'),
+ ('norm_files','norm_components')])
])
workflow.connect(segment,'modulated_csf_image',outputnode,'mod_csf')
workflow.connect(segment,'modulated_wm_image',outputnode,'mod_wm')
@@ -324,6 +328,7 @@ def create_spm_preproc(name='preproc'):
workflow.connect(segment,'normalized_wm_image',outputnode,'unmod_wm')
workflow.connect(segment,'normalized_gm_image',outputnode,'unmod_gm')
workflow.connect(mean,'outputspec.mean_image',outputnode, 'mean')
+ workflow.connect(normalize_struct, 'normalized_files', outputnode, 'normalized_struct')
return workflow
def main(config_file):
@@ -367,6 +372,7 @@ def main(config_file):
workflow.connect(outputspec,'outlier_files',sinker,'spm_preproc.art.@outlier_files')
workflow.connect(outputspec,'outlier_stats',sinker,'spm_preproc.art.@outlier_stats')
workflow.connect(outputspec,'outlier_plots',sinker,'spm_preproc.art.@outlier_plots')
+ workflow.connect(outputspec,'norm_components',sinker,'spm_preproc.art.@norm')
workflow.connect(outputspec,'reg_file',sinker,'spm_preproc.bbreg.@reg_file')
workflow.connect(outputspec,'reg_cost',sinker,'spm_preproc.bbreg.@reg_cost')
workflow.connect(outputspec,'mask_file',sinker,'spm_preproc.mask.@mask_file')
@@ -377,6 +383,7 @@ def main(config_file):
workflow.connect(outputspec,'unmod_wm',sinker,'spm_preproc.segment.unmod.@wm')
workflow.connect(outputspec,'unmod_gm',sinker,'spm_preproc.segment.unmod.@gm')
workflow.connect(outputspec,'mean',sinker,'spm_preproc.mean')
+ workflow.connect(outputspec,'normalized_struct', sinker, 'spm_preproc.normalized_struct')
if c.run_using_plugin:
workflow.run(plugin=c.plugin,plugin_args=c.plugin_args)
Oops, something went wrong.

0 comments on commit 59b5ca7

Please sign in to comment.