Ejemplo n.º 1
0
    def position(self, parent):
        parent.iROI = self.iROI
        pos0 = self.ROI.getSceneHandlePositions()
        pos = parent.pROI.mapSceneToView(pos0[0][1])
        posy = pos.y()
        posx = pos.x()
        sizex, sizey = self.ROI.size()
        xrange = (np.arange(-1 * int(sizex), 1) + int(posx)).astype(np.int32)
        yrange = (np.arange(-1 * int(sizey), 1) + int(posy)).astype(np.int32)
        yrange += int(sizey / 2)
        # what is ellipse circling?
        br = self.ROI.boundingRect()
        ellipse = np.zeros((yrange.size, xrange.size), np.bool)
        x, y = np.meshgrid(np.arange(0, xrange.size, 1),
                           np.arange(0, yrange.size, 1))
        ellipse = ((y - br.center().y())**2 / (br.height() / 2)**2 +
                   (x - br.center().x())**2 / (br.width() / 2)**2) <= 1

        ellipse = ellipse[:,
                          np.logical_and(
                              xrange >= 0, xrange < parent.ROIs[self.iROI].
                              xrange.size)]
        xrange = xrange[np.logical_and(
            xrange >= 0, xrange < parent.ROIs[self.iROI].xrange.size)]
        ellipse = ellipse[np.logical_and(
            yrange >= 0, yrange < parent.ROIs[self.iROI].yrange.size), :]
        yrange = yrange[np.logical_and(
            yrange >= 0, yrange < parent.ROIs[self.iROI].yrange.size)]

        # ellipse = lambda x,y: (((x+0.5)/(w/2.)-1)**2+ ((y+0.5)/(h/2.)-1)**2)**0.5 < 1, (w, h))
        self.ellipse = ellipse
        self.xrange = xrange
        self.yrange = yrange

        parent.reflectors[self.iROI] = utils.get_reflector(
            parent.ROIs[self.iROI].yrange, parent.ROIs[self.iROI].xrange,
            parent.rROI[self.iROI])

        parent.sl[1].setValue(parent.saturation[self.iROI] * 100 / 255)
        parent.ROIs[self.iROI].plot(parent)
