Пример #1
0
def run_m_script(m_file):
    """
        Runs a matlab m file for SPM, determining automatically if it must be launched with SPM or SPM Standalone
        If launch with spm standalone, the line 'spm_jobman('run', matlabbatch)' must be removed because unnecessary

    Args:
        m_file: (str) path to Matlab m file

    Returns:
        output_mat_file: (str) path to the SPM.mat file needed in SPM analysis
    """
    import platform
    from os import system
    from os.path import abspath, basename, dirname, isfile, join

    from nipype.interfaces.matlab import MatlabCommand, get_matlab_command

    import clinica.pipelines.statistics_volume.statistics_volume_utils as utls
    from clinica.utils.spm import spm_standalone_is_available

    assert isinstance(m_file, str), "[Error] Argument must be a string"
    if not isfile(m_file):
        raise FileNotFoundError("[Error] File " + m_file + "does not exist")
    assert m_file[-2:] == ".m", (
        "[Error] " + m_file + " is not a Matlab file (extension must be .m)")

    # Generate command line to run
    if spm_standalone_is_available():
        utls.delete_last_line(m_file)
        # SPM standalone must be run directly from its root folder
        if platform.system().lower().startswith("darwin"):
            # Mac OS
            cmdline = (
                "cd $SPMSTANDALONE_HOME && ./run_spm12.sh $MCR_HOME batch " +
                m_file)
        elif platform.system().lower().startswith("linux"):
            # Linux OS
            cmdline = "$SPMSTANDALONE_HOME/run_spm12.sh $MCR_HOME batch " + m_file
        else:
            raise SystemError("Clinica only support Mac OS and Linux")
        system(cmdline)
    else:
        MatlabCommand.set_default_matlab_cmd(get_matlab_command())
        matlab = MatlabCommand()
        if platform.system().lower().startswith("linux"):
            matlab.inputs.args = "-nosoftwareopengl"
        matlab.inputs.paths = dirname(m_file)
        matlab.inputs.script = basename(m_file)[:-2]
        matlab.inputs.single_comp_thread = False
        matlab.inputs.logfile = abspath("./matlab_output.log")
        matlab.run()
    output_mat_file = abspath(
        join(dirname(m_file), "..", "2_sample_t_test", "SPM.mat"))
    if not isfile(output_mat_file):
        raise RuntimeError("Output matrix " + output_mat_file +
                           " was not produced")
    return output_mat_file
Пример #2
0
def run_matlab(caps_dir, output_dir, subjects_visits_tsv, pipeline_parameters):
    """
    Wrap the call of SurfStat using clinicasurfstat.m Matlab script.

    Args:
        caps_dir (str): CAPS directory containing surface-based features
        output_dir (str): Output directory that will contain outputs of clinicasurfstat.m
        subjects_visits_tsv (str): TSV file containing the GLM information
        pipeline_parameters (dict): parameters of StatisticsSurface pipeline
    """
    import os
    from nipype.interfaces.matlab import MatlabCommand, get_matlab_command
    import clinica.pipelines as clinica_pipelines
    from clinica.utils.check_dependency import check_environment_variable
    from clinica.pipelines.statistics_surface.statistics_surface_utils import covariates_to_design_matrix, get_string_format_from_tsv

    path_to_matlab_script = os.path.join(
        os.path.dirname(clinica_pipelines.__path__[0]), 'lib',
        'clinicasurfstat')
    freesurfer_home = check_environment_variable('FREESURFER_HOME',
                                                 'FreeSurfer')

    MatlabCommand.set_default_matlab_cmd(get_matlab_command())
    matlab = MatlabCommand()
    matlab.inputs.paths = path_to_matlab_script
    matlab.inputs.script = """
    clinicasurfstat('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, '%s', %.3f, '%s', %.3f, '%s', %.3f);
    """ % (os.path.join(caps_dir, 'subjects'), output_dir, subjects_visits_tsv,
           covariates_to_design_matrix(pipeline_parameters['contrast'],
                                       pipeline_parameters['covariates']),
           pipeline_parameters['contrast'],
           get_string_format_from_tsv(subjects_visits_tsv),
           pipeline_parameters['glm_type'], pipeline_parameters['group_label'],
           freesurfer_home, pipeline_parameters['custom_file'],
           pipeline_parameters['measure_label'], 'sizeoffwhm',
           pipeline_parameters['full_width_at_half_maximum'],
           'thresholduncorrectedpvalue', 0.001, 'thresholdcorrectedpvalue',
           0.05, 'clusterthreshold', pipeline_parameters['cluster_threshold'])
    # This will create a file: pyscript.m , the pyscript.m is the default name
    matlab.inputs.mfile = True
    # This will stop running with single thread
    matlab.inputs.single_comp_thread = False
    matlab.inputs.logfile = 'group-' + pipeline_parameters[
        'group_label'] + '_matlab.log'

    # cprint("Matlab logfile is located at the following path: %s" % matlab.inputs.logfile)
    # cprint("Matlab script command = %s" % matlab.inputs.script)
    # cprint("MatlabCommand inputs flag: single_comp_thread = %s" % matlab.inputs.single_comp_thread)
    # cprint("MatlabCommand choose which matlab to use(matlab_cmd): %s" % get_matlab_command())
    matlab.run()

    return output_dir
Пример #3
0
def run_m_script(m_file):
    """
        Runs a matlab m file for SPM, determining automatically if it must be launched with SPM or SPM Standalone
        If launch with spm standalone, the line 'spm_jobman('run', matlabbatch)' must be removed because unnecessary

    Args:
        m_file: (str) path to Matlab m file

    Returns:
        output_mat_file: (str) path to the SPM.mat file needed in SPM analysis
    """
    from os.path import isfile, dirname, basename, abspath, join
    from os import system
    from clinica.utils.spm import use_spm_standalone
    import clinica.pipelines.statistics_volume.statistics_volume_utils as utls
    from nipype.interfaces.matlab import MatlabCommand, get_matlab_command
    import platform

    assert isinstance(m_file, str), '[Error] Argument must be a string'
    if not isfile(m_file):
        raise FileNotFoundError('[Error] File ' + m_file + 'does not exist')
    assert m_file[-2:] == '.m', '[Error] ' + m_file + ' is not a Matlab file (extension must be .m)'

    # Generate command line to run
    if use_spm_standalone():
        utls.delete_last_line(m_file)
        # SPM standalone must be run directly from its root folder
        if platform.system().lower().startswith('darwin'):
            # Mac OS
            cmdline = 'cd $SPMSTANDALONE_HOME && ./run_spm12.sh $MCR_HOME batch ' + m_file
        elif platform.system().lower().startswith('linux'):
            # Linux OS
            cmdline = '$SPMSTANDALONE_HOME/run_spm12.sh $MCR_HOME batch ' + m_file
        else:
            raise SystemError('Clinica only support Mac OS and Linux')
        system(cmdline)
    else:
        MatlabCommand.set_default_matlab_cmd(get_matlab_command())
        matlab = MatlabCommand()
        if platform.system().lower().startswith('linux'):
            matlab.inputs.args = '-nosoftwareopengl'
        matlab.inputs.paths = dirname(m_file)
        matlab.inputs.script = basename(m_file)[:-2]
        matlab.inputs.single_comp_thread = False
        matlab.inputs.logfile = abspath('./matlab_output.log')
        matlab.run()
    output_mat_file = abspath(join(dirname(m_file), '..', '2_sample_t_test', 'SPM.mat'))
    if not isfile(output_mat_file):
        raise RuntimeError('Output matrix ' + output_mat_file + ' was not produced')
    return output_mat_file
Пример #4
0
	def _run_interface(self, runtime):
		from nipype.interfaces.spm.base import scans_for_fname,scans_for_fnames
		from nipype.utils.filemanip import filename_to_list,list_to_filename

		# setup parameters
		input_dir = "."
		in_files = "{"
		asl_first = str(self.inputs.first_image_type)
		TR = str(self.inputs.TR)
		# convert images to cell array string in matlab
		for f in sorted(scans_for_fnames(filename_to_list(self.inputs.in_files))):
			in_files += "'"+f+"',\n"
			input_dir = os.path.dirname(f)
		in_files = in_files[:-2]+"}"
		self.input_dir = input_dir

		d = dict(in_files=in_files,in_dir=input_dir,first_image_type=asl_first,TR =TR)
		myscript = Template("""
		warning('off','all');
		cd('$in_dir');
		input = char($in_files);
		asl_script(input,$first_image_type,0,$TR);
		exit;
		""").substitute(d)
		mlab = MatlabCommand(script=myscript,matlab_cmd="matlab -nodesktop -nosplash",mfile=True)
		result = mlab.run()
		return result.runtime
Пример #5
0
 def _run_interface(self, runtime):  # @UnusedVariable
     script = "CreateROI('{}', '{}', '{}');".format(
         self.inputs.in_file, self.inputs.brain_mask,
         self._gen_outfilename())
     mlab = MatlabCommand(script=script, mfile=True)
     result = mlab.run()
     return result.runtime
    def _run_interface(self, runtime):
        d = dict(worldmat=self.inputs.worldmat,
        src=self.inputs.src,
        trg=self.inputs.trg,
        output_file=self.inputs.output_file)
        #this is your MATLAB code template
        script = Template("""worldmat = '$worldmat';
        src = '$src';
        trg = '$trg';
        output_file = '$output_file'
        worldmat2flirtmap(worldmat, src, trg, output_file);
        exit;
        """).substitute(d)

        # mfile = True  will create an .m file with your script and executed.
        # Alternatively
        # mfile can be set to False which will cause the matlab code to be
        # passed
        # as a commandline argument to the matlab executable
        # (without creating any files).
        # This, however, is less reliable and harder to debug
        # (code will be reduced to
        # a single line and stripped of any comments).

        mlab = MatlabCommand(script=script, mfile=True)
        result = mlab.run()
        return result.runtime
Пример #7
0
    def _run_interface(self, runtime):
        from nipype.interfaces.matlab import MatlabCommand
        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
Пример #8
0
    def _run_interface(self, runtime):
        """This is where you implement your script"""
        d = dict(in_dwis=self.inputs.in_dwis,
                 in_mask=self.inputs.in_mask,
                 in_bvals=self.inputs.in_bvals,
                 in_bvecs=self.inputs.in_bvecs,
                 in_b0threshold=self.inputs.in_b0threshold,
                 in_fname=self.inputs.in_fname,
                 in_noddi_toolbox=getNoddiToolBoxPath(),
                 in_noddi_path=getNoddiPath(),
                 extra_noddi_args=getExtraArgs(
                     self.inputs.noise_scaling_factor, self.inputs.tissue_type,
                     self.inputs.matlabpoolsize))

        # this is your MATLAB code template
        script = Template("""
        addpath(genpath('$in_noddi_path'));
        addpath(genpath('$in_noddi_toolbox'));
        in_dwis = '$in_dwis';
        in_mask = '$in_mask';
        in_bvals = '$in_bvals';
        in_bvecs = '$in_bvecs';
        in_b0threshold = $in_b0threshold;
        in_fname = '$in_fname';
        [~,~,~,~,~,~,~] = noddi_fitting(in_dwis, in_mask, in_bvals, in_bvecs, in_b0threshold, in_fname $extra_noddi_args);
        exit;
        """).substitute(d)

        mlab = MatlabCommand(script=script, mfile=True)
        result = mlab.run()
        return result.runtime
Пример #9
0
	def _run_interface(self, runtime):
		from nipype.interfaces.spm.base import scans_for_fname,scans_for_fnames
		from nipype.utils.filemanip import filename_to_list,list_to_filename

		# setup parameters
		d = dict()
		d['in_file'] = str(self.inputs.in_file).replace("[", "{").replace("]","}")
		d['out_file']  = str(self.inputs.out_file)
		myscript = Template("""
		warning('off','all');
		
		input = $in_file;
		output = '$out_file';
		
		if isstr(input)
			input = {input};	
		end			
		for in = input
			in = in{1};
			[pathstr,name,ext] = fileparts(in);
		
			if strcmp(ext,'.txt')
				plot_realignment_parameters(in,output);
			elseif strcmp(ext,'.mat')
				plot_design_matrix(in,output);
			elseif strcmp(ext,'.nii') || strcmp(ext,'.img')
				nifti2jpeg(in,'-axial -histogram');
				system(['jpeg2ps ' output ' ' pathstr '/*.jpg' ]);
			end
		end
	   	exit;
		""").substitute(d)
		mlab = MatlabCommand(script=myscript, matlab_cmd="matlab -nodesktop -nosplash",mfile=True)
		result = mlab.run()
		return result.runtime
