Exemple #1
0
def run(filenames, parent=None, proc=None, savepath=None):
    ''' uses filenames and processes fullSVD if no roi's specified '''
    ''' parent is from GUI '''
    ''' proc can be a saved ROI file from GUI '''
    ''' savepath is the folder in which to save _proc.npy '''
    print('processing videos')
    # grab files
    Lys = []
    Lxs = []
    rois=None
    if parent is not None:
        filenames = parent.filenames
        video = parent.video
        containers = []
        for fs in filenames:
            cs = []
            for f in fs:
                cs.append(av.open(f))
                cs[-1].streams.video[0].thread_type = 'AUTO'
            containers.append(cs)
        cumframes = parent.cumframes
        nframes = parent.nframes
        iframes = parent.iframes
        sbin = parent.sbin
        rois = utils.roi_to_dict(parent.ROIs, parent.rROI)
        Ly = parent.Ly
        Lx = parent.Lx
        fullSVD = parent.checkBox.isChecked()
        save_mat = parent.save_mat.isChecked()
        sy = parent.sy
        sx = parent.sx
    else:
        video=[]
        containers = []
        nframes = 0
        iframes = []
        cumframes = [0]
        k=0
        for fs in filenames:
            vs = []
            cs = []
            for f in fs:
                try:
                    vs.append(pims.Video(f))
                except:
                    vs.append(pims.PyAVReaderIndexed(f))
                cs.append(av.open(f))
                cs[-1].streams.video[0].thread_type = 'AUTO'
            video.append(vs)
            containers.append(cs)
            iframes.append(len(video[-1][0]))
            cumframes.append(cumframes[-1] + len(video[-1][0]))
            nframes += len(video[-1][0])
            if k==0:
                Ly = []
                Lx = []
                for vs in video[-1]:
                    fshape = vs.frame_shape
                    Ly.append(fshape[0])
                    Lx.append(fshape[1])
            k+=1
        iframes = np.array(iframes).astype(int)
        cumframes = np.array(cumframes).astype(int)
        if proc is None:
            sbin = 4
            fullSVD = True
            save_mat = False
            rois=None
        else:
            sbin = proc['sbin']
            fullSVD = proc['fullSVD']
            save_mat = proc['save_mat']
            rois = proc['rois']
            sy = proc['sy']
            sx = proc['sx']

    Lybin, Lxbin, iinds = binned_inds(Ly, Lx, sbin)
    LYbin,LXbin,sybin,sxbin = utils.video_placement(Lybin, Lxbin)

    nroi = 0
    if rois is not None:
        for r in rois:
            if r['rind']==1:
                r['yrange_bin'] = np.arange(np.floor(r['yrange'][0]/sbin),
                                            np.floor(r['yrange'][-1]/sbin)).astype(int)
                r['xrange_bin'] = np.arange(np.floor(r['xrange'][0]/sbin),
                                            np.floor(r['xrange'][-1])/sbin).astype(int)
                nroi+=1

    isRGB = True
    #if len(frame_shape) > 2:
    #    isRGB = True

    tic = time.time()
    # compute average frame and average motion across videos (binned by sbin)
    avgframe, avgmotion = subsampled_mean(video, cumframes, Ly, Lx, sbin)
    avgframe_reshape = utils.multivideo_reshape(np.hstack(avgframe)[:,np.newaxis],
                                          LYbin,LXbin,sybin,sxbin,Lybin,Lxbin,iinds)
    avgframe_reshape = np.squeeze(avgframe_reshape)
    avgmotion_reshape = utils.multivideo_reshape(np.hstack(avgmotion)[:,np.newaxis],
                                           LYbin,LXbin,sybin,sxbin,Lybin,Lxbin,iinds)
    avgmotion_reshape = np.squeeze(avgmotion_reshape)
    print('computed subsampled mean at %1.2fs'%(time.time() - tic))

    ncomps = 500
    if fullSVD or nroi>0:
        # compute SVD from frames subsampled across videos and return spatial components
        U = compute_SVD(video, cumframes, Ly, Lx, avgmotion, ncomps, sbin, rois, fullSVD)
        print('computed subsampled SVD at %1.2fs'%(time.time() - tic))
        U_reshape = U.copy()
        if fullSVD:
            U_reshape[0] = utils.multivideo_reshape(U_reshape[0], LYbin,LXbin,sybin,sxbin,Lybin,Lxbin,iinds)
        if nroi>0:
            k=1
            for r in rois:
                if r['rind']==1:
                    ly = r['yrange_bin'].size
                    lx = r['xrange_bin'].size
                    U_reshape[k] = np.reshape(U[k].copy(), (ly,lx,U[k].shape[-1]))
                    k+=1
    else:
        U = []
        U_reshape = []

    # project U onto all movie frames
    # and compute pupil (if selected)
    V, pups, blinks, runs = process_ROIs(containers, cumframes, Ly, Lx, avgmotion, U, sbin, tic, rois, fullSVD)

    # smooth pupil and blinks and running
    print('smoothing ...')
    for p in pups:
        if 'area' in p:
            p['area_smooth'],_ = pupil.smooth(p['area'].copy())
            p['com_smooth'] = p['com'].copy()
            p['com_smooth'][:,0],_ = pupil.smooth(p['com_smooth'][:,0].copy())
            p['com_smooth'][:,1],_ = pupil.smooth(p['com_smooth'][:,1].copy())
    for b in blinks:
        b,_ = pupil.smooth(b.copy())
    #for r in runs:
    #    r[:,0],_ = pupil.smooth(r[:,0].copy())
    #    r[:,1],_ = pupil.smooth(r[:,1].copy())

    #V_smooth = []
    #for m in V:
    #    ms,ireplace = pupil.smooth(m[:,0].copy())#, win=50)
    #    ireplace[np.logical_or(ms>ms.std()*4, ms<ms.std()*-4)] = True
    #    print(ireplace.sum())
    #    inds = ireplace.nonzero()[0][:,np.newaxis] + np.arange(-50,51,1,int)[np.newaxis,:]
    #    inds[inds<0] = 0
    #    inds[inds>=m.shape[0]] = m.shape[0]-1
    #    m[ireplace,:] = np.nanmedian(m[inds,:], axis=1)
        #V_smooth.append(m)

    print('computed projection at %1.2fs'%(time.time() - tic))

    proc = {
            'filenames': filenames, 'save_path': savepath, 'iframes': iframes, 'Ly': Ly, 'Lx': Lx,
            'sbin': sbin, 'fullSVD': fullSVD, 'save_mat': save_mat,
            'Lybin': Lybin, 'Lxbin': Lxbin,
            'sybin': sybin, 'sxbin': sxbin, 'LYbin': LYbin, 'LXbin': LXbin,
            'avgframe': avgframe, 'avgmotion': avgmotion,
            'avgframe_reshape': avgframe_reshape, 'avgmotion_reshape': avgmotion_reshape,
            'motSVD': V, 'motMask': U, 'motMask_reshape': U_reshape,
            'pupil': pups, 'running': runs, 'blink': blinks, 'rois': rois
            }

    # save processing
    savename = save(proc, savepath)
    return savename
