Example #1
0
File: plotting.py Project: wj2/2p
 def loadI(i,bitmasks,prinROI):
     #refreshes the display on the GUI, declared here due to use in initialisation
     if typ == 3:
     #self.im.set_data(self.data[i])
         self.ax_img.imshow(self.data[i],aspect='equal',cmap=plt.cm.gray,interpolation='nearest')
         for j in range(len(bitmasks[i])):
             self.ax_img.imshow(seg.overlay_labels(bitmasks[i][j]),aspect='equal',interpolation='nearest')
             for prin in prinROI:
                 if prin == [i,j]:
                     self.ax_img.imshow(seg.overlay_full(bitmasks[i][j]),aspect='equal',interpolation='nearest')
     if typ == 2:
         self.ax_img.imshow(self.avg,aspect='equal',cmap=plt.cm.gray,interpolation='nearest')
         for j in range(len(bitmasks)):
             self.ax_img.imshow(seg.overlay_labels(bitmasks[j]),aspect='equal',interpolation='nearest')
     
     """
Example #2
0
File: plotting.py Project: wj2/2p
    def __init__(self,x = None, m = None):
        xMic,yMic = None,None
        
        try:
            path = sys.argv[1]
        except:
            print 'Please enter the path to the .xml metadata of your data or the single .tif or .npy file.'
            path = raw_input('> ')
        #handles multiple input types
        self.data = loadData(path)

        
        #self.data = np.load('sample_data/3Dtiffdata.npy')
        #processes the data, this step takes longest
        try:
            typ = sys.argv[2]
        except:
            print 'Is the data 3D or 2D?'
            typ = raw_input('> ')
        if typ == None or typ == '3D' or typ == '3d':
            self.bitmasks,self.prinROI = process3D(self.data,xMic,yMic)
            typ = 3
        elif typ == '2D' or typ == '2d':
            self.bitmasks,self.prinROI,self.avg = process2D(self.data,xMic,yMic)
            self.tc_data = self.data
            typ = 2
        self.t = 0  
        self.fig = plt.figure(1)
        self.lenData,self.i = len(self.data),0
        

        def loadI(i,bitmasks,prinROI):
            #refreshes the display on the GUI, declared here due to use in initialisation
            if typ == 3:
            #self.im.set_data(self.data[i])
                self.ax_img.imshow(self.data[i],aspect='equal',cmap=plt.cm.gray,interpolation='nearest')
                for j in range(len(bitmasks[i])):
                    self.ax_img.imshow(seg.overlay_labels(bitmasks[i][j]),aspect='equal',interpolation='nearest')
                    for prin in prinROI:
                        if prin == [i,j]:
                            self.ax_img.imshow(seg.overlay_full(bitmasks[i][j]),aspect='equal',interpolation='nearest')
            if typ == 2:
                self.ax_img.imshow(self.avg,aspect='equal',cmap=plt.cm.gray,interpolation='nearest')
                for j in range(len(bitmasks)):
                    self.ax_img.imshow(seg.overlay_labels(bitmasks[j]),aspect='equal',interpolation='nearest')
            
            """
            for j in range(len(bitmasks[i])):
                self.im.set_data(seg.overlay_labels(bitmasks[i][j]))
                for prin in prinROI:
                    if prin == [i,j]:
                        self.im.set_data(seg.overlay_full(bitmasks[i][j]))
            """


        self.ax_img = self.fig.add_axes([.1,.40,.8,.55])
        self.ax_plot = self.fig.add_axes([.1,.1,.8,.25])
        self.ax_but1 = self.fig.add_axes([.1,.05,.1,.04])
        self.ax_but2 = self.fig.add_axes([.2,.05,.1,.04])
        self.ax_but3 = self.fig.add_axes([.3,.05,.1,.04])
        self.ax_but4 = self.fig.add_axes([.4,.05,.1,.04])
        self.ax_but5 = self.fig.add_axes([.8,.05,.1,.04])
        self.ax_but6 = self.fig.add_axes([.7,.05,.1,.04])
        self.ax_but7 = self.fig.add_axes([.6,.05,.1,.04])
        self.ax_but8 = self.fig.add_axes([.5,.05,.1,.04])
        self.ax_but9 = self.fig.add_axes([.8,.01,.1,.04])
        #self.ax_fullrange = self.fig.add_axes([.1,.275,.8,.15])
        #self.ax_zoom = self.fig.add_axes([.1,.1,.8,.15])
        if typ == 2:
            self.im = self.ax_img.imshow(self.avg,aspect='equal',cmap=plt.cm.gray,interpolation='nearest')
            for j in range(len(self.bitmasks)):
                self.ax_img.imshow(seg.overlay_labels(self.bitmasks[j]),aspect='equal',interpolation='nearest')
        if typ == 3:
            self.im = self.ax_img.imshow(self.data[self.i],aspect='equal',cmap=plt.cm.gray,interpolation='nearest')
            loadI(self.i,self.bitmasks,self.prinROI)
        #self.LABELS = seg.watershed_segment(self.M)
        #span = SpanSelector(self.ax_fullrange, self.onselect, 'horizontal', useblit=True,rectprops=dict(alpha=0.5, facecolor='red') )
        #self.line1, = self.ax_fullrange.plot(self.x,self.y)
        #self.line2, = self.ax_zoom.plot(self.x,self.y)
        self.but1 = Button(self.ax_but1,"<---")
        self.but2 = Button(self.ax_but2,"DELETE")
        self.but3 = Button(self.ax_but3,"USE")
        self.but4 = Button(self.ax_but4,"--->")
        self.but5 = Button(self.ax_but5,"SHOW SPIKES")
        self.but6 = Button(self.ax_but6,"CLEAR PLOT")
        self.but7 = Button(self.ax_but7,"ALL SPIKES")
        self.but8 = Button(self.ax_but8,"SINGLE SPIKE")
        self.but9 = Button(self.ax_but9,"SAVE MASKS")

        def onscroll(self,event):
            if event.button == 'up':
                prevS(event)
            else:
                nextS(event)
        
        #self.ax_img.set_yticks([])
        #self.ax_img.set_xticks([])
        #self.but3 = Button(self.ax_but3,
        self.roi_pts = None
        
        def prevS(event):
            if self.i-1 >= 0:
                self.i-=1
                print str(self.i)+'/'+str(self.lenData)
                loadI(self.i,self.bitmasks,self.prinROI)

        def nextS(event):
            if self.i+1 <= self.lenData:
                self.i+=1
                print str(self.i)+'/'+str(self.lenData)
                loadI(self.i,self.bitmasks,self.prinROI)

        def delROI(event):
            click = plt.ginput(1,300)
            if typ == 2:
                self.bitmasks = delete_roi_2D(click,self.bitmasks)
            if typ == 3:
                j = match_point(click,self.bitmasks)
                self.prinROI.remove([self.i,j])
            loadI(self.i,self.bitmasks,self.prinROI)
            print 'ROI will not be used.'

        def useROI(event):
            click = plt.ginput(1,300)
            j = match_point(click,self.bitmasks[self.i])
            self.prinROI.append([self.i,j])
            self.prinROI.sort()
            loadI(self.i,self.bitmasks,self.prinROI)
            #self.uniqueMasks[i] = vstack([self.bitmasks[si],addROI[newaxis,...]])
            print 'ROI will be used.'
            
        def returnROIs(event):
            masks = []
            for i in range(len(self.bitmasks)):
                for roi in self.prinROI:
                    if roi[0] == i:
                        masks.append(bitmasks[i][roi[1]])
            print 'What would you like to name this dataset?'
            name = raw_input('> ')
            np.save(name,masks)
            print 'ROIs have been saved.'

        def showSpikes(event):
            click = plt.ginput(1,300)
            if typ == 2:
                j = match_point(click,self.bitmasks)
                if self.t == 0:
                    train,spikes,self.fil = roip.spikeDet(self.tc_data,self.bitmasks[j])
                    self.t = 1
                elif self.t == 1:
                    train,spikes = roip.spikeDetL(self.tc_data,self.bitmasks[j],self.fil)
                else:
                    print 'Something is wrong.'
            #self.plo = plt.figure(2)
                self.ax_plot.plot(train)
                self.ax_plot.plot(spikes,np.ones(np.size(spikes))*35000,'r.')
                print 'Spiketimes for ROI '+str(j)+': ',spikes
            if typ == 3:
                print 'Not currently supported.'

        def clearPlot(event):
            self.ax_plot.clear()

        def saveAllSpkTrains(event):
            arch = []
            if typ == 2:
                for j in range(len(self.bitmasks)):
                    print 'Mask '+str(j+1)+' of '+str(len(self.bitmasks))
                    if self.t == 0:
                        train,spikes,self.fil = roip.spikeDet(self.tc_data,self.bitmasks[j])
                        self.t = 1
                    else:
                        train,spikes = roip.spikeDetL(self.tc_data,self.bitmasks[j],self.fil)
                    arch.append([train,spikes])
            elif typ == 3:
                for roi in self.prinROI:
                    if self.t == 0:
                        train,spikes,self.fil = roip.spikeDet(self.tc_data,self.bitmasks[roi[0]][roi[1]])
                    else:
                        train,spikes = roip.spikeDetL(self.tc_data,bitmasks[roi[0]][roi[1]],self.fil)
                        self.t = 1
                    arch.append([roi,train,spikes])
            print 'What would you like to name this dataset?'
            name = raw_input('> ')
            np.save(name,arch)

        def saveSingleSpkTrain(event):
            click = plt.ginput(1,300)
            if typ == 2:
                j = match_point(click,self.bitmasks)
                if self.t == 0:
                    train,spikes,self.fil = roip.spikeDet(self.tc_data,self.bitmasks[j])
                    self.t = 1
                elif self.t == 1:
                    train,spikes = roip.spikeDetL(self.tc_data,self.bitmasks[j],self.fil)
                arch = [train,spikes]
                print 'What would you like to name the data from this ROI?'
                name = raw_input('> ')
                np.save(name,arch)
                                                 
                    
                
            

        self.but1.on_clicked(prevS)
        self.but2.on_clicked(delROI)
        self.but3.on_clicked(useROI)
        self.but4.on_clicked(nextS)
        self.but5.on_clicked(showSpikes)
        self.but6.on_clicked(clearPlot)
        self.but7.on_clicked(saveAllSpkTrains)
        self.but8.on_clicked(saveSingleSpkTrain)
        self.but9.on_clicked(returnROIs)

        self.fig.canvas.draw()