Пример #10
0
	def _run_interface(self, runtime):
	
		d = dict(in_file=self.inputs.in_file,mask_file=self.inputs.mask_file,out_file=self.inputs.out_file)

		script = Template("""addpath('/home/sharad/fcon1000/lib/');in_file = '$in_file';mask_file = '$mask_file';out_file = '$out_file';reho(in_file,mask_file,out_file);exit;""").substitute(d)

		mlab = MatlabCommand(script=script, mfile=True)	
		result = mlab.run()
		return result.runtime
Пример #11
0
def run_matlab_cmd(cmd):
    delim = '????????'  # A string that won't occur in the Matlab splash
    matlab_cmd = MatlabCommand(
        script=("fprintf('{}'); fprintf({}); exit;".format(delim, cmd)))
    tmp_dir = tempfile.mkdtemp()
    try:
        result = matlab_cmd.run(cwd=tmp_dir)
        return result.runtime.stdout.split(delim)[1]
    finally:
        shutil.rmtree(tmp_dir)
Пример #12
0
 def _run_interface(self, runtime):  # @UnusedVariable
     script = """
     SaveParamsAsNIfTI('{params}', '{roi}', '{brain_mask}', '{prefix}');
     """.format(
         params=self.inputs.params_file, roi=self.inputs.roi_file,
         brain_mask=self.inputs.brain_mask_file,
         prefix=self.inputs.output_prefix)
     mlab = MatlabCommand(script=script, mfile=True)
     result = mlab.run()
     return result.runtime
    def _run_interface(self, runtime):
        d = dict(in_file=self.inputs.in_file,
        out_folder=self.inputs.out_folder,
        subject_id=self.inputs.subject_id)
        #this is your MATLAB code template
        script = Template("""
        	cd '$out_folder'
        	WaveletDespike('$in_file','$subject_id')""").substitute(d)

        mlab = MatlabCommand(script=script, mfile=True)
        result = mlab.run()
        return result.runtime
Пример #14
0
 def _run_interface(self, runtime):  # @UnusedVariable
     script = """
     protocol = FSL2Protocol('{bvals}', '{bvecs}');
     noddi = MakeModel('{model}');
     batch_fitting('{roi}', protocol, noddi, '{out_file}', {nthreads});
     """.format(
         bvecs=self.inputs.bvecs_file, bvals=self.inputs.bvals_file,
         model=self.inputs.model, roi=self.inputs.roi_file,
         out_file=self._gen_outfilename(), nthreads=self.inputs.nthreads)
     mlab = MatlabCommand(script=script, mfile=True)
     result = mlab.run()
     return result.runtime
Пример #15
0
	def _run_interface(self, runtime):
		from nipype.interfaces.spm.base import scans_for_fname,scans_for_fnames
		from nipype.utils.filemanip import filename_to_list,list_to_filename

		# setup parameters
		d = dict()
		d['in_files'] = str(self.inputs.in_files).replace("[", "{").replace("]","}")
		d['roi_names']  = str(self.inputs.roi_names).replace("[", "{").replace("]","}")
		d['task_name']  = str(self.inputs.task_name)
		d['out_file'] = str(self.inputs.out_file)
		
		myscript = Template("""
		warning('off','all');
		
		in_files = $in_files;
		roi_names = $roi_names;
		task_name = '$task_name';
		out_file = '$out_file';
		
		% sanity check
		if length(roi_names) ~= length(in_files)
			fprint('Error: number of 1D average files not the same as their names');
			exit;
		end
		
		% import 1D ROI average files into N vectors
		roi_img = cell(1,length(in_files));
		for i=1:length(in_files)          
			roi_img{i} = importdata(in_files{i});
		end
		
		% calculate Z-scores
		fid = fopen(out_file, 'wt');
		for i=1:length(roi_img)  
			for j=1:length(roi_img)   
				if i ~= j                   
 					z=atanh(corr(roi_img{i},roi_img{j}));
 					fprintf(fid,'%s,%s_%s,%d,N/A,Z_Score\\n',task_name,roi_names{i},roi_names{j},z);
				end
			end
		end
		fclose(fid);

       	
       


		exit;
		""").substitute(d)
		mlab = MatlabCommand(script=myscript, mfile=True)
		result = mlab.run()
		return result.runtime
Пример #16
0
    def _run_interface(self, runtime):
        d = dict(in_file=self.inputs.in_file, out_file=self.inputs.out_file)
        # this is your MATLAB code template
        script = Template("""oned = load('$in_file');
        bpf = bandpass(oned, [0.01 0.08]);
        bpfdt = detrend(bpf, 2);
        save('$out_file', 'bpfdt', '-ascii');
        exit;""").substitute(d)

        mlab = MatlabCommand(script=script, mfile=True)
        result = mlab.run()

        return result.runtime
Пример #17
0
    def _run_interface(self, runtime):
        a = dict(in_file_a=self.inputs.in_file_a,
                 in_file_b=self.inputs.in_file_b,
                 out_file=self.inputs.out_file)
        # this is your MATLAB code template
        conscript = Template("""moco = load('$in_file_a');
        csf = load('$in_file_b');
        regmodel = horzcat(csf, moco);
        save('$out_file', 'regmodel', '-ascii');
        exit;""").substitute(a)

        z = MatlabCommand(script=conscript, mfile=True)
        res = z.run()

        return res.runtime
Пример #18
0
	def _run_interface(self, runtime):
		# setup parameters
		in_file = "'"+str(self.inputs.in_file)+"'"
		ref_file = "'"+str(self.inputs.ref_file)+"'"
		
		d = dict(in_file=in_file,ref_file=ref_file)
		myscript = Template("""
		warning('off','all');
		cbf = cbfmap_base_pCASL($in_file,$ref_file);
		save([dirname($in_file) '/CBF_pCASL.mat'],'cbf');
		exit;
		""").substitute(d)
		mlab = MatlabCommand(script=myscript,matlab_cmd="matlab -nodesktop -nosplash",mfile=True)
		result = mlab.run()
		return result.runtime
Пример #19
0
	def _run_interface(self, runtime):
		from nipype.interfaces.spm.base import scans_for_fname,scans_for_fnames
		from nipype.utils.filemanip import filename_to_list,list_to_filename

		# setup parameters
		voi_file = str(self.inputs.voi_file)
		voi_name = "'"+str(self.inputs.voi_name)+"'"
		subject  = "'"+str(self.inputs.subject)+"'"
		spm_file = "'"+str(self.inputs.spm_mat_file)+"'"
		contrast = str(self.inputs.comp_contrasts)
		directory = os.path.dirname(re.sub("[\[\]']","",spm_file))
		
		d = dict(voi_file=voi_file,voi_name=voi_name,subject=subject,
				 spm_file=spm_file,directory=directory,contrast=contrast)
		myscript = Template("""
		warning('off','all');

		% copy input 
		spm = $spm_file;		
		load(spm);		
		SPM.VResMS.fname = [SPM.swd '/ResMS.img'];
		SPM.xVol.VRpv.fname = [SPM.swd '/RPV.img'];
		SPM.VM.fname = ls([SPM.swd '/../*/mask.img']);
		for i=1:length(SPM.Vbeta)
			if strcmp(SPM.Vbeta(i).fname(1),'/') == 0
				SPM.Vbeta(i).fname = [SPM.swd '/' SPM.Vbeta(i).fname];				
			end
		end		
		SPM.swd = '$directory';
		save(spm,'SPM');
		cd('$directory');
		%dos(['ln -sf ' path '/../*/*.[ih]* .']);
		load('ppi_master_template.mat')
		P.CompContrasts = $contrast;
		P.VOI=char($voi_file);
		P.Region=char($voi_name);
		P.subject=char($subject);
        	P.directory=char('$directory');
		P.FLmask =1;
		P.equalroi = 0;
		mat = [char($subject),'_analysis_',char($voi_name),'.mat'];
		save(mat,'P');
        	PPPI(mat);
       		exit;
		""").substitute(d)
		mlab = MatlabCommand(script=myscript, mfile=True)
		result = mlab.run()
		return result.runtime
Пример #20
0
    def _run_interface(self, runtime):
        """Creates a dictionary to insert infile and outfile name,
        runs the matlab commands specified and saves the runtime variables"""
        d = dict(in_file=self.inputs.in_file, out_file=self.inputs.out_file)
        # this is your MATLAB code template
        script = Template("""oned = load('$in_file'); 
                bpf = bandpass(oned, [0.01 0.08]);
                bpfdt = detrend(bpf, 2);
                save('$out_file', 'bpfdt', '-ascii');
                exit;
                """).substitute(d)

        mlab = MatlabCommand(script=script, mfile=True)
        result = mlab.run()

        return result.runtime
Пример #21
0
 def _run_interface(self, runtime):  # @UnusedVariable
     self.working_dir = os.path.abspath(os.getcwd())
     script = ("set_param(0,'CharacterEncoding','UTF-8');\n"
               "addpath(genpath('{matlab_dir}'));\n"
               "fillholes('{in_file}', '{out_file}');\n"
               "exit;\n").format(
                   in_file=self.inputs.in_file,
                   out_file=os.path.join(os.getcwd(),
                                         self._gen_filename('out_file')),
                   matlab_dir=os.path.abspath(
                       os.path.join(
                           os.path.dirname(nianalysis.interfaces.__file__),
                           'resources', 'matlab', 'qsm')))
     mlab = MatlabCommand(script=script, mfile=True)
     result = mlab.run()
     return result.runtime
Пример #22
0
 def _run_interface(self, runtime):  # @UnusedVariable
     self.working_dir = os.path.abspath(os.getcwd())
     script = (
         "set_param(0,'CharacterEncoding','UTF-8');\n"
         "addpath(genpath('{matlab_dir}'));\n"
         "QSM('{in_dir}', '{mask_file}', '{out_dir}', {echo_times}, {num_channels});\n"
         "exit;").format(in_dir=self.inputs.in_dir,
                         mask_file=self.inputs.mask_file,
                         out_dir=self.working_dir,
                         echo_times=self.inputs.echo_times,
                         num_channels=self.inputs.num_channels,
                         matlab_dir=os.path.abspath(
                             os.path.join(
                                 os.path.dirname(
                                     nianalysis.interfaces.__file__),
                                 'resources', 'matlab', 'qsm')))
     mlab = MatlabCommand(script=script, mfile=True)
     result = mlab.run()
     return result.runtime
Пример #23
0
	def _run_interface(self, runtime):
		from nipype.interfaces.spm.base import scans_for_fname,scans_for_fnames
		from nipype.utils.filemanip import filename_to_list,list_to_filename

		# setup parameters
		d = dict()
		d['source'] = str(self.inputs.source)
		d['brain']  = str(self.inputs.brain_mask)
		d['white']  = str(self.inputs.white_mask)
		d['movement'] = str(self.inputs.movement)
		d['regressors'] = "'"+str(self.inputs.regressors)+"'"
		#d['directory'] = os.path.dirname(re.sub("[\[\]']","",d['source']))
		myscript = Template("""
		warning('off','all');
		mCompCor($source,$white,$brain,$movement,$regressors);
       	exit;
		""").substitute(d)
		mlab = MatlabCommand(script=myscript, mfile=True)
		result = mlab.run()
		return result.runtime
Пример #24
0
    def version( matlab_cmd = None ):
        """Returns the path to the SPM directory in the Matlab path
        If path not found, returns None.

        Parameters
        ----------
        matlab_cmd : String specifying default matlab command

            default None, will look for environment variable MATLABCMD
            and use if found, otherwise falls back on MatlabCommand
            default of 'matlab -nodesktop -nosplash'

        Returns
        -------
        spm_path : string representing path to SPM directory

            returns None of path not found
        """
        if matlab_cmd is None:
            try:
                matlab_cmd = os.environ['MATLABCMD']
            except:
                matlab_cmd = 'matlab -nodesktop -nosplash'
        mlab = MatlabCommand(matlab_cmd = matlab_cmd)
        mlab.inputs.script = """
        if isempty(which('spm')),
        throw(MException('SPMCheck:NotFound','SPM not in matlab path'));
        end;
        spm_path = spm('dir');
        [name, version] = spm('ver');
        fprintf(1, 'NIPYPE path:%s|name:%s|release:%s', spm_path, name, version);
        exit;
        """
        mlab.inputs.mfile = False
        try:
            out = mlab.run()
        except (IOError,RuntimeError), e:
            # if no Matlab at all -- exception could be raised
            # No Matlab -- no spm
            logger.debug(str(e))
            return None
