def test_dpy(): fd,fname = mkstemp() dpw = Dpy(fname,'w') A=np.ones((5,3)) B=2*A.copy() C=3*A.copy() dpw.write_track(A) dpw.write_track(B) dpw.write_track(C) dpw.write_tracks([C,B,A]) dpw.close() dpr = Dpy(fname,'r') assert_equal(dpr.version()=='0.0.1',True) T=dpr.read_tracksi([0,1,2,0,0,2]) print(T) T2=dpr.read_tracks() assert_equal(len(T2),6) dpr.close() assert_array_equal(A,T[0]) assert_array_equal(C,T[5]) os.remove(fname)
def spherical_rois(fdpy,fsr,sq_radius=4): R=atlantic_points() dpr=Dpy(fdpy,'r') T=dpr.read_tracks() dpr.close() center=R['BCC'] refimg=nib.load(fref) aff=refimg.get_affine() SR={} for key in R: center=R[key] #back to world space centerw=np.dot(aff,np.array(center+(1,)))[:3] centerw.shape=(1,)+centerw.shape centerw=centerw.astype(np.float32) res= [track_roi_intersection_check(t,centerw,sq_radius) for t in T] res= np.array(res,dtype=np.int) ind=np.where(res>0)[0] SR[key]={} SR[key]['center']=center SR[key]['centerw']=tuple(np.squeeze(centerw)) SR[key]['radiusw']=np.sqrt(sq_radius) SR[key]['indices']=ind save_pickle(fsr,SR)
def load_tractogram(filename, lazy_load=False): """ Loads tractogram files (*.trk or *.tck or *.dpy) Parameters ---------- filename : str input trk filename lazy_load : {False, True}, optional If True, load streamlines in a lazy manner i.e. they will not be kept in memory and only be loaded when needed. Otherwise, load all streamlines in memory. Returns ------- streamlines : list of 2D arrays Each 2D array represents a sequence of 3D points (points, 3). hdr : dict header from a trk file """ if 'dpy' in os.path.splitext(filename)[1].lower(): dpw = Dpy(filename, 'r') streamlines = dpw.read_tracks() dpw.close() return streamlines, {} trk_file = nib.streamlines.load(filename, lazy_load) return trk_file.streamlines, trk_file.header
def loading_full_tractograpy(self, tracpath=None): """ Loading full tractography and creates StreamlineLabeler to show it all. """ # load the tracks registered in MNI space self.tracpath=tracpath basename = os.path.basename(self.tracpath) tracks_basename, tracks_format = os.path.splitext(basename) if tracks_format == '.dpy': dpr = Dpy(self.tracpath, 'r') print "Loading", self.tracpath self.T = dpr.read_tracks() dpr.close() self.T = np.array(self.T, dtype=np.object) elif tracks_format == '.trk': print "Loading", self.tracpath # Old nibabel API: # streams, self.hdr = nib.trackvis.read(self.tracpath, points_space='voxel') # self.T = np.array([s[0] for s in streams], dtype=np.object) # New nibabel API tmp = nib.streamlines.load(self.tracpath) streams = tmp.tractogram.apply_affine(np.linalg.inv(tmp.affine)).streamlines self.header = tmp.header self.T = np.array(streams, dtype=np.object) # The following code has been commented out to avoid # misalignment between original streamlines IDs and final IDs. # print "Removing short streamlines" # self.T = np.array([t for t in self.T if length(t)>= 15], dtype=np.object) tracks_directoryname = os.path.dirname(self.tracpath) + '/.temp/' general_info_filename = tracks_directoryname + tracks_basename + '.spa' # Check if there is the .spa file that contains all the # computed information from the tractography anyway and try to # load it try: print "Looking for general information file" self.load_info(general_info_filename) except (IOError, KeyError): print "General information not found, recomputing buffers" self.update_info(general_info_filename) # create the interaction system for tracks, self.streamlab = StreamlineLabeler('Bundle Picker', self.buffers, self.clusters, vol_shape=self.dims, affine=np.copy(self.affine), clustering_parameter=len(self.clusters), clustering_parameter_max=len(self.clusters), full_dissimilarity_matrix=self.full_dissimilarity_matrix) self.scene.add_actor(self.streamlab)
def skeletonize(fdpy,flsc,points=3): dpr=Dpy(fdpy,'r') T=dpr.read_tracks() dpr.close() print len(T) Td=[downsample(t,points) for t in T] C=local_skeleton_clustering(Td,d_thr=10.,points=points) #Tobject=np.array(T,dtype=np.object) #''' #r=fvtk.ren() skeleton=[] for c in C: #color=np.random.rand(3) if C[c]['N']>0: Ttmp=[] for i in C[c]['indices']: Ttmp.append(T[i]) si,s=most_similar_track_mam(Ttmp,'avg') print si,C[c]['N'] C[c]['most']=Ttmp[si] #fvtk.add(r,fvtk.line(Ttmp[si],color)) print len(skeleton) #r=fos.ren() #fos.add(r,fos.line(skeleton,color)) #fos.add(r,fos.line(T,fos.red)) #fvtk.show(r) #''' save_pickle(flsc,C)
def roi_track_counts(fdpy,fref,fatlas,roi_no,dist_transf=True,fres=None): dpr=Dpy(fdpy,'r') T=dpr.read_tracks() dpr.close() img=nib.load(fref) affine=img.get_affine() zooms = img.get_header().get_zooms() iaffine=np.linalg.inv(affine) T2=[] #go back to volume space for t in T: T2.append(np.dot(t,iaffine[:3,:3].T)+iaffine[:3,3]) del T tcs,tes=track_counts(T2,img.get_shape(),zooms,True) atlas_img=nib.load(fatlas) atlas=atlas_img.get_data() roi=atlas.copy() roi[atlas!=roi_no]=0 if dist_transf: roi2=distance_transform_cdt(roi) roi[roi2!=roi2.max()]=0 I=np.array(np.where(roi==roi_no)).T else: I=np.array(np.where(roi==roi_no)).T """ if erosion_level>0: roi2=binary_erosion(roi,cross,erosion_level) I=np.array(np.where(roi2==True)).T else: roi2=distance_transform_cdt(roi) I=np.array(np.where(roi==roi_no)).T """ #print I.shape #nib.save(nib.Nifti1Image(roi2,affine),'/tmp/test.nii.gz') Ttes=[] for iroi in I: try: Ttes.append(tes[tuple(iroi)]) except KeyError: pass Ttes=list(set(list(chain.from_iterable(Ttes)))) T2n=np.array(T2,dtype=np.object) res=list(T2n[Ttes]) #back to world space res2=[] for t in res: res2.append(np.dot(t,affine[:3,:3].T)+affine[:3,3]) np.save(fres,np.array(res2,dtype=np.object))
def load_cst(tracks_filename, cst_index_file, ext): from dipy.io.dpy import Dpy from dipy.io.pickles import load_pickle dpr_tracks = Dpy(tracks_filename, 'r') all_tracks=dpr_tracks.read_tracks() dpr_tracks.close() tracks_id = load_pickle(cst_index_file) cst = [all_tracks[i] for i in tracks_id] cst_ext = [all_tracks[i] for i in tracks_id] medoid_cst = [] #len_dis = 250 if ext: k = np.round(len(cst)*1.2) not_cst_fil = [] min_len = min(len(i) for i in cst) #print 'min_len of cst', min_len min_len = min_len*2.2/3#2./3.2# - 20 for i in np.arange(len(all_tracks)): if (i not in tracks_id) and (length(all_tracks[i]) > min_len): not_cst_fil.append(all_tracks[i]) #for st in all_tracks: # if (length(st)>=min_len) and (st not in cst): # not_cst_fil.append(st) from dipy.segment.quickbundles import QuickBundles qb = QuickBundles(cst,200,18) medoid_cst = qb.centroids[0] med_notcst_dm = bundles_distances_mam([medoid_cst], not_cst_fil) med_cst_dm = bundles_distances_mam([medoid_cst], cst) cst_rad = med_cst_dm[0][np.argmax(med_cst_dm[0])] len_dis = cst_rad * 2.8/2. #print med_cst_dm #print cst_rad #print len_dis #k_indices which close to the medoid sort = np.argsort(med_notcst_dm,axis = 1)[0] #print sort[:k+1] while (k>0 and med_notcst_dm[0][sort[k]]>=len_dis): k = k - 1 #print med_notcst_dm[0][sort[0:k]] #print k #close_indices = np.argsort(cst_dm,axis = 1)[:,0:k][0] close_indices = sort[0:k] for idx in close_indices: cst_ext.append(not_cst_fil[idx]) return cst, cst_ext, medoid_cst return cst
def loading_full_tractograpy(self, tracpath=None): """ Loading full tractography and creates StreamlineLabeler to show it all. """ # load the tracks registered in MNI space self.tracpath = tracpath basename = os.path.basename(self.tracpath) tracks_basename, tracks_format = os.path.splitext(basename) if tracks_format == '.dpy': dpr = Dpy(self.tracpath, 'r') print "Loading", self.tracpath self.T = dpr.read_tracks() dpr.close() self.T = np.array(self.T, dtype=np.object) elif tracks_format == '.trk': streams, self.hdr = nib.trackvis.read(self.tracpath, points_space='voxel') print "Loading", self.tracpath self.T = np.array([s[0] for s in streams], dtype=np.object) print "Removing short streamlines" self.T = np.array([t for t in self.T if length(t) >= 15], dtype=np.object) tracks_directoryname = os.path.dirname(self.tracpath) + '/.temp/' general_info_filename = tracks_directoryname + tracks_basename + '.spa' # Check if there is the .spa file that contains all the # computed information from the tractography anyway and try to # load it try: print "Looking for general information file" self.load_info(general_info_filename) except (IOError, KeyError): print "General information not found, recomputing buffers" self.update_info(general_info_filename) # create the interaction system for tracks, self.streamlab = StreamlineLabeler( 'Bundle Picker', self.buffers, self.clusters, vol_shape=self.dims, affine=np.copy(self.affine), clustering_parameter=len(self.clusters), clustering_parameter_max=len(self.clusters), full_dissimilarity_matrix=self.full_dissimilarity_matrix) self.scene.add_actor(self.streamlab)
def get_luigi_SLFI(): fseg1 = "/home/eg309/Devel/segmented_bundles/luigi_s1_for_eleftherios/S1_SLFI" # fseg2='/home/eg309/Devel/segmented_bundles/luigi_s1_for_eleftherios/S1_SLFII' subject = "01" fdpyw = "data/subj_" + subject + "/101_32/DTI/tracks_gqi_3M_linear.dpy" dpr = Dpy(fdpyw, "r") T = dpr.read_tracks() dpr.close() seg_inds = load_pickle(fseg1) T1 = [T[i] for i in seg_inds] # seg_inds=load_pickle(fseg2) # T2=[T[i] for i in seg_inds] return T1
def load_data(figure, data_id): if figure=='small_dataset': filename = 'ALS_Data/'+ str(data_id) + '/DIFF2DEPI_EKJ_64dirs_14/DTI/tracks_dti_10K.dpy' elif figure=='median_dataset': filename = 'ALS_Data/' + str(data_id) + '/DIFF2DEPI_EKJ_64dirs_14/DTI/tracks_dti_1M.dpy' elif figure=='big_dataset': filename = 'ALS_Data/' + str(data_id) + '/DIFF2DEPI_EKJ_64dirs_14/DTI/tracks_dti_3M.dpy' print "Loading tracks." dpr = Dpy(filename, 'r') tracks = dpr.read_tracks() dpr.close() tracks = np.array(tracks, dtype=np.object) return tracks
def load_data(figure, data_id): if figure == "small_dataset": filename = "ALS_Data/" + str(data_id) + "/DIFF2DEPI_EKJ_64dirs_14/DTI/tracks_dti_10K.dpy" elif figure == "median_dataset": filename = "ALS_Data/" + str(data_id) + "/DIFF2DEPI_EKJ_64dirs_14/DTI/tracks_dti_1M.dpy" elif figure == "big_dataset": filename = "ALS_Data/" + str(data_id) + "/DIFF2DEPI_EKJ_64dirs_14/DTI/tracks_dti_3M.dpy" print "Loading tracks." dpr = Dpy(filename, "r") tracks = dpr.read_tracks() dpr.close() tracks = np.array(tracks, dtype=np.object) return tracks
def load_whole_tract(tracks_filename): from dipy.io.pickles import load_pickle if (tracks_filename[-3:]=='dpy'): from dipy.io.dpy import Dpy dpr_tracks = Dpy(tracks_filename, 'r') all_tracks=dpr_tracks.read_tracks() dpr_tracks.close() else: import nibabel as nib streams,hdr=nib.trackvis.read(tracks_filename,points_space='voxel') all_tracks = np.array([s[0] for s in streams], dtype=np.object) all_tracks = np.array(all_tracks,dtype=np.object) return all_tracks
def create_dataset_from_tractography(size1, size2, same=True): if same: assert(size2 >= size1) filename = 'data/tracks_dti_10K_linear.dpy' print "Loading", filename dpr = Dpy(filename, 'r') tractography = dpr.read_tracks() dpr.close() print len(tractography), "streamlines" print "Removing streamlines that are too short" tractography = filter(lambda x: len(x) > 20, tractography) # remove too short streamlines print len(tractography), "streamlines" tractography = np.array(tractography, dtype=np.object) print "Creating two simulated tractographies of sizes", size1, "and", size2 if same: ids = fft(tractography, k=max([size1, size2]), distance=bundles_distances_mam) tractography1 = tractography[ids[:size1]] else: # ids1 = np.random.permutation(len(tractography))[:size1] # ids1 = sff(tractography, k=size1, distance=bundles_distances_mam) ids1 = fft(tractography, k=size1, distance=bundles_distances_mam) tractography1 = tractography[ids1[:size1]] if same: tractography2 = tractography[ids[:size2]] else: # ids2 = np.random.permutation(len(tractography))[:size2] # ids2 = sff(tractography, k=size2, distance=bundles_distances_mam) ids2 = fft(tractography, k=size2, distance=bundles_distances_mam) tractography2 = tractography[ids2] print "Done." print "Computing the distance matrices for each tractography." dm1 = bundles_distances_mam(tractography1, tractography1) dm2 = bundles_distances_mam(tractography2, tractography2) print("Computing similarity matrices.") sigma2 = np.mean([np.median(dm1), np.median(dm2)]) ** 2.0 print("sigma2 = %f" % sigma2) A = np.exp(-dm1 * dm1 / sigma2) B = np.exp(-dm2 * dm2 / sigma2) # Note: the optimization works even using distance instead of similarity: # A = dm1 # B = dm2 return A, B
def load_tracks(method="pmt"): from nibabel import trackvis as tv dname = "/home/eg309/Data/orbital_phantoms/dwi_dir/subject1/" if method == "pmt": fname = "/home/eg309/Data/orbital_phantoms/dwi_dir/workflow/tractography/_subject_id_subject1/cam2trk_pico_twoten/data_fit_pdfs_tracked.trk" streams, hdr = tv.read(fname, points_space="voxel") tracks = [s[0] for s in streams] if method == "dti": fname = dname + "dti_tracks.dpy" if method == "dsi": fname = dname + "dsi_tracks.dpy" if method == "gqs": fname = dname + "gqi_tracks.dpy" if method == "eit": fname = dname + "eit_tracks.dpy" if method in ["dti", "dsi", "gqs", "eit"]: dpr_linear = Dpy(fname, "r") tracks = dpr_linear.read_tracks() dpr_linear.close() if method != "pmt": tracks = [t - np.array([96 / 2.0, 96 / 2.0, 55 / 2.0]) for t in tracks if track_range(t, 100 / 2.5, 150 / 2.5)] tracks = [t for t in tracks if track_range(t, 100 / 2.5, 150 / 2.5)] print "final no of tracks ", len(tracks) qb = QuickBundles(tracks, 25.0 / 2.5, 18) # from dipy.viz import fvtk # r=fvtk.ren() # fvtk.add(r,fvtk.line(qb.virtuals(),fvtk.red)) # fvtk.show(r) # show_tracks(tracks)#qb.exemplars()[0]) # qb.remove_small_clusters(40) del tracks # load tl = TrackLabeler(qb, qb.downsampled_tracks(), vol_shape=None, tracks_line_width=3.0, tracks_alpha=1) # return tracks w = World() w.add(tl) # create window wi = Window(caption="Fos", bgcolor=(1.0, 1.0, 1.0, 1.0), width=1600, height=900) wi.attach(w) # create window manager wm = WindowManager() wm.add(wi) wm.run()
def see_tracks(fdpy,N=2000): dpr=Dpy(fdpy,'r') #T=dpr.read_tracksi(range(N)) T=dpr.read_tracks() dpr.close() T=[downsample(t,5) for t in T] r=fvtk.ren() colors=np.ones((len(T),3)).astype('f4') for (i,c) in enumerate(T): orient=c[0]-c[-1] orient=np.abs(orient/np.linalg.norm(orient)) colors[i,:3]=orient fvtk.add(r,fvtk.line(T,colors,opacity=0.5)) #fos.add(r,fos.sphere((0,0,0),10)) fvtk.show(r)
def see_spherical_intersections(fdpy,fsr): dpr=Dpy(fdpy,'r') T=dpr.read_tracks() dpr.close() SR=load_pickle(fsr) r=fvtk.ren() for key in SR: ind=SR[key]['indices'] intersT=[T[i] for i in ind] fvtk.add(r,fvtk.line(intersT,np.random.rand(3))) centerw=SR[key]['centerw'] radius=SR[key]['radiusw'] fvtk.add(r,fvtk.sphere(position=centerw,radius=radius)) fvtk.show(r)
def test_dpy(): fname = 'test.bin' with InTemporaryDirectory(): dpw = Dpy(fname, 'w') A = np.ones((5, 3)) B = 2 * A.copy() C = 3 * A.copy() dpw.write_track(A) dpw.write_track(B) dpw.write_track(C) dpw.write_tracks([C, B, A]) dpw.close() dpr = Dpy(fname, 'r') assert_equal(dpr.version() == '0.0.1', True) T = dpr.read_tracksi([0, 1, 2, 0, 0, 2]) T2 = dpr.read_tracks() assert_equal(len(T2), 6) dpr.close() assert_array_equal(A, T[0]) assert_array_equal(C, T[5])
def test_dpy(): fname = 'test.bin' dpw = Dpy(fname, 'w') A = np.ones((5, 3)) B = 2 * A.copy() C = 3 * A.copy() dpw.write_track(A) dpw.write_track(B) dpw.write_track(C) dpw.write_tracks([C, B, A]) dpw.close() dpr = Dpy(fname, 'r') assert_equal(dpr.version() == '0.0.1', True) T = dpr.read_tracksi([0, 1, 2, 0, 0, 2]) T2 = dpr.read_tracks() assert_equal(len(T2), 6) dpr.close() assert_array_equal(A, T[0]) assert_array_equal(C, T[5])
def compute_conmats(dir_res_out): # Connectiivty matrices are compute for all tractograms # and all .nii.gz parcellation files prefixed with 'parc_' dpys = glob.glob(dir_res_out + '/*.dpy') parcs = glob.glob(dir_res_out + '/parc_*.nii.gz') for parc_file in parcs: parc_name = os.path.split(parc_file)[1].replace('.nii.gz', '') parc_img = nib.load(parc_file) parc_dat = parc_img.get_data().astype('int32') affine = np.eye(4) for dpy_file in dpys: dpy_name = os.path.split(dpy_file)[1].replace('.dpy', '') cm_name = 'conmat__' + parc_name.replace('parc__', '') \ + dpy_name.replace('tractogram', '') mapping_name = cm_name.replace('Conmat', 'conmat_mapping') cm_file = dir_res_out + '/' + cm_name + '.h5' dpy = Dpy(dpy_file, 'r') dpy_streams = dpy.read_tracks() dpy.close() M,G = connectivity_matrix(dpy_streams,parc_dat,affine=affine, return_mapping=True,mapping_as_streamlines=False) F = h5py.File(cm_file, 'w') F.create_dataset('M', data=M) g = F.create_group('G') for k,v in G.items(): g[str(k)] = v F.close()
def save_id_tract_plus_sff(tracks_filename, id_file, num_proto, distance, out_fname): if (tracks_filename[-3:]=='dpy'): dpr_tracks = Dpy(tracks_filename, 'r') all_tracks=dpr_tracks.read_tracks() dpr_tracks.close() else: all_tracks = load_whole_tract_trk(tracks_filename) tracks_id = load_pickle(id_file) tract = [all_tracks[i] for i in tracks_id] not_tract_fil = [] id_not_tract_fil = [] min_len = min(len(i) for i in tract) #print 'min_len of cst', min_len min_len = min_len*2.2/3#2./3.2# - 20 for i in np.arange(len(all_tracks)): if (i not in tracks_id) and (length(all_tracks[i]) > min_len): not_tract_fil.append(all_tracks[i]) id_not_tract_fil.append(i) not_tract_fil = np.array(not_tract_fil,dtype=np.object) sff_pro_id = sff(not_tract_fil, num_proto, distance) tract_sff_id = [] for i in tracks_id: tract_sff_id.append(i) for idx in sff_pro_id: tract_sff_id.append(id_not_tract_fil[idx]) #tract_sff_id.append(id_not_tract_fil[i] for i in sff_pro_id) print len(tract), len(tract_sff_id) save_pickle(out_fname, tract_sff_id) return tract_sff_id
def warp_tracks_linearly(flirt_filename,fa_filename, tracks_filename,linear_filename): fsl_ref = '/usr/share/fsl/data/standard/FMRIB58_FA_1mm.nii.gz' img_fa = nib.load(fa_filename) flirt_affine= np.loadtxt(flirt_filename) img_ref =nib.load(fsl_ref) #create affine matrix from flirt mat=flirt2aff(flirt_affine,img_fa,img_ref) #read tracks dpr = Dpy(tracks_filename, 'r') tensor_tracks = dpr.read_tracks() dpr.close() #linear tranform for tractography tracks_warped_linear = transform_tracks(tensor_tracks,mat) #save tracks_warped_linear dpr_linear = Dpy(linear_filename, 'w') dpr_linear.write_tracks(tracks_warped_linear) dpr_linear.close()
def save_id_tract_plus_sff_in_ext(tracks_filename, id_file, num_proto, distance, out_fname_ext , out_fname_sff_in_ext, thres_len= 2.2/3., thres_vol = 1.4 , thres_dis = 3./2.): tract_ext_id = save_id_tract_ext1(tracks_filename,id_file, distance, out_fname_ext, thres_len, thres_vol , thres_dis) if (tracks_filename[-3:]=='dpy'): dpr_tracks = Dpy(tracks_filename, 'r') all_tracks=dpr_tracks.read_tracks() dpr_tracks.close() else: all_tracks = load_whole_tract_trk(tracks_filename) tracks_id = load_pickle(id_file) ext_not_tract_id = [] ext_not_tract = [] for idx in tract_ext_id: if idx not in tracks_id: ext_not_tract.append(all_tracks[idx]) ext_not_tract_id.append(idx) ext_not_tract = np.array(ext_not_tract,dtype=np.object) sff_pro_id = sff(ext_not_tract, num_proto, distance) tract_sff_in_ext_id = [] for i in tracks_id: tract_sff_in_ext_id.append(i) for k in sff_pro_id: tract_sff_in_ext_id.append(ext_not_tract_id[k]) #tract_sff_id.append(id_not_tract_fil[i] for i in sff_pro_id) print len(tracks_id), len(tract_sff_in_ext_id), len(tract_ext_id) save_pickle( out_fname_sff_in_ext, tract_sff_in_ext_id) return tract_sff_in_ext_id
def test_dpy(): fname = 'test.bin' with InTemporaryDirectory(): dpw = Dpy(fname, 'w') A = np.ones((5, 3)) B = 2 * A.copy() C = 3 * A.copy() dpw.write_track(A) dpw.write_track(B) dpw.write_track(C) dpw.write_tracks(Streamlines([C, B, A])) all_tracks = np.ascontiguousarray(np.vstack([A, B, C, C, B, A])) npt.assert_array_equal(all_tracks, dpw.tracks[:]) dpw.close() dpr = Dpy(fname, 'r') npt.assert_equal(dpr.version() == u'0.0.1', True) T = dpr.read_tracksi([0, 1, 2, 0, 0, 2]) T2 = dpr.read_tracks() npt.assert_equal(len(T2), 6) dpr.close() npt.assert_array_equal(A, T[0]) npt.assert_array_equal(C, T[5])
def warp_displacements_tracks(fdpy, ffa, fmat, finv, fdis, fdisa, fref, fdpyw): """ Warp tracks from native space to the FMRIB58/MNI space We use here the fsl displacements. Have a look at create_displacements to see an example of how to use these displacements. Parameters ------------ fdpy : filename of the .dpy file with the tractography ffa : filename of nifti to be warped fmat : filename of .mat (flirt) fdis : filename of displacements (fnirtfileutils) fdisa : filename of displacements (fnirtfileutils + affine) finv : filename of invwarp displacements (invwarp) fref : filename of reference volume e.g. (FMRIB58_FA_1mm.nii.gz) fdpyw : filename of the warped tractography See also ----------- dipy.external.fsl.create_displacements """ # read the tracks from the image space dpr = Dpy(fdpy, 'r') T = dpr.read_tracks() dpr.close() # copy them in a new file dpw = Dpy(fdpyw, 'w', compression=1) dpw.write_tracks(T) dpw.close() # from fa index to ref index res = flirt2aff_files(fmat, ffa, fref) # load the reference img imgref = nib.load(fref) refaff = imgref.get_affine() # load the invwarp displacements imginvw = nib.load(finv) invwdata = imginvw.get_data() invwaff = imginvw.get_affine() # load the forward displacements imgdis = nib.load(fdis) disdata = imgdis.get_data() # load the forward displacements + affine imgdis2 = nib.load(fdisa) disdata2 = imgdis2.get_data() # from their difference create the affine disaff = disdata2 - disdata del disdata del disdata2 shape = nib.load(ffa).get_data().shape # transform the displacements affine back to image space disaff0 = affine_transform(disaff[..., 0], res[:3, :3], res[:3, 3], shape, order=1) disaff1 = affine_transform(disaff[..., 1], res[:3, :3], res[:3, 3], shape, order=1) disaff2 = affine_transform(disaff[..., 2], res[:3, :3], res[:3, 3], shape, order=1) # remove the transformed affine from the invwarp displacements di = invwdata[:, :, :, 0] + disaff0 dj = invwdata[:, :, :, 1] + disaff1 dk = invwdata[:, :, :, 2] + disaff2 dprw = Dpy(fdpyw, 'r+') rows = len(dprw.f.root.streamlines.tracks) blocks = np.round(np.linspace(0, rows, 10)).astype(int) # lets work in # blocks # print rows for i in range(len(blocks) - 1): # print blocks[i],blocks[i+1] # copy a lot of tracks together caboodle = dprw.f.root.streamlines.tracks[blocks[i]:blocks[i + 1]] mci = mc(di, caboodle.T, order=1) # interpolations for i displacement mcj = mc(dj, caboodle.T, order=1) # interpolations for j displacement mck = mc(dk, caboodle.T, order=1) # interpolations for k displacement D = np.vstack((mci, mcj, mck)).T # go back to mni image space WI2 = np.dot(caboodle, res[:3, :3].T) + res[:3, 3] + D # and then to mni world space caboodlew = np.dot(WI2, refaff[:3, :3].T) + refaff[:3, 3] # write back dprw.f.root.streamlines.tracks[blocks[i]:blocks[i + 1]] = ( caboodlew.astype('f4')) dprw.close()
def dpy(workingdir, input, compressed=None, restored=None, tol_error=0.01, force=False, coords_only=False, verbose=False): if not input.endswith('tck') and not input.endswith('trk'): # we need to convert print('Invalid format') return None if not compressed: compressed = input + '_compressed' + str(tol_error) + '.dpy' if not restored: restored = input + '_restored' + str(tol_error) + '.tck' original = os.path.join(workingdir, input) # # compression # c_time = -1 if not os.path.exists(os.path.join(workingdir, compressed)) or force: # compress again! t0 = time.time() loaded_original = nib.streamlines.load(original, lazy_load=False) original_streamlines = loaded_original.streamlines # parameters from Presseau15 are 0.01 and inf c_streamlines = compress_streamlines(original_streamlines, tol_error=tol_error, max_segment_length=np.inf) # write dpy file # set compression to highest but it does have any effect dpw = Dpy(os.path.join(workingdir, compressed), mode='w', compression=9) for c_s in c_streamlines: dpw.write_track(c_s) dpw.close() c_time = time.time() - t0 if verbose: print('compression done.') # # restoring # d_time = -1 if not os.path.exists(os.path.join(workingdir, restored)) or force: # restore again! t0 = time.time() restored_data = Dpy(os.path.join(workingdir, compressed), mode='r') restored_streamlines = restored_data.read_tracks() restored_data.close() d_time = time.time() - t0 with open(os.path.join(workingdir, restored), 'w') as f: f.write('restoredok.') if verbose: print('restoring done.') # # calculate errors # stats = compressed + '_stats' + str(tol_error) + '.p' if not os.path.exists(os.path.join(workingdir, stats)) or force: statsdata = Runner.error_per_streamlines(original_streamlines, restored_streamlines) sizestatsdata = Runner.sizestats( os.path.join(workingdir, original), os.path.join(workingdir, compressed)) statsdata = [ c_time, d_time, sizestatsdata, statsdata[0], statsdata[1] ] with open(os.path.join(workingdir, stats), 'wb') as f: pickle.dump(statsdata, f) else: with open(os.path.join(workingdir, stats), 'rb') as f: statsdata = pickle.load(f) [c_time, d_time, sizestatsdata, (min_e, max_e, mean_e, std_e), (end_min_e, end_max_e, end_mean_e, end_std_e)] = \ statsdata if verbose: print('Times', c_time, d_time) print('Size Stats', sizestatsdata) print('Error', min_e, max_e, mean_e, std_e) print('Endpoint Error', end_min_e, end_max_e, end_mean_e, end_std_e) return statsdata
#tracks_chosen_filename = 'ALS/ALS_Segmentation/s201_corticospinal_left_3M_Nivedita.pkl' #subj = 201 tracks_id=load_pickle(tracks_chosen_filename) root = Tkinter.Tk() root.wm_title('Subject Selection') subsel = SubjectSelector(root, default_value=1) root.wait_window() mapping = [0,101,201,102,202,103,203,104,204,105,205,106,206,107,207,109,208,110,209,111,210,112,212,113,213] subj = mapping[subsel.value] #load the tracks tracks_filename = 'ALS/ALS_Data/'+str(subj)+'/DIFF2DEPI_EKJ_64dirs_14/DTI/tracks_dti_'+str(num_seeds)+'M_linear.dpy' dpr_tracks = Dpy(tracks_filename, 'r') tensor_all_tracks=dpr_tracks.read_tracks(); dpr_tracks.close() T = [tensor_all_tracks[i] for i in tracks_id] #print len(T) print len(tracks_id) #load the volume img = nib.load('ALS/ALS_Data/'+str(subj)+'/MP_Rage_1x1x1_ND_3/T1_flirt_out.nii.gz') data = img.get_data() affine = img.get_affine() print len(data) #T = [t for t in T if length(t)>= 15] # T = [downsample(t, 18) - np.array(data.shape[:3]) / 2. for t in T]
dirname = "ALS/ALS_Data/101" for root, dirs, files in os.walk(dirname): if root.endswith('DTI'): #if root.endswith('101_32'): base_dir = root+'/' basedir_recon = os.getcwd() + '/' + base_dir + '/' #loading fa_image fa_file = basedir_recon + 'fa_warped.nii.gz' fa_img = nib.load(fa_file) fa = fa_img.get_data() fa[np.isnan(fa)] = 0 for i in range(len(tracks_filename_arr)): tracks_filename = basedir_recon + tracks_filename_arr[i] dpr_tracks = Dpy(tracks_filename, 'r') tensor_tracks_1=dpr_tracks.read_tracks(); dpr_tracks.close() tensor_tracks = T #end of moi them vao 20130604 #---------------------------------------------------------------------------- """ We can now save the results in the disk. For this purpose we can use the TrackVis format (*.trk). First, we need to create a header. """
do_simulated_annealing = True iterations_random = 10000 iterations_anneal = 1000 filename_1 = 'data/101_tracks_dti_10K.dpy' filename_2 = 'data/104_tracks_dti_10K.dpy' prototype_policies = ['random', 'fft', 'sff'] num_prototypes = 10 size1 = num_prototypes size2 = size1 + 20 print "Loading tracks." dpr_1 = Dpy(filename_1, 'r') tracks_1_all = dpr_1.read_tracks() dpr_1.close() tracks_1 = [] for st in tracks_1_all: if (length(st)>50): tracks_1.append(st) tracks_1 = np.array(tracks_1, dtype=np.object) dpr_2 = Dpy(filename_2, 'r') tracks_2_all = dpr_2.read_tracks() dpr_2.close() tracks_2 = [] for st in tracks_2_all:
def ok(self): self.value = self.s.get() self.parent.destroy() if __name__ == '__main__': #load T1 volume registered in MNI space #img = nib.load('data/subj_05/MPRAGE_32/T1_flirt_out.nii.gz') #data = img.get_data() #affine = img.get_affine() #load the tracks registered in MNI space fdpyw = 'data/subj_05/101_32/DTI/tracks_gqi_1M_linear.dpy' dpr = Dpy(fdpyw, 'r') T = dpr.read_tracks() dpr.close() #load initial QuickBundles with threshold 30mm fpkl = 'data/subj_05/101_32/DTI/qb_gqi_1M_linear_30.pkl' #qb=QuickBundles(T,30.,12) #save_pickle(fpkl,qb) qb = load_pickle(fpkl) #create the interaction system for tracks tl = TrackLabeler('Bundle Picker', qb, qb.downsampled_tracks(), vol_shape=(182, 218, 182), tracks_alpha=1) #add a interactive slicing/masking tool #sl = Slicer(affine,data)
from dipy.io.dpy import Dpy from dipy.tracking.distances import mam_distances, bundles_distances_mam from dipy.viz import fvtk if __name__ == '__main__': np.random.seed(0) filename = 'data/tracks_dti_10K_linear.dpy' dpr = Dpy(filename, 'r') tracks = dpr.read_tracks() dpr.close() tracks = np.array(tracks, dtype=np.object) size1 = 100 size2 = 100 tracks1 = tracks[np.random.permutation(len(tracks))[:size1]] tracks2 = tracks[np.random.permutation(len(tracks))[:size2]] # solution = np.random.permutation(len(tracks1)) # tracks2 = tracks1[solution] # inverse_solution = np.argsort(solution) dm1 = bundles_distances_mam(tracks1, tracks1) dm2 = bundles_distances_mam(tracks2, tracks2) print "For each s1 and for each s2 we reorder their distances and create a mapping from the first ordered ids to the second ordered ids" print "Then we compute the loss of this mapping" print "This approach is suboptimal but should provide a very good guess." idx2_best = None
if __name__ == '__main__': do_random_search = False#True do_simulated_annealing = True iterations_random = 10000 iterations_anneal = 1000 two_tractography = True if two_tractography == False: filename = 'data/tracks_dti_10K_linear.dpy' print "Loading", filename dpr = Dpy(filename, 'r') tractography = dpr.read_tracks() dpr.close() print len(tractography), "streamlines" print "Removing streamlines that are too short" tractography = filter(lambda x: len(x) > 20, tractography) # remove too short streamlines print len(tractography), "streamlines" tractography = np.array(tractography, dtype=np.object) size1 = 100#100 size2 = 100#100 print "Creating two simulated tractographies of sizes", size1, "and", size2 np.random.seed(1) # this is the random seed to create tractography1 and tractography2 ids1 = np.random.permutation(len(tractography))[:size1] # ids1 = sff(tractography, k=size1, distance=bundles_distances_mam) # ids1 = fft(tractography, k=size1, distance=bundles_distances_mam) tractography1 = tractography[ids1]
def warp_displacements_tracks(fdpy, ffa, fmat, finv, fdis, fdisa, fref, fdpyw): """ Warp tracks from native space to the FMRIB58/MNI space We use here the fsl displacements. Have a look at create_displacements to see an example of how to use these displacements. Parameters ------------ fdpy : filename of the .dpy file with the tractography ffa : filename of nifti to be warped fmat : filename of .mat (flirt) fdis : filename of displacements (fnirtfileutils) fdisa : filename of displacements (fnirtfileutils + affine) finv : filename of invwarp displacements (invwarp) fref : filename of reference volume e.g. (FMRIB58_FA_1mm.nii.gz) fdpyw : filename of the warped tractography See also ----------- dipy.external.fsl.create_displacements """ # read the tracks from the image space dpr = Dpy(fdpy, 'r') T = dpr.read_tracks() dpr.close() # copy them in a new file dpw = Dpy(fdpyw, 'w', compression=1) dpw.write_tracks(T) dpw.close() # from fa index to ref index res = flirt2aff_files(fmat, ffa, fref) # load the reference img imgref = nib.load(fref) refaff = imgref.affine # load the invwarp displacements imginvw = nib.load(finv) invwdata = imginvw.get_data() # load the forward displacements imgdis = nib.load(fdis) disdata = imgdis.get_data() # load the forward displacements + affine imgdis2 = nib.load(fdisa) disdata2 = imgdis2.get_data() # from their difference create the affine disaff = disdata2 - disdata del disdata del disdata2 shape = nib.load(ffa).get_data().shape # transform the displacements affine back to image space disaff0 = affine_transform(disaff[..., 0], res[:3, :3], res[:3, 3], shape, order=1) disaff1 = affine_transform(disaff[..., 1], res[:3, :3], res[:3, 3], shape, order=1) disaff2 = affine_transform(disaff[..., 2], res[:3, :3], res[:3, 3], shape, order=1) # remove the transformed affine from the invwarp displacements di = invwdata[:, :, :, 0] + disaff0 dj = invwdata[:, :, :, 1] + disaff1 dk = invwdata[:, :, :, 2] + disaff2 dprw = Dpy(fdpyw, 'r+') rows = len(dprw.f.root.streamlines.tracks) blocks = np.round(np.linspace(0, rows, 10)).astype(int) # lets work in # blocks # print rows for i in range(len(blocks) - 1): # print blocks[i],blocks[i+1] # copy a lot of tracks together caboodle = dprw.f.root.streamlines.tracks[blocks[i]:blocks[i + 1]] mci = mc(di, caboodle.T, order=1) # interpolations for i displacement mcj = mc(dj, caboodle.T, order=1) # interpolations for j displacement mck = mc(dk, caboodle.T, order=1) # interpolations for k displacement D = np.vstack((mci, mcj, mck)).T # go back to mni image space WI2 = np.dot(caboodle, res[:3, :3].T) + res[:3, 3] + D # and then to mni world space caboodlew = np.dot(WI2, refaff[:3, :3].T) + refaff[:3, 3] # write back dprw.f.root.streamlines.tracks[blocks[i]:blocks[i + 1]] = ( caboodlew.astype('f4')) dprw.close()
subsel = SubjectSelector(root, default_value=1) root.wait_window() mapping = [0,101,201,102,202,103,203,104,204,105,205,106,206,107,207,109,208,110,209,111,210,112,212,113,213] subj = mapping[subsel.value] #load the volume img = nib.load('ALS_Data/'+str(subj)+'/MP_Rage_1x1x1_ND_3/T1_flirt_out.nii.gz') data = img.get_data() affine = img.get_affine() print len(data) #load the tracks tracks_filename = 'ALS_Data/'+str(subj)+'/DIFF2DEPI_EKJ_64dirs_14/DTI/tracks_dti_'+str(num_seeds)+'M_linear.dpy' dpr_tracks = Dpy(tracks_filename, 'r') T = dpr_tracks.read_tracks(); dpr_tracks.close() #print len(T) print len(T) T = T[:200] T = [t for t in T if length(t)>= 15] # T = [downsample(t, 18) - np.array(data.shape[:3]) / 2. for t in T] # axis = np.array([1, 0, 0]) # theta = - 90. # T = np.dot(T,rotation_matrix(axis, theta)) # axis = np.array([0, 1, 0]) # theta = 180.
def load_tracks(filename): dpr = Dpy(filename, "r") tracks = dpr.read_tracks() dpr.close() return tracks
def load_tractogram(filename, reference, to_space=Space.RASMM, shifted_origin=False, bbox_valid_check=True, trk_header_check=True): """ Load the stateful tractogram from any format (trk, tck, vtk, fib, dpy) Parameters ---------- filename : string Filename with valid extension reference : Nifti or Trk filename, Nifti1Image or TrkFile, Nifti1Header or trk.header (dict), or 'same' if the input is a trk file. Reference that provides the spatial attribute. Typically a nifti-related object from the native diffusion used for streamlines generation to_space : Enum (dipy.io.stateful_tractogram.Space) Space to which the streamlines will be transformed after loading. shifted_origin : bool Information on the position of the origin, False is Trackvis standard, default (center of the voxel) True is NIFTI standard (corner of the voxel) bbox_valid_check : bool Verification for negative voxel coordinates or values above the volume dimensions. Default is True, to enforce valid file. trk_header_check : bool Verification that the reference has the same header as the spatial attributes as the input tractogram when a Trk is loaded Returns ------- output : StatefulTractogram The tractogram to load (must have been saved properly) """ _, extension = os.path.splitext(filename) if extension not in ['.trk', '.tck', '.vtk', '.fib', '.dpy']: logging.error('Output filename is not one of the supported format') return False if to_space not in Space: logging.error('Space MUST be one of the 3 choices (Enum)') return False if reference == 'same': if extension == '.trk': reference = filename else: logging.error('Reference must be provided, "same" is only ' + 'available for Trk file.') return False if trk_header_check and extension == '.trk': if not is_header_compatible(filename, reference): logging.error('Trk file header does not match the provided ' + 'reference') return False timer = time.time() data_per_point = None data_per_streamline = None if extension in ['.trk', '.tck']: tractogram_obj = nib.streamlines.load(filename).tractogram streamlines = tractogram_obj.streamlines if extension == '.trk': data_per_point = tractogram_obj.data_per_point data_per_streamline = tractogram_obj.data_per_streamline elif extension in ['.vtk', '.fib']: streamlines = load_vtk_streamlines(filename) elif extension in ['.dpy']: dpy_obj = Dpy(filename, mode='r') streamlines = list(dpy_obj.read_tracks()) dpy_obj.close() logging.debug('Load %s with %s streamlines in %s seconds', filename, len(streamlines), round(time.time() - timer, 3)) sft = StatefulTractogram(streamlines, reference, Space.RASMM, shifted_origin=shifted_origin, data_per_point=data_per_point, data_per_streamline=data_per_streamline) if to_space == Space.VOX: sft.to_vox() elif to_space == Space.VOXMM: sft.to_voxmm() if bbox_valid_check and not sft.is_bbox_in_vox_valid(): raise ValueError('Bounding box is not valid in voxel space, cannot ' + 'load a valid file if some coordinates are invalid.' + 'Please set bbox_valid_check to False and then use' + 'the function remove_invalid_streamlines to discard' + 'invalid streamlines.') return sft
filename = 'ALS_Data/101/DIFF2DEPI_EKJ_64dirs_14/DTI/tracks_dti_10K.dpy' prototype_policies = ['random', 'fft', 'sff'] color_policies = ['ko--', 'kx:', 'k^-'] elif figure=='big_dataset': filename = 'ALS_Data/101/DIFF2DEPI_EKJ_64dirs_14/DTI/tracks_dti_3M.dpy' prototype_policies = ['random', 'sff'] color_policies = ['ko--', 'k^-'] num_trks = [500,1000,5000,10000,15000,0] num_clusters = [50,150,250] num_prototypes = 40 print "Loading tracks." dpr = Dpy(filename, 'r') tracks_all = dpr.read_tracks() dpr.close() tracks_all = np.array(tracks_all, dtype=np.object) t_batch=0 t_batch_random=0 print "Num tracks \t num clusters \t btree \t dissimilarity \t minibatch (random) \t medoids" for i in range(len(num_trks)): ############################################################################## # define some parameters num_trk = num_trks[i] if num_trk!=0: tracks = tracks_all[:num_trk] else: tracks = tracks_all print "tracks:", tracks.size