Ejemplo n.º 2
0
def process_ROIs(containers, cumframes, Ly, Lx, avgmotion, U, sbin=3, tic=None, rois=None, fullSVD=True):
    # project U onto each frame in the video and compute the motion energy
    # also compute pupil on single frames on non binned data
    # the pixels are binned in spatial bins of size sbin
    # containers is a list of videos loaded with av
    # cumframes are the cumulative frames across videos
    if tic is None:
        tic=time.time()
    nframes = cumframes[-1]

    pups = []
    pupreflector = []
    blinks = []
    runs = []

    motind=[]
    pupind=[]
    blind=[]
    runind = []
    ivid = []
    nroi=0 # number of motion ROIs

    if fullSVD:
        ncomps = U[0].shape[-1]
        V = [np.zeros((nframes, ncomps), np.float32)]
    else:
        V = [np.zeros((0,1), np.float32)]
    if rois is not None:
        for i,r in enumerate(rois):
            ivid.append(r['ivid'])
            if r['rind']==0:
                pupind.append(i)
                pups.append({'area': np.zeros((nframes,)), 'com': np.zeros((nframes,2)),
                             'axdir': np.zeros((nframes,2,2)), 'axlen': np.zeros((nframes,2))})
                if 'reflector' in r:
                    pupreflector.append(utils.get_reflector(r['yrange'], r['xrange'], rROI=None, rdict=r['reflector']))
                else:
                    pupreflector.append(np.array([]))

            elif r['rind']==1:
                motind.append(i)
                nroi+=1
                V.append(np.zeros((nframes, U[nroi].shape[1]), np.float32))
            elif r['rind']==2:
                blind.append(i)
                blinks.append(np.zeros((nframes,)))
            elif r['rind']==3:
                runind.append(i)
                runs.append(np.zeros((nframes,2)))
    ivid = np.array(ivid).astype(np.int32)
    motind = np.array(motind).astype(np.int32)

    # compute in chunks of 500
    nt0 = 500
    nsegs = int(np.ceil(nframes / nt0))
    img = imall_init(nt0, Ly, Lx)
    # binned Ly and Lx and their relative inds in concatenated movies
    Lyb, Lxb, ir = binned_inds(Ly, Lx, sbin)
    imend = []
    for ii in range(len(Ly)):
        imend.append([])
    t=0
    nt1=0
    for n in range(nsegs):
        t += nt1
        get_frames(img, containers, [t, min(cumframes[-1],t+nt0)], cumframes)
        nt1 = img[0].shape[-1]
        # compute pupil
        if len(pupind)>0:
            k=0
            for p in pupind:
                imgp = img[ivid[p]][rois[p]['yrange'][0]:rois[p]['yrange'][-1]+1,
                                    rois[p]['xrange'][0]:rois[p]['xrange'][-1]+1]
                #imgp = gaussian_filter(imgp.astype(np.float32), [1,1], axis=(0,1))
                #imgp = gaussian_filter1d(imgp, 1, axis=1)
                imgp[~rois[p]['ellipse']] = 255
                com, area, axdir, axlen = pupil.process(imgp.astype(np.float32), rois[p]['saturation'],
                                                        rois[p]['pupil_sigma'], pupreflector[k])
                pups[k]['com'][t:t+nt1,:] = com
                pups[k]['area'][t:t+nt1] = area
                pups[k]['axdir'][t:t+nt1,:,:] = axdir
                pups[k]['axlen'][t:t+nt1,:] = axlen
                #print(area)
                k+=1

        if len(blind)>0:
            k=0
            for b in blind:
                imgp = img[ivid[b]][rois[b]['yrange'][0]:rois[b]['yrange'][-1]+1,
                                    rois[b]['xrange'][0]:rois[b]['xrange'][-1]+1]
                imgp[~rois[b]['ellipse']] = 255.0
                bl = np.maximum(0, (255 - imgp - (255-rois[b]['saturation']))).sum(axis=(0,1))
                blinks[k][t:t+nt0] = bl
                k+=1

        # compute running
        if len(runind)>0:
            k=0
            for r in runind:
                imr = img[ivid[r]][rois[r]['yrange'][0]:rois[r]['yrange'][-1]+1,
                                   rois[r]['xrange'][0]:rois[r]['xrange'][-1]+1]
                imr = np.transpose(imr, (2,0,1)).copy()
                # append last frame from previous set
                if n>0:
                    imr = np.concatenate((rend[k][np.newaxis,:,:],imr), axis=0)
                # save last frame
                if k==0:
                    rend=[]
                rend.append(imr[-1].copy())

                # compute phase correaltion between consecutive frames
                dy, dx = running.process(imr)
                if n>0:
                    runs[k][t:t+nt1] = np.concatenate((dy[:,np.newaxis], dx[:,np.newaxis]),axis=1)
                else:
                    runs[k][t+1:t+nt1] = np.concatenate((dy[:,np.newaxis], dx[:,np.newaxis]),axis=1)
                k+=1

        # bin and get motion
        if fullSVD:
            if n>0:
                imall = np.zeros(((Lyb*Lxb).sum(), img[0].shape[-1]), np.float32)
            else:
                imall = np.zeros(((Lyb*Lxb).sum(), img[0].shape[-1]-1), np.float32)
        if fullSVD or nroi > 0:
            for ii,im in enumerate(img):
                usevid=False
                if fullSVD:
                    usevid=True
                if nroi>0:
                    wmot = (ivid[motind]==ii).nonzero()[0]
                    if wmot.size>0:
                        usevid=True
                if usevid:
                    imbin = spatial_bin(im, sbin, Lyb[ii], Lxb[ii])
                    if n>0:
                        imbin = np.concatenate((imend[ii][:,np.newaxis], imbin), axis=-1)
                    imend[ii] = imbin[:,-1]
                    # compute motion energy
                    imbin = np.abs(np.diff(imbin, axis=-1))
                    imbin -= avgmotion[ii][:,np.newaxis]
                    if fullSVD:
                        imall[ir[ii]] = imbin
                if nroi > 0 and wmot.size>0:
                    wmot=np.array(wmot).astype(int)
                    imbin = np.reshape(imbin, (Lyb[ii], Lxb[ii], -1))
                    wroi = motind[wmot]
                    for i in range(wroi.size):
                        lilbin = imbin[rois[wroi[i]]['yrange_bin'][0]:rois[wroi[i]]['yrange_bin'][-1]+1,
                                       rois[wroi[i]]['xrange_bin'][0]:rois[wroi[i]]['xrange_bin'][-1]+1]
                        lilbin = np.reshape(lilbin, (-1, lilbin.shape[-1]))
                        vproj = lilbin.T @ U[wmot[i]+1]
                        if n==0:
                            vproj = np.concatenate((vproj[0,:][np.newaxis, :], vproj), axis=0)
                        V[wmot[i]+1][t:t+vproj.shape[0], :] = vproj
            if fullSVD:
                vproj = imall.T @ U[0]
                if n==0:
                    vproj = np.concatenate((vproj[0,:][np.newaxis, :], vproj), axis=0)
                V[0][t:t+vproj.shape[0], :] = vproj

        if n%20==0:
            print('segment %d / %d, time %1.2f'%(n+1, nsegs, time.time() - tic))

    return V, pups, blinks, runs
