Beispiel #1
0
def skeletonize_both():
    from dipy.viz import fos
    from dipy.core.track_metrics import downsample
    from dipy.core.track_performance import local_skeleton_clustering, most_similar_track_mam
    
    froi='/home/eg309/Data/ICBM_Wmpm/ICBM_WMPM.nii'
    wI=get_roi(froi,9,0) #4 is genu    
    fname='/home/eg309/Data/PROC_MR10032/subj_03/101/1312211075232351192010092217244332311282470ep2dadvdiffDSI10125x25x25STs004a001_QA_warp.dpy'
    #fname='/home/eg309/Data/PROC_MR10032/subj_03/101/1312211075232351192010092217244332311282470ep2dadvdiffDSI10125x25x25STs004a001_QA_native.dpy'
    #fname='/home/eg309/Data/PROC_MR10032/subj_06/101/13122110752323511930000010092916083910900000227ep2dadvdiffDSI10125x25x25STs004a001_QA_native.dpy'
    fname2='/home/eg309/Data/PROC_MR10032/subj_06/101/13122110752323511930000010092916083910900000227ep2dadvdiffDSI10125x25x25STs004a001_QA_warp.dpy'
    r=fos.ren()
    #'''
    dpr=Dpy(fname,'r')    
    T=dpr.read_indexed(range(2*10**4))
    dpr.close()    
    print len(T)    
    Td=[downsample(t,3) for t in T if length(t)>40]
    C=local_skeleton_clustering(Td,d_thr=20.)
    
    for c in C:
        #color=np.random.rand(3)
        color=fos.red
        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']                
            fos.add(r,fos.line(Ttmp[si],color))                

    dpr=Dpy(fname2,'r')    
    T=dpr.read_indexed(range(2*10**4))
    dpr.close()    
    print len(T)    
    Td=[downsample(t,3) for t in T if length(t)>40]
    C=local_skeleton_clustering(Td,d_thr=20.)
    #r=fos.ren()
    for c in C:
        #color=np.random.rand(3)
        color=fos.yellow
        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']                
            fos.add(r,fos.line(Ttmp[si],color))            
    #'''
    fos.add(r,fos.point(wI,fos.green))
    fos.show(r)
Beispiel #2
0
def test_picking_trajectories():

    curves=[100*np.random.rand(10,3),100*np.random.rand(5,3),100*np.random.rand(3,3)]
    curves=[100*np.array([[0,0,0],[1,0,0]]), 100*np.array([[0,1,0],[0,1,3]]),100*np.array([[0,2,0],[0,2,3]])]
    
    '''
    from nibabel import trackvis as tv
    #fname='/home/eg309/Data/PROC_MR10032/subj_01/101/1312211075232351192010091419011391228126452ep2dadvdiffDSI25x25x25b4000s003a001_FA_warp.trk'
    fname='/home/eg309/Data/fibers.trk'
    streams,hdr=tv.read(fname)
    T=[s[0] for s in streams]
    curves=T[:200000]
    '''
    fname='/home/eg309/Data/PROC_MR10032/subj_02/101/1312211075232351192010091708112071055601107ep2dadvdiffDSI10125x25x25STs002a001_QA_native.dpy'
    from dipy.io.dpy import Dpy
    dpr=Dpy(fname,'r')
    T=dpr.read_indexed(range(20000))
    
    from dipy.core.track_metrics import length
    curves=[t for t in T if length(t) > 20]
    
    dpr.close()
    #colors=np.random.rand(len(curves),4).astype('f4')
    colors=0.5*np.ones((len(curves),4)).astype('f4')
    for (i,c) in enumerate(curves):        
        orient=c[0]-c[-1]
        orient=np.abs(orient/np.linalg.norm(orient))
        colors[i,:3]=orient
    
    c=InteractiveCurves(curves,colors=colors)
    w=World()
    w.add(c)
    wi=Window()
    wi.attach(w)
Beispiel #3
0
def sums_length(dname,type='64'):
        for root, dirs, files in os.walk(dname):
            if root.endswith(type):                    
                for file in files:
                    if file.endswith('_warp.dpy'):       
                        fname=os.path.join(root,file)                    
                        dpr=Dpy(fname,'r')
                        sum=0
                        for i in range(dpr.track_no):
                            sum+=length(dpr.read_track())
                        dpr.close()
                        print fname, sum               