Пример #25
0
    def version(matlab_cmd=None):
        """Returns the path to the SPM directory in the Matlab path
        If path not found, returns None.

        Parameters
        ----------
        matlab_cmd : String specifying default matlab command

            default None, will look for environment variable MATLABCMD
            and use if found, otherwise falls back on MatlabCommand
            default of 'matlab -nodesktop -nosplash'

        Returns
        -------
        spm_path : string representing path to SPM directory

            returns None of path not found
        """
        if matlab_cmd is None:
            try:
                matlab_cmd = os.environ['MATLABCMD']
            except:
                matlab_cmd = 'matlab -nodesktop -nosplash'
        mlab = MatlabCommand(matlab_cmd=matlab_cmd)
        mlab.inputs.script = """
        if isempty(which('spm')),
        throw(MException('SPMCheck:NotFound','SPM not in matlab path'));
        end;
        spm_path = spm('dir');
        [name, version] = spm('ver');
        fprintf(1, 'NIPYPE path:%s|name:%s|release:%s', spm_path, name, version);
        exit;
        """
        mlab.inputs.mfile = False
        try:
            out = mlab.run()
        except (IOError, RuntimeError), e:
            # if no Matlab at all -- exception could be raised
            # No Matlab -- no spm
            logger.debug(str(e))
            return None
Пример #26
0
    def _run_interface(self, runtime):
        d = dict(in_file=self.inputs.in_file, out_file=self.inputs.out_file)
        # This is your MATLAB code template
        script = Template("""in_file = '$in_file';
                             out_file = '$out_file';
                             ConmapTxt2Mat(in_file, out_file);
                             exit;
                          """).substitute(d)

        # mfile = True  will create an .m file with your script and executed.
        # Alternatively
        # mfile can be set to False which will cause the matlab code to be
        # passed
        # as a commandline argument to the matlab executable
        # (without creating any files).
        # This, however, is less reliable and harder to debug
        # (code will be reduced to
        # a single line and stripped of any comments).
        mlab = MatlabCommand(script=script, mfile=True)
        result = mlab.run()
        return result.runtime
Пример #27
0
    def version( matlab_cmd = None ):
        """Returns the path to the SPM directory in the Matlab path
        If path not found, returns None.

        Parameters
        ----------
        matlab_cmd : String specifying default matlab command
        
            default None, will look for environment variable MATLABCMD
            and use if found, otherwise falls back on MatlabCommand
            default of 'matlab -nodesktop -nosplash'

        Returns
        -------
        spm_path : string representing path to SPM directory

            returns None of path not found
        """
        if matlab_cmd is None:
            try:
                matlab_cmd = os.environ['MATLABCMD']
            except:
                matlab_cmd = 'matlab -nodesktop -nosplash'
        mlab = MatlabCommand(matlab_cmd = matlab_cmd)
        mlab.inputs.script_file = 'spminfo'
        mlab.inputs.script = """
        if isempty(which('spm')),
        throw(MException('SPMCheck:NotFound','SPM not in matlab path'));
        end;
        spm_path = spm('dir');
        fprintf(1, 'NIPYPE  %s', spm_path);
        """
        out = mlab.run()
        if out.runtime.returncode == 0:
            spm_path = sd._strip_header(out.runtime.stdout)
        else:
            logger.debug(out.runtime.stderr)
            return None
        return spm_path
Пример #28
0
class SPMCommand(BaseInterface):
    """Extends `BaseInterface` class to implement SPM specific interfaces.

    WARNING: Pseudo prototype class, meant to be subclassed
    """
    input_spec = SPMCommandInputSpec

    _jobtype = 'basetype'
    _jobname = 'basename'

    _matlab_cmd = None
    _paths = None
    _use_mcr = None

    def __init__(self, **inputs):
        super(SPMCommand, self).__init__(**inputs)
        self.inputs.on_trait_change(self._matlab_cmd_update, ['matlab_cmd',
                                                              'mfile',
                                                              'paths',
                                                              'use_mcr'])
        self._check_mlab_inputs()
        self._matlab_cmd_update()

    @classmethod
    def set_mlab_paths(cls, matlab_cmd=None, paths = None, use_mcr=None):
        cls._matlab_cmd = matlab_cmd
        cls._paths = paths
        cls._use_mcr = use_mcr

    def _matlab_cmd_update(self):
        # MatlabCommand has to be created here,
        # because matlab_cmb is not a proper input
        # and can be set only during init
        self.mlab = MatlabCommand(matlab_cmd=self.inputs.matlab_cmd,
                                  mfile=self.inputs.mfile,
                                  paths=self.inputs.paths,
                                  uses_mcr=self.inputs.use_mcr)
        self.mlab.inputs.script_file = 'pyscript_%s.m' % \
        self.__class__.__name__.split('.')[-1].lower()

    @property
    def jobtype(self):
        return self._jobtype

    @property
    def jobname(self):
        return self._jobname

    def _check_mlab_inputs(self):
        if not isdefined(self.inputs.matlab_cmd) and self._matlab_cmd:
            self.inputs.matlab_cmd = self._matlab_cmd
        if not isdefined(self.inputs.paths) and self._paths:
            self.inputs.paths = self._paths
        if not isdefined(self.inputs.use_mcr) and self._use_mcr:
            self.inputs.use_mcr = self._use_mcr

    def _run_interface(self, runtime):
        """Executes the SPM function using MATLAB."""
        self.mlab.inputs.script = self._make_matlab_command(deepcopy(self._parse_inputs()))
        results = self.mlab.run()
        runtime.returncode = results.runtime.returncode
        if self.mlab.inputs.uses_mcr:
            if 'Skipped' in results.runtime.stdout:
                self.raise_exception(runtime)
        runtime.stdout = results.runtime.stdout
        runtime.stderr = results.runtime.stderr
        runtime.merged = results.runtime.merged
        return runtime

    def _list_outputs(self):
        """Determine the expected outputs based on inputs."""

        raise NotImplementedError


    def _format_arg(self, opt, spec, val):
        """Convert input to appropriate format for SPM."""

        return val

    def _parse_inputs(self, skip=()):
        spmdict = {}
        metadata=dict(field=lambda t : t is not None)
        for name, spec in self.inputs.traits(**metadata).items():
            if skip and name in skip:
                continue
            value = getattr(self.inputs, name)
            if not isdefined(value):
                continue
            field = spec.field
            if '.' in field:
                fields = field.split('.')
                dictref = spmdict
                for f in fields[:-1]:
                    if f not in dictref.keys():
                        dictref[f] = {}
                    dictref = dictref[f]
                dictref[fields[-1]] = self._format_arg(name, spec, value)
            else:
                spmdict[field] = self._format_arg(name, spec, value)
        return [spmdict]

    def _reformat_dict_for_savemat(self, contents):
        """Encloses a dict representation within hierarchical lists.

        In order to create an appropriate SPM job structure, a Python
        dict storing the job needs to be modified so that each dict
        embedded in dict needs to be enclosed as a list element.

        Examples
        --------
        >>> a = SPMCommand()._reformat_dict_for_savemat(dict(a=1,b=dict(c=2,d=3)))
        >>> print a
        [{'a': 1, 'b': [{'c': 2, 'd': 3}]}]

        """
        newdict = {}
        try:
            for key, value in contents.items():
                if isinstance(value, dict):
                    if value:
                        newdict[key] = self._reformat_dict_for_savemat(value)
                    # if value is None, skip
                else:
                    newdict[key] = value

            return [newdict]
        except TypeError:
            print 'Requires dict input'

    def _generate_job(self, prefix='', contents=None):
        """Recursive function to generate spm job specification as a string

        Parameters
        ----------
        prefix : string
            A string that needs to get
        contents : dict
            A non-tuple Python structure containing spm job
            information gets converted to an appropriate sequence of
            matlab commands.

        """
        jobstring = ''
        if contents is None:
            return jobstring
        if isinstance(contents, list):
            for i,value in enumerate(contents):
                newprefix = "%s(%d)" % (prefix, i+1)
                jobstring += self._generate_job(newprefix, value)
            return jobstring
        if isinstance(contents, dict):
            for key,value in contents.items():
                newprefix = "%s.%s" % (prefix, key)
                jobstring += self._generate_job(newprefix, value)
            return jobstring
        if isinstance(contents, np.ndarray):
            if contents.dtype == np.dtype(object):
                if prefix:
                    jobstring += "%s = {...\n"%(prefix)
                else:
                    jobstring += "{...\n"
                for i,val in enumerate(contents):
                    if isinstance(val, np.ndarray):
                        jobstring += self._generate_job(prefix=None,
                                                        contents=val)
                    elif isinstance(val,str):
                        jobstring += '\'%s\';...\n'%(val)
                    else:
                        jobstring += '%s;...\n'%str(val)
                jobstring += '};\n'
            else:
                for i,val in enumerate(contents):
                    for field in val.dtype.fields:
                        if prefix:
                            newprefix = "%s(%d).%s"%(prefix, i+1, field)
                        else:
                            newprefix = "(%d).%s"%(i+1, field)
                        jobstring += self._generate_job(newprefix,
                                                        val[field])
            return jobstring
        if isinstance(contents, str):
            jobstring += "%s = '%s';\n" % (prefix,contents)
            return jobstring
        jobstring += "%s = %s;\n" % (prefix,str(contents))
        return jobstring

    def _make_matlab_command(self, contents, postscript=None):
        """Generates a mfile to build job structure
        Parameters
        ----------

        contents : list
            a list of dicts generated by _parse_inputs
            in each subclass

        cwd : string
            default os.getcwd()

        Returns
        -------
        mscript : string
            contents of a script called by matlab

        """
        cwd = os.getcwd()
        mscript  = """
        %% Generated by nipype.interfaces.spm
        if isempty(which('spm')),
             throw(MException('SPMCheck:NotFound','SPM not in matlab path'));
        end
        [name, ver] = spm('ver');
        fprintf('SPM version: %s Release: %s\\n',name, ver);
        fprintf('SPM path: %s\\n',which('spm'));
        spm('Defaults','fMRI');

        if strcmp(spm('ver'),'SPM8'), spm_jobman('initcfg');end\n
        """
        if self.mlab.inputs.mfile:
            if self.jobname in ['st','smooth','preproc','preproc8','fmri_spec','fmri_est',
                                'factorial_design'] :
                # parentheses
                mscript += self._generate_job('jobs{1}.%s{1}.%s(1)' %
                                              (self.jobtype,self.jobname), contents[0])
            else:
                #curly brackets
                mscript += self._generate_job('jobs{1}.%s{1}.%s{1}' %
                                              (self.jobtype,self.jobname), contents[0])
        else:
            jobdef = {'jobs':[{self.jobtype:[{self.jobname:self.reformat_dict_for_savemat
                                         (contents[0])}]}]}
            savemat(os.path.join(cwd,'pyjobs_%s.mat'%self.jobname), jobdef)
            mscript += "load pyjobs_%s;\n\n" % self.jobname
        mscript += """
        if strcmp(spm('ver'),'SPM8'),
           jobs=spm_jobman('spm5tospm8',{jobs});
        end
        spm_jobman(\'run_nogui\',jobs);\n
        """
        if postscript is not None:
            mscript += postscript
        return mscript
