def reducetractnumber_all(trkpath, str_identifier1, str_identifier2, subject, ratio, verbose): trkoldpath = gettrkpath(trkpath, subject, str_identifier1 + "_pruned", verbose) trknewpath = gettrkpath(trkpath, subject, str_identifier2 + "_pruned", verbose) if trknewpath is None: trknewpath = (trkpath + '/' + subject + "" + str_identifier2 + '.trk') reducetractnumber(trkoldpath,trknewpath, getdata=False, ratio=ratio, return_affine=False, verbose=True) else: if verbose: txt = ("Subject "+ subject +" is already done") send_mail(txt, subject="reduce code in process") print(txt)
def extract_nii_info(path, verbose=None): if verbose: txt = "Extracting information from the nifti file located at " + path print(txt) send_mail(txt, subject="Begin data extraction") img = nib.load(path) data = img.get_data() vox_size = img.header.get_zooms()[:3] affine = img.affine header = img.header del (img) ref_info = get_reference_info(path) return data, affine, vox_size, header, ref_info
def getrefpath(mypath, subject, reference='fa', verbose=None): subjfolder = glob.glob(os.path.join(mypath, "*" + subject + "*/")) if os.path.exists( os.path.join(mypath, subject + "_subjspace_" + reference + ".nii.gz")): refpath = (os.path.join( mypath, subject + "_subjspace_" + reference + ".nii.gz")) elif os.path.exists( os.path.join(mypath, subject + "_" + reference + "_RAS.nii.gz")): refpath = (os.path.join(mypath, subject + "_coreg_RAS.nii.gz")) if 'refpath' not in locals(): txt = "The subject " + subject + " was not detected, exit" print(txt) send_mail(txt, subject="Error") return None return refpath
def LifEcreate_fig(fiber_fit_beta, mean_rmse, model_rmse, vox_coords, dwidata, subject, t1_data=None, outpathfig=None, interactive=False, strproperty="_", verbose=False): #fiber_fit_beta_path = glob.glob(pickles_folder + '/*beta.p')[0] #mean_rmse_path = glob.glob(pickles_folder + '/*mean_rmse.p')[0] #model_rmse_path = glob.glob(pickles_folder + '/*model_rmse.p')[0] #fiber_fit_beta = pickle.load(open(fiber_fit_beta_path, "rb")) #mean_rmse = pickle.load(open(mean_rmse_path, "rb")) #model_rmse = pickle.load(open(model_rmse_path, "rb")) fig, ax = plt.subplots(1) ax.hist(fiber_fit_beta, bins=100, histtype='step') ax.set_xlabel('Fiber weights') ax.set_ylabel('# fibers') #ROI_actor = actor.contour_from_roi(roimask, color=(1., 1., 0.), # opacity=0.5) #sizebeta=getsize(fiber_fit_beta) if interactive: plt.show() if outpathfig is not None: histofig_path = (outpathfig + subject + strproperty + "_beta_histogram.png") fig.savefig(histofig_path) if verbose: txt = "file saved at " + histofig_path print(txt) send_mail(txt, subject="LifE save msg ") """ vol_actor = actor.slicer(t1_data) vol_actor.display(x=40) vol_actor2 = vol_actor.copy() vol_actor2.display(z=35) """ fig, ax = plt.subplots(1) ax.hist(mean_rmse - model_rmse, bins=100, histtype='step') ax.text(0.2, 0.9, 'Median RMSE, mean model: %.2f' % np.median(mean_rmse), horizontalalignment='left', verticalalignment='center', transform=ax.transAxes) ax.text(0.2, 0.8, 'Median RMSE, LiFE: %.2f' % np.median(model_rmse), horizontalalignment='left', verticalalignment='center', transform=ax.transAxes) ax.set_xlabel('RMS Error') ax.set_ylabel('# voxels') if interactive: plt.show() if outpathfig is not None: errorhistofig_path = (outpathfig + subject + strproperty + "_error_histograms.png") fig.savefig(errorhistofig_path) if verbose: txt = "file saved at " + errorhistofig_path print(txt) send_mail(txt, subject="LifE save msg ") runspatialerrors = True try: dwidata.shape[:3] except AttributeError: runspatialerrors = False if runspatialerrors: vol_model = np.ones(dwidata.shape[:3]) * np.nan vol_model[vox_coords[:, 0], vox_coords[:, 1], vox_coords[:, 2]] = model_rmse vol_mean = np.ones(dwidata.shape[:3]) * np.nan vol_mean[vox_coords[:, 0], vox_coords[:, 1], vox_coords[:, 2]] = mean_rmse vol_improve = np.ones(dwidata.shape[:3]) * np.nan vol_improve[vox_coords[:, 0], vox_coords[:, 1], vox_coords[:, 2]] = mean_rmse - model_rmse sl_idx = 49 fig = plt.figure() fig.subplots_adjust(left=0.05, right=0.95) ax = AxesGrid(fig, 111, nrows_ncols=(1, 3), label_mode="1", share_all=True, cbar_location="top", cbar_mode="each", cbar_size="10%", cbar_pad="5%") ax[0].matshow(np.rot90(t1_data[sl_idx, :, :]), cmap=matplotlib.cm.bone) im = ax[0].matshow(np.rot90(vol_model[sl_idx, :, :]), cmap=matplotlib.cm.hot) ax.cbar_axes[0].colorbar(im) ax[1].matshow(np.rot90(t1_data[sl_idx, :, :]), cmap=matplotlib.cm.bone) im = ax[1].matshow(np.rot90(vol_mean[sl_idx, :, :]), cmap=matplotlib.cm.hot) ax.cbar_axes[1].colorbar(im) ax[2].matshow(np.rot90(t1_data[sl_idx, :, :]), cmap=matplotlib.cm.bone) im = ax[2].matshow(np.rot90(vol_improve[sl_idx, :, :]), cmap=matplotlib.cm.RdBu) ax.cbar_axes[2].colorbar(im) for lax in ax: lax.set_xticks([]) lax.set_yticks([]) if outpathfig is not None: histofig_path = (outpathfig + subject + strproperty + "_spatial_errors.png") fig.savefig(histofig_path) if verbose: txt = "spatial errors figure saved at " + histofig_path print(txt) send_mail(txt, subject="LifE save msg ")
print(labelslist) if isempty(labelslist) and roi.lower() != "wholebrain" and roi.lower() != "brain": txt = "Warning: Unrecognized roi, will take whole brain as ROI. The roi specified was: " + roi print(txt) bvec_orient=[1,2,-3] tall = time() tract_results = [] if verbose: txt=("Process running with % d max processes available on % d subjects with % d subjects in parallel each using % d processes" % (mp.cpu_count(), np.size(l), subject_processes, function_processes)) print(txt) send_mail(txt,subject="Main process start msg ") duration1=time() overwrite = False get_params = False forcestart = True if forcestart: print("WARNING: FORCESTART EMPLOYED. THIS WILL COPY OVER PREVIOUS DATA") picklesave = True donelist = [] notdonelist = [] for subject in l: picklepath_connect = figspath + subject + str_identifier + '_connectomes.p' excel_path = figspath + subject + str_identifier + "_connectomes.xlsx" if os.path.exists(picklepath_connect) and os.path.exists(excel_path):
if isempty(labelslist) and roi.lower() != "wholebrain" and roi.lower() != "brain": txt = "Warning: Unrecognized roi, will take whole brain as ROI. The roi specified was: " + roi print(txt) #labelslist=None bvec_orient=[-2,1,3] # --------------------------------------------------------- tall = time() tract_results=[] if verbose: txt=("Process running with % d max processes available on % d subjects with % d subjects in parallel each using % d processes" % (mp.cpu_count(), np.size(l), subject_processes, function_processes)) print(txt) send_mail(txt,subject="Main process start msg ") duration1=time() if subject_processes>1: if function_processes>1: pool = MyPool(subject_processes) else: pool = mp.Pool(subject_processes) # tract_results = pool.starmap_async(create_tracts, [(dwipath, outtrkpath, subject, stepsize, function_processes, strproperty, # saved_streamlines, savefa, labelslist, bvec_orient, verbose) for subject in # l]).get() tract_results = pool.starmap_async(evaluate_tracts, [(dwipath, outtrkpath, subject, stepsize, saved_streamlines, labelslist, outpathpickle, figspath, function_processes, allsave, display, strproperty, ratio, verbose) for subject in l]).get()
def QCSA_tractmake(data, affine, vox_size, gtab, mask, masktype, header, step_size, peak_processes, outpathtrk, subject='NA', ratio=1, overwrite=False, get_params=False, doprune=False, figspath=None, verbose=None): # Compute odfs in Brain Mask t2 = time() if os.path.isfile(outpathtrk) and not overwrite: txt = "Subject already saved at "+outpathtrk print(txt) streamlines_generator = None params = None return outpathtrk, streamlines_generator, params csa_model = CsaOdfModel(gtab, 6) if peak_processes == 1: parallel = False else: parallel = True if verbose: send_mail("Starting calculation of Constant solid angle model for subject " + subject,subject="CSA model start") wholemask = np.where(mask == 0, False, True) print(f"There are {peak_processes} and {parallel} here") csa_peaks = peaks_from_model(model=csa_model, data=data, sphere=peaks.default_sphere, # issue with complete sphere mask=wholemask, relative_peak_threshold=.5, min_separation_angle=25, parallel=parallel, nbr_processes=peak_processes) duration = time() - t2 if verbose: print(subject + ' CSA duration %.3f' % (duration,)) t3 = time() if verbose: send_mail('Computing classifier for local tracking for subject ' + subject + ',it has been ' + str(round(duration)) + 'seconds since the start of tractmaker',subject="Seed computation" ) print('Computing classifier for local tracking for subject ' + subject) if masktype == "FA": #tensor_model = dti.TensorModel(gtab) #tenfit = tensor_model.fit(data, mask=labels > 0) #FA = fractional_anisotropy(tenfit.evals) FA_threshold = 0.05 classifier = ThresholdStoppingCriterion(mask, FA_threshold) if figspath is not None: fig = plt.figure() mask_fa = mask.copy() mask_fa[mask_fa < FA_threshold] = 0 plt.xticks([]) plt.yticks([]) plt.imshow(mask_fa[:, :, data.shape[2] // 2].T, cmap='gray', origin='lower', interpolation='nearest') fig.tight_layout() fig.savefig(figspath + 'threshold_fa.png') else: classifier = BinaryStoppingCriterion(wholemask) # generates about 2 seeds per voxel # seeds = utils.random_seeds_from_mask(fa > .2, seeds_count=2, # affine=np.eye(4)) # generates about 2 million streamlines # seeds = utils.seeds_from_mask(fa > .2, density=1, # affine=np.eye(4)) if verbose: print('Computing seeds') seeds = utils.seeds_from_mask(wholemask, density=1, affine=np.eye(4)) #streamlines_generator = local_tracking.local_tracker(csa_peaks,classifier,seeds,affine=np.eye(4),step_size=step_size) if verbose: print('Computing the local tracking') duration = time() - t2 send_mail('Start of the local tracking ' + ',it has been ' + str(round(duration)) + 'seconds since the start of tractmaker', subject="Seed computation") #stepsize = 2 #(by default) stringstep = str(step_size) stringstep = stringstep.replace(".", "_") if verbose: print("stringstep is "+stringstep) streamlines_generator = LocalTracking(csa_peaks, classifier, seeds, affine=np.eye(4), step_size=step_size) if verbose: duration = time() - t2 txt = 'About to save streamlines at ' + outpathtrk + ',it has been ' + str(round(duration)) + \ 'seconds since the start of tractmaker', send_mail(txt,subject="Tract saving" ) cutoff = 2 if doprune: streamlines_generator = prune_streamlines(list(streamlines_generator), data[:, :, :, 0], cutoff=cutoff, verbose=verbose) myheader = create_tractogram_header(outpathtrk, *header) sg = lambda: (s for i, s in enumerate(streamlines_generator) if i % ratio == 0) save_trk_heavy_duty(outpathtrk, streamlines=sg, affine=affine, header=myheader, shape=mask.shape, vox_size=vox_size) else: sg = lambda: (s for i, s in enumerate(streamlines_generator) if i % ratio == 0) myheader = create_tractogram_header(outpathtrk, *header) save_trk_heavy_duty(outpathtrk, streamlines=sg, affine=affine, header=myheader, shape=mask.shape, vox_size=vox_size) if verbose: duration = time() - t2 txt = "Tract files were saved at "+outpathtrk + ',it has been ' + str(round(duration)) + \ 'seconds since the start of tractmaker' print(txt) send_mail(txt,subject="Tract saving" ) # save everything - will generate a 20+ GBytes of data - hard to manipulate # possibly add parameter in csv file or other to decide whether to save large tractogram file # outpathfile=outpath+subject+"bmCSA_detr"+stringstep+".trk" # myheader=create_tractogram_header(outpathfile,*get_reference_info(fdwi)) duration3 = time() - t2 if verbose: print(duration3) print(subject + ' Tracking duration %.3f' % (duration3,)) send_mail("Finished file save at "+outpathtrk+" with tracking duration of " + str(duration3) + "seconds", subject="file save update" ) if get_params: numtracts, minlength, maxlength, meanlength, stdlength = get_trk_params(streamlines_generator, verbose) params = [numtracts, minlength, maxlength, meanlength, stdlength] if verbose: print("For subject " + str(subject) + " the number of tracts is " + str(numtracts) + ", the minimum length is " + str(minlength) + ", the maximum length is " + str(maxlength) + ", the mean length is " + str(meanlength) + ", the std is " + str(stdlength)) else: params = None return outpathtrk, streamlines_generator, params
def getdiffpath(mypath, subject, denoise="", verbose=None): if denoise is None: denoise = "" subjfolder = glob.glob(os.path.join(mypath, "*" + subject + "*/")) if np.size(subjfolder) == 1: subjfolder = subjfolder[0] else: subjfolder = None print('hi') print(os.path.join(mypath, subject + "_subjspace_coreg.nii.gz")) if os.path.isfile(mypath) and os.path.exists(mypath): fdiffpath = mypath #elif os.path.exists(os.path.join(mypath,subject+"_"+denoise+"_diff.nii.gz")): # fdiffpath = (os.path.join(mypath,subject+"_"+denoise+"_diff.nii.gz")) #elif os.path.exists(os.path.join(mypath,subject+"_"+denoise+".nii.gz")): # fdiffpath = (os.path.join(mypath,subject+"_"+denoise+".nii.gz")) #elif os.path.exists(os.path.join(mypath,subject+"_rawnii.nii.gz")): # fdiffpath = (os.path.join(mypath,subject+"_rawnii.nii.gz")) elif os.path.exists( os.path.join(mypath, subject + "_subjspace_coreg.nii.gz")): fdiffpath = (os.path.join(mypath, subject + "_subjspace_coreg.nii.gz")) #elif os.path.exists(os.path.join(mypath,subject+"_coreg.nii.gz")): # fdiffpath = (os.path.join(mypath,subject+"_coreg.nii.gz")) #elif os.path.exists(os.path.join(mypath,subject+"_coreg.nii.gz")): # fdiffpath = (os.path.join(mypath,subject+"_coreg.nii.gz")) elif os.path.exists(os.path.join(mypath, subject + "_coreg_RAS.nii.gz")): fdiffpath = (os.path.join(mypath, subject + "_coreg_RAS.nii.gz")) #elif np.size(glob.glob(os.path.join(mypath,subject+"*_dwi.nii.gz"))) == 1: # fdiffpath = glob.glob(os.path.join(mypath,subject+"*_dwi.nii.gz"))[0] #elif os.path.exists(mypath + '/Reg_' + subject + '_nii4D.nii.gz'): # fdiffpath = mypath + '/Reg_' + subject + '_nii4D.nii.gz' #elif os.path.exists(mypath + '/nii4D_' + subject + '.nii'): # fdiffpath = mypath + '/nii4D_' + subject + '.nii' #elif os.path.exists(mypath + '/'+subject+'_nii4D_RAS.nii.gz'): # fdiffpath = mypath + '/'+subject+'_nii4D_RAS.nii.gz' #elif os.path.exists(mypath + '/4Dnii/'+subject+'_nii4D_RAS.nii.gz'): # fdiffpath = mypath + '/4Dnii/'+subject+'_nii4D_RAS.nii.gz' #elif os.path.exists(mypath + '/'+subject+'_nii4D_RAS.nii.gz'): # fdiffpath = mypath + '/'+subject+'_nii4D_RAS.nii.gz' #elif os.path.exists(mypath + '/' + subject + '/') and np.size(glob.glob(os.path.join(subjfolder, subject + '*nii4D*.nii*'))) > 0: # fdiffpath = glob.glob(os.path.join(subjfolder, subject + '*nii4D*.nii*'))[0] #elif os.path.exists(os.path.join(mypath,subject+"_dwi.nii.gz")): # fdiffpath = (os.path.join(mypath,subject+"_dwi.nii.gz")) elif os.path.exists(mypath) and subjfolder is not None and np.size( glob.glob(os.path.join(subjfolder, "*.bxh"))) > 0: subjbxh = glob.glob(os.path.join(subjfolder, "*.bxh")) for bxhfile in subjbxh: bxhtype = checkbxh(bxhfile, False) if bxhtype == "diff": fdiffpath = bxhfile.replace(".bxh", ".nii.gz") break if 'fdiffpath' not in locals(): txt = "The subject " + subject + " was not detected, exit" print(txt) send_mail(txt, subject="Error") return None return (fdiffpath)
def getfa(mypath, subject, bvec_orient, verbose=None): # fdwi = mypath + '4Dnii/' + subject + '_nii4D_RAS.nii.gz' fapath = mypath + '/' + subject + '_fa_RAS.nii.gz' if os.path.exists(fapath): fapath = mypath + '/' + subject + '_fa_RAS.nii.gz' # fdwi_data, affine, vox_size = load_nifti(fdwipath, return_voxsize=True) if os.path.exists(mypath + '/' + subject + '_fa_RAS.nii.gz'): fapath = (mypath + '/' + subject + '_fa_RAS.nii.gz') elif os.path.exists(mypath + '/' + 'bmfa' + subject + '_wholebrain_.nii.gz'): fapath = (mypath + '/' + 'bmfa' + subject + '_wholebrain_.nii.gz') elif os.path.exists(mypath + '/' + subject + '/' + 'bmfa' + subject + '.nii.gz'): fapath = (mypath + '/' + subject + '/' + 'bmfa' + subject + '.nii.gz') else: print("Could not find the fa file anywhere") print("Will attempt to create new fa file") fdiff_data, affine, gtab, mask, vox_size, fdiffpath, hdr, header = getdiffdata( mypath, subject, bvec_orient) fapath = make_tensorfit(fdiff_data, mask, gtab, affine, subject, outpath=os.path.dirname(fdiffpath), strproperty="", verbose=verbose) if verbose: txt = "Extracting information from the fa file located at " + fapath print(txt) send_mail(txt, subject="Begin data extraction") if 'fapath' not in locals(): txt = "The fa of subject " + subject + " was not detected at " + fapath + ", exit" print(txt) send_mail(txt, subject="Error") return (0, 0, 0, 0, 0, 0, 0, 0) img = nib.load(fapath) fa_data = img.get_data() vox_size = img.header.get_zooms()[:3] affine = img.affine hdr = img.header header = get_reference_info(fapath) del (img) """ try: fbvals = glob.glob(mypath + '/' + subject + '*_bvals_fix.txt')[0] fbvecs = glob.glob(mypath + '/' + subject + '*_bvec_fix.txt')[0] except IndexError: fbvals = glob.glob(mypath + '/' + subject + '*_bvals.txt')[0] fbvecs = glob.glob(mypath + '/' + subject + '*_bvec.txt')[0] fbvals, fbvecs = fix_bvals_bvecs(fbvals, fbvecs) print(fbvecs) bvals, bvecs = read_bvals_bvecs(fbvals, fbvecs) # bvecs = np.c_[bvecs[:, 0], -bvecs[:, 1], bvecs[:, 2]] # FOR RAS according to Alex # bvecs = np.c_[bvecs[:, 0], bvecs[:, 1], -bvecs[:, 2]] #FOR RAS # bvecs = np.c_[bvecs[:, -], bvecs[:, 0], -bvecs[:, 2]] #estimated for RAS based on headfile info bvec_sign = bvec_orient / np.abs(bvec_orient) bvecs = np.c_[bvec_sign[0] * bvecs[:, np.abs(bvec_orient[0]) - 1], bvec_sign[1] * bvecs[:, np.abs(bvec_orient[1]) - 1], bvec_sign[2] * bvecs[:, np.abs(bvec_orient[2]) - 1]] # bvecs = np.c_[bvecs[:, 1], bvecs[:, 0], -bvecs[:, 2]] # bvecs = np.c_[-bvecs[:, 1], bvecs[:, 0], bvecs[:, 2]] gtab = gradient_table(bvals, bvecs) # Build Brain Mask # bm = np.where(labels == 0, False, True) # mask = bm return fa_data, affine, gtab, vox_size, hdr, header """ return fa_data, affine, vox_size, hdr, header
def LiFEvaluation(dwidata, trk_streamlines, gtab, subject="lifesubj", header=None, roimask=None, affine=None, display=True, outpathpickle=None, outpathtrk=None, processes=1, outpathfig=None, strproperty="", verbose=None): """ Implementation of Linear Fascicle Evaluation, outputs histograms, evals Parameters ---------- dwidata : array output trk filename trkdata : array gtab : array og bval & bvec table outpath: string folder location for resulting values and figures display : boolean, optional Condition to display the results (default = False) savefig: boolean, optional Condition to save the results in outpath (default = True) Defined by Pestilli, F., Yeatman, J, Rokem, A. Kay, K. and Wandell B.A. (2014). Validation and statistical inference in living connectomes and recreated by Dipy :param dwidata: array of diffusion data :param trkdata: array of tractography data obtained from dwi :param gtab: bval & bvec table :param outpath: location to save analysis outputs :param display: :param savefig: :return: """ """""" """ if not op.exists('lr-superiorfrontal.trk'): else: # We'll need to know where the corpus callosum is from these variables: from dipy.data import (read_stanford_labels, fetch_stanford_t1, read_stanford_t1) hardi_img, gtab, labels_img = read_stanford_labels() labels = labels_img.get_data() cc_slice = labels == 2 fetch_stanford_t1() t1 = read_stanford_t1() t1_data = t1.get_data() data = hardi_img.get_data() """ """""" # Read the candidates from file in voxel space: if roimask is None: roimask = dwidata > 0 else: dwidataroi = dwidata * np.repeat( roimask[:, :, :, None], np.shape(dwidata)[3], axis=3) print("verbose: " + str(verbose) + " outpathpickle: " + str(outpathpickle)) fiber_model = life.FiberModel(gtab) # inv_affine must be used if the streamlines are in the world space, and thus we must useapply the inverse affine of dwi #when comparing the diffusion directions gtab and the voxels of trk #inv_affine = np.linalg.inv(hardi_img.affine) #fiber_fit will fit the streamlines to the original diffusion data and if verbose: txt = "Begin the evaluation over " + str( np.size(trk_streamlines)) + " streamlines" print(txt) send_mail(txt, subject="LifE start msg ") fiber_fit = fiber_model.fit(dwidata, trk_streamlines, affine=np.eye(4), processes=processes, verbose=verbose) #fiber_fit_roi = fiber_model.fit(dwidataroi, trk_streamlines, affine=np.eye(4), processes=processes, verbose=verbose) optimized_sl = list( np.array(trk_streamlines)[np.where(fiber_fit.beta > 0)[0]]) plt.ioff() if verbose: txt = "End of the evaluation over " + str(np.size(trk_streamlines)) print(txt) send_mail(txt, subject="LifE status msg ") if outpathtrk is not None: outpathfile = str( outpathtrk) + subject + strproperty + "_lifeopt_test.trk" myheader = create_tractogram_header(outpathfile, *header) optimized_sl_gen = lambda: (s for s in optimized_sl) save_trk_heavy_duty(outpathfile, streamlines=optimized_sl_gen, affine=affine, header=myheader) txt = ("Saved final trk at " + outpathfile) print(txt) send_mail(txt, subject="LifE save msg ") """ except TypeError: txt=('Could not save new tractogram file, header of original trk file not properly implemented into ' 'LifEvaluation') print(txt) send_mail(txt,subject="LifE error msg ") """ """ if interactive: ren = window.Renderer() ren.add(actor.streamtube(optimized_sl, cmap.line_colors(optimized_sl))) ren.add(ROI_actor) #ren.add(vol_actor) if interactive: window.show(ren) if outpathfig is not None: print("reached windowrecord") window.record(ren, n_frames=1, out_path=outpathfig +'_life_optimized.png', size=(800, 800)) print("did window record") """ maxsize_var = 20525023825 sizebeta = getsize(fiber_fit.beta) if sizebeta < maxsize_var: picklepath = outpathpickle + subject + strproperty + '_beta.p' txt = ("fiber_fit.beta saved at " + picklepath) pickle.dump(fiber_fit.beta, open(picklepath, "wb")) if verbose: print(txt) send_mail(txt, subject="LifE save msg ") else: txt = ( "Object fiber_fit.beta exceeded the imposed the 20GB limit with a size of: " + str(sizebeta / (10 ^ 9)) + "GB") print(txt) send_mail(txt, subject="LifE error msg") sizecoords = getsize(fiber_fit.vox_coords) if sizecoords < maxsize_var: picklepath = outpathpickle + subject + strproperty + '_voxcoords.p' txt = ("fiber_fit.voxcoords saved at " + picklepath) pickle.dump(fiber_fit.vox_coords, open(picklepath, "wb")) if verbose: print(txt) send_mail(txt, subject="LifE save msg ") else: txt = ( "Object fiber_fit.beta exceeded the imposed the 20GB limit with a size of: " + str(sizebeta / (10 ^ 9)) + "GB") print(txt) send_mail(txt, subject="LifE error msg") #predict diffusion data based on new model model_predict = fiber_fit.predict( ) #possible to predict based on different gtab or base signal (change gtab, S0) model_error = model_predict - fiber_fit.data #compare original dwi data and the model fit, calculate error model_rmse = np.sqrt( np.mean(model_error[:, 10:]**2, -1)) #this is good, but must check ways to avoid overfitting #how does the model get built? add lasso? JS beta_baseline = np.zeros( fiber_fit.beta.shape[0] ) #baseline assumption where the streamlines weight is 0 pred_weighted = np.reshape( opt.spdot(fiber_fit.life_matrix, beta_baseline), (fiber_fit.vox_coords.shape[0], np.sum(~gtab.b0s_mask))) mean_pred = np.empty((fiber_fit.vox_coords.shape[0], gtab.bvals.shape[0])) S0 = fiber_fit.b0_signal mean_pred[..., gtab.b0s_mask] = S0[:, None] mean_pred[..., ~gtab.b0s_mask] = \ (pred_weighted + fiber_fit.mean_signal[:, None]) * S0[:, None] mean_error = mean_pred - fiber_fit.data mean_rmse = np.sqrt(np.mean(mean_error**2, -1)) size_meanrmse = getsize(mean_rmse) if size_meanrmse < maxsize_var: picklepath = outpathpickle + subject + strproperty + '_mean_rmse.p' txt = ("mean_rmse saved at " + picklepath) pickle.dump(mean_rmse, open(picklepath, "wb")) if verbose: print(txt) send_mail(txt, subject="LifE save msg ") else: txt = ( "Object mean_rmse exceeded the imposed the 20GB limit with a size of: " + str(size_meanrmse / (10 ^ 9)) + " GB") print(txt) send_mail(txt, subject="LifE error msg") size_modelrmse = getsize(model_rmse) if size_modelrmse < maxsize_var: picklepath = outpathpickle + subject + strproperty + '_model_rmse.p' txt = ("model_rmse saved at " + picklepath) pickle.dump(model_rmse, open(picklepath, "wb")) if verbose: print(txt) send_mail(txt, subject="LifE save msg ") else: txt = ( "Object model_rmse exceeded the imposed the 20GB limit with a size of: " + str(size_modelrmse / (10 ^ 9)) + " GB") print(txt) send_mail(txt, subject="LifE error msg") if outpathfig is not None: try: import matplotlib.pyplot as myplot fig, ax = plt.subplots(1) ax.hist(fiber_fit.beta, bins=100, histtype='step') LifEcreate_fig(fiber_fit.beta, mean_rmse, model_rmse, fiber_fit.vox_coords, dwidata, subject, t1_data=dwidata[:, :, :, 0], outpathfig=outpathfig, interactive=False, strproperty=strproperty, verbose=verbose) except: print( "Coult not launch life create fig, possibly qsub location (this is a template warning, to be improved upon" ) return model_error, mean_error
if os.path.exists(output_trk_file) and overwrite is False: print("The tract creation of subject " + subject + " is already done") if verbose: print('Running the ' + subject + ' file') diff_data, affine, vox_size, header, ref_info = extract_nii_info(input_diff_file, verbose) gtab = getgtab(os.path.dirname(input_diff_file), subject, bvec_orient) if np.size(np.shape(mask)) == 1: mask = mask[0] if np.size(np.shape(mask)) == 4: mask = mask[:, :, :, 0] print("Mask shape is " + str(np.shape(mask))) #if classifier == "FA": # outpathbmfa, mask = make_tensorfit(diff_data ,mask ,gtab ,affine ,subject ,outpath=diffpath ,verbose=verbose) print(verbose) if verbose: txt = ("The QCSA Tractmake is ready to launch for subject " + subject) print(txt) send_mail(txt ,subject="QCSA main function start") print("email sent") outpathtrk, trkstreamlines, params = QCSA_tractmake(diff_data, affine, vox_size, gtab, mask, masktype, ref_info, step_size, peak_processes, output_trk_file, subject, ratio, overwrite, get_params, doprune, figspath=None, verbose=verbose)