Beispiel #4
0
def generate_lengths():
    for root, dirs, files in os.walk(dname):          
                for file in files:
                    if file.endswith('_warp.dpy'):       
                        fname=os.path.join(root,file)                    
                        dpr=Dpy(fname,'r')
                        lengths=np.zeros((dpr.track_no,))
                        for i in range(dpr.track_no):
                            lengths[i]=length(dpr.read_track())
                        dpr.close()
                        fname2=fname.split('_warp.dpy')[0]+'_warp_lengths.npy'
                        print fname2
                        np.save(fname2,lengths)
Beispiel #5
0
    def init(self):

        import dipy.io.trackvis as tv

        lines,hdr = tv.read(self.fname)

        ras = tv.aff_from_hdr(hdr)

        self.affine=ras

        tracks = [l[0] for l in lines]

        if self.yellow_indices != None :

            tracks = [t for t in tracks if tm.length(t) > 20]

        print 'tracks loaded'

        #self.data = [100*np.array([[0,0,0],[1,0,0],[2,0,0]]).astype(np.float32) ,100*np.array([[0,1,0],[0,2,0],[0,3,0]]).astype(np.float32)]#tracks[:20000]

        if self.dummy_data:

            self.data = [100*np.array([[0,0,0],[1,0,0],[2,0,0]]).astype(np.float32) ,100*np.array([[0,1,0],[0,2,0],[0,3,0]]).astype(np.float32)]

        if self.data_subset!=None:

            self.data = tracks[self.data_subset[0]:self.data_subset[1]]

        else:

            self.data = tracks

        data_stats = np.concatenate(tracks)

        self.min=np.min(data_stats,axis=0)
         
        self.max=np.max(data_stats,axis=0)

        self.mean=np.mean(data_stats,axis=0)

        del data_stats
        
        del lines

        self.multiple_colors()
Beispiel #6
0
    def __init__(self,fname,colormap=None, line_width=1., shrink=None, thinning = 0,
                 angle_table = None, manycolors = False, brain_color=[1,1,1]):

        self.position = (0,0,0)

        self.fname = fname
        
        self.manycolors = manycolors

        #self.color = monocolor
        
        self.bbox = None

        self.list_index = None

        self.affine = None

        self.data = None

        self.list_index = None

        self.rot_angle = 0

        self.colormap = None
                
        self.min = None
         
        self.max = None

        self.mean = None

        self.material_color = False

        self.fadeout = False

        self.fadein = False

        self.fadeout_speed = 0.

        self.fadein_speed = 0.

        self.min_length = 20.

        self.angle = 0.

        self.angular_speed = .5

        self.line_width = line_width

        self.opacity = 1.

        self.opacity_rate = 0.

        self.near_pick = None

        self.far_pick = None

        self.near_pick_prev = None

        self.far_pick_prev = None

        self.picked_track = None

        self.pick_color = [1,1,0]

        #self.brain_color = [1,1,1] # white
        #self.brain_color = [.941,.862,.510] # buff
        self.brain_color = brain_color
        
        self.yellow_indices = None

        self.dummy_data = False

        self.data_subset = [0,20000]#None


        self.orbit_demo = False          

        self.orbit_anglez =0.

        self.orbit_anglez_rate = 10.
        

        self.orbit_anglex = 0.

        self.orbit_anglex_rate = 2.


        self.angle_table = angle_table

        if angle_table != None:
            print 'Tracks angle_table shape %s' % str(self.angle_table.shape)

        self.angle_table_index = 0

        #print 'angle_table_index %d' % self.angle_table_index

        self.shrink = shrink

        self.picking_example = False

        import dipy.io.trackvis as tv

        lines,hdr = tv.read(self.fname)

        ras = tv.aff_from_hdr(hdr
)
        self.affine=ras

        tracks = [l[0] for l in lines]

        print 'tracks %d loaded' % len(tracks)

        self.thinning = thinning

        if self.yellow_indices != None :

            tracks = [t for t in tracks if tm.length(t) > 20]

        if self.thinning != 0:

            tracks = [tracks[k] for k in range(0,len(tracks),self.thinning)]

        print '%d tracks active' % len(tracks)

        #self.data = [100*np.array([[0,0,0],[1,0,0],[2,0,0]]).astype(np.float32) ,100*np.array([[0,1,0],[0,2,0],[0,3,0]]).astype(np.float32)]#tracks[:20000]

        if self.dummy_data:

            self.data = [100*np.array([[0,0,0],[1,0,0],[2,0,0]]).astype(np.float32) ,100*np.array([[0,1,0],[0,2,0],[0,3,0]]).astype(np.float32)]

        if self.data_subset!=None:

            self.data = tracks[self.data_subset[0]:self.data_subset[1]]

        else:

            self.data = tracks

        if self.shrink != None:

            self.data = [ self.shrink*t  for t in self.data]
            

        data_stats = np.concatenate(tracks)

        self.min=np.min(data_stats,axis=0)
         
        self.max=np.max(data_stats,axis=0)

        self.mean=np.mean(data_stats,axis=0)

        del data_stats
        
        del lines