Пример #29
0
    def _run_interface(self, runtime):
        data_dir = op.abspath('./denoise/components')
        if not os.path.exists(data_dir):
            os.makedirs(data_dir)

        in_files = self.inputs.in_files
        if len(self.inputs.in_files) > 1:
            print 'Multiple ({n}) input images detected! Copying to {d}...'.format(n=len(self.inputs.in_files), d=data_dir)
            for in_file in self.inputs.in_files:
                path, name, ext = split_filename(in_file)
                shutil.copyfile(in_file, op.join(data_dir, name) + ext)
                if ext == '.img':
                    shutil.copyfile(op.join(path, name) + '.hdr',
                                    op.join(data_dir, name) + '.hdr')
                elif ext == '.hdr':
                    shutil.copyfile(op.join(path, name) + '.img',
                                    op.join(data_dir, name) + '.img')
            print 'Copied!'
            in_files = self.inputs.in_files

        elif isdefined(self.inputs.in_file4d):
            print 'Single four-dimensional image selected. Splitting and copying to {d}'.format(d=data_dir)
            in_files = nb.four_to_three(self.inputs.in_file4d)
            for in_file in in_files:
                path, name, ext = split_filename(in_file)
                shutil.copyfile(in_file, op.join(data_dir, name) + ext)
            print 'Copied!'

        else:
            print 'Single functional image provided. Ending...'
            in_files = self.inputs.in_files

        nComponents = len(in_files)
        path, name, ext = split_filename(self.inputs.time_course_image)
        shutil.copyfile(self.inputs.time_course_image,
                        op.join(data_dir, name) + ext)

        if ext == '.img':
            shutil.copyfile(op.join(path, name) + '.hdr',
                            op.join(data_dir, name) + '.hdr')
        elif ext == '.hdr':
            shutil.copyfile(op.join(path, name) + '.img',
                            op.join(data_dir, name) + '.img')

        data_dir = op.abspath('./denoise')
        path, name, ext = split_filename(self.inputs.ica_mask_image)
        shutil.copyfile(self.inputs.ica_mask_image,
                        op.join(data_dir, name) + ext)
        if ext == '.img':
            shutil.copyfile(op.join(path, name) + '.hdr',
                            op.join(data_dir, name) + '.hdr')
        elif ext == '.hdr':
            shutil.copyfile(op.join(path, name) + '.img',
                            op.join(data_dir, name) + '.img')
        mask_file = op.join(data_dir, name)
        repetition_time = self.inputs.repetition_time
        neuronal_image = op.abspath(self.inputs.out_neuronal_image)
        non_neuronal_image = op.abspath(self.inputs.out_non_neuronal_image)
        coma_rest_lib_path = op.abspath(self.inputs.coma_rest_lib_path)
        d = dict(
            data_dir=data_dir, mask_name=mask_file, nComponents=nComponents, Tr=repetition_time,
            nameNeuronal=neuronal_image, nameNonNeuronal=non_neuronal_image, coma_rest_lib_path=coma_rest_lib_path)
        script = Template("""
        restlib_path = '$coma_rest_lib_path';
        setup_restlib_paths(restlib_path)
        dataDir = '$data_dir';
        maskName = '$mask_name';
        nCompo = $nComponents;
        Tr = $Tr;
        nameNeuronalData = '$nameNeuronal';
        nameNonNeuronalData = '$nameNonNeuronal';
        denoiseImage(dataDir,maskName,nCompo,Tr,nameNeuronalData,nameNonNeuronalData, restlib_path);
        """).substitute(d)
        result = MatlabCommand(script=script, mfile=True,
                               prescript=[''], postscript=[''])
        r = result.run()
        print 'Neuronal component image saved as {n}'.format(n=neuronal_image)
        print 'Non-neuronal component image saved as {n}'.format(n=non_neuronal_image)
        return runtime
Пример #30
0
        '/opt/Laskenta/Control_Room/Biomedicum/DRIFTER-toolbox/DRIFTER/',
        '/opt/MATLAB/R2015a/spm8/', '/opt/MATLAB/NIfTI_20140122/'
    ])
    mlab = MatlabCommand(
        script=script,
        mfile=True,
        paths=[
            '/opt/Laskenta/Control_Room/Biomedicum/DRIFTER-toolbox/DRIFTER/',
            '/opt/MATLAB/R2015a/spm8/', '/opt/MATLAB/NIfTI_20140122/'
        ],
        terminal_output="stream")

    try:
        os.stat(results_path + data + '_1/drifter/drifter_corrected.nii.gz')
    except:
        drifter_result = mlab.run()
        os.mkdir(results_path + data + '_1/drifter/')
        os.rename(
            results_path + '/drifter_corrected.nii.gz',
            results_path + '/' + data + '_1/drifter/drifter_corrected.nii.gz')

    # Initialize workflow2
    workflow2 = pe.Workflow(name=data + '_2')
    workflow2.base_dir = '.'

    inputnode2 = pe.Node(
        interface=util.IdentityInterface(fields=['drifter_result']),
        name='inputspec2')
    outputnode2 = pe.Node(
        interface=util.IdentityInterface(fields=['result_func']),
        name='outputspec2')
Пример #31
0
    def _run_interface(self, runtime):
        list_path = op.abspath("SubjectList.lst")
        pet_path, _ = nifti_to_analyze(self.inputs.pet_file)
        t1_path, _ = nifti_to_analyze(self.inputs.t1_file)
        f = open(list_path, 'w')
        f.write("%s;%s" % (pet_path, t1_path))
        f.close()

        orig_t1 = nb.load(self.inputs.t1_file)
        orig_affine = orig_t1.get_affine()

        gm_uint8 = switch_datatype(self.inputs.grey_matter_file)
        gm_path, _ = nifti_to_analyze(gm_uint8)
        iflogger.info("Writing to %s" % gm_path)

        fixed_roi_file, fixed_wm, fixed_csf, remap_dict = fix_roi_values(
            self.inputs.roi_file, self.inputs.grey_matter_binary_mask,
            self.inputs.white_matter_file, self.inputs.csf_file,
            self.inputs.use_fs_LUT)

        rois_path, _ = nifti_to_analyze(fixed_roi_file)
        iflogger.info("Writing to %s" % rois_path)
        iflogger.info("Writing to %s" % fixed_wm)
        iflogger.info("Writing to %s" % fixed_csf)

        wm_uint8 = switch_datatype(fixed_wm)
        wm_path, _ = nifti_to_analyze(wm_uint8)
        iflogger.info("Writing to %s" % wm_path)

        csf_uint8 = switch_datatype(fixed_csf)
        csf_path, _ = nifti_to_analyze(csf_uint8)
        iflogger.info("Writing to %s" % csf_path)

        if self.inputs.use_fs_LUT:
            fs_dir = os.environ['FREESURFER_HOME']
            LUT = op.join(fs_dir, "FreeSurferColorLUT.txt")
            dat_path = write_config_dat(fixed_roi_file, LUT, remap_dict)
        else:
            dat_path = write_config_dat(fixed_roi_file)
        iflogger.info("Writing to %s" % dat_path)

        d = dict(list_path=list_path,
                 gm_path=gm_path,
                 wm_path=wm_path,
                 csf_path=csf_path,
                 rois_path=rois_path,
                 dat_path=dat_path,
                 X_PSF=self.inputs.x_dir_point_spread_function_FWHM,
                 Y_PSF=self.inputs.y_dir_point_spread_function_FWHM,
                 Z_PSF=self.inputs.z_dir_point_spread_function_FWHM)
        script = Template("""       
        filelist = '$list_path';
        gm = '$gm_path';
        wm = '$wm_path';
        csf = '$csf_path';
        rois = '$rois_path';
        dat = '$dat_path';
        x_fwhm = '$X_PSF';
        y_fwhm = '$Y_PSF';
        z_fwhm = '$Z_PSF';
        runbatch_nogui(filelist, gm, wm, csf, rois, dat, x_fwhm, y_fwhm, z_fwhm)
        """).substitute(d)
        mlab = MatlabCommand(script=script,
                             mfile=True,
                             prescript=[''],
                             postscript=[''])
        result = mlab.run()

        _, foldername, _ = split_filename(self.inputs.pet_file)
        occu_MG_img = glob.glob("pve_%s/r_volume_Occu_MG.img" % foldername)[0]
        analyze_to_nifti(occu_MG_img, affine=orig_affine)
        occu_meltzer_img = glob.glob("pve_%s/r_volume_Occu_Meltzer.img" %
                                     foldername)[0]
        analyze_to_nifti(occu_meltzer_img, affine=orig_affine)
        meltzer_img = glob.glob("pve_%s/r_volume_Meltzer.img" % foldername)[0]
        analyze_to_nifti(meltzer_img, affine=orig_affine)
        MG_rousset_img = glob.glob("pve_%s/r_volume_MGRousset.img" %
                                   foldername)[0]
        analyze_to_nifti(MG_rousset_img, affine=orig_affine)
        MGCS_img = glob.glob("pve_%s/r_volume_MGCS.img" % foldername)[0]
        analyze_to_nifti(MGCS_img, affine=orig_affine)
        virtual_PET_img = glob.glob("pve_%s/r_volume_Virtual_PET.img" %
                                    foldername)[0]
        analyze_to_nifti(virtual_PET_img, affine=orig_affine)
        centrum_semiovalue_WM_img = glob.glob("pve_%s/r_volume_CSWMROI.img" %
                                              foldername)[0]
        analyze_to_nifti(centrum_semiovalue_WM_img, affine=orig_affine)
        alfano_alfano_img = glob.glob("pve_%s/r_volume_AlfanoAlfano.img" %
                                      foldername)[0]
        analyze_to_nifti(alfano_alfano_img, affine=orig_affine)
        alfano_cs_img = glob.glob("pve_%s/r_volume_AlfanoCS.img" %
                                  foldername)[0]
        analyze_to_nifti(alfano_cs_img, affine=orig_affine)
        alfano_rousset_img = glob.glob("pve_%s/r_volume_AlfanoRousset.img" %
                                       foldername)[0]
        analyze_to_nifti(alfano_rousset_img, affine=orig_affine)
        mg_alfano_img = glob.glob("pve_%s/r_volume_MGAlfano.img" %
                                  foldername)[0]
        analyze_to_nifti(mg_alfano_img, affine=orig_affine)
        mask_img = glob.glob("pve_%s/r_volume_Mask.img" % foldername)[0]
        analyze_to_nifti(mask_img, affine=orig_affine)
        PSF_img = glob.glob("pve_%s/r_volume_PSF.img" % foldername)[0]
        analyze_to_nifti(PSF_img)

        try:
            rousset_mat_file = glob.glob("pve_%s/r_volume_Rousset.mat" %
                                         foldername)[0]
        except IndexError:
            # On Ubuntu using pve64, the matlab file is saved with a capital M
            rousset_mat_file = glob.glob("pve_%s/r_volume_Rousset.Mat" %
                                         foldername)[0]

        shutil.copyfile(rousset_mat_file, op.abspath("r_volume_Rousset.mat"))

        results_text_file = glob.glob("pve_%s/r_volume_pve.txt" %
                                      foldername)[0]
        shutil.copyfile(results_text_file, op.abspath("r_volume_pve.txt"))

        results_matlab_mat = op.abspath("%s_pve.mat" % foldername)
        results_numpy_npz = op.abspath("%s_pve.npz" % foldername)

        out_data = parse_pve_results(results_text_file)
        sio.savemat(results_matlab_mat, mdict=out_data)
        np.savez(results_numpy_npz, **out_data)
        return result.runtime
Пример #32
0
    def _run_interface(self, runtime):
        path, name, ext = split_filename(self.inputs.time_course_image)
        data_dir = op.abspath('./matching')
        copy_to = op.join(data_dir, 'components')
        if not op.exists(copy_to):
            os.makedirs(copy_to)
        copy_to = op.join(copy_to, name)
        shutil.copyfile(self.inputs.time_course_image, copy_to + ext)
        if ext == '.img':
            shutil.copyfile(op.join(path, name) + '.hdr', copy_to + '.hdr')
        elif ext == '.hdr':
            shutil.copyfile(op.join(path, name) + '.img', copy_to + '.img')
        time_course_file = copy_to + '.img'

        path, name, ext = split_filename(self.inputs.ica_mask_image)
        shutil.copyfile(self.inputs.ica_mask_image,
                        op.join(data_dir, name) + ext)
        if ext == '.img':
            shutil.copyfile(
                op.join(path, name) + '.hdr',
                op.join(data_dir, name) + '.hdr')
        elif ext == '.hdr':
            shutil.copyfile(
                op.join(path, name) + '.img',
                op.join(data_dir, name) + '.img')
        mask_file = op.abspath(self.inputs.ica_mask_image)
        repetition_time = self.inputs.repetition_time
        component_file = op.abspath(self.inputs.in_file)
        coma_rest_lib_path = op.abspath(self.inputs.coma_rest_lib_path)
        component_index = self.inputs.component_index
        if isdefined(self.inputs.out_stats_file):
            path, name, ext = split_filename(self.inputs.out_stats_file)
            if not ext == '.mat':
                ext = '.mat'
            out_stats_file = op.abspath(name + ext)
        else:
            if isdefined(self.inputs.subject_id):
                out_stats_file = op.abspath(self.inputs.subject_id + '_IC_' +
                                            str(self.inputs.component_index) +
                                            '.mat')
            else:
                out_stats_file = op.abspath('IC_' +
                                            str(self.inputs.component_index) +
                                            '.mat')

        d = dict(component_file=component_file,
                 IC=component_index,
                 time_course_file=time_course_file,
                 mask_name=mask_file,
                 Tr=repetition_time,
                 coma_rest_lib_path=coma_rest_lib_path,
                 out_stats_file=out_stats_file)
        script = Template("""
        restlib_path = '$coma_rest_lib_path';
        setup_restlib_paths(restlib_path);
        Tr = $Tr;
        out_stats_file = '$out_stats_file';
        component_file = '$component_file';
        maskName = '$mask_name';
        maskData = load_nii(maskName);        
        dataCompSpatial = load_nii(component_file)
        time_course_file = '$time_course_file'
        timeData = load_nii(time_course_file)
        IC = $IC
        [feature dataZ temporalData] = computeFingerprintSpaceTime(dataCompSpatial.img,timeData.img(:,IC),maskData.img,Tr);
        save '$out_stats_file'
        """).substitute(d)
        result = MatlabCommand(script=script,
                               mfile=True,
                               prescript=[''],
                               postscript=[''])
        r = result.run()
        print 'Saving stats file as {s}'.format(s=out_stats_file)
        return runtime
