def getInputs(): #pylint: disable=unused-variable from mrtrix3 import app, path, run run.command('mrconvert ' + path.fromUser(app.args.in_5tt, True) + ' ' + path.toTemp('5tt.mif', True)) if app.args.dirs: run.command('mrconvert ' + path.fromUser(app.args.dirs, True) + ' ' + path.toTemp('dirs.mif', True) + ' -strides 0,0,0,1')
def getInputs(): import os from mrtrix3 import app, path, run run.command('mrconvert ' + path.fromUser(app.args.in_5tt, True) + ' ' + path.toTemp('5tt.mif', True)) if app.args.dirs: run.command('mrconvert ' + path.fromUser(app.args.dirs, True) + ' ' + path.toTemp('dirs.mif', True) + ' -stride 0,0,0,1')
def getInputs(): import os, shutil from mrtrix3 import app, path, run run.command('mrconvert ' + path.fromUser(app.args.input, True) + ' ' + path.toTemp('input.mif', True)) if app.args.lut: run.function(shutil.copyfile, path.fromUser(app.args.lut, False), path.toTemp('LUT.txt', False))
def getInputs(): import os from mrtrix3 import app, image, path, run if app.args.mask: run.command('mrconvert ' + path.fromUser(app.args.mask, True) + ' ' + path.toTemp('mask.mif', True) + ' -datatype bit -stride -1,+2,+3') if app.args.t2: if not image.match(app.args.input, app.args.t2): app.error('Provided T2 image does not match input T1 image') run.command('mrconvert ' + path.fromUser(app.args.t2, True) + ' ' + path.toTemp('T2.nii', True) + ' -stride -1,+2,+3')
def getInputs(): #pylint: disable=unused-variable import os from mrtrix3 import app, path, run mask_path = path.toTemp('mask.mif', False) if os.path.exists(mask_path): app.warn('-mask option is ignored by algorithm \'manual\'') os.remove(mask_path) run.command('mrconvert ' + path.fromUser(app.args.in_voxels, True) + ' ' + path.toTemp('in_voxels.mif', True)) if app.args.dirs: run.command('mrconvert ' + path.fromUser(app.args.dirs, True) + ' ' + path.toTemp('dirs.mif', True) + ' -strides 0,0,0,1')
def getInputs(): #pylint: disable=unused-variable from mrtrix3 import app, image, path, run image.check3DNonunity(path.fromUser(app.args.input, False)) run.command('mrconvert ' + path.fromUser(app.args.input, True) + ' ' + path.toTemp('input.mif', True)) if app.args.mask: run.command('mrconvert ' + path.fromUser(app.args.mask, True) + ' ' + path.toTemp('mask.mif', True) + ' -datatype bit -strides -1,+2,+3') if app.args.t2: if not image.match(app.args.input, app.args.t2): app.error('Provided T2 image does not match input T1 image') run.command('mrconvert ' + path.fromUser(app.args.t2, True) + ' ' + path.toTemp('T2.nii', True) + ' -strides -1,+2,+3')
def getInputs(): import os from mrtrix3 import app, path, run mask_path = path.toTemp('mask.mif', False) if os.path.exists(mask_path): app.warn('-mask option is ignored by algorithm \'manual\'') os.remove(mask_path) run.command('mrconvert ' + path.fromUser(app.args.in_voxels, True) + ' ' + path.toTemp('in_voxels.mif', True)) if app.args.dirs: run.command('mrconvert ' + path.fromUser(app.args.dirs, True) + ' ' + path.toTemp('dirs.mif', True) + ' -stride 0,0,0,1')
def execute(): #pylint: disable=unused-variable import shutil from mrtrix3 import app, image, path, run bvalues = [ int(round(float(x))) for x in image.mrinfo('dwi.mif', 'shell_bvalues').split() ] if len(bvalues) < 2: app.error('Need at least 2 unique b-values (including b=0).') lmax_option = '' if app.args.lmax: lmax_option = ' -lmax ' + app.args.lmax if not app.args.mask: run.command('maskfilter mask.mif erode mask_eroded.mif -npass ' + str(app.args.erode)) mask_path = 'mask_eroded.mif' else: mask_path = 'mask.mif' run.command('dwi2tensor dwi.mif -mask ' + mask_path + ' tensor.mif') run.command( 'tensor2metric tensor.mif -fa fa.mif -vector vector.mif -mask ' + mask_path) if app.args.threshold: run.command('mrthreshold fa.mif voxels.mif -abs ' + str(app.args.threshold)) else: run.command('mrthreshold fa.mif voxels.mif -top ' + str(app.args.number)) run.command( 'dwiextract dwi.mif - -singleshell -no_bzero | amp2response - voxels.mif vector.mif response.txt' + lmax_option) run.function(shutil.copyfile, 'response.txt', path.fromUser(app.args.output, False))
def execute(): #pylint: disable=unused-variable import os, shutil from mrtrix3 import app, image, path, run shells = [ int(round(float(x))) for x in image.mrinfo('dwi.mif', 'shell_bvalues').split() ] # Get lmax information (if provided) lmax = [ ] if app.args.lmax: lmax = [ int(x.strip()) for x in app.args.lmax.split(',') ] if not len(lmax) == len(shells): app.error('Number of manually-defined lmax\'s (' + str(len(lmax)) + ') does not match number of b-value shells (' + str(len(shells)) + ')') for l in lmax: if l%2: app.error('Values for lmax must be even') if l<0: app.error('Values for lmax must be non-negative') # Do we have directions, or do we need to calculate them? if not os.path.exists('dirs.mif'): run.command('dwi2tensor dwi.mif - -mask in_voxels.mif | tensor2metric - -vector dirs.mif') # Get response function bvalues_option = ' -shells ' + ','.join(map(str,shells)) lmax_option = '' if lmax: lmax_option = ' -lmax ' + ','.join(map(str,lmax)) run.command('amp2response dwi.mif in_voxels.mif dirs.mif response.txt' + bvalues_option + lmax_option) run.function(shutil.copyfile, 'response.txt', path.fromUser(app.args.output, False)) run.function(shutil.copyfile, 'in_voxels.mif', 'voxels.mif')
def execute(): import os, shutil from mrtrix3 import app, image, path, run shells = [ int(round(float(x))) for x in image.headerField('dwi.mif', 'shells').split() ] # Get lmax information (if provided) lmax = [] if app.args.lmax: lmax = [int(x.strip()) for x in app.args.lmax.split(',')] if not len(lmax) == len(shells): app.error('Number of manually-defined lmax\'s (' + str(len(lmax)) + ') does not match number of b-value shells (' + str(len(shells)) + ')') for l in lmax: if l % 2: app.error('Values for lmax must be even') if l < 0: app.error('Values for lmax must be non-negative') # Do we have directions, or do we need to calculate them? if not os.path.exists('dirs.mif'): run.command( 'dwi2tensor dwi.mif - -mask in_voxels.mif | tensor2metric - -vector dirs.mif' ) # Get response function bvalues_option = ' -shell ' + ','.join(map(str, shells)) lmax_option = '' if lmax: lmax_option = ' -lmax ' + ','.join(map(str, lmax)) run.command('amp2response dwi.mif in_voxels.mif dirs.mif response.txt' + bvalues_option + lmax_option) run.function(shutil.copyfile, 'response.txt', path.fromUser(app.args.output, False)) run.function(shutil.copyfile, 'in_voxels.mif', 'voxels.mif')
def execute(): #pylint: disable=unused-variable import shutil from mrtrix3 import app, image, path, run bvalues = [ int(round(float(x))) for x in image.mrinfo('dwi.mif', 'shell_bvalues').split() ] if len(bvalues) < 2: app.error('Need at least 2 unique b-values (including b=0).') lmax_option = '' if app.args.lmax: lmax_option = ' -lmax ' + app.args.lmax if not app.args.mask: run.command('maskfilter mask.mif erode mask_eroded.mif -npass ' + str(app.args.erode)) mask_path = 'mask_eroded.mif' else: mask_path = 'mask.mif' run.command('dwi2tensor dwi.mif -mask ' + mask_path + ' tensor.mif') run.command('tensor2metric tensor.mif -fa fa.mif -vector vector.mif -mask ' + mask_path) if app.args.threshold: run.command('mrthreshold fa.mif voxels.mif -abs ' + str(app.args.threshold)) else: run.command('mrthreshold fa.mif voxels.mif -top ' + str(app.args.number)) run.command('dwiextract dwi.mif - -singleshell -no_bzero | amp2response - voxels.mif vector.mif response.txt' + lmax_option) run.function(shutil.copyfile, 'response.txt', path.fromUser(app.args.output, False))
def getInputs(): #pylint: disable=unused-variable import os, shutil #pylint: disable=unused-variable from mrtrix3 import app, path, run checkGIFinput(path.fromUser(app.args.input, True)) run.command('mrconvert ' + path.fromUser(app.args.input, True) + ' ' + path.toTemp('input.mif', True))
def execute(): import math, os, shutil from mrtrix3 import app, image, path, run # Ideally want to use the oversampling-based regridding of the 5TT image from the SIFT model, not mrtransform # May need to commit 5ttregrid... # Verify input 5tt image run.command('5ttcheck 5tt.mif', False) # Get shell information shells = [ int(round(float(x))) for x in image.headerField('dwi.mif', 'shells').split() ] if len(shells) < 3: app.warn('Less than three b-value shells; response functions will not be applicable in resolving three tissues using MSMT-CSD algorithm') # Get lmax information (if provided) wm_lmax = [ ] if app.args.lmax: wm_lmax = [ int(x.strip()) for x in app.args.lmax.split(',') ] if not len(wm_lmax) == len(shells): app.error('Number of manually-defined lmax\'s (' + str(len(wm_lmax)) + ') does not match number of b-value shells (' + str(len(shells)) + ')') for l in wm_lmax: if l%2: app.error('Values for lmax must be even') if l<0: app.error('Values for lmax must be non-negative') run.command('dwi2tensor dwi.mif - -mask mask.mif | tensor2metric - -fa fa.mif -vector vector.mif') if not os.path.exists('dirs.mif'): run.function(shutil.copy, 'vector.mif', 'dirs.mif') run.command('mrtransform 5tt.mif 5tt_regrid.mif -template fa.mif -interp linear') # Basic tissue masks run.command('mrconvert 5tt_regrid.mif - -coord 3 2 -axes 0,1,2 | mrcalc - ' + str(app.args.pvf) + ' -gt mask.mif -mult wm_mask.mif') run.command('mrconvert 5tt_regrid.mif - -coord 3 0 -axes 0,1,2 | mrcalc - ' + str(app.args.pvf) + ' -gt fa.mif ' + str(app.args.fa) + ' -lt -mult mask.mif -mult gm_mask.mif') run.command('mrconvert 5tt_regrid.mif - -coord 3 3 -axes 0,1,2 | mrcalc - ' + str(app.args.pvf) + ' -gt fa.mif ' + str(app.args.fa) + ' -lt -mult mask.mif -mult csf_mask.mif') # Revise WM mask to only include single-fibre voxels app.console('Calling dwi2response recursively to select WM single-fibre voxels using \'' + app.args.wm_algo + '\' algorithm') recursive_cleanup_option='' if not app._cleanup: recursive_cleanup_option = ' -nocleanup' run.command('dwi2response ' + app.args.wm_algo + ' dwi.mif wm_ss_response.txt -mask wm_mask.mif -voxels wm_sf_mask.mif -tempdir ' + app._tempDir + recursive_cleanup_option) # Check for empty masks wm_voxels = int(image.statistic('wm_sf_mask.mif', 'count', 'wm_sf_mask.mif')) gm_voxels = int(image.statistic('gm_mask.mif', 'count', 'gm_mask.mif')) csf_voxels = int(image.statistic('csf_mask.mif', 'count', 'csf_mask.mif')) empty_masks = [ ] if not wm_voxels: empty_masks.append('WM') if not gm_voxels: empty_masks.append('GM') if not csf_voxels: empty_masks.append('CSF') if empty_masks: message = ','.join(empty_masks) message += ' tissue mask' if len(empty_masks) > 1: message += 's' message += ' empty; cannot estimate response function' if len(empty_masks) > 1: message += 's' app.error(message) # For each of the three tissues, generate a multi-shell response bvalues_option = ' -shell ' + ','.join(map(str,shells)) sfwm_lmax_option = '' if wm_lmax: sfwm_lmax_option = ' -lmax ' + ','.join(map(str,wm_lmax)) run.command('amp2response dwi.mif wm_sf_mask.mif dirs.mif wm.txt' + bvalues_option + sfwm_lmax_option) run.command('amp2response dwi.mif gm_mask.mif dirs.mif gm.txt' + bvalues_option + ' -isotropic') run.command('amp2response dwi.mif csf_mask.mif dirs.mif csf.txt' + bvalues_option + ' -isotropic') run.function(shutil.copyfile, 'wm.txt', path.fromUser(app.args.out_wm, False)) run.function(shutil.copyfile, 'gm.txt', path.fromUser(app.args.out_gm, False)) run.function(shutil.copyfile, 'csf.txt', path.fromUser(app.args.out_csf, False)) # Generate output 4D binary image with voxel selections; RGB as in MSMT-CSD paper run.command('mrcat csf_mask.mif gm_mask.mif wm_sf_mask.mif voxels.mif -axis 3')
def getInputs(): import os, shutil from mrtrix3 import app, path, run if app.args.lut: run.function(shutil.copyfile, path.fromUser(app.args.lut, False), path.toTemp('LUT.txt', False))
run.command('mrtransform -linear rigidXform' + str(idx) + 'to0.txt dwipretf' + str(idx) + '.mif dwitf' + str(idx) + '.mif') miflist.append('dwitf' + str(idx) + '.mif') DWImif = ' '.join(miflist) run.command('mrcat -axis 3 dwitf0.mif ' + DWImif + ' dwitf.mif') run.function(os.remove,'working.mif') run.command('mrconvert dwitf.mif working.mif') # epi + eddy current and motion correction # if number of input volumes is greater than 1, make a new acqp and index file. if app.args.eddy: print("...Beginning EDDY") if app.args.rpe_none: run.command('dwipreproc -eddy_options " --repol --data_is_shelled" -rpe_none -pe_dir ' + app.args.pe_dir + ' working.mif dwiec.mif') elif app.args.rpe_pair: run.command('dwiextract -bzero dwi.mif - | mrconvert -coord 3 0 - b0pe.mif') rpe_size = [ int(s) for s in image.Header(path.fromUser(app.args.rpe_pair,True)).size() ] if len(rpe_size) == 4: run.command('mrconvert -coord 3 0 ' + path.fromUser(app.args.rpe_pair,True) + ' b0rpe.mif') else: run.command('mrconvert ' + path.fromUser(app.args.rpe_pair,True) + ' b0rpe.mif') run.command('mrcat -axis 3 b0pe.mif b0rpe.mif rpepair.mif') run.command('dwipreproc -eddy_options " --repol --data_is_shelled" -rpe_pair -se_epi rpepair.mif -pe_dir ' + app.args.pe_dir + ' working.mif dwiec.mif') elif app.args.rpe_all: run.command('mrconvert -export_grad_mrtrix grad.txt dwi.mif tmp.mif') run.command('mrconvert -grad grad.txt ' + path.fromUser(app.args.rpe_all,True) + ' dwirpe.mif') run.command('mrcat -axis 3 working.mif dwirpe.mif dwipe_rpe.mif') run.command('dwipreproc -eddy_options " --repol --data_is_shelled" -rpe_all -pe_dir ' + app.args.pe_dir + ' dwipe_rpe.mif dwiec.mif') run.function(os.remove,'tmp.mif') elif app.args.rpe_header: run.command('dwipreproc -eddy_options " --repol --data_is_shelled" -rpe_header dwipe_rpe.mif dwiec.mif') elif not app.args.rpe_header and not app.args.rpe_all and not app.args.rpe_pair: print("the eddy option must run alongside -rpe_header, -rpe_all, or -rpe_pair option")
run.function(os.remove, 'working.mif') run.command('mrconvert dwitf.mif working.mif') # epi + eddy current and motion correction # if number of input volumes is greater than 1, make a new acqp and index file. if app.args.eddy: print("...Beginning EDDY") if app.args.rpe_none: run.command( 'dwipreproc -eddy_options " --repol --data_is_shelled" -rpe_none -pe_dir ' + app.args.pe_dir + ' working.mif dwiec.mif') elif app.args.rpe_pair: run.command( 'dwiextract -bzero dwi.mif - | mrconvert -coord 3 0 - b0pe.mif') rpe_size = [ int(s) for s in image.Header(path.fromUser(app.args.rpe_pair, True)).size() ] if len(rpe_size) == 4: run.command('mrconvert -coord 3 0 ' + path.fromUser(app.args.rpe_pair, True) + ' b0rpe.mif') else: run.command('mrconvert ' + path.fromUser(app.args.rpe_pair, True) + ' b0rpe.mif') run.command('mrcat -axis 3 b0pe.mif b0rpe.mif rpepair.mif') run.command( 'dwipreproc -eddy_options " --repol --data_is_shelled" -rpe_pair -se_epi rpepair.mif -pe_dir ' + app.args.pe_dir + ' working.mif dwiec.mif') elif app.args.rpe_all: run.command('mrconvert -export_grad_mrtrix grad.txt dwi.mif tmp.mif') run.command('mrconvert -grad grad.txt ' + path.fromUser(app.args.rpe_all, True) + ' dwirpe.mif')
def execute(): #pylint: disable=unused-variable import os, shutil from mrtrix3 import app, file, image, path, run #pylint: disable=redefined-builtin lmax_option = '' if app.args.lmax: lmax_option = ' -lmax ' + app.args.lmax if app.args.max_iters < 2: app.error('Number of iterations must be at least 2') for iteration in range(0, app.args.max_iters): prefix = 'iter' + str(iteration) + '_' if iteration == 0: RF_in_path = 'init_RF.txt' mask_in_path = 'mask.mif' init_RF = '1 -1 1' with open(RF_in_path, 'w') as f: f.write(init_RF) iter_lmax_option = ' -lmax 4' else: RF_in_path = 'iter' + str(iteration-1) + '_RF.txt' mask_in_path = 'iter' + str(iteration-1) + '_SF_dilated.mif' iter_lmax_option = lmax_option # Run CSD run.command('dwi2fod csd dwi.mif ' + RF_in_path + ' ' + prefix + 'FOD.mif -mask ' + mask_in_path + iter_lmax_option) # Get amplitudes of two largest peaks, and direction of largest run.command('fod2fixel ' + prefix + 'FOD.mif ' + prefix + 'fixel -peak peaks.mif -mask ' + mask_in_path + ' -fmls_no_thresholds') file.delTemporary(prefix + 'FOD.mif') if iteration: file.delTemporary(mask_in_path) run.command('fixel2voxel ' + prefix + 'fixel/peaks.mif split_data ' + prefix + 'amps.mif -number 2') run.command('mrconvert ' + prefix + 'amps.mif ' + prefix + 'first_peaks.mif -coord 3 0 -axes 0,1,2') run.command('mrconvert ' + prefix + 'amps.mif ' + prefix + 'second_peaks.mif -coord 3 1 -axes 0,1,2') file.delTemporary(prefix + 'amps.mif') run.command('fixel2voxel ' + prefix + 'fixel/directions.mif split_dir ' + prefix + 'all_dirs.mif -number 1') file.delTemporary(prefix + 'fixel') run.command('mrconvert ' + prefix + 'all_dirs.mif ' + prefix + 'first_dir.mif -coord 3 0:2') file.delTemporary(prefix + 'all_dirs.mif') # Calculate the 'cost function' Donald derived for selecting single-fibre voxels # https://github.com/MRtrix3/mrtrix3/pull/426 # sqrt(|peak1|) * (1 - |peak2| / |peak1|)^2 run.command('mrcalc ' + prefix + 'first_peaks.mif -sqrt 1 ' + prefix + 'second_peaks.mif ' + prefix + 'first_peaks.mif -div -sub 2 -pow -mult '+ prefix + 'CF.mif') file.delTemporary(prefix + 'first_peaks.mif') file.delTemporary(prefix + 'second_peaks.mif') # Select the top-ranked voxels run.command('mrthreshold ' + prefix + 'CF.mif -top ' + str(app.args.sf_voxels) + ' ' + prefix + 'SF.mif') # Generate a new response function based on this selection run.command('amp2response dwi.mif ' + prefix + 'SF.mif ' + prefix + 'first_dir.mif ' + prefix + 'RF.txt' + iter_lmax_option) file.delTemporary(prefix + 'first_dir.mif') # Should we terminate? if iteration > 0: run.command('mrcalc ' + prefix + 'SF.mif iter' + str(iteration-1) + '_SF.mif -sub ' + prefix + 'SF_diff.mif') file.delTemporary('iter' + str(iteration-1) + '_SF.mif') max_diff = image.statistic(prefix + 'SF_diff.mif', 'max') file.delTemporary(prefix + 'SF_diff.mif') if int(max_diff) == 0: app.console('Convergence of SF voxel selection detected at iteration ' + str(iteration)) file.delTemporary(prefix + 'CF.mif') run.function(shutil.copyfile, prefix + 'RF.txt', 'response.txt') run.function(shutil.move, prefix + 'SF.mif', 'voxels.mif') break # Select a greater number of top single-fibre voxels, and dilate (within bounds of initial mask); # these are the voxels that will be re-tested in the next iteration run.command('mrthreshold ' + prefix + 'CF.mif -top ' + str(app.args.iter_voxels) + ' - | maskfilter - dilate - -npass ' + str(app.args.dilate) + ' | mrcalc mask.mif - -mult ' + prefix + 'SF_dilated.mif') file.delTemporary(prefix + 'CF.mif') # Commence the next iteration # If terminating due to running out of iterations, still need to put the results in the appropriate location if not os.path.exists('response.txt'): app.console('Exiting after maximum ' + str(app.args.max_iters) + ' iterations') run.function(shutil.copyfile, 'iter' + str(app.args.max_iters-1) + '_RF.txt', 'response.txt') run.function(shutil.move, 'iter' + str(app.args.max_iters-1) + '_SF.mif', 'voxels.mif') run.function(shutil.copyfile, 'response.txt', path.fromUser(app.args.output, False))
def getInputs(): #pylint: disable=unused-variable from mrtrix3 import app, path, run checkGIFinput(path.fromUser(app.args.input, True)) run.command('mrconvert ' + path.fromUser(app.args.input, True) + ' ' + path.toTemp('input.mif', True))
def execute(): import math, os, shutil from mrtrix3 import app, file, image, path, run lmax_option = '' if app.args.lmax: lmax_option = ' -lmax ' + app.args.lmax convergence_change = 0.01 * app.args.convergence for iteration in range(0, app.args.max_iters): prefix = 'iter' + str(iteration) + '_' # How to initialise response function? # old dwi2response command used mean & standard deviation of DWI data; however # this may force the output FODs to lmax=2 at the first iteration # Chantal used a tensor with low FA, but it'd be preferable to get the scaling right # Other option is to do as before, but get the ratio between l=0 and l=2, and # generate l=4,6,... using that amplitude ratio if iteration == 0: RF_in_path = 'init_RF.txt' mask_in_path = 'mask.mif' # TODO This can be changed once #71 is implemented (mrstats statistics across volumes) volume_means = [float(x) for x in image.statistic('dwi.mif', 'mean', 'mask.mif').split()] mean = sum(volume_means) / float(len(volume_means)) volume_stds = [float(x) for x in image.statistic('dwi.mif', 'std', 'mask.mif').split()] std = sum(volume_stds) / float(len(volume_stds)) # Scale these to reflect the fact that we're moving to the SH basis mean *= math.sqrt(4.0 * math.pi) std *= math.sqrt(4.0 * math.pi) # Now produce the initial response function # Let's only do it to lmax 4 init_RF = [ str(mean), str(-0.5*std), str(0.25*std*std/mean) ] with open('init_RF.txt', 'w') as f: f.write(' '.join(init_RF)) else: RF_in_path = 'iter' + str(iteration-1) + '_RF.txt' mask_in_path = 'iter' + str(iteration-1) + '_SF.mif' # Run CSD run.command('dwi2fod csd dwi.mif ' + RF_in_path + ' ' + prefix + 'FOD.mif -mask ' + mask_in_path) # Get amplitudes of two largest peaks, and directions of largest run.command('fod2fixel ' + prefix + 'FOD.mif ' + prefix + 'fixel -peak peaks.mif -mask ' + mask_in_path + ' -fmls_no_thresholds') file.delTempFile(prefix + 'FOD.mif') run.command('fixel2voxel ' + prefix + 'fixel/peaks.mif split_data ' + prefix + 'amps.mif') run.command('mrconvert ' + prefix + 'amps.mif ' + prefix + 'first_peaks.mif -coord 3 0 -axes 0,1,2') run.command('mrconvert ' + prefix + 'amps.mif ' + prefix + 'second_peaks.mif -coord 3 1 -axes 0,1,2') file.delTempFile(prefix + 'amps.mif') run.command('fixel2voxel ' + prefix + 'fixel/directions.mif split_dir ' + prefix + 'all_dirs.mif') file.delTempFolder(prefix + 'fixel') run.command('mrconvert ' + prefix + 'all_dirs.mif ' + prefix + 'first_dir.mif -coord 3 0:2') file.delTempFile(prefix + 'all_dirs.mif') # Revise single-fibre voxel selection based on ratio of tallest to second-tallest peak run.command('mrcalc ' + prefix + 'second_peaks.mif ' + prefix + 'first_peaks.mif -div ' + prefix + 'peak_ratio.mif') file.delTempFile(prefix + 'first_peaks.mif') file.delTempFile(prefix + 'second_peaks.mif') run.command('mrcalc ' + prefix + 'peak_ratio.mif ' + str(app.args.peak_ratio) + ' -lt ' + mask_in_path + ' -mult ' + prefix + 'SF.mif -datatype bit') file.delTempFile(prefix + 'peak_ratio.mif') # Make sure image isn't empty SF_voxel_count = int(image.statistic(prefix + 'SF.mif', 'count', prefix + 'SF.mif')) if not SF_voxel_count: app.error('Aborting: All voxels have been excluded from single-fibre selection') # Generate a new response function run.command('amp2response dwi.mif ' + prefix + 'SF.mif ' + prefix + 'first_dir.mif ' + prefix + 'RF.txt' + lmax_option) file.delTempFile(prefix + 'first_dir.mif') # Detect convergence # Look for a change > some percentage - don't bother looking at the masks if iteration > 0: with open(RF_in_path, 'r') as old_RF_file: old_RF = [ float(x) for x in old_RF_file.read().split() ] with open(prefix + 'RF.txt', 'r') as new_RF_file: new_RF = [ float(x) for x in new_RF_file.read().split() ] reiterate = False for index in range(0, len(old_RF)): mean = 0.5 * (old_RF[index] + new_RF[index]) diff = math.fabs(0.5 * (old_RF[index] - new_RF[index])) ratio = diff / mean if ratio > convergence_change: reiterate = True if not reiterate: app.console('Exiting at iteration ' + str(iteration) + ' with ' + str(SF_voxel_count) + ' SF voxels due to unchanged response function coefficients') run.function(shutil.copyfile, prefix + 'RF.txt', 'response.txt') run.function(shutil.copyfile, prefix + 'SF.mif', 'voxels.mif') break file.delTempFile(RF_in_path) file.delTempFile(mask_in_path) # Go to the next iteration # If we've terminated due to hitting the iteration limiter, we still need to copy the output file(s) to the correct location if not os.path.exists('response.txt'): app.console('Exiting after maximum ' + str(app.args.max_iters-1) + ' iterations with ' + str(SF_voxel_count) + ' SF voxels') run.function(shutil.copyfile, 'iter' + str(app.args.max_iters-1) + '_RF.txt', 'response.txt') run.function(shutil.copyfile, 'iter' + str(app.args.max_iters-1) + '_SF.mif', 'voxels.mif') run.function(shutil.copyfile, 'response.txt', path.fromUser(app.args.output, False))
def execute(): import math, os, shutil from mrtrix3 import app, image, path, run # Ideally want to use the oversampling-based regridding of the 5TT image from the SIFT model, not mrtransform # May need to commit 5ttregrid... # Verify input 5tt image run.command('5ttcheck 5tt.mif', False) # Get shell information shells = [ int(round(float(x))) for x in image.headerField('dwi.mif', 'shells').split() ] if len(shells) < 3: app.warn( 'Less than three b-value shells; response functions will not be applicable in resolving three tissues using MSMT-CSD algorithm' ) # Get lmax information (if provided) wm_lmax = [] if app.args.lmax: wm_lmax = [int(x.strip()) for x in app.args.lmax.split(',')] if not len(wm_lmax) == len(shells): app.error('Number of manually-defined lmax\'s (' + str(len(wm_lmax)) + ') does not match number of b-value shells (' + str(len(shells)) + ')') for l in wm_lmax: if l % 2: app.error('Values for lmax must be even') if l < 0: app.error('Values for lmax must be non-negative') run.command( 'dwi2tensor dwi.mif - -mask mask.mif | tensor2metric - -fa fa.mif -vector vector.mif' ) if not os.path.exists('dirs.mif'): run.function(shutil.copy, 'vector.mif', 'dirs.mif') run.command( 'mrtransform 5tt.mif 5tt_regrid.mif -template fa.mif -interp linear') # Basic tissue masks run.command( 'mrconvert 5tt_regrid.mif - -coord 3 2 -axes 0,1,2 | mrcalc - ' + str(app.args.pvf) + ' -gt mask.mif -mult wm_mask.mif') run.command( 'mrconvert 5tt_regrid.mif - -coord 3 0 -axes 0,1,2 | mrcalc - ' + str(app.args.pvf) + ' -gt fa.mif ' + str(app.args.fa) + ' -lt -mult mask.mif -mult gm_mask.mif') run.command( 'mrconvert 5tt_regrid.mif - -coord 3 3 -axes 0,1,2 | mrcalc - ' + str(app.args.pvf) + ' -gt fa.mif ' + str(app.args.fa) + ' -lt -mult mask.mif -mult csf_mask.mif') # Revise WM mask to only include single-fibre voxels app.console( 'Calling dwi2response recursively to select WM single-fibre voxels using \'' + app.args.wm_algo + '\' algorithm') recursive_cleanup_option = '' if not app._cleanup: recursive_cleanup_option = ' -nocleanup' run.command( 'dwi2response ' + app.args.wm_algo + ' dwi.mif wm_ss_response.txt -mask wm_mask.mif -voxels wm_sf_mask.mif -tempdir ' + app._tempDir + recursive_cleanup_option) # Check for empty masks wm_voxels = int( image.statistic('wm_sf_mask.mif', 'count', 'wm_sf_mask.mif')) gm_voxels = int(image.statistic('gm_mask.mif', 'count', 'gm_mask.mif')) csf_voxels = int(image.statistic('csf_mask.mif', 'count', 'csf_mask.mif')) empty_masks = [] if not wm_voxels: empty_masks.append('WM') if not gm_voxels: empty_masks.append('GM') if not csf_voxels: empty_masks.append('CSF') if empty_masks: message = ','.join(empty_masks) message += ' tissue mask' if len(empty_masks) > 1: message += 's' message += ' empty; cannot estimate response function' if len(empty_masks) > 1: message += 's' app.error(message) # For each of the three tissues, generate a multi-shell response bvalues_option = ' -shell ' + ','.join(map(str, shells)) sfwm_lmax_option = '' if wm_lmax: sfwm_lmax_option = ' -lmax ' + ','.join(map(str, wm_lmax)) run.command('amp2response dwi.mif wm_sf_mask.mif dirs.mif wm.txt' + bvalues_option + sfwm_lmax_option) run.command('amp2response dwi.mif gm_mask.mif dirs.mif gm.txt' + bvalues_option + ' -isotropic') run.command('amp2response dwi.mif csf_mask.mif dirs.mif csf.txt' + bvalues_option + ' -isotropic') run.function(shutil.copyfile, 'wm.txt', path.fromUser(app.args.out_wm, False)) run.function(shutil.copyfile, 'gm.txt', path.fromUser(app.args.out_gm, False)) run.function(shutil.copyfile, 'csf.txt', path.fromUser(app.args.out_csf, False)) # Generate output 4D binary image with voxel selections; RGB as in MSMT-CSD paper run.command( 'mrcat csf_mask.mif gm_mask.mif wm_sf_mask.mif voxels.mif -axis 3')
def execute(): import math, os, shutil from mrtrix3 import app, image, path, run # Get b-values and number of volumes per b-value. bvalues = [ int(round(float(x))) for x in image.headerField('dwi.mif', 'shells').split() ] bvolumes = [ int(x) for x in image.headerField('dwi.mif', 'shellcounts').split() ] app.console(str(len(bvalues)) + ' unique b-value(s) detected: ' + ','.join(map(str,bvalues)) + ' with ' + ','.join(map(str,bvolumes)) + ' volumes.') if len(bvalues) < 2: app.error('Need at least 2 unique b-values (including b=0).') # Get lmax information (if provided). sfwm_lmax = [ ] if app.args.lmax: sfwm_lmax = [ int(x.strip()) for x in app.args.lmax.split(',') ] if not len(sfwm_lmax) == len(bvalues): app.error('Number of lmax\'s (' + str(len(sfwm_lmax)) + ', as supplied to the -lmax option: ' + ','.join(map(str,sfwm_lmax)) + ') does not match number of unique b-values.') for l in sfwm_lmax: if l%2: app.error('Values supplied to the -lmax option must be even.') if l<0: app.error('Values supplied to the -lmax option must be non-negative.') # Erode (brain) mask. if app.args.erode > 0: run.command('maskfilter mask.mif erode eroded_mask.mif -npass ' + str(app.args.erode)) else: run.command('mrconvert mask.mif eroded_mask.mif -datatype bit') # Get volumes, compute mean signal and SDM per b-value; compute overall SDM; get rid of erroneous values. totvolumes = 0 fullsdmcmd = 'mrcalc' errcmd = 'mrcalc' zeropath = 'mean_b' + str(bvalues[0]) + '.mif' for i, b in enumerate(bvalues): meanpath = 'mean_b' + str(b) + '.mif' run.command('dwiextract dwi.mif -shell ' + str(b) + ' - | mrmath - mean ' + meanpath + ' -axis 3') errpath = 'err_b' + str(b) + '.mif' run.command('mrcalc ' + meanpath + ' -finite ' + meanpath + ' 0 -if 0 -le ' + errpath + ' -datatype bit') errcmd += ' ' + errpath if i>0: errcmd += ' -add' sdmpath = 'sdm_b' + str(b) + '.mif' run.command('mrcalc ' + zeropath + ' ' + meanpath + ' -divide -log ' + sdmpath) totvolumes += bvolumes[i] fullsdmcmd += ' ' + sdmpath + ' ' + str(bvolumes[i]) + ' -mult' if i>1: fullsdmcmd += ' -add' fullsdmcmd += ' ' + str(totvolumes) + ' -divide full_sdm.mif' run.command(fullsdmcmd) run.command('mrcalc full_sdm.mif -finite full_sdm.mif 0 -if 0 -le err_sdm.mif -datatype bit') errcmd += ' err_sdm.mif -add 0 eroded_mask.mif -if safe_mask.mif -datatype bit' run.command(errcmd) run.command('mrcalc safe_mask.mif full_sdm.mif 0 -if 10 -min safe_sdm.mif') # Compute FA and principal eigenvectors; crude WM versus GM-CSF separation based on FA. run.command('dwi2tensor dwi.mif - -mask safe_mask.mif | tensor2metric - -fa safe_fa.mif -vector safe_vecs.mif -modulate none -mask safe_mask.mif') run.command('mrcalc safe_mask.mif safe_fa.mif 0 -if ' + str(app.args.fa) + ' -gt crude_wm.mif -datatype bit') run.command('mrcalc crude_wm.mif 0 safe_mask.mif -if _crudenonwm.mif -datatype bit') # Crude GM versus CSF separation based on SDM. crudenonwmmedian = image.statistic('safe_sdm.mif', 'median', '_crudenonwm.mif') run.command('mrcalc _crudenonwm.mif safe_sdm.mif ' + str(crudenonwmmedian) + ' -subtract 0 -if - | mrthreshold - - -mask _crudenonwm.mif | mrcalc _crudenonwm.mif - 0 -if crude_csf.mif -datatype bit') run.command('mrcalc crude_csf.mif 0 _crudenonwm.mif -if crude_gm.mif -datatype bit') # Refine WM: remove high SDM outliers. crudewmmedian = image.statistic('safe_sdm.mif', 'median', 'crude_wm.mif') run.command('mrcalc crude_wm.mif safe_sdm.mif 0 -if ' + str(crudewmmedian) + ' -gt _crudewmhigh.mif -datatype bit') run.command('mrcalc _crudewmhigh.mif 0 crude_wm.mif -if _crudewmlow.mif -datatype bit') crudewmQ1 = float(image.statistic('safe_sdm.mif', 'median', '_crudewmlow.mif')) crudewmQ3 = float(image.statistic('safe_sdm.mif', 'median', '_crudewmhigh.mif')) crudewmoutlthresh = crudewmQ3 + (crudewmQ3 - crudewmQ1) run.command('mrcalc crude_wm.mif safe_sdm.mif 0 -if ' + str(crudewmoutlthresh) + ' -gt _crudewmoutliers.mif -datatype bit') run.command('mrcalc _crudewmoutliers.mif 0 crude_wm.mif -if refined_wm.mif -datatype bit') # Refine GM: separate safer GM from partial volumed voxels. crudegmmedian = image.statistic('safe_sdm.mif', 'median', 'crude_gm.mif') run.command('mrcalc crude_gm.mif safe_sdm.mif 0 -if ' + str(crudegmmedian) + ' -gt _crudegmhigh.mif -datatype bit') run.command('mrcalc _crudegmhigh.mif 0 crude_gm.mif -if _crudegmlow.mif -datatype bit') run.command('mrcalc _crudegmhigh.mif safe_sdm.mif ' + str(crudegmmedian) + ' -subtract 0 -if - | mrthreshold - - -mask _crudegmhigh.mif -invert | mrcalc _crudegmhigh.mif - 0 -if _crudegmhighselect.mif -datatype bit') run.command('mrcalc _crudegmlow.mif safe_sdm.mif ' + str(crudegmmedian) + ' -subtract -neg 0 -if - | mrthreshold - - -mask _crudegmlow.mif -invert | mrcalc _crudegmlow.mif - 0 -if _crudegmlowselect.mif -datatype bit') run.command('mrcalc _crudegmhighselect.mif 1 _crudegmlowselect.mif -if refined_gm.mif -datatype bit') # Refine CSF: recover lost CSF from crude WM SDM outliers, separate safer CSF from partial volumed voxels. crudecsfmin = image.statistic('safe_sdm.mif', 'min', 'crude_csf.mif') run.command('mrcalc _crudewmoutliers.mif safe_sdm.mif 0 -if ' + str(crudecsfmin) + ' -gt 1 crude_csf.mif -if _crudecsfextra.mif -datatype bit') run.command('mrcalc _crudecsfextra.mif safe_sdm.mif ' + str(crudecsfmin) + ' -subtract 0 -if - | mrthreshold - - -mask _crudecsfextra.mif | mrcalc _crudecsfextra.mif - 0 -if refined_csf.mif -datatype bit') # Get final voxels for single-fibre WM response function estimation from WM using 'tournier' algorithm. refwmcount = float(image.statistic('refined_wm.mif', 'count', 'refined_wm.mif')) voxsfwmcount = int(round(refwmcount * app.args.sfwm / 100.0)) app.console('Running \'tournier\' algorithm to select ' + str(voxsfwmcount) + ' single-fibre WM voxels.') cleanopt = '' if not app._cleanup: cleanopt = ' -nocleanup' run.command('dwi2response tournier dwi.mif _respsfwmss.txt -sf_voxels ' + str(voxsfwmcount) + ' -iter_voxels ' + str(voxsfwmcount * 10) + ' -mask refined_wm.mif -voxels voxels_sfwm.mif -tempdir ' + app._tempDir + cleanopt) # Get final voxels for GM response function estimation from GM. refgmmedian = image.statistic('safe_sdm.mif', 'median', 'refined_gm.mif') run.command('mrcalc refined_gm.mif safe_sdm.mif 0 -if ' + str(refgmmedian) + ' -gt _refinedgmhigh.mif -datatype bit') run.command('mrcalc _refinedgmhigh.mif 0 refined_gm.mif -if _refinedgmlow.mif -datatype bit') refgmhighcount = float(image.statistic('_refinedgmhigh.mif', 'count', '_refinedgmhigh.mif')) refgmlowcount = float(image.statistic('_refinedgmlow.mif', 'count', '_refinedgmlow.mif')) voxgmhighcount = int(round(refgmhighcount * app.args.gm / 100.0)) voxgmlowcount = int(round(refgmlowcount * app.args.gm / 100.0)) run.command('mrcalc _refinedgmhigh.mif safe_sdm.mif 0 -if - | mrthreshold - - -bottom ' + str(voxgmhighcount) + ' -ignorezero | mrcalc _refinedgmhigh.mif - 0 -if _refinedgmhighselect.mif -datatype bit') run.command('mrcalc _refinedgmlow.mif safe_sdm.mif 0 -if - | mrthreshold - - -top ' + str(voxgmlowcount) + ' -ignorezero | mrcalc _refinedgmlow.mif - 0 -if _refinedgmlowselect.mif -datatype bit') run.command('mrcalc _refinedgmhighselect.mif 1 _refinedgmlowselect.mif -if voxels_gm.mif -datatype bit') # Get final voxels for CSF response function estimation from CSF. refcsfcount = float(image.statistic('refined_csf.mif', 'count', 'refined_csf.mif')) voxcsfcount = int(round(refcsfcount * app.args.csf / 100.0)) run.command('mrcalc refined_csf.mif safe_sdm.mif 0 -if - | mrthreshold - - -top ' + str(voxcsfcount) + ' -ignorezero | mrcalc refined_csf.mif - 0 -if voxels_csf.mif -datatype bit') # Show summary of voxels counts. textarrow = ' --> ' app.console('Summary of voxel counts:') app.console('Mask: ' + str(int(image.statistic('mask.mif', 'count', 'mask.mif'))) + textarrow + str(int(image.statistic('eroded_mask.mif', 'count', 'eroded_mask.mif'))) + textarrow + str(int(image.statistic('safe_mask.mif', 'count', 'safe_mask.mif')))) app.console('WM: ' + str(int(image.statistic('crude_wm.mif', 'count', 'crude_wm.mif'))) + textarrow + str(int(image.statistic('refined_wm.mif', 'count', 'refined_wm.mif'))) + textarrow + str(int(image.statistic('voxels_sfwm.mif', 'count', 'voxels_sfwm.mif'))) + ' (SF)') app.console('GM: ' + str(int(image.statistic('crude_gm.mif', 'count', 'crude_gm.mif'))) + textarrow + str(int(image.statistic('refined_gm.mif', 'count', 'refined_gm.mif'))) + textarrow + str(int(image.statistic('voxels_gm.mif', 'count', 'voxels_gm.mif')))) app.console('CSF: ' + str(int(image.statistic('crude_csf.mif', 'count', 'crude_csf.mif'))) + textarrow + str(int(image.statistic('refined_csf.mif', 'count', 'refined_csf.mif'))) + textarrow + str(int(image.statistic('voxels_csf.mif', 'count', 'voxels_csf.mif')))) # Generate single-fibre WM, GM and CSF responses bvalues_option = ' -shell ' + ','.join(map(str,bvalues)) sfwm_lmax_option = '' if sfwm_lmax: sfwm_lmax_option = ' -lmax ' + ','.join(map(str,sfwm_lmax)) run.command('amp2response dwi.mif voxels_sfwm.mif safe_vecs.mif response_sfwm.txt' + bvalues_option + sfwm_lmax_option) run.command('amp2response dwi.mif voxels_gm.mif safe_vecs.mif response_gm.txt' + bvalues_option + ' -isotropic') run.command('amp2response dwi.mif voxels_csf.mif safe_vecs.mif response_csf.txt' + bvalues_option + ' -isotropic') run.function(shutil.copyfile, 'response_sfwm.txt', path.fromUser(app.args.out_sfwm, False)) run.function(shutil.copyfile, 'response_gm.txt', path.fromUser(app.args.out_gm, False)) run.function(shutil.copyfile, 'response_csf.txt', path.fromUser(app.args.out_csf, False)) # Generate 4D binary images with voxel selections at major stages in algorithm (RGB as in MSMT-CSD paper). run.command('mrcat crude_csf.mif crude_gm.mif crude_wm.mif crude.mif -axis 3') run.command('mrcat refined_csf.mif refined_gm.mif refined_wm.mif refined.mif -axis 3') run.command('mrcat voxels_csf.mif voxels_gm.mif voxels_sfwm.mif voxels.mif -axis 3')
eddyShell = '--slm=linear' else: eddyShell = '--data_is_shelled' if app.args.rpe_none: call('dwipreproc -eddyqc_all ' + path2qc + ' -eddy_options "--repol ' + eddyShell + '"' + ' -rpe_none -pe_dir ' + app.args.pe_dir + ' working.mif dwiec.mif -nthreads 16 -force', shell=True) elif app.args.rpe_pair: call( 'dwiextract -bzero dwi.mif - | mrconvert -force -coord 3 0 - b0pe.mif', shell=True) rpe_size = [ int(s) for s in image.Header(path.fromUser(app.args.rpe_pair, True)).size() ] if len(rpe_size) == 4: call('mrconvert -force -coord 3 0 ' + path.fromUser(app.args.rpe_pair, True) + ' b0rpe.mif', shell=True) else: call('mrconvert -force ' + path.fromUser(app.args.rpe_pair, True) + ' b0rpe.mif', shell=True) call('mrcat -axis 3 b0pe.mif b0rpe.mif rpepair.mif', shell=True) call('dwipreproc -eddyqc_all ' + path2qc + ' -eddy_options "--repol ' + eddyShell + '"' + ' -rpe_pair -se_epi rpepair.mif -pe_dir ' + app.args.pe_dir + ' working.mif dwiec.mif -nthreads 16 -force', shell=True) elif app.args.rpe_all:
def execute(): #pylint: disable=unused-variable import os, shutil from mrtrix3 import app, file, image, path, run #pylint: disable=redefined-builtin lmax_option = '' if app.args.lmax: lmax_option = ' -lmax ' + app.args.lmax if app.args.max_iters < 2: app.error('Number of iterations must be at least 2') for iteration in range(0, app.args.max_iters): prefix = 'iter' + str(iteration) + '_' if iteration == 0: RF_in_path = 'init_RF.txt' mask_in_path = 'mask.mif' init_RF = '1 -1 1' with open(RF_in_path, 'w') as f: f.write(init_RF) iter_lmax_option = ' -lmax 4' else: RF_in_path = 'iter' + str(iteration - 1) + '_RF.txt' mask_in_path = 'iter' + str(iteration - 1) + '_SF_dilated.mif' iter_lmax_option = lmax_option # Run CSD run.command('dwi2fod csd dwi.mif ' + RF_in_path + ' ' + prefix + 'FOD.mif -mask ' + mask_in_path + iter_lmax_option) # Get amplitudes of two largest peaks, and direction of largest run.command('fod2fixel ' + prefix + 'FOD.mif ' + prefix + 'fixel -peak peaks.mif -mask ' + mask_in_path + ' -fmls_no_thresholds') file.delTemporary(prefix + 'FOD.mif') if iteration: file.delTemporary(mask_in_path) run.command('fixel2voxel ' + prefix + 'fixel/peaks.mif split_data ' + prefix + 'amps.mif -number 2') run.command('mrconvert ' + prefix + 'amps.mif ' + prefix + 'first_peaks.mif -coord 3 0 -axes 0,1,2') run.command('mrconvert ' + prefix + 'amps.mif ' + prefix + 'second_peaks.mif -coord 3 1 -axes 0,1,2') file.delTemporary(prefix + 'amps.mif') run.command('fixel2voxel ' + prefix + 'fixel/directions.mif split_dir ' + prefix + 'all_dirs.mif -number 1') file.delTemporary(prefix + 'fixel') run.command('mrconvert ' + prefix + 'all_dirs.mif ' + prefix + 'first_dir.mif -coord 3 0:2') file.delTemporary(prefix + 'all_dirs.mif') # Calculate the 'cost function' Donald derived for selecting single-fibre voxels # https://github.com/MRtrix3/mrtrix3/pull/426 # sqrt(|peak1|) * (1 - |peak2| / |peak1|)^2 run.command('mrcalc ' + prefix + 'first_peaks.mif -sqrt 1 ' + prefix + 'second_peaks.mif ' + prefix + 'first_peaks.mif -div -sub 2 -pow -mult ' + prefix + 'CF.mif') file.delTemporary(prefix + 'first_peaks.mif') file.delTemporary(prefix + 'second_peaks.mif') # Select the top-ranked voxels run.command('mrthreshold ' + prefix + 'CF.mif -top ' + str(app.args.sf_voxels) + ' ' + prefix + 'SF.mif') # Generate a new response function based on this selection run.command('amp2response dwi.mif ' + prefix + 'SF.mif ' + prefix + 'first_dir.mif ' + prefix + 'RF.txt' + iter_lmax_option) file.delTemporary(prefix + 'first_dir.mif') # Should we terminate? if iteration > 0: run.command('mrcalc ' + prefix + 'SF.mif iter' + str(iteration - 1) + '_SF.mif -sub ' + prefix + 'SF_diff.mif') file.delTemporary('iter' + str(iteration - 1) + '_SF.mif') max_diff = image.statistic(prefix + 'SF_diff.mif', 'max') file.delTemporary(prefix + 'SF_diff.mif') if int(max_diff) == 0: app.console( 'Convergence of SF voxel selection detected at iteration ' + str(iteration)) file.delTemporary(prefix + 'CF.mif') run.function(shutil.copyfile, prefix + 'RF.txt', 'response.txt') run.function(shutil.move, prefix + 'SF.mif', 'voxels.mif') break # Select a greater number of top single-fibre voxels, and dilate (within bounds of initial mask); # these are the voxels that will be re-tested in the next iteration run.command('mrthreshold ' + prefix + 'CF.mif -top ' + str(app.args.iter_voxels) + ' - | maskfilter - dilate - -npass ' + str(app.args.dilate) + ' | mrcalc mask.mif - -mult ' + prefix + 'SF_dilated.mif') file.delTemporary(prefix + 'CF.mif') # Commence the next iteration # If terminating due to running out of iterations, still need to put the results in the appropriate location if not os.path.exists('response.txt'): app.console('Exiting after maximum ' + str(app.args.max_iters) + ' iterations') run.function(shutil.copyfile, 'iter' + str(app.args.max_iters - 1) + '_RF.txt', 'response.txt') run.function(shutil.move, 'iter' + str(app.args.max_iters - 1) + '_SF.mif', 'voxels.mif') run.function(shutil.copyfile, 'response.txt', path.fromUser(app.args.output, False))
def execute(): import math, os, shutil from mrtrix3 import app, image, path, run # Get b-values and number of volumes per b-value. bvalues = [ int(round(float(x))) for x in image.headerField('dwi.mif', 'shells').split() ] bvolumes = [ int(x) for x in image.headerField('dwi.mif', 'shellcounts').split() ] app.console( str(len(bvalues)) + ' unique b-value(s) detected: ' + ','.join(map(str, bvalues)) + ' with ' + ','.join(map(str, bvolumes)) + ' volumes.') if len(bvalues) < 2: app.error('Need at least 2 unique b-values (including b=0).') # Get lmax information (if provided). sfwm_lmax = [] if app.args.lmax: sfwm_lmax = [int(x.strip()) for x in app.args.lmax.split(',')] if not len(sfwm_lmax) == len(bvalues): app.error('Number of lmax\'s (' + str(len(sfwm_lmax)) + ', as supplied to the -lmax option: ' + ','.join(map(str, sfwm_lmax)) + ') does not match number of unique b-values.') for l in sfwm_lmax: if l % 2: app.error('Values supplied to the -lmax option must be even.') if l < 0: app.error( 'Values supplied to the -lmax option must be non-negative.' ) # Erode (brain) mask. if app.args.erode > 0: run.command('maskfilter mask.mif erode eroded_mask.mif -npass ' + str(app.args.erode)) else: run.command('mrconvert mask.mif eroded_mask.mif -datatype bit') # Get volumes, compute mean signal and SDM per b-value; compute overall SDM; get rid of erroneous values. totvolumes = 0 fullsdmcmd = 'mrcalc' errcmd = 'mrcalc' zeropath = 'mean_b' + str(bvalues[0]) + '.mif' for i, b in enumerate(bvalues): meanpath = 'mean_b' + str(b) + '.mif' run.command('dwiextract dwi.mif -shell ' + str(b) + ' - | mrmath - mean ' + meanpath + ' -axis 3') errpath = 'err_b' + str(b) + '.mif' run.command('mrcalc ' + meanpath + ' -finite ' + meanpath + ' 0 -if 0 -le ' + errpath + ' -datatype bit') errcmd += ' ' + errpath if i > 0: errcmd += ' -add' sdmpath = 'sdm_b' + str(b) + '.mif' run.command('mrcalc ' + zeropath + ' ' + meanpath + ' -divide -log ' + sdmpath) totvolumes += bvolumes[i] fullsdmcmd += ' ' + sdmpath + ' ' + str(bvolumes[i]) + ' -mult' if i > 1: fullsdmcmd += ' -add' fullsdmcmd += ' ' + str(totvolumes) + ' -divide full_sdm.mif' run.command(fullsdmcmd) run.command( 'mrcalc full_sdm.mif -finite full_sdm.mif 0 -if 0 -le err_sdm.mif -datatype bit' ) errcmd += ' err_sdm.mif -add 0 eroded_mask.mif -if safe_mask.mif -datatype bit' run.command(errcmd) run.command('mrcalc safe_mask.mif full_sdm.mif 0 -if 10 -min safe_sdm.mif') # Compute FA and principal eigenvectors; crude WM versus GM-CSF separation based on FA. run.command( 'dwi2tensor dwi.mif - -mask safe_mask.mif | tensor2metric - -fa safe_fa.mif -vector safe_vecs.mif -modulate none -mask safe_mask.mif' ) run.command('mrcalc safe_mask.mif safe_fa.mif 0 -if ' + str(app.args.fa) + ' -gt crude_wm.mif -datatype bit') run.command( 'mrcalc crude_wm.mif 0 safe_mask.mif -if _crudenonwm.mif -datatype bit' ) # Crude GM versus CSF separation based on SDM. crudenonwmmedian = image.statistic('safe_sdm.mif', 'median', '_crudenonwm.mif') run.command( 'mrcalc _crudenonwm.mif safe_sdm.mif ' + str(crudenonwmmedian) + ' -subtract 0 -if - | mrthreshold - - -mask _crudenonwm.mif | mrcalc _crudenonwm.mif - 0 -if crude_csf.mif -datatype bit' ) run.command( 'mrcalc crude_csf.mif 0 _crudenonwm.mif -if crude_gm.mif -datatype bit' ) # Refine WM: remove high SDM outliers. crudewmmedian = image.statistic('safe_sdm.mif', 'median', 'crude_wm.mif') run.command('mrcalc crude_wm.mif safe_sdm.mif 0 -if ' + str(crudewmmedian) + ' -gt _crudewmhigh.mif -datatype bit') run.command( 'mrcalc _crudewmhigh.mif 0 crude_wm.mif -if _crudewmlow.mif -datatype bit' ) crudewmQ1 = float( image.statistic('safe_sdm.mif', 'median', '_crudewmlow.mif')) crudewmQ3 = float( image.statistic('safe_sdm.mif', 'median', '_crudewmhigh.mif')) crudewmoutlthresh = crudewmQ3 + (crudewmQ3 - crudewmQ1) run.command('mrcalc crude_wm.mif safe_sdm.mif 0 -if ' + str(crudewmoutlthresh) + ' -gt _crudewmoutliers.mif -datatype bit') run.command( 'mrcalc _crudewmoutliers.mif 0 crude_wm.mif -if refined_wm.mif -datatype bit' ) # Refine GM: separate safer GM from partial volumed voxels. crudegmmedian = image.statistic('safe_sdm.mif', 'median', 'crude_gm.mif') run.command('mrcalc crude_gm.mif safe_sdm.mif 0 -if ' + str(crudegmmedian) + ' -gt _crudegmhigh.mif -datatype bit') run.command( 'mrcalc _crudegmhigh.mif 0 crude_gm.mif -if _crudegmlow.mif -datatype bit' ) run.command( 'mrcalc _crudegmhigh.mif safe_sdm.mif ' + str(crudegmmedian) + ' -subtract 0 -if - | mrthreshold - - -mask _crudegmhigh.mif -invert | mrcalc _crudegmhigh.mif - 0 -if _crudegmhighselect.mif -datatype bit' ) run.command( 'mrcalc _crudegmlow.mif safe_sdm.mif ' + str(crudegmmedian) + ' -subtract -neg 0 -if - | mrthreshold - - -mask _crudegmlow.mif -invert | mrcalc _crudegmlow.mif - 0 -if _crudegmlowselect.mif -datatype bit' ) run.command( 'mrcalc _crudegmhighselect.mif 1 _crudegmlowselect.mif -if refined_gm.mif -datatype bit' ) # Refine CSF: recover lost CSF from crude WM SDM outliers, separate safer CSF from partial volumed voxels. crudecsfmin = image.statistic('safe_sdm.mif', 'min', 'crude_csf.mif') run.command('mrcalc _crudewmoutliers.mif safe_sdm.mif 0 -if ' + str(crudecsfmin) + ' -gt 1 crude_csf.mif -if _crudecsfextra.mif -datatype bit') run.command( 'mrcalc _crudecsfextra.mif safe_sdm.mif ' + str(crudecsfmin) + ' -subtract 0 -if - | mrthreshold - - -mask _crudecsfextra.mif | mrcalc _crudecsfextra.mif - 0 -if refined_csf.mif -datatype bit' ) # Get final voxels for single-fibre WM response function estimation from WM using 'tournier' algorithm. refwmcount = float( image.statistic('refined_wm.mif', 'count', 'refined_wm.mif')) voxsfwmcount = int(round(refwmcount * app.args.sfwm / 100.0)) app.console('Running \'tournier\' algorithm to select ' + str(voxsfwmcount) + ' single-fibre WM voxels.') cleanopt = '' if not app._cleanup: cleanopt = ' -nocleanup' run.command('dwi2response tournier dwi.mif _respsfwmss.txt -sf_voxels ' + str(voxsfwmcount) + ' -iter_voxels ' + str(voxsfwmcount * 10) + ' -mask refined_wm.mif -voxels voxels_sfwm.mif -tempdir ' + app._tempDir + cleanopt) # Get final voxels for GM response function estimation from GM. refgmmedian = image.statistic('safe_sdm.mif', 'median', 'refined_gm.mif') run.command('mrcalc refined_gm.mif safe_sdm.mif 0 -if ' + str(refgmmedian) + ' -gt _refinedgmhigh.mif -datatype bit') run.command( 'mrcalc _refinedgmhigh.mif 0 refined_gm.mif -if _refinedgmlow.mif -datatype bit' ) refgmhighcount = float( image.statistic('_refinedgmhigh.mif', 'count', '_refinedgmhigh.mif')) refgmlowcount = float( image.statistic('_refinedgmlow.mif', 'count', '_refinedgmlow.mif')) voxgmhighcount = int(round(refgmhighcount * app.args.gm / 100.0)) voxgmlowcount = int(round(refgmlowcount * app.args.gm / 100.0)) run.command( 'mrcalc _refinedgmhigh.mif safe_sdm.mif 0 -if - | mrthreshold - - -bottom ' + str(voxgmhighcount) + ' -ignorezero | mrcalc _refinedgmhigh.mif - 0 -if _refinedgmhighselect.mif -datatype bit' ) run.command( 'mrcalc _refinedgmlow.mif safe_sdm.mif 0 -if - | mrthreshold - - -top ' + str(voxgmlowcount) + ' -ignorezero | mrcalc _refinedgmlow.mif - 0 -if _refinedgmlowselect.mif -datatype bit' ) run.command( 'mrcalc _refinedgmhighselect.mif 1 _refinedgmlowselect.mif -if voxels_gm.mif -datatype bit' ) # Get final voxels for CSF response function estimation from CSF. refcsfcount = float( image.statistic('refined_csf.mif', 'count', 'refined_csf.mif')) voxcsfcount = int(round(refcsfcount * app.args.csf / 100.0)) run.command( 'mrcalc refined_csf.mif safe_sdm.mif 0 -if - | mrthreshold - - -top ' + str(voxcsfcount) + ' -ignorezero | mrcalc refined_csf.mif - 0 -if voxels_csf.mif -datatype bit' ) # Show summary of voxels counts. textarrow = ' --> ' app.console('Summary of voxel counts:') app.console( 'Mask: ' + str(int(image.statistic('mask.mif', 'count', 'mask.mif'))) + textarrow + str(int(image.statistic('eroded_mask.mif', 'count', 'eroded_mask.mif'))) + textarrow + str(int(image.statistic('safe_mask.mif', 'count', 'safe_mask.mif')))) app.console( 'WM: ' + str(int(image.statistic('crude_wm.mif', 'count', 'crude_wm.mif'))) + textarrow + str(int(image.statistic('refined_wm.mif', 'count', 'refined_wm.mif'))) + textarrow + str(int(image.statistic('voxels_sfwm.mif', 'count', 'voxels_sfwm.mif'))) + ' (SF)') app.console( 'GM: ' + str(int(image.statistic('crude_gm.mif', 'count', 'crude_gm.mif'))) + textarrow + str(int(image.statistic('refined_gm.mif', 'count', 'refined_gm.mif'))) + textarrow + str(int(image.statistic('voxels_gm.mif', 'count', 'voxels_gm.mif')))) app.console( 'CSF: ' + str(int(image.statistic('crude_csf.mif', 'count', 'crude_csf.mif'))) + textarrow + str(int(image.statistic('refined_csf.mif', 'count', 'refined_csf.mif'))) + textarrow + str(int(image.statistic('voxels_csf.mif', 'count', 'voxels_csf.mif')))) # Generate single-fibre WM, GM and CSF responses bvalues_option = ' -shell ' + ','.join(map(str, bvalues)) sfwm_lmax_option = '' if sfwm_lmax: sfwm_lmax_option = ' -lmax ' + ','.join(map(str, sfwm_lmax)) run.command( 'amp2response dwi.mif voxels_sfwm.mif safe_vecs.mif response_sfwm.txt' + bvalues_option + sfwm_lmax_option) run.command( 'amp2response dwi.mif voxels_gm.mif safe_vecs.mif response_gm.txt' + bvalues_option + ' -isotropic') run.command( 'amp2response dwi.mif voxels_csf.mif safe_vecs.mif response_csf.txt' + bvalues_option + ' -isotropic') run.function(shutil.copyfile, 'response_sfwm.txt', path.fromUser(app.args.out_sfwm, False)) run.function(shutil.copyfile, 'response_gm.txt', path.fromUser(app.args.out_gm, False)) run.function(shutil.copyfile, 'response_csf.txt', path.fromUser(app.args.out_csf, False)) # Generate 4D binary images with voxel selections at major stages in algorithm (RGB as in MSMT-CSD paper). run.command( 'mrcat crude_csf.mif crude_gm.mif crude_wm.mif crude.mif -axis 3') run.command( 'mrcat refined_csf.mif refined_gm.mif refined_wm.mif refined.mif -axis 3' ) run.command( 'mrcat voxels_csf.mif voxels_gm.mif voxels_sfwm.mif voxels.mif -axis 3' )
def getInputs(): #pylint: disable=unused-variable import shutil from mrtrix3 import app, path, run run.command('mrconvert ' + path.fromUser(app.args.input, True) + ' ' + path.toTemp('input.mif', True)) if app.args.lut: run.function(shutil.copyfile, path.fromUser(app.args.lut, False), path.toTemp('LUT.txt', False))
def splitext_(path): for ext in ['.tar.gz', '.tar.bz2', '.nii.gz']: if path.endswith(ext): return path[:-len(ext)], path[-len(ext):] return os.path.splitext(path) designer_root = os.path.dirname(os.path.realpath(__file__)) DKI_root = os.path.abspath(os.path.join(designer_root, '..')) app.makeTempDir() fsl_suffix = fsl.suffix() Userpath = path.fromUser(app.args.input, True).rsplit('/', 1)[0] if os.path.exists(app.args.input): DWIlist = [i for i in app.args.input.split(',')] else: DWIlist = [Userpath + '/' + i for i in app.args.input.split(',')] DWIflist = [splitext_(i) for i in DWIlist] DWInlist = [i[0] for i in DWIflist] DWIext = [i[1] for i in DWIflist] miflist = [] idxlist = [] dwi_ind_size = [[0, 0, 0, 0]] if len(DWInlist) == 1: run.command('mrconvert -stride -1,2,3,4 -fslgrad ' + ''.join(DWInlist) + '.bvec ' + ''.join(DWInlist) + '.bval ' + ''.join(DWInlist) + ''.join(DWIext) + ' ' + path.toTemp('dwi.mif', True))