Beispiel #7
0
tracks2=[t[0] for t in tracks2]
tracks3=[t[0] for t in tracks3]

print 'Before thresholding'

print len(tracks1)
print len(tracks2)
print len(tracks3)

print hdr1['dim']
print hdr2['dim']
print hdr3['dim']

#Apply thresholds

tracks1=[t for t in tracks1 if tm.length(t) > min_len]
tracks2=[t for t in tracks2 if tm.length(t) > min_len]
tracks3=[t for t in tracks3 if tm.length(t) > min_len]

print 'After thresholding'

print len(tracks1)
print len(tracks2)
print len(tracks3)

print 'Downsampling'

tracks1z=[tm.downsample(t,down) for t in tracks1] 
tracks2z=[tm.downsample(t,down) for t in tracks2] 
tracks3z=[tm.downsample(t,down) for t in tracks3] 
    color=np.random.rand(3)    
    r,g,b = color
    bundle=[]

    for i in C[c]['indices']:
        
        colors2[i]=np.tile(np.array([r,g,b,opacity],'f'),(len(data[i]),1))    
        bundle.append(data[i])

    
    bi=pf.most_similar_track_mam(bundle)[0]
    C[c]['skeletal']=bundle[bi]
    

    if len(C[c]['indices'])>100 and tm.length(bundle[bi])>30.:        
        colors_sk.append( np.tile(np.array([r,g,b,opacity],'f'),(len(bundle[bi]),1)) )
        skeletals.append(bundle[bi])

        

print 'len_data', len(data)
print 'len_skeletals', len(skeletals)
print 'len_colors2', len(colors2)
print 'len_colors_sk', len(colors_sk)
    
t2=Tracks(data,colors2,line_width=1.)
t2.position=(100,0,0)

sk=Tracks(skeletals,colors_sk,line_width=3.)
sk.position=(0,0,0)
for c in C:

    color = np.random.rand(3)
    r, g, b = color
    bundle = []

    for i in C[c]['indices']:

        colors2[i] = np.tile(np.array([r, g, b, opacity], 'f'),
                             (len(data[i]), 1))
        bundle.append(data[i])

    bi = pf.most_similar_track_mam(bundle)[0]
    C[c]['skeletal'] = bundle[bi]

    if len(C[c]['indices']) > 100 and tm.length(bundle[bi]) > 30.:
        colors_sk.append(
            np.tile(np.array([r, g, b, opacity], 'f'), (len(bundle[bi]), 1)))
        skeletals.append(bundle[bi])

print 'len_data', len(data)
print 'len_skeletals', len(skeletals)
print 'len_colors2', len(colors2)
print 'len_colors_sk', len(colors_sk)

t2 = Tracks(data, colors2, line_width=1.)
t2.position = (100, 0, 0)

sk = Tracks(skeletals, colors_sk, line_width=3.)
sk.position = (0, 0, 0)
Beispiel #10
0
dpr=Dpy(ftrack,'r')

print dpr.track_no

T=dpr.read_indexed([0,1,2,3,2000,1000000])

for t in T:
    print t.shape
