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): 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
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
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
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_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)
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()
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
def satisfied(self): if self.test_func is None: return True # No test available script = ( "try\n" " {}\n" "catch E\n" " fprintf(E.identifier);\n" "end\n".format(self.test_func)) result = MatlabCommand(script=script, mfile=True).run() output = result.runtime.stdout return output != 'MATLAB:UndefinedFunction'
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
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
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
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
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
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
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
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
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
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
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
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
print "MISSING PULS DATA!" physsig_b = physsig4 if not os.path.isfile(physsig4): print "MISSING RESP DATA!" print "Running DRIFTER..." script = "run_drifter_noSPM_kopio('%s','%s','%s')" % (infile, physsig_a, physsig_b) MatlabCommand.set_default_paths([ '/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')
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
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
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
def matlab_cmd(cmd): return MatlabCommand(script=SCRIPT_TEMPLATE.format(cmd=cmd), mfile=True)
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
physsig_a = physsig3 else: physsig_a = None if os.path.isfile(physsig4): physsig_b = physsig4 else: physsig_b = None print "Running DRIFTER..." script = "run_drifter_noSPM_kopio('%s','%s','%s')" % ( infile, physsig_a, physsig_b) MatlabCommand.set_default_paths( ['/usr/share/spm8/', '/opt2/MATLAB/NIFTI20140122/']) mlab = MatlabCommand( script=script, mfile=True, paths= '/opt/Laskenta/Control_Room/Biomedicum/DRIFTER-toolbox/DRIFTER/', terminal_output="stream") try: os.stat(results_path + subj + '/' + data + '_1/drifter/drifter_corrected.nii.gz') except: drifter_result = mlab.run() os.mkdir(results_path + subj + '/' + data + '_1/drifter/') os.rename( results_path + subj + '/drifter_corrected.nii.gz', results_path + subj + '/' + data + '_1/drifter/drifter_corrected.nii.gz') os.rename( results_path + subj + '/drifter_noise.nii.gz', results_path +
if not os.path.isfile(physsig4): print "MISSING RESP DATA!" print "Running DRIFTER..." script = "run_drifter_noSPM_kopio('%s','%s','%s')" % ( infile, physsig_a, physsig_b) # TODO: muuta seuraavat polut oikeiksi!! # tahan Matlabin asennuspolku MatlabCommand.set_default_paths([ '/usr/local/MATLAB/R2015a/spm8/toolbox/DRIFTER-toolbox/DRIFTER', '/usr/local/MATLAB/R2015a/spm8/', '/usr/local/MATLAB/NIFTI20130306' ]) # TODO: tahan DRIFTER-toolboxin polku mlab = MatlabCommand( script=script, mfile=True, paths='/media/HannaHalmeLevy/scripts/DRIFTER-toolbox/DRIFTER/', terminal_output="stream") try: os.stat(results_path + subj + '/' + data + '_1/drifter/drifter_corrected.nii.gz') except: drifter_result = mlab.run() os.mkdir(results_path + subj + '/' + data + '_1/drifter/') os.rename( results_path + subj + '/drifter_corrected.nii.gz', results_path + subj + '/' + data + '_1/drifter/drifter_corrected.nii.gz') # Initialize workflow2