#tractography2 = target_cst_ext[:num_pro*2]

print "Source", len(tractography1)
print "Target", len(tractography2)


#print "Computing the distance matrices for each tractography."
dm1 = bundles_distances_mam(tractography1, tractography1)
dm2 = bundles_distances_mam(tractography2, tractography2)

size1 = len(tractography1)
size2 = len(tractography2)

if vis:
    ren = fvtk.ren() 
    ren = visualize_tract(ren, tractography1, fvtk.yellow)
    ren = visualize_tract(ren, tractography2, fvtk.blue)
    fvtk.show(ren)
  

y_dm1 = dm1
x_dm2 = dm2
L = []
print 'Optimizing ...........................'    

max_nfe = 50000

#optimizing with SLSPQ  ---------------------
#-----------with bounds and (constrains OR NO constrains)
for t in np.arange(1):
    L = []
Example #2
0
visualize = False#True#True#False#True# False
print '---------------------------------------------------------------------------'
print 'Source \t Len \t Entropy '
for s_id in np.arange(len(source_ids)):
    
    source = str(source_ids[s_id])
    
    #tract_file = '/home/bao/Personal/PhD_at_Trento/Research/ALS_Nivedita_Bao/Code/Segmentation/ROI/' + source + '/cst_right.trk'        
    #tract = load_whole_tract_trk(tract_file) 

    
    #tracks_ind_file  = '/home/bao/tiensy/Tractography_Mapping/data/ROI_seg/CST_ROI_R_control/' + source + '_CST_ROI_R_3M.pkl'
    #tracks_ind_file  = '/home/bao/tiensy/Tractography_Mapping/data/ROI_seg/CST_ROI_L_control/' + source + '_CST_ROI_L_3M.pkl'
    #tracks_ind_file  = '/home/bao/tiensy/Tractography_Mapping/data/BOI_seg/' + source + '_corticospinal_R_3M.pkl'
    #tract_file = '/home/bao/tiensy/Tractography_Mapping/data/' + source + '_tracks_dti_3M.dpy'        
    #tract = load_tract(tract_file,tracks_ind_file)     
    
    tracks_ind_file  = '/home/bao/tiensy/Tractography_Mapping/data/trackvis_tractography/ROI_seg_tvis/ROI_seg_tvis_native/' + source + '_corticospinal_R_tvis.pkl'
    tract_file = '/home/bao/tiensy/Tractography_Mapping/data/trackvis_tractography/tvis_tractography/' + source + '_tracks_dti_tvis.trk'        
        
    tract = load_tract_trk(tract_file,tracks_ind_file)     
     
   
    entropy = Shannon_entropy(tract)    
    
    print source, '\t', len(tract), '\t ', entropy 
    
    if (visualize==True):
        ren = fvtk.ren()
        ren = visualize_tract(ren, tract, fvtk.yellow)
        fvtk.show(ren)
        
for s_id in np.arange(len(source_ids)):
    print '-----------------------------------------------------------------------------------------'
    print 'Source: ', source_ids[s_id]
    source = str(source_ids[s_id])

    flirt_filename = '/home/bao/Personal/PhD_at_Trento/Research/ALS_Nivedita_Bao/Code/data/' + source+'/DIFF2DEPI_EKJ_64dirs_14/DTI/flirt.mat'
    fa_filename= '/home/bao/Personal/PhD_at_Trento/Research/ALS_Nivedita_Bao/Segmentation_CST_francesca/' + source + '/DTI/dti_fa_brain.nii.gz' 
        
    tracks_filename = '/home/bao/Personal/PhD_at_Trento/Research/ALS_Nivedita_Bao/Segmentation_CST_francesca/' + source + '/DTI/dti.trk'
    linear_filename =  '/home/bao/Personal/PhD_at_Trento/Research/ALS_Nivedita_Bao/Segmentation_CST_francesca/' + source + '/DTI/dti_linear.dpy'        
    warp_tracks_linearly(flirt_filename,fa_filename, tracks_filename,linear_filename)
    print 'Saved ', linear_filename
    tract_linear = load_whole_tract(linear_filename)
    if save:
        from common_functions import save_tract_trk
        fa_warped = '/home/bao/Personal/PhD_at_Trento/Research/ALS_Nivedita_Bao/Code/data/' + source+'/DIFF2DEPI_EKJ_64dirs_14/DTI/fa_warped.nii.gz'     
        fname_out = '/home/bao/Personal/PhD_at_Trento/Research/ALS_Nivedita_Bao/Segmentation_CST_francesca/' + source + '/DTI/dti_linear.trk'        
                
        save_tract_trk(tract_linear, fa_warped, fname_out )
        print 'Saved ', fname_out
    if vis:
        tract = load_whole_tract(tracks_filename)        
        from dipy.viz import fvtk
        from common_functions import visualize_tract
        ren = fvtk.ren()
        visualize_tract(ren, tract,color=fvtk.red)
        visualize_tract(ren, tract_linear,color=fvtk.blue)
        fvtk.show(ren)
    clearall()
        
            
                #Step 4: Segment the cst_s in the target using nearest neighbor of each fiber_source 
                #        in the space of the kdt_t_ext - result is nn_cst_s_in_t
        
                k = 1         
                dst_nn, idx_nn = kdt_t_ext.query(dis_s, k)

                #print 'Distance'
                #print dst_nn
                #print 'Index '
                #print idx_nn                
                
                mapped_s_cst = [t_cst_ext[idx_nn[i][0]] for i in np.arange(cst_len)]
                
                #Step 5: Compute JAC and BFN between cst_t and nn_cst_s_in_t
                
                jac0, bfn0 = Jac_BFN(s_cst, t_cst, vol_dims, disp=False)
                jac1, bfn1 = Jac_BFN(mapped_s_cst, t_cst, vol_dims, disp=False)
                
                print "\t\t", target_ids[t_id], "\t", jac0,"\t",  bfn0, "\t", jac1,"\t",  bfn1
                print "Before mapping: ", jac0, bfn0
                print "After mapping: ", jac1, bfn1
               
                if vis:
                   #visualize target cst and mapped source cst - yellow and blue
                    ren = fvtk.ren()                
                    ren = visualize_tract(ren, s_cst, fvtk.yellow)
                    ren = visualize_tract(ren, t_cst, fvtk.blue)
                    ren = visualize_tract(ren, mapped_s_cst, fvtk.red)
                    fvtk.show(ren)
                    