Ejemplo n.º 3
0
    def position(self, parent):
        if parent.iROI != self.iROI:
            if self.rind == 0:
                print('change to pupil')
                for i in range(len(parent.rROI[self.iROI])):
                    parent.pROI.addItem(parent.rROI[self.iROI][i].ROI)
            elif parent.ROIs[parent.iROI].rind == 0:
                for i in range(len(parent.rROI[parent.iROI])):
                    parent.pROI.removeItem(parent.rROI[parent.iROI][i].ROI)
            parent.iROI = self.iROI
        pos0 = self.ROI.getSceneHandlePositions()
        pos = parent.p0.mapSceneToView(pos0[0][1])
        posy = pos.y()
        posx = pos.x()
        sizex, sizey = self.ROI.size()
        xrange = (np.arange(-1 * int(sizex), 1) + int(posx)).astype(np.int32)
        yrange = (np.arange(-1 * int(sizey), 1) + int(posy)).astype(np.int32)
        if self.rind == 0 or self.rind == 2:
            yrange += int(sizey / 2)
        # what is ellipse circling?
        br = self.ROI.boundingRect()
        ellipse = np.zeros((yrange.size, xrange.size), np.bool)
        x, y = np.meshgrid(np.arange(0, xrange.size, 1),
                           np.arange(0, yrange.size, 1))
        ellipse = ((y - br.center().y())**2 / (br.height() / 2)**2 +
                   (x - br.center().x())**2 / (br.width() / 2)**2) <= 1

        ellipse = ellipse[:, np.logical_and(xrange >= 0, xrange < parent.LX)]
        xrange = xrange[np.logical_and(xrange >= 0, xrange < parent.LX)]
        ellipse = ellipse[np.logical_and(yrange >= 0, yrange < parent.LY), :]
        yrange = yrange[np.logical_and(yrange >= 0, yrange < parent.LY)]

        # ellipse = lambda x,y: (((x+0.5)/(w/2.)-1)**2+ ((y+0.5)/(h/2.)-1)**2)**0.5 < 1, (w, h))
        # which movie is this ROI in?
        vvals = parent.vmap[np.ix_(yrange, xrange)]
        ivid = np.zeros((len(parent.Ly), ))
        for i in range(len(parent.Ly)):
            ivid[i] = (vvals == i).sum()
        ivid = np.argmax(ivid)
        # crop yrange and xrange
        ix = np.logical_and(xrange >= parent.sx[ivid],
                            xrange < parent.sx[ivid] + parent.Lx[ivid])
        ellipse = ellipse[:, ix]
        xrange = xrange[ix]
        iy = np.logical_and(yrange >= parent.sy[ivid],
                            yrange < parent.sy[ivid] + parent.Ly[ivid])
        yrange = yrange[iy]
        ellipse = ellipse[iy, :]
        self.ellipse = ellipse

        xrange -= parent.sx[ivid]
        yrange -= parent.sy[ivid]
        self.xrange = xrange
        self.yrange = yrange
        self.ivid = ivid

        if self.rind == 0:
            #ifr = np.array(parent.video[0][ivid][:50])
            #ifr = ifr.mean(axis=-1)
            #self.rmin = ifr[np.ix_(np.arange(0,ifr.shape[0],1,int), self.yrange, self.xrange)].min()
            self.rmin = 0
            parent.reflectors[self.iROI] = utils.get_reflector(
                parent.ROIs[self.iROI].yrange,
                parent.ROIs[self.iROI].xrange,
                rROI=parent.rROI[self.iROI])
        parent.sl[1].setValue(parent.saturation[self.iROI] * 100 / 255)
        self.plot(parent)