def test_mlab_init(): yield assert_equal, mlab.MatlabCommand._cmd, 'matlab' yield assert_equal, mlab.MatlabCommand.input_spec, mlab.MatlabInputSpec yield assert_equal, mlab.MatlabCommand().cmd, matlab_cmd mc = mlab.MatlabCommand(matlab_cmd='foo_m') yield assert_equal, mc.cmd, 'foo_m'
def test_run_interface(): default_script_file = clean_workspace_and_get_default_script_file() mc = mlab.MatlabCommand(matlab_cmd='foo_m') yield assert_false, os.path.exists(default_script_file), 'scriptfile should not exist 1.' yield assert_raises, ValueError, mc.run # script is mandatory yield assert_false, os.path.exists(default_script_file), 'scriptfile should not exist 2.' if os.path.exists(default_script_file): # cleanup os.remove(default_script_file) mc.inputs.script = 'a=1;' yield assert_false, os.path.exists(default_script_file), 'scriptfile should not exist 3.' yield assert_raises, IOError, mc.run # foo_m is not an executable yield assert_true, os.path.exists(default_script_file), 'scriptfile should exist 3.' if os.path.exists(default_script_file): # cleanup os.remove(default_script_file) cwd = os.getcwd() basedir = mkdtemp() os.chdir(basedir) # bypasses ubuntu dash issue mc = mlab.MatlabCommand(script='foo;', paths=[basedir], mfile=True) yield assert_false, os.path.exists(default_script_file), 'scriptfile should not exist 4.' yield assert_raises, RuntimeError, mc.run yield assert_true, os.path.exists(default_script_file), 'scriptfile should exist 4.' if os.path.exists(default_script_file): # cleanup os.remove(default_script_file) # bypasses ubuntu dash issue res = mlab.MatlabCommand(script='a=1;', paths=[basedir], mfile=True).run() yield assert_equal, res.runtime.returncode, 0 yield assert_true, os.path.exists(default_script_file), 'scriptfile should exist 5.' os.chdir(cwd) rmtree(basedir)
def test_mlab_init(): default_script_file = clean_workspace_and_get_default_script_file() yield assert_equal, mlab.MatlabCommand._cmd, 'matlab' yield assert_equal, mlab.MatlabCommand.input_spec, mlab.MatlabInputSpec yield assert_equal, mlab.MatlabCommand().cmd, matlab_cmd mc = mlab.MatlabCommand(matlab_cmd='foo_m') yield assert_equal, mc.cmd, 'foo_m'
def test_mlab_init(): default_script_file = clean_workspace_and_get_default_script_file() assert mlab.MatlabCommand._cmd == 'matlab' assert mlab.MatlabCommand.input_spec == mlab.MatlabInputSpec assert mlab.MatlabCommand().cmd == matlab_cmd mc = mlab.MatlabCommand(matlab_cmd='foo_m') assert mc.cmd == 'foo_m'
def runCSbyMatlab(inputScan, inputMask, CSScanFileName, Path_to_Matlab_Func): """ This Function takes in... :param inputScan: :param inputMask: :param CSScanFileName: :param Path_to_Matlab_Func: :return: outputCSFilename """ import os import nipype.interfaces.matlab as matlab script = "runCS('" + inputScan + "','" + inputMask + "','" + CSScanFileName + "')" mlab = matlab.MatlabCommand() mlab.set_default_matlab_cmd("matlab") mlab.inputs.single_comp_thread = False mlab.inputs.nodesktop = True mlab.inputs.nosplash = True mlab.inputs.paths = Path_to_Matlab_Func mlab.inputs.script = script mlab.run() outputCSFilename = os.path.join( os.getcwd(), CSScanFileName) # return output CS filename assert os.path.isfile( outputCSFilename), "CS file is not found: %s" % outputCSFilename return outputCSFilename
def spm_matrix(params): """Run the spm function spm_matrix through Matlab. Parameters ========== params : 1D numpy.ndarray, shape > 6 Parameters to convert to the affine transfomation matrix. Returns ======= affine : numpy.ndarray of shape (4, 4) The affine transformation matrix. """ # TODO write using _make_matlab_command to rely only on SPM params_str = '[{0}, {1}, {2}, {3}, {4}, {5}]'.format(*params) mat_path = '/tmp/matrix.mat' # Generate the Matlab script mlab = matlab.MatlabCommand() mlab.inputs.script = "\naddpath '/i2bm/local/spm8-6313/' \n" + \ "matrix = spm_matrix(" + params_str + ");\n" + \ "save('" + mat_path + "', 'matrix')" # Run the script and load the output matrix mlab.run() mat_dict = loadmat(mat_path) affine = mat_dict['matrix'] return affine
def test_run_interface(): mc = mlab.MatlabCommand(matlab_cmd='foo_m') yield assert_raises, ValueError, mc.run # script is mandatory mc.inputs.script = 'a=1;' yield assert_raises, IOError, mc.run # foo_m is not an executable cwd = os.getcwd() basedir = mkdtemp() os.chdir(basedir) # bypasses ubuntu dash issue mc = mlab.MatlabCommand(script='foo;', paths=[basedir], mfile=True) yield assert_raises, RuntimeError, mc.run # bypasses ubuntu dash issue res = mlab.MatlabCommand(script='a=1;', paths=[basedir], mfile=True).run() yield assert_equal, res.runtime.returncode, 0 os.chdir(cwd) rmtree(basedir)
def reslice(space_define, infile): """ uses spm_reslice to resample infile into the space of space_define, assumes they are already in register""" startdir = os.getcwd() pth, _ = os.path.split(infile) os.chdir(pth) mlab_cmd = mlab.MatlabCommand(matlab_cmd='matlab-spm8') mlab_cmd.inputs.nodesktop = True mlab_cmd.inputs.nosplash = True mlab_cmd.inputs.mfile = True mlab_cmd.inputs.ignore_exception = True mlab_cmd.inputs.script_file = 'pyspm8_reslice.m' script = """ flags.mean = 0; flags.which = 1; flags.mask = 1; flags.interp = 0; infiles = strvcat(\'%s\', \'%s\'); invols = spm_vol(infiles); spm_reslice(invols, flags); """ % (space_define, infile) mlab_cmd.inputs.script = script mout = mlab_cmd.run() os.chdir(startdir) return mout
def __init__(self, script='', **options): import nipype.interfaces.matlab as matlab mlab = matlab.MatlabCommand(mfile=False) mlab.inputs.script = script for ef in options: setattr(mlab.inputs, ef, options[ef]) self.res = mlab.run()
def spm_imatrix(affine): """Run the spm function spm_imatrix through Matlab. Parameters ========== affine : numpy.ndarray of shape (4, 4) The affine transformation matrix. Returns ======= params : 1D numpy.ndarray The parameters of the affine transformation. """ affine_str = "[" for row in affine: affine_str += '{0}, {1}, {2}, {3};'.format(*row) affine_str = affine_str[:-1] affine_str += "]" # Generate the Matlab script mat_path = '/tmp/params.mat' mlab = matlab.MatlabCommand() mlab.inputs.script = "\naddpath '/i2bm/local/spm8-6313/' \n" +\ "parameters = spm_imatrix(" + affine_str + ");\n" +\ "save('" + mat_path + "', 'parameters')" # Run the script and load the output vector mlab.run() mat_dict = loadmat(mat_path) params = mat_dict['parameters'] params = np.squeeze(params) return params
def forward_coreg(mri, pet, transform): """ coregisters pet to mri and applies to pet, saves parameters in transform """ startdir = os.getcwd() pth, _ = os.path.split(pet) os.chdir(pth) mlab_cmd = mlab.MatlabCommand(matlab_cmd='matlab-spm8') mlab_cmd.inputs.nodesktop = True mlab_cmd.inputs.nosplash = True mlab_cmd.inputs.mfile = True mlab_cmd.inputs.script_file = 'pyspm8_invert_coreg.m' script = """ pet = \'%s\'; mri = \'%s\'; petv = spm_vol(pet); mriv = spm_vol(mri); x = spm_coreg(petv, mriv); M = spm_matrix(x(:)'); save( \'%s\' , \'M\' ); petspace = spm_get_space(pet); spm_get_space(pet, M * petspace); """ % (pet, mri, transform) mlab_cmd.inputs.script = script mout = mlab_cmd.run() os.chdir(startdir) return mout
def test_set_matlabcmd(): default_script_file = clean_workspace_and_get_default_script_file() mi = mlab.MatlabCommand() mi.set_default_matlab_cmd('foo') yield assert_false, os.path.exists(default_script_file), 'scriptfile should not exist.' yield assert_equal, mi._default_matlab_cmd, 'foo' mi.set_default_matlab_cmd(matlab_cmd)
def test_set_matlabcmd(): default_script_file = clean_workspace_and_get_default_script_file() mi = mlab.MatlabCommand() mi.set_default_matlab_cmd('foo') assert not os.path.exists( default_script_file), 'scriptfile should not exist.' assert mi._default_matlab_cmd == 'foo' mi.set_default_matlab_cmd(matlab_cmd)
def CBFscale_PWI_data(all_aligned_dir): # Envoke matlab to calculate scaled-CBF from PWI data mlc = mlab.MatlabCommand() cmd = "cd('%s');raw_pwi = spm_vol('diffdata_mean.nii');scaled_pwi = raw_pwi;scaled_pwi.fname = 'CBF_Scaled_PWI.nii';scaled_pwi.descript = 'Scaled from the PWI Image';pwi_data = spm_read_vols(raw_pwi);Lamda = 0.9000;Alpha = 0.9500;Tau = 22.50;R1A = (1684)^-1;PER100G = 100;SEC_PER_MIN = 60;MSEC_PER_SEC = 1000;TI1 = 700;TI2 = 1800;PWI_scale = zeros(size(pwi_data));sliceNumbers = (1:size(pwi_data, 3))';Constant = Lamda / (2 * Alpha * TI1) * (PER100G * SEC_PER_MIN * MSEC_PER_SEC);Slice_based_const = exp(R1A * (TI2 + (sliceNumbers - 1) * Tau));Numerator = pwi_data;for n =1:size(sliceNumbers); PWI_scale(:,:,n) = Constant * Slice_based_const(n) * Numerator(:,:,n);end; spm_write_vol(scaled_pwi, PWI_scale);" % ( all_aligned_dir) mlc.inputs.script = cmd out = mlc.run()
def mat_to_nii(mat_roi_path): import nipype.interfaces.matlab as matlab import os mlab = matlab.MatlabCommand() mlab.inputs.script = """ marsbar on load %s save_as_image(roi, '%s') """ % (mat_roi_path, mat_roi_path.strip('_roi.mat') + '.nii') mlab.run()
def test_run_interface(): default_script_file = clean_workspace_and_get_default_script_file() mc = mlab.MatlabCommand(matlab_cmd='foo_m') assert not os.path.exists( default_script_file), 'scriptfile should not exist 1.' with pytest.raises(ValueError): mc.run() # script is mandatory assert not os.path.exists( default_script_file), 'scriptfile should not exist 2.' if os.path.exists(default_script_file): # cleanup os.remove(default_script_file) mc.inputs.script = 'a=1;' assert not os.path.exists( default_script_file), 'scriptfile should not exist 3.' with pytest.raises(IOError): mc.run() # foo_m is not an executable assert os.path.exists(default_script_file), 'scriptfile should exist 3.' if os.path.exists(default_script_file): # cleanup os.remove(default_script_file) cwd = os.getcwd() basedir = mkdtemp() os.chdir(basedir) # bypasses ubuntu dash issue mc = mlab.MatlabCommand(script='foo;', paths=[basedir], mfile=True) assert not os.path.exists( default_script_file), 'scriptfile should not exist 4.' with pytest.raises(RuntimeError): mc.run() assert os.path.exists(default_script_file), 'scriptfile should exist 4.' if os.path.exists(default_script_file): # cleanup os.remove(default_script_file) # bypasses ubuntu dash issue res = mlab.MatlabCommand(script='a=1;', paths=[basedir], mfile=True).run() assert res.runtime.returncode == 0 assert os.path.exists(default_script_file), 'scriptfile should exist 5.' os.chdir(cwd) rmtree(basedir)
def ExploreDTI_tractography(flipped_mat): import nipype.interfaces.matlab as Matlab import os from shutil import copy import re matlab = Matlab.MatlabCommand() # below is where you add paths that matlab might require, this is equivalent to addpath() matlab.inputs.paths = ['/home/amr/SCRIPTS/'] matlab.inputs.single_comp_thread = False cwd = os.getcwd() subj_no = re.findall('\d+', cwd)[-1] matlab.inputs.script = """ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Tractography%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% params.SeedPointRes = [2 2 2] params.LinearGeoTrackRange = [0 1] params.SphericalGeoTrackRange = [0 1] params.InterpolationMethod = 1 params.PlanarGeoTrackRange = [0 1] params.SeedFAThresh = 0.2 params.AngleThresh = 30 params.MDTrackRange = [0 Inf] params.FiberLengthRange = [5 500] params.ran_sam = 0 params.StepSize = 1 params.FAThresh = 0.2 params.SeedFAThreshold = 0.2 params.FATrackRange = [0.2 1] tracts_thick = ['{0}/Diff_Multishell_{1}_Tracts_Thick.mat'] tracts_thin = ['{0}/Diff_Multishell_{1}_Tracts_Thin.mat'] %on the other hand, this will return only the major bundles E_DTI_WholeBrainTracking('{2}', tracts_thick, params) % params.FiberLengthRange = [50 500] % this will written only the major tracts % WholeBrainTrackingDTI_fast('{2}', tracts_thin, params) """.format( cwd, subj_no, flipped_mat ) #Ihave no idea why '' solved the problem of writing to different dir, but it did res = matlab.run() tracts = os.path.abspath( 'Diff_Multishell_{0}_Tracts_Thick.mat'.format(subj_no)) return tracts #You always need return
def test_run_interface(tmpdir): default_script_file = clean_workspace_and_get_default_script_file() mc = mlab.MatlabCommand(matlab_cmd="foo_m") assert not os.path.exists( default_script_file), "scriptfile should not exist 1." with pytest.raises(ValueError): mc.run() # script is mandatory assert not os.path.exists( default_script_file), "scriptfile should not exist 2." if os.path.exists(default_script_file): # cleanup os.remove(default_script_file) mc.inputs.script = "a=1;" assert not os.path.exists( default_script_file), "scriptfile should not exist 3." with pytest.raises(IOError): mc.run() # foo_m is not an executable assert os.path.exists(default_script_file), "scriptfile should exist 3." if os.path.exists(default_script_file): # cleanup os.remove(default_script_file) cwd = tmpdir.chdir() # bypasses ubuntu dash issue mc = mlab.MatlabCommand(script="foo;", paths=[tmpdir.strpath], mfile=True) assert not os.path.exists( default_script_file), "scriptfile should not exist 4." with pytest.raises(OSError): mc.run() assert os.path.exists(default_script_file), "scriptfile should exist 4." if os.path.exists(default_script_file): # cleanup os.remove(default_script_file) # bypasses ubuntu dash issue res = mlab.MatlabCommand(script="a=1;", paths=[tmpdir.strpath], mfile=True).run() assert res.runtime.returncode == 0 assert os.path.exists(default_script_file), "scriptfile should exist 5." cwd.chdir()
def ExploreDTI_sort(eddy_file): import nipype.interfaces.matlab as Matlab import os import re from shutil import copy matlab = Matlab.MatlabCommand() # below is where you add paths that matlab might require, this is equivalent to addpath() matlab.inputs.paths = ['/home/amr/SCRIPTS/'] matlab.inputs.single_comp_thread = False Bmatrix = '/media/amr/Amr_4TB/Work/October_Acquistion/Bmatrix_ExploreDTI.txt' experiment_dir = '/media/amr/Amr_4TB/Work/October_Acquistion' working_dir = 'Diffusion_Multishell_ExploreDTI_workingdir' workflow = 'Multishell_ExploreDTI_workflow' cwd = os.getcwd() subj_no = re.findall('\d+', cwd)[-1] copy( Bmatrix, '%s/%s/%s/_subject_id_%s/decompress/Diff_Multishell_%s_edc_chdt.txt' % (experiment_dir, working_dir, workflow, subj_no, subj_no)) matlab.inputs.script = """ eddy_file = '{0}' name_wo_ext = eddy_file(145:end-4) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Sort eddy_corrected nii%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % You need the absolute path for things to work E_DTI_sort_DWIs_wrt_b_val_ex(eddy_file,['{1}' '/' '' name_wo_ext '_sorted.nii'],... ['{1}' '/' '' name_wo_ext '_sorted.nii']) """.format( eddy_file, cwd ) #Ihave no idea why '' solved the problem of writing to different dir, but it did res = matlab.run() sorted_nii = os.path.abspath( 'Diff_Multishell_{0}_edc_chdt_sorted.nii'.format(subj_no)) sorted_txt = os.path.abspath( 'Diff_Multishell_{0}_edc_chdt_sorted.txt'.format(subj_no)) return sorted_nii, sorted_txt #You always need return
def check_system_spm_available(): """Check SPM can be found by Matlab Make sure SPM can be found by Matlab and terminate the program if it cannot. Args: N/A Returns: spm_found (Boolean): True if SPM folder in Matlab path, False otherwise spm_path (string): path to the SPM folder found by Matlab. Retrieved with the 'which spm' Matlab command. None if not found spmscript_path (string): path to the spm.m in the SPM folder found by Matlab. None if SPM folder not found. """ # initialise output variables #-- SPM found boolean spm_found = False #-- SPM folder spm_path = None #-- SPM script ([folder]/spm.m) spmscript_path = None # Check if SPM path can be found by Matlab # (e.g., if file $HOME/matlab/startup.m contains the line # addpath [spm_folder]) res = mlab.MatlabCommand(script='which spm', mfile=False).run() whichspm_output = res.runtime.stdout.splitlines()[-2] if whichspm_output == '\'spm\' not found.': #-- SPM found boolean spm_found = False #-- SPM folder spm_path = None #-- SPM script ([folder]/spm.m) spmscript_path = None else: # SPM found spm_found = True spmscript_path = whichspm_output spm_path = os.path.dirname(spmscript_path) return spm_found, spm_path, spmscript_path
def NODDI(brain_nii, brain_mask_nii): import os import nipype.interfaces.matlab as Matlab matlab = Matlab.MatlabCommand() bval = '/media/amr/HDD/Work/October_Acquistion/bval_multishell' bvec = '/media/amr/HDD/Work/October_Acquistion/bvec_multishell' brain_mat_name = 'NODDI_brain.mat' prefix = 'NODDI' # below is where you add paths that matlab might require, this is equivalent to addpath() matlab.inputs.paths = ['/home/amr/SCRIPTS/'] matlab.inputs.single_comp_thread = False import os matlab.inputs.script = """ bval = '/media/amr/HDD/Work/October_Acquistion/bval_multishell' bvec = '/media/amr/HDD/Work/October_Acquistion/bvec_multishell' brain_mat_name = 'NODDI_brain.mat' prefix = 'NODDI' CreateROI('%s', '%s', brain_mat_name); protocol = FSL2Protocol('%s', '%s'); noddi = MakeModel('WatsonSHStickTortIsoV_B0'); batch_fitting_single(brain_mat_name, protocol, noddi, 'FittedParams.mat'); SaveParamsAsNIfTI('FittedParams.mat', brain_mat_name, '%s', prefix) """ % (brain_nii, brain_mask_nii, bval, bvec, brain_mask_nii) res = matlab.run() odi = os.path.abspath('NODDI_odi.nii') ficvf = os.path.abspath('NODDI_ficvf.nii') return odi, ficvf
def test_cmdline(): default_script_file = clean_workspace_and_get_default_script_file() mi = mlab.MatlabCommand(script='whos', script_file='testscript', mfile=False) yield assert_equal, mi.cmdline, \ matlab_cmd + (' -nodesktop -nosplash -singleCompThread -r "fprintf(1,' '\'Executing code at %s:\\n\',datestr(now));ver,try,' 'whos,catch ME,fprintf(2,\'MATLAB code threw an ' 'exception:\\n\');fprintf(2,\'%s\\n\',ME.message);if ' 'length(ME.stack) ~= 0, fprintf(2,\'File:%s\\nName:%s\\n' 'Line:%d\\n\',ME.stack.file,ME.stack.name,' 'ME.stack.line);, end;end;;exit"') yield assert_equal, mi.inputs.script, 'whos' yield assert_equal, mi.inputs.script_file, 'testscript' yield assert_false, os.path.exists(mi.inputs.script_file), 'scriptfile should not exist' yield assert_false, os.path.exists(default_script_file), 'default scriptfile should not exist.'
def ExploreDTI_mat(sorted_nii, sorted_txt): import nipype.interfaces.matlab as Matlab import os from shutil import copy import re matlab = Matlab.MatlabCommand() # below is where you add paths that matlab might require, this is equivalent to addpath() matlab.inputs.paths = ['/home/amr/SCRIPTS/'] matlab.inputs.single_comp_thread = False cwd = os.getcwd() subj_no = re.findall('\d+', cwd)[-1] matlab.inputs.script = """ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%convert to kurtosis.mat%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Mask_par.tune_NDWI = 0.7; % recommened by Leemans Mask_par.tune_DWI = 0.7; Mask_par.mfs = 5 f_DWI = '{0}' f_BM = '{1}' basename = f_DWI(1:end-4) f_mat = [basename '.mat'] E_DTI_quick_and_dirty_DKI_convert_from_nii_txt_to_mat(f_DWI, f_BM, f_mat, Mask_par, 6, 2, 1) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Now flip R-L%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% flipped_mat = ['{2}/Diff_Multishell_{3}_sorted_flipped.mat'] E_DTI_Flip_left_right_mat(f_mat,flipped_mat) """.format( sorted_nii, sorted_txt, cwd, subj_no ) #Ihave no idea why '' solved the problem of writing to different dir, but it did res = matlab.run() flipped_mat = os.path.abspath( 'Diff_Multishell_{0}_sorted_flipped.mat'.format(subj_no)) return flipped_mat #You always need return
def test_cmdline(): basedir = mkdtemp() mi = mlab.MatlabCommand(script='whos', script_file='testscript', mfile=False) yield assert_equal, mi.cmdline, \ matlab_cmd + (' -nodesktop -nosplash -singleCompThread -r "fprintf(1,' '\'Executing code at %s:\\n\',datestr(now));ver,try,' 'whos,catch ME,fprintf(2,\'MATLAB code threw an ' 'exception:\\n\');fprintf(2,\'%s\\n\',ME.message);if ' 'length(ME.stack) ~= 0, fprintf(2,\'File:%s\\nName:%s\\n' 'Line:%d\\n\',ME.stack.file,ME.stack.name,' 'ME.stack.line);, end;end;;exit"') yield assert_equal, mi.inputs.script, 'whos' yield assert_equal, mi.inputs.script_file, 'testscript' path_exists = os.path.exists(os.path.join(basedir, 'testscript.m')) yield assert_false, path_exists rmtree(basedir)
def apply_transform_onefile(transform, file): """ applies transform to files using spm """ startdir = os.getcwd() pth, _ = os.path.split(file) os.chdir(pth) mlab_cmd = mlab.MatlabCommand(matlab_cmd='matlab-spm8') mlab_cmd.inputs.nodesktop = True mlab_cmd.inputs.nosplash = True mlab_cmd.inputs.ignore_exception = True mlab_cmd.inputs.mfile = True mlab_cmd.inputs.script_file = 'pyspm8_apply_transform.m' script = """ infile = \'%s\'; transform = load(\'%s\'); imgspace = spm_get_space(infile); spm_get_space(infile ,transform.M*imgspace); """ % (file, transform) mlab_cmd.inputs.script = script mout = mlab_cmd.run() os.chdir(startdir) return mout
def spm_get_space(in_file): """Run the spm function spm_get_space through Matlab. Parameters ========== in_file : str Path to an existant nifti image. Returns ======= affine : numpy.ndarray of shape (4, 4) The affine transformation matrix. """ mat_path = '/tmp/space.mat' mlab = matlab.MatlabCommand() mlab.inputs.script = "\naddpath '/i2bm/local/spm8-6313/' \n" + \ "matrix = spm_get_space(" + in_file + ");\n" + \ "save('" + mat_path + "', 'matrix')" mlab.run() mat_dict = loadmat(mat_path) affine = mat_dict['matrix'] return affine
def test_cmdline(): default_script_file = clean_workspace_and_get_default_script_file() mi = mlab.MatlabCommand(script="whos", script_file="testscript", mfile=False) assert mi.cmdline == matlab_cmd + ( ' -nodesktop -nosplash -singleCompThread -r "fprintf(1,' "'Executing code at %s:\\n',datestr(now));ver,try," "whos,catch ME,fprintf(2,'MATLAB code threw an " "exception:\\n');fprintf(2,'%s\\n',ME.message);if " "length(ME.stack) ~= 0, fprintf(2,'File:%s\\nName:%s\\n" "Line:%d\\n',ME.stack.file,ME.stack.name," 'ME.stack.line);, end;end;;exit"') assert mi.inputs.script == "whos" assert mi.inputs.script_file == "testscript" assert not os.path.exists( mi.inputs.script_file), "scriptfile should not exist" assert not os.path.exists( default_script_file), "default scriptfile should not exist."
def ExploreDTI_calculate_tracts_from_masks(cwd_masks, tracts, mat): import nipype.interfaces.matlab as Matlab import os from shutil import copy import re matlab = Matlab.MatlabCommand() # below is where you add paths that matlab might require, this is equivalent to addpath() matlab.inputs.paths = ['/home/amr/SCRIPTS/'] matlab.inputs.single_comp_thread = False cwd = os.getcwd() subj_no = re.findall('\d+', cwd)[-1] matlab.inputs.script = """ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%calculate tracts from masks%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% E_DTI_Analyse_tracts_ff... ('{0}', '{1}', '{2}', '{3}/CC_Tracts_{4}.mat') %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Extract Tract Info%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% E_DTI_Convert_Tract_mats_2_xls('{3}', '{3}/Tracts_Desc_Stats_{4}.csv') %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Extract Tract Info Other Modalities%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Native space, do not forget % put all images you wish to extract tract info from % matlab from inside python does not accept curly braces aka cells, i tried many combinations without much luck %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Diff_20 fa_diff_20 = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_20_workingdir/DTI_workflow/_subject_id_{4}/fit_tensor/dtifit__FA.nii.gz' E_DTI_Export_Tract_Volume_Info(fa_diff_20 ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') fa_diff_20 = load('{3}/CC_Tracts_{4}_dtifit__FA.nii_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['FA_Diff_20_{4},' num2str(fa_diff_20.mean_TV)], 'delimiter','','-append'); md_diff_20 = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_20_workingdir/DTI_workflow/_subject_id_{4}/fit_tensor/dtifit__MD.nii.gz' E_DTI_Export_Tract_Volume_Info(md_diff_20 ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') md_diff_20 = load('{3}/CC_Tracts_{4}_dtifit__MD.nii_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['MD_Diff_20_{4},' num2str(md_diff_20.mean_TV)], 'delimiter','','-append'); ad_diff_20 = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_20_workingdir/DTI_workflow/_subject_id_{4}/fit_tensor/dtifit__L1.nii.gz' E_DTI_Export_Tract_Volume_Info(ad_diff_20 ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') ad_diff_20 = load('{3}/CC_Tracts_{4}_dtifit__L1.nii_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['AD_Diff_20_{4},' num2str(ad_diff_20.mean_TV)], 'delimiter','','-append'); rd_diff_20 = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_20_workingdir/DTI_workflow/_subject_id_{4}/RD/dtifit__L2_maths_maths.nii.gz' E_DTI_Export_Tract_Volume_Info(rd_diff_20 ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') rd_diff_20 = load('{3}/CC_Tracts_{4}_dtifit__L2_maths_maths.nii_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['RD_Diff_20_{4},' num2str(rd_diff_20.mean_TV)], 'delimiter','','-append'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%CHARMED FA_CHARMED = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_CHARMED_workingdir/Multishell_CHARMED_workflow/_subject_id_{4}/CHARMED_r2/CHARMED_r2/Tensor.FA.nii.gz' E_DTI_Export_Tract_Volume_Info(FA_CHARMED ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') FA_CHARMED = load('{3}/CC_Tracts_{4}_Tensor.FA.nii_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['FA_CHARMED_{4},' num2str(FA_CHARMED.mean_TV)], 'delimiter','','-append'); MD_CHARMED = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_CHARMED_workingdir/Multishell_CHARMED_workflow/_subject_id_{4}/CHARMED_r2/CHARMED_r2/Tensor.MD.nii.gz' E_DTI_Export_Tract_Volume_Info(MD_CHARMED ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') MD_CHARMED = load('{3}/CC_Tracts_{4}_Tensor.MD.nii_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['MD_CHARMED_{4},' num2str(MD_CHARMED.mean_TV)], 'delimiter','','-append'); AD_CHARMED = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_CHARMED_workingdir/Multishell_CHARMED_workflow/_subject_id_{4}/CHARMED_r2/CHARMED_r2/Tensor.AD.nii.gz' E_DTI_Export_Tract_Volume_Info(AD_CHARMED ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') AD_CHARMED = load('{3}/CC_Tracts_{4}_Tensor.AD.nii_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['AD_CHARMED_{4},' num2str(AD_CHARMED.mean_TV)], 'delimiter','','-append'); RD_CHARMED = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_CHARMED_workingdir/Multishell_CHARMED_workflow/_subject_id_{4}/CHARMED_r2/CHARMED_r2/Tensor.RD.nii.gz' E_DTI_Export_Tract_Volume_Info(RD_CHARMED ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') RD_CHARMED = load('{3}/CC_Tracts_{4}_Tensor.RD.nii_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['RD_CHARMED_{4},' num2str(RD_CHARMED.mean_TV)], 'delimiter','','-append'); FR_CHARMED = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_CHARMED_workingdir/Multishell_CHARMED_workflow/_subject_id_{4}/CHARMED_r2/CHARMED_r2/FR.nii.gz' E_DTI_Export_Tract_Volume_Info(FR_CHARMED ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') FR_CHARMED = load('{3}/CC_Tracts_{4}_FR.nii_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['FR_CHARMED_{4},' num2str(FR_CHARMED.mean_TV)], 'delimiter','','-append'); IAD_CHARMED = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_CHARMED_workingdir/Multishell_CHARMED_workflow/_subject_id_{4}/CHARMED_r2/CHARMED_r2/CHARMEDRestricted0.d.nii.gz' E_DTI_Export_Tract_Volume_Info(IAD_CHARMED ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') IAD_CHARMED = load('{3}/CC_Tracts_{4}_CHARMEDRestricted0.d.nii_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['IAD_CHARMED_{4},' num2str(IAD_CHARMED.mean_TV)], 'delimiter','','-append'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%NODDI ODI_NODDI = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_NODDI_workingdir/Multishell_NODDI_workflow/_subject_id_{4}/NODDI/NODDI_odi.nii' E_DTI_Export_Tract_Volume_Info(ODI_NODDI ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') ODI_NODDI = load('{3}/CC_Tracts_{4}_NODDI_odi_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['ODI_NODDI_{4},' num2str(ODI_NODDI.mean_TV)], 'delimiter','','-append'); FICVF_NODDI = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_NODDI_workingdir/Multishell_NODDI_workflow/_subject_id_{4}/NODDI/NODDI_ficvf.nii' E_DTI_Export_Tract_Volume_Info(FICVF_NODDI ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') FICVF_NODDI = load('{3}/CC_Tracts_{4}_NODDI_ficvf_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['FICVF_NODDI_{4},' num2str(FICVF_NODDI.mean_TV)], 'delimiter','','-append'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Kurtosis_dipy FA_Kurtosis_dipy = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_Kurtosis_workingdir/Multishell_workflow_Kurtosis/_subject_id_{4}/Kurtosis/DKI_FA.nii' E_DTI_Export_Tract_Volume_Info(FA_Kurtosis_dipy ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') FA_Kurtosis_dipy = load('{3}/CC_Tracts_{4}_DKI_FA_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['FA_Kurtosis_dipy_{4},' num2str(FA_Kurtosis_dipy.mean_TV)], 'delimiter','','-append'); MD_Kurtosis_dipy = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_Kurtosis_workingdir/Multishell_workflow_Kurtosis/_subject_id_{4}/Kurtosis/DKI_MD.nii' E_DTI_Export_Tract_Volume_Info(MD_Kurtosis_dipy ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') MD_Kurtosis_dipy = load('{3}/CC_Tracts_{4}_DKI_MD_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['MD_Kurtosis_dipy_{4},' num2str(MD_Kurtosis_dipy.mean_TV)], 'delimiter','','-append'); AD_Kurtosis_dipy = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_Kurtosis_workingdir/Multishell_workflow_Kurtosis/_subject_id_{4}/Kurtosis/DKI_AD.nii' E_DTI_Export_Tract_Volume_Info(AD_Kurtosis_dipy ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') AD_Kurtosis_dipy = load('{3}/CC_Tracts_{4}_DKI_AD_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['AD_Kurtosis_dipy_{4},' num2str(AD_Kurtosis_dipy.mean_TV)], 'delimiter','','-append'); RD_Kurtosis_dipy = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_Kurtosis_workingdir/Multishell_workflow_Kurtosis/_subject_id_{4}/Kurtosis/DKI_RD.nii' E_DTI_Export_Tract_Volume_Info(RD_Kurtosis_dipy ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') RD_Kurtosis_dipy = load('{3}/CC_Tracts_{4}_DKI_RD_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['RD_Kurtosis_dipy_{4},' num2str(RD_Kurtosis_dipy.mean_TV)], 'delimiter','','-append'); AK_Kurtosis_dipy = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_Kurtosis_workingdir/Multishell_workflow_Kurtosis/_subject_id_{4}/Kurtosis/DKI_AK.nii' E_DTI_Export_Tract_Volume_Info(AK_Kurtosis_dipy ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') AK_Kurtosis_dipy = load('{3}/CC_Tracts_{4}_DKI_AK_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['AK_Kurtosis_dipy_{4},' num2str(AK_Kurtosis_dipy.mean_TV)], 'delimiter','','-append'); AWF_Kurtosis_dipy = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_Kurtosis_workingdir/Multishell_workflow_Kurtosis/_subject_id_{4}/Kurtosis/DKI_AWF.nii' E_DTI_Export_Tract_Volume_Info(AWF_Kurtosis_dipy ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') AWF_Kurtosis_dipy = load('{3}/CC_Tracts_{4}_DKI_AWF_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['AWF_Kurtosis_dipy_{4},' num2str(AWF_Kurtosis_dipy.mean_TV)], 'delimiter','','-append'); MK_Kurtosis_dipy = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_Kurtosis_workingdir/Multishell_workflow_Kurtosis/_subject_id_{4}/Kurtosis/DKI_MK.nii' E_DTI_Export_Tract_Volume_Info(MK_Kurtosis_dipy ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') MK_Kurtosis_dipy = load('{3}/CC_Tracts_{4}_DKI_MK_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['MK_Kurtosis_dipy_{4},' num2str(MK_Kurtosis_dipy.mean_TV)], 'delimiter','','-append'); RK_Kurtosis_dipy = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_Kurtosis_workingdir/Multishell_workflow_Kurtosis/_subject_id_{4}/Kurtosis/DKI_RK.nii' E_DTI_Export_Tract_Volume_Info(RK_Kurtosis_dipy ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') RK_Kurtosis_dipy = load('{3}/CC_Tracts_{4}_DKI_RK_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['RK_Kurtosis_dipy_{4},' num2str(RK_Kurtosis_dipy.mean_TV)], 'delimiter','','-append'); TORT_Kurtosis_dipy = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_Kurtosis_workingdir/Multishell_workflow_Kurtosis/_subject_id_{4}/Kurtosis/DKI_TORT.nii' E_DTI_Export_Tract_Volume_Info(TORT_Kurtosis_dipy ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') TORT_Kurtosis_dipy = load('{3}/CC_Tracts_{4}_DKI_TORT_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['TORT_Kurtosis_dipy_{4},' num2str(TORT_Kurtosis_dipy.mean_TV)], 'delimiter','','-append'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Kurtosis_ExploreDTI FA_Kurtosis_E_DTI = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_ExploreDTI_workingdir/Multishell_ExploreDTI_workflow/_subject_id_{4}/ExploreDTI_kurtosis/Diff_Multishell_{4}_sorted_flipped_FA.nii' E_DTI_Export_Tract_Volume_Info(FA_Kurtosis_E_DTI ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') FA_Kurtosis_E_DTI = load('{3}/CC_Tracts_{4}_Diff_Multishell_{4}_sorted_flipped_FA_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['FA_Kurtosis_E_DTI_{4},' num2str(FA_Kurtosis_E_DTI.mean_TV)], 'delimiter','','-append'); MD_Kurtosis_E_DTI = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_ExploreDTI_workingdir/Multishell_ExploreDTI_workflow/_subject_id_{4}/ExploreDTI_kurtosis/Diff_Multishell_{4}_sorted_flipped_MD.nii' E_DTI_Export_Tract_Volume_Info(MD_Kurtosis_E_DTI ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') MD_Kurtosis_E_DTI = load('{3}/CC_Tracts_{4}_Diff_Multishell_{4}_sorted_flipped_MD_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['MD_Kurtosis_E_DTI_{4},' num2str(MD_Kurtosis_E_DTI.mean_TV)], 'delimiter','','-append'); AD_Kurtosis_E_DTI = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_ExploreDTI_workingdir/Multishell_ExploreDTI_workflow/_subject_id_{4}/ExploreDTI_kurtosis/Diff_Multishell_{4}_sorted_flipped_L1.nii' E_DTI_Export_Tract_Volume_Info(AD_Kurtosis_E_DTI ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') AD_Kurtosis_E_DTI = load('{3}/CC_Tracts_{4}_Diff_Multishell_{4}_sorted_flipped_L1_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['AD_Kurtosis_E_DTI_{4},' num2str(AD_Kurtosis_E_DTI.mean_TV)], 'delimiter','','-append'); RD_Kurtosis_E_DTI = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_ExploreDTI_workingdir/Multishell_ExploreDTI_workflow/_subject_id_{4}/ExploreDTI_kurtosis/Diff_Multishell_{4}_sorted_flipped_RD.nii' E_DTI_Export_Tract_Volume_Info(RD_Kurtosis_E_DTI ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') RD_Kurtosis_E_DTI = load('{3}/CC_Tracts_{4}_Diff_Multishell_{4}_sorted_flipped_RD_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['RD_Kurtosis_E_DTI_{4},' num2str(RD_Kurtosis_E_DTI.mean_TV)], 'delimiter','','-append'); AK_Kurtosis_E_DTI = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_ExploreDTI_workingdir/Multishell_ExploreDTI_workflow/_subject_id_{4}/ExploreDTI_kurtosis/Diff_Multishell_{4}_sorted_flipped_AK.nii' E_DTI_Export_Tract_Volume_Info(AK_Kurtosis_E_DTI ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') AK_Kurtosis_E_DTI = load('{3}/CC_Tracts_{4}_Diff_Multishell_{4}_sorted_flipped_AK_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['AK_Kurtosis_E_DTI_{4},' num2str(AK_Kurtosis_E_DTI.mean_TV)], 'delimiter','','-append'); AWF_Kurtosis_E_DTI = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_ExploreDTI_workingdir/Multishell_ExploreDTI_workflow/_subject_id_{4}/ExploreDTI_kurtosis/Diff_Multishell_{4}_sorted_flipped_AWF.nii' E_DTI_Export_Tract_Volume_Info(AWF_Kurtosis_E_DTI ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') AWF_Kurtosis_E_DTI = load('{3}/CC_Tracts_{4}_Diff_Multishell_{4}_sorted_flipped_AWF_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['AWF_Kurtosis_E_DTI_{4},' num2str(AWF_Kurtosis_E_DTI.mean_TV)], 'delimiter','','-append'); KA_Kurtosis_E_DTI = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_ExploreDTI_workingdir/Multishell_ExploreDTI_workflow/_subject_id_{4}/ExploreDTI_kurtosis/Diff_Multishell_{4}_sorted_flipped_KA.nii' E_DTI_Export_Tract_Volume_Info(KA_Kurtosis_E_DTI ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') KA_Kurtosis_E_DTI = load('{3}/CC_Tracts_{4}_Diff_Multishell_{4}_sorted_flipped_KA_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['KA_Kurtosis_E_DTI_{4},' num2str(KA_Kurtosis_E_DTI.mean_TV)], 'delimiter','','-append'); MK_Kurtosis_E_DTI = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_ExploreDTI_workingdir/Multishell_ExploreDTI_workflow/_subject_id_{4}/ExploreDTI_kurtosis/Diff_Multishell_{4}_sorted_flipped_MK.nii' E_DTI_Export_Tract_Volume_Info(MK_Kurtosis_E_DTI ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') MK_Kurtosis_E_DTI = load('{3}/CC_Tracts_{4}_Diff_Multishell_{4}_sorted_flipped_MK_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['MK_Kurtosis_E_DTI_{4},' num2str(MK_Kurtosis_E_DTI.mean_TV)], 'delimiter','','-append'); RK_Kurtosis_E_DTI = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_ExploreDTI_workingdir/Multishell_ExploreDTI_workflow/_subject_id_{4}/ExploreDTI_kurtosis/Diff_Multishell_{4}_sorted_flipped_RK.nii' E_DTI_Export_Tract_Volume_Info(RK_Kurtosis_E_DTI ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') RK_Kurtosis_E_DTI = load('{3}/CC_Tracts_{4}_Diff_Multishell_{4}_sorted_flipped_RK_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['RK_Kurtosis_E_DTI_{4},' num2str(RK_Kurtosis_E_DTI.mean_TV)], 'delimiter','','-append'); TORT_Kurtosis_E_DTI = '/media/amr/Amr_4TB/Work/October_Acquistion/Diffusion_Multishell_ExploreDTI_workingdir/Multishell_ExploreDTI_workflow/_subject_id_{4}/ExploreDTI_kurtosis/Diff_Multishell_{4}_sorted_flipped_TORT.nii' E_DTI_Export_Tract_Volume_Info(TORT_Kurtosis_E_DTI ,'{3}/CC_Tracts_{4}.mat','{2}','{3}') TORT_Kurtosis_E_DTI = load('{3}/CC_Tracts_{4}_Diff_Multishell_{4}_sorted_flipped_TORT_Vol_Info.mat'); dlmwrite('{3}/Tracts_Others_Stats_{4}.csv', ['TORT_Kurtosis_E_DTI_{4},' num2str(TORT_Kurtosis_E_DTI.mean_TV)], 'delimiter','','-append'); """.format( cwd_masks, tracts, mat, cwd, subj_no ) #Ihave no idea why '' solved the problem of writing to different dir, but it did res = matlab.run() cc_tracts = os.path.abspath('CC_Tracts_{0}.mat'.format(subj_no)) return cc_tracts #You always need return
def ExploreDTI_kurtosis(flipped_mat): import nipype.interfaces.matlab as Matlab import os from shutil import copy import re matlab = Matlab.MatlabCommand() # below is where you add paths that matlab might require, this is equivalent to addpath() matlab.inputs.paths = ['/home/amr/SCRIPTS/'] matlab.inputs.single_comp_thread = False cwd = os.getcwd() subj_no = re.findall('\d+', cwd)[-1] matlab.inputs.script = """ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Get Kurtosis params%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% addpath('/media/amr/HDD/Softwares/ExploreDTI') MainExploreDTI; vars = E_DTI_Complete_List_var; % FA -> 1 % MD -> 2 % L1 aka AD ->3 % RD -> 6 % % 'Mean kurtosis (''_MK.nii'')' -> 46 % 'Axial kurtosis (''_AK.nii'')' -> 47 % 'Radial kurtosis (''_RK.nii'')' -> 48 % 'Kurtosis anisotropy (''_KA.nii'')' -> 49 % AWF -> 53 % TORT -> 54 E_DTI_Convert_mat_2_nii('{0}', '{1}', vars([1:3,6,46:49,53,54])) """.format( flipped_mat, cwd ) #Ihave no idea why '' solved the problem of writing to different dir, but it did res = matlab.run() ak = os.path.abspath( 'Diff_Multishell_{0}_sorted_flipped_AK.nii'.format(subj_no)) awf = os.path.abspath( 'Diff_Multishell_{0}_sorted_flipped_AWF.nii'.format(subj_no)) fa = os.path.abspath( 'Diff_Multishell_{0}_sorted_flipped_FA.nii'.format(subj_no)) ka = os.path.abspath( 'Diff_Multishell_{0}_sorted_flipped_KA.nii'.format(subj_no)) ad = os.path.abspath( 'Diff_Multishell_{0}_sorted_flipped_L1.nii'.format(subj_no)) md = os.path.abspath( 'Diff_Multishell_{0}_sorted_flipped_MD.nii'.format(subj_no)) mk = os.path.abspath( 'Diff_Multishell_{0}_sorted_flipped_MK.nii'.format(subj_no)) rd = os.path.abspath( 'Diff_Multishell_{0}_sorted_flipped_RD.nii'.format(subj_no)) rk = os.path.abspath( 'Diff_Multishell_{0}_sorted_flipped_RK.nii'.format(subj_no)) tort = os.path.abspath( 'Diff_Multishell_{0}_sorted_flipped_TORT.nii'.format(subj_no)) return ak, awf, fa, ka, ad, md, mk, rd, rk, tort #You always need return
def test_set_matlabcmd(): mi = mlab.MatlabCommand() mi.set_default_matlab_cmd('foo') yield assert_equal, mi._default_matlab_cmd, 'foo' mi.set_default_matlab_cmd(matlab_cmd)