Exemple #2
0
    def load_movies(self, filelist=None):
        if filelist is not None:
            self.filelist = filelist
        try:
            v = []
            nframes = 0
            iframes = []
            cumframes = [0]
            k=0
            for fs in self.filelist:
                vs = []
                for f in fs:
                    try:
                        vs.append(pims.Video(f))
                    except:
                        print('pyavreaderindexed used - may be slower (try installing pims github version)')
                        vs.append(pims.PyAVReaderIndexed(f))
                v.append(vs)
                iframes.append(len(v[-1][0]))
                cumframes.append(cumframes[-1] + len(v[-1][0]))
                nframes += len(v[-1][0])
                if k==0:
                    Ly = []
                    Lx = []
                    for vs in v[-1]:
                        fshape = vs.frame_shape
                        Ly.append(fshape[0])
                        Lx.append(fshape[1])
                k+=1
            good = True
        except Exception as e:
            print("ERROR: not a supported movie file")
            print(e)
            good = False
        if good:
            if len(self.rROI)>0:
                for r in self.rROI:
                    if len(r) > 0:
                        for rr in r:
                            rr.remove(self)
            if len(self.ROIs)>0:
                for r in self.ROIs[::-1]:
                    r.remove(self)
            self.ROIs = []
            self.rROI=[]
            self.reflectors=[]
            self.saturation = []
            self.iROI=0
            self.nROIs=0
            self.saturation=[]
            # clear checkboxes
            for k in range(len(self.lbls)):
                self.lbls[k].setText('')
                self.cbs1[k].setEnabled(False)
                self.cbs2[k].setEnabled(False)
                self.cbs1[k].setChecked(False)
                self.cbs2[k].setChecked(False)
            self.video = v
            self.filenames = self.filelist
            self.nframes = nframes
            self.iframes = np.array(iframes).astype(int)
            self.cumframes = np.array(cumframes).astype(int)
            self.Ly = Ly
            self.Lx = Lx
            self.p1.clear()
            self.p2.clear()
            if len(self.Ly)<2:
                self.LY = self.Ly[0]
                self.LX = self.Lx[0]
                self.sx = np.array([int(0)])
                self.sy = np.array([int(0)])
                self.vmap = np.zeros((self.LY,self.LX), np.int32)
            else:
                # make placement of movies
                Ly = np.array(self.Ly.copy())
                Lx = np.array(self.Lx.copy())

                LY, LX, sy, sx = utils.video_placement(Ly, Lx)
                print(LY, LX)
                self.vmap = -1 * np.ones((LY,LX), np.int32)
                for i in range(Ly.size):
                    self.vmap[sy[i]:sy[i]+Ly[i], sx[i]:sx[i]+Lx[i]] = i
                self.sy = sy
                self.sx = sx
                self.LY = LY
                self.LX = LX

            self.fullimg = np.zeros((self.LY, self.LX, 3))
            self.imgs = []
            self.img = []
            for i in range(len(self.Ly)):
                self.imgs.append(np.zeros((self.Ly[i], self.Lx[i], 3, 3)))
                self.img.append(np.zeros((self.Ly[i], self.Lx[i], 3)))
            #self.srange = []
            # get scaling from 100 random frames in the first video
            #for n in range(len(self.Ly)):
            #    rperm = np.random.permutation(iframes[0])
            #    frames = np.zeros((self.Ly[n],self.Lx[n], min(40, iframes[0]-1)))
            #    for r in range(frames.shape[-1]):
            #        frames[:,:,r] = np.array(self.video[0][n][rperm[r]]).mean(axis=-1)
            #    self.srange.append(frames.mean() + frames.std()*np.array([-3,3]))
            self.movieLabel.setText(os.path.dirname(self.filenames[0][0]))
            self.frameDelta = int(np.maximum(5,self.nframes/200))
            self.frameSlider.setSingleStep(self.frameDelta)
            if self.nframes > 0:
                self.updateFrameSlider()
                self.updateButtons()
            self.cframe = 1
            self.loaded = True
            self.processed = False
            self.jump_to_frame()
        return good