dpr.close()
track=T[4]
im2im = flirt2aff_files(flirtaff, ffa, ref_fname) #ref_name to be replaced by ffareg
print im2im
from dipy.core.track_metrics import length
print len(track)
print length(track)
#ntrack=np.dot(im2im[:3,:3],track.T)+im2im[:3,[3]]
ntrack=np.dot(track,im2im[:3,:3].T)+im2im[:3,3]
print length(ntrack)
#print length(ntrack.T)
print length(ntrack)/length(track)
#print npl.det(im2im)**(1/3.)
disimg=nib.load(fdis)
ddata=disimg.get_data()
daff=disimg.affine

from scipy.ndimage.interpolation import map_coordinates as mc
di=ddata[:,:,:,0]
dj=ddata[:,:,:,1]
dk=ddata[:,:,:,2]
mci=mc(di,ntrack.T)
Beispiel #11
0
    def init(self, given_tracks=None):


        global b1

        global b2

        #devel06

        #b1_fname='/home/ian/Data/dipy/brain2_scan1_fiber_track_mni.trk'

        #devel07

        b1_fname='/home/eg01/Data_Backup/Data/PBC/pbc2009icdm/brain2/brain2_scan1_fiber_track_mni.trk'

         
        
        b1=tracks.TracksModified(b1_fname,subset=[0,1000])

        b1.angular_speed = 0.

        b1.picking_example = True

        b1.min_length = 15.

        b1.opacity = 0.1

        b1.manycolors = False

        b1.brain_color = [1, 1, 1]

        b1.init()


        global texim


        #devel06

        #fname = '/home/ian/Data/dipy/Streaks4.bmp'

        #devel07

        fname = '/home/eg01/Devel/Fos/fos/core/tests/data/Streaks4.bmp'

        texim = texture.Texture_Demo(fname,red=False,green=True, blue=False)

        #texim.orbit = b1.data[4246]


        number_of_spritetracks = 30
        random_inx=np.floor(len(b1.data)*np.random.rand(number_of_spritetracks)).astype(np.int)
        systematic_inx = range(0,len(b1.data),len(b1.data)/number_of_spritetracks)

        #texim.orbits = [b1.data[4246],b1.data[3000],b1.data[2000],b1.data[1000]]

        texim.orbits =[]

        #indices = random_inx
        indices = systematic_inx
        
        for i in indices:

            #print i

            if tm.length(b1.data[i]) > 20.:

                #print i

                texim.orbits.append(b1.data[i])

        global b2


        b2=tracks.TracksModified(None,line_width=2,tracks=[b1.data[i] for i in indices],colormap=False,text='M')

        b2.angular_speed = 0.

        b2.picking_example = True

        b2.min_length = 15.

        b2.opacity = 0.9

        b2.manycolors = False

        b2.brain_color = [0.2, 0.8, 0.4]


        b2.init()
        

        texim.orbits_index = np.zeros((len(texim.orbits),),np.int)
        
        texim.init()

        #'''

        self.slots={0:{'actor':texim,'slot':( 0*MS, 55*MS )},
                    1:{'actor':b1,'slot':(0*MS, 40*MS)},
                    2:{'actor':b2,'slot':(35*MS, 200*MS)}}

        #'''

        '''
Beispiel #12
0
    def init(self):


        br1path='/home/eg01/Data_Backup/Data/Eleftherios/CBU090133_METHODS/20090227_145404/Series_003_CBU_DTI_64D_iso_1000/dtk_dti_out/dti_FACT.trk'

        br2path='/home/eg01/Data_Backup/Data/Eleftherios/CBU090134_METHODS/20090227_154122/Series_003_CBU_DTI_64D_iso_1000/dtk_dti_out/dti_FACT.trk'

        br3path='/home/eg01/Data_Backup/Data/Eleftherios/CBU090133_METHODS/20090227_145404/Series_003_CBU_DTI_64D_iso_1000/dtk_dti_out/dti_RK2.trk'


        min_len=20

        down=20

        rand_tracks=-1 #default 10 min_search_len=70
        
        min_search_len=70
        
        max_search_len=140


        corr_mat_demo=np.array([[ 1, 10560,  3609],[ 2, 17872, 15377],[ 3,  6447,  3897], [4, 18854,  6409], [ 5, 14416,  4515], [ 7,  9956, 13913], [8, 10853, 15572], [ 9, 13280,  8461], [ 0, 11275,  9224]])


        print 'Minimum track length', min_len, 'mm'
        print 'Number of segments for downsampling',down
        print 'Number of tracks for detection',rand_tracks
        print 'Minimum searched track length', min_search_len, 'mm'
        print 'Maximum searched track length', max_search_len, 'mm'

        tracks1,hdr1=tv.read(br1path)

        tracks2,hdr2=tv.read(br2path)

        tracks3,hdr3=tv.read(br3path)

        #Load only track points, no scalars or parameters.

        tracks1=[t[0] for t in tracks1]

        tracks2=[t[0] for t in tracks2]

        tracks3=[t[0] for t in tracks3]

        print 'Before thresholding'

        print len(tracks1)

        print len(tracks2)

        print len(tracks3)

        print hdr1['dim']

        print hdr2['dim']

        print hdr3['dim']

        #Apply thresholds

        tracks1=[t for t in tracks1 if tm.length(t) > min_len]

        tracks2=[t for t in tracks2 if tm.length(t) > min_len]

        tracks3=[t for t in tracks3 if tm.length(t) > min_len]

        print 'After thresholding'

        print len(tracks1)
        print len(tracks2)
        print len(tracks3)

        print 'Downsampling'

        tracks1z=[tm.downsample(t,down) for t in tracks1]

        tracks2z=[tm.downsample(t,down) for t in tracks2]

        tracks3z=[tm.downsample(t,down) for t in tracks3] 

        print 'Detecting random tracks'

        lt1=len(tracks1)

        lt2=len(tracks2)

        lt3=len(tracks3)

        if rand_tracks==-1:
            
	    #use already stored indices
            t_ind=corr_mat_demo[:,1]
            
				
	t_ind=np.array(t_ind)

        print 'Indices of tracks for detection', t_ind

        print 'Finding corresponding tracks'

        global track2track        
        track2track= self.corresponding_tracks(t_ind,tracks1z,tracks2z)

        global track2track2
        track2track2=self.corresponding_tracks(t_ind,tracks1z,tracks3z)

        print 'First Correspondance Matrix'
        print track2track

        print 'Second Correspondance Matrix'
        print track2track2

        print 'first brain'
        print track2track[:,1].T

        print 'second brain'
        print track2track[:,2].T

        print 'third brain'
        print track2track2[:,2].T


        #fos.add(r,fos.line(tracks1,fos.red,opacity=0.01))
        #fos.add(r,fos.line(tracks2,fos.cyan,opacity=0.01))

        tracks1zshift = tracks1z
        tracks2zshift = tracks2z
        tracks3zshift = tracks3z

        m1z=np.concatenate(tracks1zshift).mean(axis=0)
        
        m2z=np.concatenate(tracks2zshift).mean(axis=0)

        m3z=np.concatenate(tracks3zshift).mean(axis=0)

        #tracks1zshift=[t+np.array([-70,0,0]) for t in tracks1z]

        #tracks2zshift=[t+np.array([70,0,0]) for t in tracks2z]

        tracks1zshift=[t-m1z for t in tracks1z]
        
        tracks2zshift=[t-m2z for t in tracks2z]
          
        tracks3zshift=[t-m3z for t in tracks3z]


        global t1

        #devel07

        t1=tracks.Tracks(None,data_ext=tracks1zshift)

        t1.angular_speed = 0.1

        t1.brain_color=[1,0,0]

        t1.manycolors=False

        t1.opacity = 0.01

        t1.orbit_demo=True

        t1.orbit_anglez_rate = 0.
                
        t1.orbit_anglex_rate = 0.

        t1.orbit_angley_rate = .2

        

        t1.init()

        t1.position = np.array([-120,0,-30])

        print 't1p',t1.position

        

        global t2

        #devel07

        t2=tracks.Tracks(None,data_ext=tracks2zshift)

        t2.angular_speed = 0.1

        t2.brain_color=[0,1,1]

        t2.manycolors=False

        t2.opacity = 0.01

        t2.orbit_demo=True

        t2.orbit_anglez_rate = 0.
                
        t2.orbit_anglex_rate = 0.

        t2.orbit_angley_rate = .2

        t2.init()
        
        t2.position = np.array([0,0,-30])

        print 't2p', t2.position
        
        

        global t3

        #devel07

        t3=tracks.Tracks(None,data_ext=tracks3zshift)

        t3.angular_speed = 0.1

        t3.manycolors=False

        t3.brain_color=[0,0,1]

        t3.opacity = 0.01

        t3.orbit_demo=True

        t3.orbit_anglez_rate = 0.
                
        t3.orbit_anglex_rate = 0.

        t3.orbit_angley_rate = .2        

        t3.init()
        
        #t3.position = -
        #np.concatenate(tracks3zshift).mean(axis=0)+np.array([70,0,0])

        t3.position = np.array([120,0,-30])

        print 't3p', t3.position
        
        

        self.slots={0:{'actor':t1,'slot':( 0, 800*MS ) },
                    1:{'actor':t2,'slot':( 0, 800*MS ) },
                    2:{'actor':t3,'slot':( 0, 800*MS ) }}
Beispiel #13
0
    def init(self):


        global b1

        #devel06

        b1_fname='/home/ian/Data/dipy/brain2_scan1_fiber_track_mni.trk'

        #devel07

        #b1_fname='/home/eg01/Data_Backup/Data/PBC/pbc2009icdm/brain2/brain2_scan1_fiber_track_mni.trk'

         
        
        b1=tracks.Tracks(b1_fname,subset=[0,200])

        b1.angular_speed = 0.

        b1.picking_example = True

        b1.min_length = 20.

        b1.opacity = 0.8

        b1.manycolors = False

        b1.brain_color = [0, 0, 0]


        b1.init()




        global texim


        #devel06

        fname = '/home/ian/Data/dipy/Streaks4.bmp'

        #devel07

        #fname = '/home/eg01/Devel/Fos/fos/core/tests/data/Streaks4.bmp'

        texim = texture.Texture_Demo(fname,red=False,green=False, blue=True)

        #texim.orbit = b1.data[4246]

        #print 'len(b1.data)', len(b1.data)

        random_inx=np.trunc(len(b1.data)*np.random.rand(200)).astype(np.int)
        #print random_inx

        #texim.orbits = [b1.data[4246],b1.data[3000],b1.data[2000],b1.data[1000]]

        texim.orbits =[]
        
        for i in random_inx:

            #print i

            if tm.length(b1.data[i]) > 20.:

                #print i

                texim.orbits.append(b1.data[i])

                
        

        texim.orbits_index = np.zeros((len(texim.orbits),),np.int)
        
        texim.init()

        self.slots={0:{'actor':texim,'slot':( 0, 800*MS )}}#,
Beispiel #14
0
dpr=Dpy(ftrack,'r')

print dpr.track_no

T=dpr.read_indexed([0,1,2,3,2000,1000000])

for t in T:
    print t.shape
dpr.close()
track=T[4]
im2im = flirt2aff_files(flirtaff, ffa, ref_fname) #ref_name to be replaced by ffareg
print im2im
from dipy.core.track_metrics import length
print len(track)
print length(track)
#ntrack=np.dot(im2im[:3,:3],track.T)+im2im[:3,[3]]
ntrack=np.dot(track,im2im[:3,:3].T)+im2im[:3,3]
print length(ntrack)
#print length(ntrack.T)
print length(ntrack)/length(track)
#print npl.det(im2im)**(1/3.)
disimg=nib.load(fdis)
ddata=disimg.get_data()
daff=disimg.affine

from scipy.ndimage.interpolation import map_coordinates as mc
di=ddata[:,:,:,0]
dj=ddata[:,:,:,1]
dk=ddata[:,:,:,2]
mci=mc(di,ntrack.T)
Beispiel #15
0
    def multiple_colors(self):

        from dipy.viz.colormaps import boys2rgb

        from dipy.core.track_metrics import mean_orientation, length, downsample

        colors=np.random.rand(1,3).astype(np.float32)

        print colors

        self.list_index = gl.glGenLists(1)

        gl.glNewList( self.list_index,gl.GL_COMPILE)

        gl.glPushMatrix()

        gl.glDisable(gl.GL_LIGHTING)
        
        gl.glEnable(gl.GL_LINE_SMOOTH)

        gl.glEnable(gl.GL_BLEND)

        gl.glBlendFunc(gl.GL_SRC_ALPHA,gl.GL_ONE_MINUS_SRC_ALPHA)

        #gl.glBlendFunc(gl.GL_SRC_ALPHA_SATURATE,gl.GL_ONE_MINUS_SRC_ALPHA)
        
        #gl.glBlendFunc(gl.GL_SRC_ALPHA,gl.GL_ONE)

        gl.glHint(gl.GL_LINE_SMOOTH_HINT,gl.GL_DONT_CARE)

        #gl.glHint(gl.GL_LINE_SMOOTH_HINT,gl.GL_NICEST)

        gl.glLineWidth(self.line_width)

        #gl.glDepthMask(gl.GL_FALSE)


        gl.glEnableClientState(gl.GL_VERTEX_ARRAY)        

        for d in self.data:

            if length(d)> self.min_length:
            
                #mo=mean_orientation(d)

                if self.manycolors:
                
                    ds=downsample(d,6)

                    mo=ds[3]-ds[2]

                    mo=mo/np.sqrt(np.sum(mo**2))

                    mo.shape=(1,3)
            
                    color=boys2rgb(mo)

                    color4=np.array([color[0][0],color[0][1],color[0][2],self.opacity],np.float32)
                    gl.glColor4fv(color4)

                else:

                    color4=np.array([self.brain_color[0],self.brain_color[1],\
                                         self.brain_color[2],self.opacity],np.float32)

                    gl.glColor4fv(color4)
                    

                gl.glVertexPointerf(d)
                               
                gl.glDrawArrays(gl.GL_LINE_STRIP, 0, len(d))

        

        gl.glDisableClientState(gl.GL_VERTEX_ARRAY)


        #gl.glDisable(gl.GL_BLEND)
        
        gl.glEnable(gl.GL_LIGHTING)
        
        gl.glPopMatrix()

        gl.glEndList()
Beispiel #16
0
    def multiple_colors(self):

        from dipy.viz.colormaps import boys2rgb

        from dipy.core.track_metrics import mean_orientation, length, downsample

        colors=np.random.rand(1,3).astype(np.float32)

        print colors

        self.list_index = gl.glGenLists(1)

        gl.glNewList( self.list_index,gl.GL_COMPILE)

        #gl.glPushMatrix()

        gl.glDisable(gl.GL_LIGHTING)
        
        #!!!gl.glEnable(gl.GL_LINE_SMOOTH)

        gl.glDisable(gl.GL_DEPTH_TEST)

        #gl.glDepthFunc(gl.GL_NEVER)

        gl.glEnable(gl.GL_BLEND)

        gl.glBlendFunc(gl.GL_SRC_ALPHA,gl.GL_ONE_MINUS_SRC_ALPHA)

        #gl.glBlendFunc(gl.GL_SRC_ALPHA_SATURATE,gl.GL_ONE_MINUS_SRC_ALPHA)
        
        #gl.glBlendFunc(gl.GL_SRC_ALPHA,gl.GL_ONE)

        #!!!gl.glHint(gl.GL_LINE_SMOOTH_HINT,gl.GL_DONT_CARE)

        #gl.glHint(gl.GL_LINE_SMOOTH_HINT,gl.GL_NICEST)

        gl.glLineWidth(self.line_width)

        #gl.glDepthMask(gl.GL_FALSE)


        gl.glEnableClientState(gl.GL_VERTEX_ARRAY)        

        for d in self.data:

            if length(d)> self.min_length:
            
                #mo=mean_orientation(d)

                if self.manycolors:
                
                    ds=downsample(d,6)

                    mo=ds[3]-ds[2]

                    mo=mo/np.sqrt(np.sum(mo**2))

                    mo.shape=(1,3)
            
                    color=boys2rgb(mo)

                    color4=np.array([color[0][0],color[0][1],color[0][2],self.opacity],np.float32)
                    

                else:

                    color4=np.array([self.brain_color[0],self.brain_color[1],\
                                         self.brain_color[2],self.opacity],np.float32)


                if self.fadein == True:

                    color4[3] += self.fadein_speed

                if self.fadeout == True:

                    color4[3] -= self.fadeout_speed

                gl.glColor4fv(color4)                

                gl.glVertexPointerf(d)
                               
                gl.glDrawArrays(gl.GL_LINE_STRIP, 0, len(d))

        

        gl.glDisableClientState(gl.GL_VERTEX_ARRAY)

        #gl.glDisable(gl.GL_BLEND)
        
        gl.glEnable(gl.GL_LIGHTING)

        gl.glEnable(gl.GL_DEPTH_TEST)
        
        #gl.glPopMatrix()

        gl.glEndList()
Beispiel #17
0
    def __init__(self,fname,ang_table=None,colormap=None, line_width=3., shrink=None,subset=None,tracks=None,text=None,text_color=np.array([1,0,0])):

        self.position = (0,0,0)

        self.fname = fname
        
        self.manycolors = True
        
        self.bbox = None

        self.list_index = None

        self.affine = None

        self.data = None

        self.list_index = None

        self.rot_angle = 0

        self.colormap = None

        self.text = text
        
        self.min = None
         
        self.max = None

        self.mean = None

        self.material_color = False

        self.fadeout = False

        self.fadein = False

        self.fadeout_speed = 0.

        self.fadein_speed = 0.

        self.min_length = 20.

        self.angle = 0.

        self.angular_speed = .5

        self.line_width = line_width

        self.opacity = 1.

        self.near_pick = None

        self.far_pick = None

        self.near_pick_prev = None

        self.far_pick_prev = None

        self.picked_track = None

        self.pick_color = [1,1,0]

        self.brain_color = [1,1,1]

        self.yellow_indices = None

        self.dummy_data = False

        self.tracks = tracks

        if subset != None:

            self.data_subset = subset #[0,20000]#None

        else:

            self.data_subset = None

        self.orbit_demo = False          

        self.orbit_anglez = 0.

        self.orbit_anglez_rate = 10.
        

        self.orbit_anglex = 0.

        self.orbit_anglex_rate = 2.


        self.angle_table = ang_table

        
        self.angle_table_index = 0



        

        self.shrink = shrink

        self.picking_example = False


        self.partial_colors = False
        

        import dipy.io.trackvis as tv


        if self.tracks == None:

            lines,hdr = tv.read(self.fname)

            ras = tv.aff_from_hdr(hdr)

            self.affine=ras

            tracks = [l[0] for l in lines]

            del lines
        


        else:

            tracks = self.tracks


        if self.yellow_indices != None :

            tracks = [t for t in tracks if tm.length(t) > 20]

        print '%d tracks loaded' % len(tracks)

        #self.data = [100*np.array([[0,0,0],[1,0,0],[2,0,0]]).astype(np.float32) ,100*np.array([[0,1,0],[0,2,0],[0,3,0]]).astype(np.float32)]#tracks[:20000]

        if self.dummy_data:

            self.data = [100*np.array([[0,0,0],[1,0,0],[2,0,0]]).astype(np.float32) ,100*np.array([[0,1,0],[0,2,0],[0,3,0]]).astype(np.float32)]

        if self.data_subset!=None:

            self.data = tracks[self.data_subset[0]:self.data_subset[1]]

        else:

            self.data = tracks


        

        if self.shrink != None:

            self.data = [ self.shrink*t  for t in self.data]
            

            
        data_stats = np.concatenate(tracks)

        self.min=np.min(data_stats,axis=0)
         
        self.max=np.max(data_stats,axis=0)

        self.mean=np.mean(data_stats,axis=0)

        del data_stats
for c in C:

    color = np.random.rand(3)
    r, g, b = color
    bundle = []

    for i in C[c]["indices"]:

        colors2[i] = np.tile(np.array([r, g, b, opacity], "f"), (len(data[i]), 1))
        bundle.append(data[i])

    bi = pf.most_similar_track_mam(bundle)[0]
    C[c]["skeletal"] = bundle[bi]

    if len(C[c]["indices"]) > 100 and tm.length(bundle[bi]) > 30.0:
        colors_sk.append(np.tile(np.array([r, g, b, opacity], "f"), (len(bundle[bi]), 1)))
        skeletals.append(bundle[bi])


print "len_data", len(data)
print "len_skeletals", len(skeletals)
print "len_colors2", len(colors2)
print "len_colors_sk", len(colors_sk)

t2 = Tracks(data, colors2, line_width=1.0)
t2.position = (100, 0, 0)

sk = Tracks(skeletals, colors_sk, line_width=3.0)
sk.position = (0, 0, 0)