Пример #33
0
    def _run_interface(self, runtime):
        data_dir = op.abspath('./denoise/components')
        if not os.path.exists(data_dir):
            os.makedirs(data_dir)

        in_files = self.inputs.in_files
        if len(self.inputs.in_files) > 1:
            print 'Multiple ({n}) input images detected! Copying to {d}...'.format(
                n=len(self.inputs.in_files), d=data_dir)
            for in_file in self.inputs.in_files:
                path, name, ext = split_filename(in_file)
                shutil.copyfile(in_file, op.join(data_dir, name) + ext)
                if ext == '.img':
                    shutil.copyfile(
                        op.join(path, name) + '.hdr',
                        op.join(data_dir, name) + '.hdr')
                elif ext == '.hdr':
                    shutil.copyfile(
                        op.join(path, name) + '.img',
                        op.join(data_dir, name) + '.img')
            print 'Copied!'
            in_files = self.inputs.in_files

        elif isdefined(self.inputs.in_file4d):
            print 'Single four-dimensional image selected. Splitting and copying to {d}'.format(
                d=data_dir)
            in_files = nb.four_to_three(self.inputs.in_file4d)
            for in_file in in_files:
                path, name, ext = split_filename(in_file)
                shutil.copyfile(in_file, op.join(data_dir, name) + ext)
            print 'Copied!'

        else:
            print 'Single functional image provided. Ending...'
            in_files = self.inputs.in_files

        nComponents = len(in_files)
        path, name, ext = split_filename(self.inputs.time_course_image)
        shutil.copyfile(self.inputs.time_course_image,
                        op.join(data_dir, name) + ext)

        if ext == '.img':
            shutil.copyfile(
                op.join(path, name) + '.hdr',
                op.join(data_dir, name) + '.hdr')
        elif ext == '.hdr':
            shutil.copyfile(
                op.join(path, name) + '.img',
                op.join(data_dir, name) + '.img')

        data_dir = op.abspath('./denoise')
        path, name, ext = split_filename(self.inputs.ica_mask_image)
        shutil.copyfile(self.inputs.ica_mask_image,
                        op.join(data_dir, name) + ext)
        if ext == '.img':
            shutil.copyfile(
                op.join(path, name) + '.hdr',
                op.join(data_dir, name) + '.hdr')
        elif ext == '.hdr':
            shutil.copyfile(
                op.join(path, name) + '.img',
                op.join(data_dir, name) + '.img')
        mask_file = op.join(data_dir, name)
        repetition_time = self.inputs.repetition_time
        neuronal_image = op.abspath(self.inputs.out_neuronal_image)
        non_neuronal_image = op.abspath(self.inputs.out_non_neuronal_image)
        coma_rest_lib_path = op.abspath(self.inputs.coma_rest_lib_path)
        d = dict(data_dir=data_dir,
                 mask_name=mask_file,
                 nComponents=nComponents,
                 Tr=repetition_time,
                 nameNeuronal=neuronal_image,
                 nameNonNeuronal=non_neuronal_image,
                 coma_rest_lib_path=coma_rest_lib_path)
        script = Template("""
        restlib_path = '$coma_rest_lib_path';
        setup_restlib_paths(restlib_path)
        dataDir = '$data_dir';
        maskName = '$mask_name';
        nCompo = $nComponents;
        Tr = $Tr;
        nameNeuronalData = '$nameNeuronal';
        nameNonNeuronalData = '$nameNonNeuronal';
        denoiseImage(dataDir,maskName,nCompo,Tr,nameNeuronalData,nameNonNeuronalData, restlib_path);
        """).substitute(d)
        result = MatlabCommand(script=script,
                               mfile=True,
                               prescript=[''],
                               postscript=[''])
        r = result.run()
        print 'Neuronal component image saved as {n}'.format(n=neuronal_image)
        print 'Non-neuronal component image saved as {n}'.format(
            n=non_neuronal_image)
        return runtime
Пример #34
0
    def _run_interface(self, runtime):
        list_path = op.abspath("SubjectList.lst")
        pet_path, _ = nifti_to_analyze(self.inputs.pet_file)
        t1_path, _ = nifti_to_analyze(self.inputs.t1_file)
        f = open(list_path, 'w')
        f.write("%s;%s" % (pet_path, t1_path))
        f.close()

        orig_t1 = nb.load(self.inputs.t1_file)
        orig_affine = orig_t1.get_affine()

        gm_uint8 = switch_datatype(self.inputs.grey_matter_file)
        gm_path, _ = nifti_to_analyze(gm_uint8)
        iflogger.info("Writing to %s" % gm_path)

        fixed_roi_file, fixed_wm, fixed_csf, remap_dict = fix_roi_values(
            self.inputs.roi_file, self.inputs.grey_matter_binary_mask, 
            self.inputs.white_matter_file,
            self.inputs.csf_file, self.inputs.use_fs_LUT)


        

        rois_path, _ = nifti_to_analyze(fixed_roi_file)
        iflogger.info("Writing to %s" % rois_path)
        iflogger.info("Writing to %s" % fixed_wm)
        iflogger.info("Writing to %s" % fixed_csf)

        wm_uint8 = switch_datatype(fixed_wm)
        wm_path, _ = nifti_to_analyze(wm_uint8)
        iflogger.info("Writing to %s" % wm_path)

        csf_uint8 = switch_datatype(fixed_csf)
        csf_path, _ = nifti_to_analyze(csf_uint8)
        iflogger.info("Writing to %s" % csf_path)

        if self.inputs.use_fs_LUT:
            fs_dir = os.environ['FREESURFER_HOME']
            LUT = op.join(fs_dir, "FreeSurferColorLUT.txt")
            dat_path = write_config_dat(
                fixed_roi_file, LUT, remap_dict)
        else:
            dat_path = write_config_dat(
                fixed_roi_file)
        iflogger.info("Writing to %s" % dat_path)

        d = dict(
            list_path=list_path,
            gm_path=gm_path,
            wm_path=wm_path,
            csf_path=csf_path,
            rois_path=rois_path,
            dat_path=dat_path,
            X_PSF=self.inputs.x_dir_point_spread_function_FWHM,
            Y_PSF=self.inputs.y_dir_point_spread_function_FWHM,
            Z_PSF=self.inputs.z_dir_point_spread_function_FWHM)
        script = Template("""       
        filelist = '$list_path';
        gm = '$gm_path';
        wm = '$wm_path';
        csf = '$csf_path';
        rois = '$rois_path';
        dat = '$dat_path';
        x_fwhm = '$X_PSF';
        y_fwhm = '$Y_PSF';
        z_fwhm = '$Z_PSF';
        runbatch_nogui(filelist, gm, wm, csf, rois, dat, x_fwhm, y_fwhm, z_fwhm)
        """).substitute(d)
        mlab = MatlabCommand(script=script, mfile=True,
                             prescript=[''], postscript=[''])
        result = mlab.run()

        _, foldername, _ = split_filename(self.inputs.pet_file)
        occu_MG_img = glob.glob("pve_%s/r_volume_Occu_MG.img" % foldername)[0]
        analyze_to_nifti(occu_MG_img, affine=orig_affine)
        occu_meltzer_img = glob.glob(
            "pve_%s/r_volume_Occu_Meltzer.img" % foldername)[0]
        analyze_to_nifti(occu_meltzer_img, affine=orig_affine)
        meltzer_img = glob.glob("pve_%s/r_volume_Meltzer.img" % foldername)[0]
        analyze_to_nifti(meltzer_img, affine=orig_affine)
        MG_rousset_img = glob.glob(
            "pve_%s/r_volume_MGRousset.img" % foldername)[0]
        analyze_to_nifti(MG_rousset_img, affine=orig_affine)
        MGCS_img = glob.glob("pve_%s/r_volume_MGCS.img" % foldername)[0]
        analyze_to_nifti(MGCS_img, affine=orig_affine)
        virtual_PET_img = glob.glob(
            "pve_%s/r_volume_Virtual_PET.img" % foldername)[0]
        analyze_to_nifti(virtual_PET_img, affine=orig_affine)
        centrum_semiovalue_WM_img = glob.glob(
            "pve_%s/r_volume_CSWMROI.img" % foldername)[0]
        analyze_to_nifti(centrum_semiovalue_WM_img, affine=orig_affine)
        alfano_alfano_img = glob.glob(
            "pve_%s/r_volume_AlfanoAlfano.img" % foldername)[0]
        analyze_to_nifti(alfano_alfano_img, affine=orig_affine)
        alfano_cs_img = glob.glob("pve_%s/r_volume_AlfanoCS.img" %
                                  foldername)[0]
        analyze_to_nifti(alfano_cs_img, affine=orig_affine)
        alfano_rousset_img = glob.glob(
            "pve_%s/r_volume_AlfanoRousset.img" % foldername)[0]
        analyze_to_nifti(alfano_rousset_img, affine=orig_affine)
        mg_alfano_img = glob.glob("pve_%s/r_volume_MGAlfano.img" %
                                  foldername)[0]
        analyze_to_nifti(mg_alfano_img, affine=orig_affine)
        mask_img = glob.glob("pve_%s/r_volume_Mask.img" % foldername)[0]
        analyze_to_nifti(mask_img, affine=orig_affine)
        PSF_img = glob.glob("pve_%s/r_volume_PSF.img" % foldername)[0]
        analyze_to_nifti(PSF_img)

        try:
            rousset_mat_file = glob.glob(
                "pve_%s/r_volume_Rousset.mat" % foldername)[0]
        except IndexError:
            # On Ubuntu using pve64, the matlab file is saved with a capital M
            rousset_mat_file = glob.glob(
                "pve_%s/r_volume_Rousset.Mat" % foldername)[0]

        shutil.copyfile(rousset_mat_file, op.abspath("r_volume_Rousset.mat"))

        results_text_file = glob.glob(
            "pve_%s/r_volume_pve.txt" % foldername)[0]
        shutil.copyfile(results_text_file, op.abspath("r_volume_pve.txt"))

        results_matlab_mat = op.abspath("%s_pve.mat" % foldername)
        results_numpy_npz = op.abspath("%s_pve.npz" % foldername)

        out_data = parse_pve_results(results_text_file)
        sio.savemat(results_matlab_mat, mdict=out_data)
        np.savez(results_numpy_npz, **out_data)
        return result.runtime
