def save_ROIs(self): self.sbin = int(self.binSpinBox.value()) # save running parameters as defaults ops = self.save_ops() if len(self.save_path) > 0: savepath = self.save_path else: savepath = None print(savepath) if len(self.ROIs) > 0: rois = utils.roi_to_dict(self.ROIs, self.rROI) else: rois = None proc = { 'Ly': self.Ly, 'Lx': self.Lx, 'sy': self.sy, 'sx': self.sx, 'LY': self.LY, 'LX': self.LX, 'sbin': ops['sbin'], 'fullSVD': ops['fullSVD'], 'rois': rois, 'save_mat': ops['save_mat'], 'save_path': ops['save_path'], 'filenames': self.filenames, 'iframes': self.iframes } savename = process.save(proc, savepath=savepath) self.batchlist.append(savename) basename, filename = os.path.split(savename) filename, ext = os.path.splitext(filename) self.batchname[len(self.batchlist) - 1].setText(filename) self.processbatch.setEnabled(True)
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