Example #5
0
    ROIs = ROIs_subject[source]    
    print ROIs

    #common = intersec_ROIs_dpy(tracks, ROIs, Rs, vis = True)
    common = intersec_ROIs(tracks, ROIs, Rs, vis = True)

    print "\t Total ", len(tracks), " and  the number of fibers cross the ROIs ", len(common)
    
    tracks_ind_file  = '/home/bao/tiensy/Tractography_Mapping/data/trackvis_tractography/ROI_seg_tvis_MNI/' + source + '_corticospinal_L_tvis_MNI.pkl'
    save_pickle(tracks_ind_file, common)
    print "Saved file", tracks_ind_file
    
    segment = [tracks[i] for i  in common]
    '''
    if save==True:
        fa_file = '/home/bao/Personal/PhD_at_Trento/Research/ALS_Nivedita_Bao/Segmentation_CST_francesca/' + source + '/DTI/dti_fa.nii'     
        fname = '/home/bao/Personal/PhD_at_Trento/Research/ALS_Nivedita_Bao/Code/Segmentation/ROI/' + source + '/' + source + '_cst_right_ROI_tvis_native.trk'
        from common_functions import save_tract_trk
        save_tract_trk(segment, fa_file, fname)
        print 'Saved ', fname
    '''
    if (visualize==True):
        ren = fvtk.ren()
        ren = visualize_tract(ren, segment, fvtk.yellow)
        fvtk.add(ren, fvtk.sphere(ROIs[0],Rs[0],color = fvtk.red, opacity=1.0)) 
        fvtk.add(ren, fvtk.sphere(ROIs[1],Rs[1],color = fvtk.blue, opacity=1.0)) 
        fvtk.show(ren)
    #clearall() 
    
    
          
Example #6
0
         #t_tracts = '/home/bao/tiensy/Tractography_Mapping/data/' + target + '_tracks_dti_3M.dpy'
         t_idx = '/home/bao/tiensy/Tractography_Mapping/data/' + target + '_corticospinal_L_3M.pkl'            
         #t_cst = load_tract_trk(t_tracts,t_idx)      
         t_cst = load_tract(t_tracts,t_idx) 
         
         '''
         dm = bundles_distances_mam(s_cst, t_cst)
         dm = np.array(dm, dtype=float)
         avg_dis = np.sum(dm) / (len(s_cst)*len(t_cst))
         min_dis = dm.min()
         max_dis = dm.max()           
             
         s_qb = QuickBundles(s_cst,200,18)
 
         s_medoid = s_qb.centroids[0]
         
         t_qb = QuickBundles(t_cst,200,18)
 
         t_medoid = t_qb.centroids[0]
         
         #print len(s_cst), '\t', len(t_cst), '\t', min_dis, '\t', max_dis, '\t', avg_dis, '\t', bundles_distances_mam([s_medoid],[t_medoid])[0][0]
         print min_dis, '\t', max_dis, '\t', avg_dis, '\t', bundles_distances_mam([s_medoid],[t_medoid])[0][0]
         '''
         
         if (vis==True):
             ren = fvtk.ren()       
             ren = visualize_tract(ren,s_cst,fvtk.red)                
             ren = visualize_tract(ren, t_cst, fvtk.blue)
             fvtk.show(ren)
     else:
         print '  '