Пример #35
0
    def _run_interface(self, runtime):
        in_files = self.inputs.in_files
        data_dir = op.join(os.getcwd(),'origdata')
        if not op.exists(data_dir):
            os.makedirs(data_dir)
        all_names = []
        print 'Multiple ({n}) input images detected! Copying to {d}...'.format(n=len(self.inputs.in_files), d=data_dir)
        for in_file in self.inputs.in_files:
            path, name, ext = split_filename(in_file)
            shutil.copyfile(in_file, op.join(data_dir, name) + ext)
            if ext == '.img':
                shutil.copyfile(op.join(path, name) + '.hdr', op.join(data_dir, name) + '.hdr')
            elif ext == '.hdr':
                shutil.copyfile(op.join(path, name) + '.img', op.join(data_dir, name) + '.img')
            all_names.append(name)
        print 'Copied!'

        input_files_as_str = op.join(data_dir, os.path.commonprefix(all_names) + '*' + ext)
        number_of_components = self.inputs.desired_number_of_components
        output_dir = os.getcwd()
        prefix = self.inputs.prefix
        d = dict(output_dir=output_dir, prefix=prefix, number_of_components=number_of_components, in_files=input_files_as_str)
        variables = Template("""

        %% After entering the parameters, use icatb_batch_file_run(inputFile);

        modalityType = 'fMRI';
        which_analysis = 1;
        perfType = 1;
        keyword_designMatrix = 'no';
        dataSelectionMethod = 4;
        input_data_file_patterns = {'$in_files'};
        dummy_scans = 0;
        outputDir = '$output_dir';
        prefix = '$prefix';
        maskFile = [];
        group_pca_type = 'subject specific';
        backReconType = 'gica';

        %% Data Pre-processing options
        % 1 - Remove mean per time point
        % 2 - Remove mean per voxel
        % 3 - Intensity normalization
        % 4 - Variance normalization
        preproc_type = 3;
        pcaType = 1;
        pca_opts.stack_data = 'yes';
        pca_opts.precision = 'single';
        pca_opts.tolerance = 1e-4;
        pca_opts.max_iter = 1000;
        numReductionSteps = 2;
        doEstimation = 0;
        estimation_opts.PC1 = 'mean';
        estimation_opts.PC2 = 'mean';
        estimation_opts.PC3 = 'mean';

        numOfPC1 = $number_of_components;
        numOfPC2 = $number_of_components;
        numOfPC3 = 0;

        %% Scale the Results. Options are 0, 1, 2, 3 and 4
        % 0 - Don't scale
        % 1 - Scale to Percent signal change
        % 2 - Scale to Z scores
        % 3 - Normalize spatial maps using the maximum intensity value and multiply timecourses using the maximum intensity value
        % 4 - Scale timecourses using the maximum intensity value and spatial maps using the standard deviation of timecourses
        scaleType = 0;
        algoType = 1;
        refFunNames = {'Sn(1) right*bf(1)', 'Sn(1) left*bf(1)'};
        refFiles = {which('ref_default_mode.nii'), which('ref_left_visuomotor.nii'), which('ref_right_visuomotor.nii')};
        %% ICA Options - Name by value pairs in a cell array. Options will vary depending on the algorithm. See icatb_icaOptions for more details. Some options are shown below.
        %% Infomax -  {'posact', 'off', 'sphering', 'on', 'bias', 'on', 'extended', 0}
        %% FastICA - {'approach', 'symm', 'g', 'tanh', 'stabilization', 'on'}
        icaOptions =  {'posact', 'off', 'sphering', 'on', 'bias', 'on', 'extended', 0};
        """).substitute(d)

        file = open('input_batch.m', 'w')
        file.writelines(variables)
        file.close()

        script = """param_file = icatb_read_batch_file('input_batch.m');
        load(param_file);
        global FUNCTIONAL_DATA_FILTER;
        global ZIP_IMAGE_FILES;
        FUNCTIONAL_DATA_FILTER = '*.nii';
        ZIP_IMAGE_FILES = 'No';
        icatb_runAnalysis(sesInfo, 1);"""

        result = MatlabCommand(script=script, mfile=True, prescript=[''], postscript=[''])
        r = result.run()
        return runtime
Пример #36
0
def runmatlab(input_directory,
              output_directory,
              subjects_visits_tsv,
              design_matrix, contrast,
              str_format,
              glm_type,
              group_label,
              freesurfer_home,
              surface_file,
              path_to_matscript,
              full_width_at_half_maximum,
              threshold_uncorrected_pvalue,
              threshold_corrected_pvalue,
              cluster_threshold,
              feature_label):
    """
        a wrapper the matlab script of surfstat with nipype.

    Args:
        input_directory: surfstat_input_dir where containing all the subjects' output in CAPS directory
        output_directory: output folder to contain the result in CAPS folder
        subjects_visits_tsv: tsv file containing the glm information
        design_matrix: str, the linear model that fits into the GLM, for example '1+group'.
        contrast: string, the contrast matrix for GLM, if the factor you choose is categorized variable, clinica_surfstat will create two contrasts,
                  for example, contrast = 'Label', this will create contrastpos = Label.AD - Label.CN, contrastneg = Label.CN - Label.AD; if the fac-
                  tory that you choose is a continuous factor, clinica_surfstat will just create one contrast, for example, contrast = 'Age', but note,
                  the string name that you choose should be exactly the same with the columns names in your subjects_visits_tsv.
        str_format:string, the str_format which uses to read your tsv file, the type of the string should corresponds exactly with the columns in the tsv file.
                  Defaut parameters, we set these parameters to be some default values, but you can also set it by yourself:
        glm_type: based on the hypothesis, you should define one of the glm types, "group_comparison", "correlation"
        group_label: current group name for this analysis
        freesurfer_home: the environmental variable $FREESURFER_HOME
        surface_file: Specify where to find the data surfaces file in the "CAPS/subject" directory, using specific keywords.
                     For instance, to catch for each subject the cortical thickness, the string used will be:
                     '@subject/@session/t1/freesurfer_cross_sectional/@subject_@session/surf/@[email protected]'
                     More information is available on the documentation page of the surfstat pipelines. The keywords @subject @ session @hemi @fwhm
                     represents the variable parts.
        path_to_matscript: path to find the matlab script
        full_width_at_half_maximum: fwhm for the surface smoothing, default is 20, integer.
        threshold_uncorrected_pvalue: threshold to display the uncorrected Pvalue, float, default is 0.001.
        threshold_corrected_pvalue: the threshold to display the corrected cluster, default is 0.05, float.
        cluster_threshold: threshold to define a cluster in the process of cluster-wise correction, default is 0.001, float.

    Returns:

    """
    from nipype.interfaces.matlab import MatlabCommand, get_matlab_command
    from os.path import join
    import sys
    from clinica.utils.stream import cprint

    MatlabCommand.set_default_matlab_cmd(
        get_matlab_command())  # this is to set the matlab_path(os.environ) in your bashrc file, to choose which version of matlab do you wanna use
    # here, set_default_matlab_cmd is a @classmethod
    matlab = MatlabCommand()

    # add the dynamic traits
    # openGL_trait = traits.Bool(True, argstr='-nosoftwareopengl', usedefault=True, desc='Switch on hardware openGL', nohash=True)
    # matlab.input_spec.add_trait(matlab.input_spec(), 'nosoftwareopengl', openGL_trait() )
    if sys.platform.startswith('linux'):
        matlab.inputs.args = '-nosoftwareopengl'  # Bug, for my laptop, it does not work, but the command line does have the flag -nosoftwareopengl, we should try on other computer's matlab to check if this flag works!
    matlab.inputs.paths = path_to_matscript  # CLINICA_HOME, this is the path to add into matlab, addpath

    matlab.inputs.script = """
    clinicasurfstat('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, '%s', %.3f, '%s', %.3f, '%s', %.3f);
    """ % (input_directory, output_directory, subjects_visits_tsv, design_matrix, contrast, str_format, glm_type, group_label, freesurfer_home, surface_file, feature_label, 'sizeoffwhm',
           full_width_at_half_maximum,
           'thresholduncorrectedpvalue', threshold_uncorrected_pvalue, 'thresholdcorrectedpvalue',
           threshold_corrected_pvalue, 'clusterthreshold',
           cluster_threshold)  # here, we should define the inputs for the matlab function that you want to use
    matlab.inputs.mfile = True  # this will create a file: pyscript.m , the pyscript.m is the default name
    matlab.inputs.single_comp_thread = False  # this will stop runing with single thread
    matlab.inputs.logfile = join(output_directory, "matlab_output.log")
    cprint("Matlab logfile is located in the folder: %s" % matlab.inputs.logfile)
    cprint("Matlab script command = %s" % matlab.inputs.script)
    cprint("MatlabCommand inputs flag: single_comp_thread = %s" % matlab.inputs.single_comp_thread)
    cprint("MatlabCommand choose which matlab to use(matlab_cmd): %s" % get_matlab_command())
    if sys.platform.startswith('linux'):
        cprint("MatlabCommand inputs flag: nosoftwareopengl = %s" % matlab.inputs.args)
    out = matlab.run()
    return out
Пример #37
0
    def _run_interface(self, runtime):
        in_files = self.inputs.in_files
        data_dir = op.join(os.getcwd(), 'origdata')
        if not op.exists(data_dir):
            os.makedirs(data_dir)
        all_names = []
        print 'Multiple ({n}) input images detected! Copying to {d}...'.format(
            n=len(self.inputs.in_files), d=data_dir)
        for in_file in self.inputs.in_files:
            path, name, ext = split_filename(in_file)
            shutil.copyfile(in_file, op.join(data_dir, name) + ext)
            if ext == '.img':
                shutil.copyfile(
                    op.join(path, name) + '.hdr',
                    op.join(data_dir, name) + '.hdr')
            elif ext == '.hdr':
                shutil.copyfile(
                    op.join(path, name) + '.img',
                    op.join(data_dir, name) + '.img')
            all_names.append(name)
        print 'Copied!'

        input_files_as_str = op.join(
            data_dir,
            os.path.commonprefix(all_names) + '*' + ext)
        number_of_components = self.inputs.desired_number_of_components
        output_dir = os.getcwd()
        prefix = self.inputs.prefix
        d = dict(output_dir=output_dir,
                 prefix=prefix,
                 number_of_components=number_of_components,
                 in_files=input_files_as_str)
        variables = Template("""

        %% After entering the parameters, use icatb_batch_file_run(inputFile);

        modalityType = 'fMRI';
        which_analysis = 1;
        perfType = 1;
        keyword_designMatrix = 'no';
        dataSelectionMethod = 4;
        input_data_file_patterns = {'$in_files'};
        dummy_scans = 0;
        outputDir = '$output_dir';
        prefix = '$prefix';
        maskFile = [];
        group_pca_type = 'subject specific';
        backReconType = 'gica';

        %% Data Pre-processing options
        % 1 - Remove mean per time point
        % 2 - Remove mean per voxel
        % 3 - Intensity normalization
        % 4 - Variance normalization
        preproc_type = 3;
        pcaType = 1;
        pca_opts.stack_data = 'yes';
        pca_opts.precision = 'single';
        pca_opts.tolerance = 1e-4;
        pca_opts.max_iter = 1000;
        numReductionSteps = 2;
        doEstimation = 0;
        estimation_opts.PC1 = 'mean';
        estimation_opts.PC2 = 'mean';
        estimation_opts.PC3 = 'mean';

        numOfPC1 = $number_of_components;
        numOfPC2 = $number_of_components;
        numOfPC3 = 0;

        %% Scale the Results. Options are 0, 1, 2, 3 and 4
        % 0 - Don't scale
        % 1 - Scale to Percent signal change
        % 2 - Scale to Z scores
        % 3 - Normalize spatial maps using the maximum intensity value and multiply timecourses using the maximum intensity value
        % 4 - Scale timecourses using the maximum intensity value and spatial maps using the standard deviation of timecourses
        scaleType = 0;
        algoType = 1;
        refFunNames = {'Sn(1) right*bf(1)', 'Sn(1) left*bf(1)'};
        refFiles = {which('ref_default_mode.nii'), which('ref_left_visuomotor.nii'), which('ref_right_visuomotor.nii')};
        %% ICA Options - Name by value pairs in a cell array. Options will vary depending on the algorithm. See icatb_icaOptions for more details. Some options are shown below.
        %% Infomax -  {'posact', 'off', 'sphering', 'on', 'bias', 'on', 'extended', 0}
        %% FastICA - {'approach', 'symm', 'g', 'tanh', 'stabilization', 'on'}
        icaOptions =  {'posact', 'off', 'sphering', 'on', 'bias', 'on', 'extended', 0};
        """).substitute(d)

        file = open('input_batch.m', 'w')
        file.writelines(variables)
        file.close()

        script = """param_file = icatb_read_batch_file('input_batch.m');
        load(param_file);
        global FUNCTIONAL_DATA_FILTER;
        global ZIP_IMAGE_FILES;
        FUNCTIONAL_DATA_FILTER = '*.nii';
        ZIP_IMAGE_FILES = 'No';
        icatb_runAnalysis(sesInfo, 1);"""

        result = MatlabCommand(script=script,
                               mfile=True,
                               prescript=[''],
                               postscript=[''])
        r = result.run()
        return runtime
Пример #38
0
    def _run_interface(self, runtime):
        path, name, ext = split_filename(self.inputs.time_course_image)
        data_dir = op.abspath('./matching')
        copy_to = op.join(data_dir, 'components')
        if not op.exists(copy_to):
            os.makedirs(copy_to)
        copy_to = op.join(copy_to, name)
        shutil.copyfile(self.inputs.time_course_image, copy_to + ext)
        if ext == '.img':
            shutil.copyfile(op.join(path, name) + '.hdr', copy_to + '.hdr')
        elif ext == '.hdr':
            shutil.copyfile(op.join(path, name) + '.img', copy_to + '.img')

        data_dir = op.abspath('./matching/components')
        in_files = self.inputs.in_files
        if len(self.inputs.in_files) > 1:
            print 'Multiple ({n}) input images detected! Copying to {d}...'.format(
                n=len(self.inputs.in_files), d=data_dir)
            for in_file in self.inputs.in_files:
                path, name, ext = split_filename(in_file)
                shutil.copyfile(in_file, op.join(data_dir, name) + ext)
                if ext == '.img':
                    shutil.copyfile(
                        op.join(path, name) + '.hdr',
                        op.join(data_dir, name) + '.hdr')
                elif ext == '.hdr':
                    shutil.copyfile(
                        op.join(path, name) + '.img',
                        op.join(data_dir, name) + '.img')
            print 'Copied!'

        elif isdefined(self.inputs.in_file4d):
            print 'Single four-dimensional image selected. Splitting and copying to {d}'.format(
                d=data_dir)
            in_files = nb.four_to_three(self.inputs.in_file4d)
            for in_file in in_files:
                path, name, ext = split_filename(in_file)
                shutil.copyfile(in_file, op.join(data_dir, name) + ext)
            print 'Copied!'

        else:
            raise Exception('Single functional image provided. Ending...')
            in_files = self.inputs.in_files

        nComponents = len(in_files)
        repetition_time = self.inputs.repetition_time
        coma_rest_lib_path = op.abspath(self.inputs.coma_rest_lib_path)

        data_dir = op.abspath('./matching')
        if not op.exists(data_dir):
            os.makedirs(data_dir)

        path, name, ext = split_filename(self.inputs.ica_mask_image)
        copy_to = op.join(data_dir, 'components')
        if not op.exists(copy_to):
            os.makedirs(copy_to)
        copy_to = op.join(copy_to, name)
        shutil.copyfile(self.inputs.ica_mask_image, copy_to + ext)

        if ext == '.img':
            shutil.copyfile(op.join(path, name) + '.hdr', copy_to + '.hdr')
        elif ext == '.hdr':
            shutil.copyfile(op.join(path, name) + '.img', copy_to + '.img')

        mask_file = op.abspath(self.inputs.ica_mask_image)
        out_stats_file = op.abspath(self.inputs.out_stats_file)
        d = dict(out_stats_file=out_stats_file,
                 data_dir=data_dir,
                 mask_name=mask_file,
                 timecourse=op.abspath(self.inputs.time_course_image),
                 subj_id=self.inputs.subject_id,
                 nComponents=nComponents,
                 Tr=repetition_time,
                 coma_rest_lib_path=coma_rest_lib_path)

        script = Template("""
        restlib_path = '$coma_rest_lib_path';
        setup_restlib_paths(restlib_path)
        namesTemplate = {'rAuditory_corr','rCerebellum_corr','rDMN_corr','rECN_L_corr','rECN_R_corr','rSalience_corr','rSensorimotor_corr','rVisual_lateral_corr','rVisual_medial_corr','rVisual_occipital_corr'};
        indexNeuronal = 1:$nComponents;
        nCompo = $nComponents;
        out_stats_file = '$out_stats_file'; 
        Tr = $Tr;
        data_dir = '$data_dir'
        mask_name = '$mask_name'
        subj_id = '$subj_id'
        time_course_name = '$timecourse'

        [dataAssig maxGoF] = selectionMatchClassification(data_dir, subj_id, mask_name, time_course_name, namesTemplate,indexNeuronal,nCompo,Tr,restlib_path)
                            
        for i=1:size(dataAssig,1)
            str{i} = sprintf('Template %d: %s to component %d with GoF %f is neuronal %d prob=%f',dataAssig(i,1),namesTemplate{i},dataAssig(i,2),dataAssig(i,3),dataAssig(i,4),dataAssig(i,5));
            disp(str{i});
        end
        maxGoF
        templates = dataAssig(:,1)
        components = dataAssig(:,2)
        gofs = dataAssig(:,3)
        neuronal_bool = dataAssig(:,4)
        neuronal_prob = dataAssig(:,5)
        save '$out_stats_file'
        """).substitute(d)
        print 'Saving stats file as {s}'.format(s=out_stats_file)
        result = MatlabCommand(script=script,
                               mfile=True,
                               prescript=[''],
                               postscript=[''])
        r = result.run()
        return runtime
Пример #39
0
class SPMCommand(BaseInterface):
    """Extends `BaseInterface` class to implement SPM specific interfaces.

    WARNING: Pseudo prototype class, meant to be subclassed
    """
    input_spec = SPMCommandInputSpec

    _jobtype = 'basetype'
    _jobname = 'basename'

    _matlab_cmd = None
    _paths = None
    _use_mcr = None

    def __init__(self, **inputs):
        super(SPMCommand, self).__init__(**inputs)
        self.inputs.on_trait_change(
            self._matlab_cmd_update,
            ['matlab_cmd', 'mfile', 'paths', 'use_mcr'])
        self._check_mlab_inputs()
        self._matlab_cmd_update()

    @classmethod
    def set_mlab_paths(cls, matlab_cmd=None, paths=None, use_mcr=None):
        cls._matlab_cmd = matlab_cmd
        cls._paths = paths
        cls._use_mcr = use_mcr

    def _matlab_cmd_update(self):
        # MatlabCommand has to be created here,
        # because matlab_cmb is not a proper input
        # and can be set only during init
        self.mlab = MatlabCommand(matlab_cmd=self.inputs.matlab_cmd,
                                  mfile=self.inputs.mfile,
                                  paths=self.inputs.paths,
                                  uses_mcr=self.inputs.use_mcr)
        self.mlab.inputs.script_file = 'pyscript_%s.m' % \
        self.__class__.__name__.split('.')[-1].lower()

    @property
    def jobtype(self):
        return self._jobtype

    @property
    def jobname(self):
        return self._jobname

    def _check_mlab_inputs(self):
        if not isdefined(self.inputs.matlab_cmd) and self._matlab_cmd:
            self.inputs.matlab_cmd = self._matlab_cmd
        if not isdefined(self.inputs.paths) and self._paths:
            self.inputs.paths = self._paths
        if not isdefined(self.inputs.use_mcr) and self._use_mcr:
            self.inputs.use_mcr = self._use_mcr

    def _run_interface(self, runtime):
        """Executes the SPM function using MATLAB."""
        self.mlab.inputs.script = self._make_matlab_command(
            deepcopy(self._parse_inputs()))
        results = self.mlab.run()
        runtime.returncode = results.runtime.returncode
        if self.mlab.inputs.uses_mcr:
            if 'Skipped' in results.runtime.stdout:
                self.raise_exception(runtime)
        runtime.stdout = results.runtime.stdout
        runtime.stderr = results.runtime.stderr
        runtime.merged = results.runtime.merged
        return runtime

    def _list_outputs(self):
        """Determine the expected outputs based on inputs."""

        raise NotImplementedError

    def _format_arg(self, opt, spec, val):
        """Convert input to appropriate format for SPM."""
        if spec.is_trait_type(traits.Bool):
            return int(val)
        else:
            return val

    def _parse_inputs(self, skip=()):
        spmdict = {}
        metadata = dict(field=lambda t: t is not None)
        for name, spec in self.inputs.traits(**metadata).items():
            if skip and name in skip:
                continue
            value = getattr(self.inputs, name)
            if not isdefined(value):
                continue
            field = spec.field
            if '.' in field:
                fields = field.split('.')
                dictref = spmdict
                for f in fields[:-1]:
                    if f not in dictref.keys():
                        dictref[f] = {}
                    dictref = dictref[f]
                dictref[fields[-1]] = self._format_arg(name, spec, value)
            else:
                spmdict[field] = self._format_arg(name, spec, value)
        return [spmdict]

    def _reformat_dict_for_savemat(self, contents):
        """Encloses a dict representation within hierarchical lists.

        In order to create an appropriate SPM job structure, a Python
        dict storing the job needs to be modified so that each dict
        embedded in dict needs to be enclosed as a list element.

        Examples
        --------
        >>> a = SPMCommand()._reformat_dict_for_savemat(dict(a=1,b=dict(c=2,d=3)))
        >>> print a
        [{'a': 1, 'b': [{'c': 2, 'd': 3}]}]

        """
        newdict = {}
        try:
            for key, value in contents.items():
                if isinstance(value, dict):
                    if value:
                        newdict[key] = self._reformat_dict_for_savemat(value)
                    # if value is None, skip
                else:
                    newdict[key] = value

            return [newdict]
        except TypeError:
            print 'Requires dict input'

    def _generate_job(self, prefix='', contents=None):
        """Recursive function to generate spm job specification as a string

        Parameters
        ----------
        prefix : string
            A string that needs to get
        contents : dict
            A non-tuple Python structure containing spm job
            information gets converted to an appropriate sequence of
            matlab commands.

        """
        jobstring = ''
        if contents is None:
            return jobstring
        if isinstance(contents, list):
            for i, value in enumerate(contents):
                if prefix.endswith(")"):
                    newprefix = "%s,%d)" % (prefix[:-1], i + 1)
                else:
                    newprefix = "%s(%d)" % (prefix, i + 1)
                jobstring += self._generate_job(newprefix, value)
            return jobstring
        if isinstance(contents, dict):
            for key, value in contents.items():
                newprefix = "%s.%s" % (prefix, key)
                jobstring += self._generate_job(newprefix, value)
            return jobstring
        if isinstance(contents, np.ndarray):
            if contents.dtype == np.dtype(object):
                if prefix:
                    jobstring += "%s = {...\n" % (prefix)
                else:
                    jobstring += "{...\n"
                for i, val in enumerate(contents):
                    if isinstance(val, np.ndarray):
                        jobstring += self._generate_job(prefix=None,
                                                        contents=val)
                    elif isinstance(val, str):
                        jobstring += '\'%s\';...\n' % (val)
                    else:
                        jobstring += '%s;...\n' % str(val)
                jobstring += '};\n'
            else:
                for i, val in enumerate(contents):
                    for field in val.dtype.fields:
                        if prefix:
                            newprefix = "%s(%d).%s" % (prefix, i + 1, field)
                        else:
                            newprefix = "(%d).%s" % (i + 1, field)
                        jobstring += self._generate_job(newprefix, val[field])
            return jobstring
        if isinstance(contents, str):
            jobstring += "%s = '%s';\n" % (prefix, contents)
            return jobstring
        jobstring += "%s = %s;\n" % (prefix, str(contents))
        return jobstring

    def _make_matlab_command(self, contents, postscript=None):
        """Generates a mfile to build job structure
        Parameters
        ----------

        contents : list
            a list of dicts generated by _parse_inputs
            in each subclass

        cwd : string
            default os.getcwd()

        Returns
        -------
        mscript : string
            contents of a script called by matlab

        """
        cwd = os.getcwd()
        mscript = """
        %% Generated by nipype.interfaces.spm
        if isempty(which('spm')),
             throw(MException('SPMCheck:NotFound','SPM not in matlab path'));
        end
        [name, ver] = spm('ver');
        fprintf('SPM version: %s Release: %s\\n',name, ver);
        fprintf('SPM path: %s\\n',which('spm'));
        spm('Defaults','fMRI');

        if strcmp(spm('ver'),'SPM8'), spm_jobman('initcfg');end\n
        """
        if self.mlab.inputs.mfile:
            if self.jobname in [
                    'st', 'smooth', 'preproc', 'preproc8', 'fmri_spec',
                    'fmri_est', 'factorial_design', 'defs'
            ]:
                # parentheses
                mscript += self._generate_job(
                    'jobs{1}.%s{1}.%s(1)' % (self.jobtype, self.jobname),
                    contents[0])
            else:
                #curly brackets
                mscript += self._generate_job(
                    'jobs{1}.%s{1}.%s{1}' % (self.jobtype, self.jobname),
                    contents[0])
        else:
            jobdef = {
                'jobs': [{
                    self.jobtype: [{
                        self.jobname:
                        self.reformat_dict_for_savemat(contents[0])
                    }]
                }]
            }
            savemat(os.path.join(cwd, 'pyjobs_%s.mat' % self.jobname), jobdef)
            mscript += "load pyjobs_%s;\n\n" % self.jobname
        mscript += """
        if strcmp(spm('ver'),'SPM8'),
           jobs=spm_jobman('spm5tospm8',{jobs});
        end
        spm_jobman(\'run_nogui\',jobs);\n
        """
        if postscript is not None:
            mscript += postscript
        return mscript
Пример #40
0
def runmatlab(output_dir, noddi_img, brain_mask, roi_mask, bval, bvec, prefix,
              bStep, num_cores, path_to_matscript, noddi_toolbox_dir,
              nifti_matlib_dir):
    """
    The wrapper to call noddi matlab script.
    Args:
        output_dir:
        noddi_img:
        brain_mask:
        roi_mask:
        bval:
        bvec:
        prefix:
        bStep:
        num_cores:

    Returns:

    """
    from nipype.interfaces.matlab import MatlabCommand, get_matlab_command
    from os.path import join
    import sys
    import os
    # here, we check out the os, basically, clinica works for linux and MAC OS X.
    if sys.platform.startswith('linux'):
        print "###Note: your platform is linux, the default command line for Matlab(matlab_cmd) is matlab, but you can also export a variable MATLABCMD,  which points to your matlab,  in your .bashrc to set matlab_cmd, this can help you to choose which Matlab to run when you have more than one Matlab. "
    elif sys.platform.startswith('darwin'):
        try:
            if 'MATLABCMD' not in os.environ:
                raise RuntimeError(
                    "###Note: your platform is MAC OS X, the default command line for Matlab(matlab_cmd) is matlab, but it does not work on OS X, you mush export a variable MATLABCMD, which points to your matlab, in your .bashrc to set matlab_cmd. Note, Mac os x will always choose to use OpengGl hardware mode."
                )
        except Exception as e:
            print(str(e))
            exit(1)
    else:
        print "Clinica will not work on your platform "

    MatlabCommand.set_default_matlab_cmd(
        get_matlab_command()
    )  # this is to set the matlab_path(os.environ) in your bashrc file, to choose which version of matlab do you wanna use
    # here, set_default_matlab_cmd is a @classmethod
    matlab = MatlabCommand()

    # add the dynamic traits
    # openGL_trait = traits.Bool(True, argstr='-nosoftwareopengl', usedefault=True, desc='Switch on hardware openGL', nohash=True)
    # matlab.input_spec.add_trait(matlab.input_spec(), 'nosoftwareopengl', openGL_trait() )
    if sys.platform.startswith('linux'):
        matlab.inputs.args = '-nosoftwareopengl'  # Bug, for my laptop, it does not work, but the command line does have the flag -nosoftwareopengl, we should try on other computer's matlab to check if this flag works!
    matlab.inputs.paths = path_to_matscript  # CLINICA_HOME, this is the path to add into matlab, addpath

    matlab.inputs.script = """
    noddiprocessing('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d');
    """ % (
        output_dir, noddi_img, brain_mask, roi_mask, bval, bvec, prefix, bStep,
        noddi_toolbox_dir, nifti_matlib_dir, num_cores
    )  # here, we should define the inputs for the matlab function that you want to use
    matlab.inputs.mfile = True  # this will create a file: pyscript.m , the pyscript.m is the default name
    matlab.inputs.single_comp_thread = False  # this will stop runing with single thread
    matlab.inputs.logfile = join(output_dir, prefix + "_matlab_output.log")
    print "Matlab logfile is located in the folder: %s" % matlab.inputs.logfile
    print "Matlab script command = %s" % matlab.inputs.script
    print "MatlabCommand inputs flag: single_comp_thread = %s" % matlab.inputs.single_comp_thread
    print "MatlabCommand choose which matlab to use(matlab_cmd): %s" % get_matlab_command(
    )
    if sys.platform.startswith('linux'):
        print "MatlabCommand inputs flag: nosoftwareopengl = %s" % matlab.inputs.args

    matlab.run()

    # grab the output images
    fit_icvf = os.path.join(output_dir, prefix + '_ficvf.nii')
    fit_isovf = os.path.join(output_dir, prefix + '_fiso.nii')
    fit_od = os.path.join(output_dir, prefix + '_odi.nii')

    return fit_icvf, fit_isovf, fit_od
Пример #41
0
    def _run_interface(self, runtime):
        path, name, ext = split_filename(self.inputs.time_course_image)
        data_dir = op.abspath('./matching')
        copy_to = op.join(data_dir, 'components')
        if not op.exists(copy_to):
            os.makedirs(copy_to)
        copy_to = op.join(copy_to, name)
        shutil.copyfile(self.inputs.time_course_image, copy_to + ext)
        if ext == '.img':
            shutil.copyfile(op.join(path, name) + '.hdr', copy_to + '.hdr')
        elif ext == '.hdr':
            shutil.copyfile(op.join(path, name) + '.img', copy_to + '.img')

        data_dir = op.abspath('./matching/components')
        in_files = self.inputs.in_files
        if len(self.inputs.in_files) > 1:
            print 'Multiple ({n}) input images detected! Copying to {d}...'.format(n=len(self.inputs.in_files), d=data_dir)
            for in_file in self.inputs.in_files:
                path, name, ext = split_filename(in_file)
                shutil.copyfile(in_file, op.join(data_dir, name) + ext)
                if ext == '.img':
                    shutil.copyfile(op.join(path, name) + '.hdr',
                                    op.join(data_dir, name) + '.hdr')
                elif ext == '.hdr':
                    shutil.copyfile(op.join(path, name) + '.img',
                                    op.join(data_dir, name) + '.img')
            print 'Copied!'

        elif isdefined(self.inputs.in_file4d):
            print 'Single four-dimensional image selected. Splitting and copying to {d}'.format(d=data_dir)
            in_files = nb.four_to_three(self.inputs.in_file4d)
            for in_file in in_files:
                path, name, ext = split_filename(in_file)
                shutil.copyfile(in_file, op.join(data_dir, name) + ext)
            print 'Copied!'

        else:
            raise Exception('Single functional image provided. Ending...')
            in_files = self.inputs.in_files

        nComponents = len(in_files)
        repetition_time = self.inputs.repetition_time
        coma_rest_lib_path = op.abspath(self.inputs.coma_rest_lib_path)

        data_dir = op.abspath('./matching')
        if not op.exists(data_dir):
            os.makedirs(data_dir)

        path, name, ext = split_filename(self.inputs.ica_mask_image)
        copy_to = op.join(data_dir, 'components')
        if not op.exists(copy_to):
            os.makedirs(copy_to)
        copy_to = op.join(copy_to, name)
        shutil.copyfile(self.inputs.ica_mask_image, copy_to + ext)
        
        if ext == '.img':
            shutil.copyfile(op.join(path, name) + '.hdr', copy_to + '.hdr')
        elif ext == '.hdr':
            shutil.copyfile(op.join(path, name) + '.img', copy_to + '.img')

        mask_file = op.abspath(self.inputs.ica_mask_image)
        out_stats_file = op.abspath(self.inputs.out_stats_file)
        d = dict(
            out_stats_file=out_stats_file, data_dir=data_dir, mask_name=mask_file,
            timecourse=op.abspath(self.inputs.time_course_image),
            subj_id=self.inputs.subject_id,
            nComponents=nComponents, Tr=repetition_time, coma_rest_lib_path=coma_rest_lib_path)

        script = Template("""
        restlib_path = '$coma_rest_lib_path';
        setup_restlib_paths(restlib_path)
        namesTemplate = {'rAuditory_corr','rCerebellum_corr','rDMN_corr','rECN_L_corr','rECN_R_corr','rSalience_corr','rSensorimotor_corr','rVisual_lateral_corr','rVisual_medial_corr','rVisual_occipital_corr'};
        indexNeuronal = 1:$nComponents;
        nCompo = $nComponents;
        out_stats_file = '$out_stats_file'; 
        Tr = $Tr;
        data_dir = '$data_dir'
        mask_name = '$mask_name'
        subj_id = '$subj_id'
        time_course_name = '$timecourse'

        [dataAssig maxGoF] = selectionMatchClassification(data_dir, subj_id, mask_name, time_course_name, namesTemplate,indexNeuronal,nCompo,Tr,restlib_path)
                            
        for i=1:size(dataAssig,1)
            str{i} = sprintf('Template %d: %s to component %d with GoF %f is neuronal %d prob=%f',dataAssig(i,1),namesTemplate{i},dataAssig(i,2),dataAssig(i,3),dataAssig(i,4),dataAssig(i,5));
            disp(str{i});
        end
        maxGoF
        templates = dataAssig(:,1)
        components = dataAssig(:,2)
        gofs = dataAssig(:,3)
        neuronal_bool = dataAssig(:,4)
        neuronal_prob = dataAssig(:,5)
        save '$out_stats_file'
        """).substitute(d)
        print 'Saving stats file as {s}'.format(s=out_stats_file)
        result = MatlabCommand(script=script, mfile=True,
                               prescript=[''], postscript=[''])
        r = result.run()
        return runtime
Пример #42
0
    def _run_interface(self, runtime):
        path, name, ext = split_filename(self.inputs.time_course_image)
        data_dir = op.abspath('./matching')
        copy_to = op.join(data_dir, 'components')
        if not op.exists(copy_to):
            os.makedirs(copy_to)
        copy_to = op.join(copy_to, name)
        shutil.copyfile(self.inputs.time_course_image, copy_to + ext)
        if ext == '.img':
            shutil.copyfile(op.join(path, name) + '.hdr', copy_to + '.hdr')
        elif ext == '.hdr':
            shutil.copyfile(op.join(path, name) + '.img', copy_to + '.img')
        time_course_file = copy_to + '.img'

        path, name, ext = split_filename(self.inputs.ica_mask_image)
        shutil.copyfile(self.inputs.ica_mask_image,
                        op.join(data_dir, name) + ext)
        if ext == '.img':
            shutil.copyfile(op.join(path, name) + '.hdr',
                            op.join(data_dir, name) + '.hdr')
        elif ext == '.hdr':
            shutil.copyfile(op.join(path, name) + '.img',
                            op.join(data_dir, name) + '.img')
        mask_file = op.abspath(self.inputs.ica_mask_image)
        repetition_time = self.inputs.repetition_time
        component_file = op.abspath(self.inputs.in_file)
        coma_rest_lib_path = op.abspath(self.inputs.coma_rest_lib_path)
        component_index = self.inputs.component_index
        if isdefined(self.inputs.out_stats_file):
            path, name, ext = split_filename(self.inputs.out_stats_file)
            if not ext == '.mat':
                ext = '.mat'
            out_stats_file = op.abspath(name + ext)
        else:
            if isdefined(self.inputs.subject_id):
                out_stats_file = op.abspath(
                    self.inputs.subject_id + '_IC_' + str(self.inputs.component_index) + '.mat')
            else:
                out_stats_file = op.abspath(
                    'IC_' + str(self.inputs.component_index) + '.mat')

        d = dict(
            component_file=component_file, IC=component_index, time_course_file=time_course_file,
            mask_name=mask_file, Tr=repetition_time, coma_rest_lib_path=coma_rest_lib_path, out_stats_file=out_stats_file)
        script = Template("""
        restlib_path = '$coma_rest_lib_path';
        setup_restlib_paths(restlib_path);
        Tr = $Tr;
        out_stats_file = '$out_stats_file';
        component_file = '$component_file';
        maskName = '$mask_name';
        maskData = load_nii(maskName);        
        dataCompSpatial = load_nii(component_file)
        time_course_file = '$time_course_file'
        timeData = load_nii(time_course_file)
        IC = $IC
        [feature dataZ temporalData] = computeFingerprintSpaceTime(dataCompSpatial.img,timeData.img(:,IC),maskData.img,Tr);
        save '$out_stats_file'
        """).substitute(d)
        result = MatlabCommand(script=script, mfile=True,
                               prescript=[''], postscript=[''])
        r = result.run()
        print 'Saving stats file as {s}'.format(s=out_stats_file)
        return runtime