def play_all_movies(self): """ Public function that plays the original data, rigid corrected and non-rigid data file side by side for comparison. """ offset_orig = np.nanmin(self.data_orig[:1000]) offset_rig = np.min(self.data_rig[:1000]) offset_pwrig = np.nanmin(self.data_pwrig[:1000]) cm.concatenate([ self.data_orig.resize(0.6, 0.6, 0.2) - offset_orig, self.data_rig.resize(0.6, 0.6, 0.2) - offset_rig, self.data_pwrig.resize(0.6, 0.6, 0.2) - offset_pwrig ], axis=2).play(gain=1.5, offset=0, bord_px=self._border_correction()) return self
def motion_corr(fnames, dview, opts, disp_movie, is_3d=False): """Perform motion correction""" # Create a motion correction object with the parameters specified. Note # that the file is not loaded in memory mc = MotionCorrect(fnames, dview=dview, **opts.get_group('motion')) # Run piecewise-rigid motion correction using NoRMCorre mc.motion_correct(save_movie=True) # Determine maximum shift to be used for trimming against NaNs border_to_0 = 0 if mc.border_nan is 'copy' else mc.border_to_0 # Compare with original movie if disp_movie and not is_3d: m_els = cm.load(mc.fname_tot_els) m_orig = cm.load_movie_chain(fnames) ds_ratio = 0.2 cm.concatenate([ m_orig.resize(1, 1, ds_ratio) - mc.min_mov * mc.nonneg_movie, m_els.resize(1, 1, ds_ratio) ], axis=2).play(fr=60, gain=15, magnification=2, offset=0) # press q to exit return mc, border_to_0
def reconstructed_movie(estimates, fnames, idx, scope, flip_signal): """ Create reconstructed movie in VolPy. The movie has three panels: motion corrected movie on the left panel, movie removed from the baseline on the mid panel and reconstructed movie on the right panel. Args: estimates: dict estimates dictionary contain results of VolPy fnames: list motion corrected movie in F-order memory mapping format idx: list index of selected neurons scope: list scope of number of frames in reconstructed movie flip_signal: boolean if True the signal will be flipped (for voltron) Return: mv_all: 3-D array motion corrected movie, movie removed from baseline, reconstructed movie concatenated into one matrix """ # motion corrected movie and movie removed from baseline mv = cm.load(fnames, fr=400)[scope[0]:scope[1]] dims = (mv.shape[1], mv.shape[2]) mv_bl = mv.computeDFF(secsWindow=0.1)[0] mv = (mv - mv.min()) / (mv.max() - mv.min()) if flip_signal: mv_bl = -mv_bl mv_bl[mv_bl < np.percentile(mv_bl, 3)] = np.percentile(mv_bl, 3) mv_bl[mv_bl > np.percentile(mv_bl, 98)] = np.percentile(mv_bl, 98) mv_bl = (mv_bl - mv_bl.min()) / (mv_bl.max() - mv_bl.min()) # reconstructed movie estimates['weights'][estimates['weights'] < 0] = 0 A = estimates['weights'][idx].transpose([1, 2, 0]).reshape((-1, len(idx))) C = estimates['t_rec'][idx, scope[0]:scope[1]] mv_rec = np.dot(A, C).reshape( (dims[0], dims[1], scope[1] - scope[0])).transpose((2, 0, 1)) mv_rec = cm.movie(mv_rec, fr=400) mv_rec = (mv_rec - mv_rec.min()) / (mv_rec.max() - mv_rec.min()) mv_all = cm.concatenate((mv, mv_bl, mv_rec), axis=2) return mv_all
def run_alignmnet(selected_rows, parameters, dview): ''' This is the main function for the alignment step. It applies methods from the CaImAn package used originally in motion correction to do alignment. Args: df: pd.DataFrame A dataframe containing the analysis states you want to have aligned. parameters: dict The alignment parameters. dview: object The dview object Returns: df: pd.DataFrame A dataframe containing the aligned analysis states. ''' # Sort the dataframe correctly df = selected_rows.copy() df = df.sort_values(by=paths.multi_index_structure) # Determine the mouse and session of the dataset index = df.iloc[0].name mouse, session, *r = index # alignment_v = index[len(paths.data_structure) + step_index] alignment_v = len(df) alignment_index = (mouse, session, alignment_v) # Determine the output .mmap file name file_name = f'mouse_{mouse}_session_{session}_v{alignment_v}' output_mmap_file_path = os.environ['DATA_DIR'] + f'data/interim/alignment/main/{file_name}.mmap' try: df.reset_index()[['session','trial', 'is_rest']].set_index(['session','trial', 'is_rest'], verify_integrity=True) except ValueError: logging.error('You passed multiple of the same trial in the dataframe df') return df output = { 'meta': { 'analysis': { 'analyst': os.environ['ANALYST'], 'date': datetime.datetime.today().strftime("%m-%d-%Y"), 'time': datetime.datetime.today().strftime("%H:%M:%S") }, 'duration': {} } } # Get necessary parameters motion_correction_parameters_list = [] motion_correction_output_list = [] input_mmap_file_list = [] trial_index_list = [] x_ = [] _x = [] y_ = [] _y = [] for idx, row in df.iterrows(): motion_correction_parameters_list.append(eval(row.loc['motion_correction_parameters'])) motion_correction_output = eval(row.loc['motion_correction_output']) motion_correction_output_list.append(motion_correction_output) input_mmap_file_list.append(motion_correction_output['main']) trial_index_list.append(db.get_trial_name(idx[2], idx[3])) [x1,x2,y1,y2] = motion_correction_output['meta']['cropping_points'] x_.append(x1) _x.append(x2) y_.append(y1) _y.append(y2) new_x1 = max(x_) new_x2 = max(_x) new_y1 = max(y_) new_y2 = max(_y) m_list = [] for i in range(len(input_mmap_file_list)): m = cm.load(input_mmap_file_list[i]) motion_correction_output = eval(df.iloc[i].loc['motion_correction_output']) [x1,x2,y1,y2] = motion_correction_output['meta']['cropping_points'] m = m.crop(new_x1 - x1, new_x2 - x2, new_y1 - y1, new_y2 - y2, 0, 0) m_list.append(m) # Concatenate them using the concat function m_concat = cm.concatenate(m_list, axis=0) data_dir = os.environ['DATA_DIR'] + 'data/interim/alignment/main/' file_name = db.create_file_name(step_index, index) fname= m_concat.save(data_dir + file_name + '.mmap', order='C') #meta_pkl_dict['pw_rigid']['cropping_points'] = [x_, _x, y_, _y] #output['meta']['cropping_points'] = [x_, _x, y_, _y] # Save the movie #fname_tot_els = m_els.save(data_dir + 'main/' + file_name + '_els' + '.mmap', order='C') #logging.info(f'{index} Cropped and saved rigid movie as {fname_tot_els}') # MOTION CORRECTING EACH INDIVIDUAL MOVIE WITH RESPECT TO A TEMPLATE MADE OF THE FIRST MOVIE logging.info(f'{alignment_index} Performing motion correction on all movies with respect to a template made of \ the first movie.') t0 = datetime.datetime.today() # Create a template of the first movie template_index = trial_index_list.index(parameters['make_template_from_trial']) m0 = cm.load(input_mmap_file_list[template_index ]) [x1, x2, y1, y2] = motion_correction_output_list[template_index]['meta']['cropping_points'] m0 = m0.crop(new_x1 - x1, new_x2 - x2, new_y1 - y1, new_y2 - y2, 0, 0) m0_filt = cm.movie( np.array([high_pass_filter_space(m_, parameters['gSig_filt']) for m_ in m0])) template0 = cm.motion_correction.bin_median( m0_filt.motion_correct(5, 5, template=None)[0]) # may be improved in the future # Setting the parameters opts = params.CNMFParams(params_dict=parameters) # Create a motion correction object mc = MotionCorrect(fname, dview=dview, **opts.get_group('motion')) # Perform non-rigid motion correction mc.motion_correct(template=template0, save_movie=True) # Cropping borders x_ = math.ceil(abs(np.array(mc.shifts_rig)[:, 1].max()) if np.array(mc.shifts_rig)[:, 1].max() > 0 else 0) _x = math.ceil(abs(np.array(mc.shifts_rig)[:, 1].min()) if np.array(mc.shifts_rig)[:, 1].min() < 0 else 0) y_ = math.ceil(abs(np.array(mc.shifts_rig)[:, 0].max()) if np.array(mc.shifts_rig)[:, 0].max() > 0 else 0) _y = math.ceil(abs(np.array(mc.shifts_rig)[:, 0].min()) if np.array(mc.shifts_rig)[:, 0].min() < 0 else 0) # Load the motion corrected movie into memory movie= cm.load(mc.fname_tot_rig[0]) # Crop all movies to those border pixels movie.crop(x_, _x, y_, _y, 0, 0) output['meta']['cropping_points'] = [x_, _x, y_, _y] #save motion corrected and cropped movie output_mmap_file_path_tot = movie.save(data_dir + file_name + '.mmap', order='C') logging.info(f'{index} Cropped and saved rigid movie as {output_mmap_file_path_tot}') # Save the path in teh output dictionary output['main'] = output_mmap_file_path_tot # Remove the remaining non-cropped movie os.remove(mc.fname_tot_rig[0]) # Create a timeline and store it timeline = [[trial_index_list[0], 0]] timepoints = [0] for i in range(1, len(m_list)): m = m_list[i] timeline.append([trial_index_list[i], timeline[i - 1][1] + m.shape[0]]) timepoints.append(timepoints[i-1]+ m.shape[0]) timeline_pkl_file_path = os.environ['DATA_DIR'] + f'data/interim/alignment/meta/timeline/{file_name}.pkl' with open(timeline_pkl_file_path,'wb') as f: pickle.dump(timeline,f) output['meta']['timeline'] = timeline_pkl_file_path timepoints.append(movie.shape[0]) dt = int((datetime.datetime.today() - t0).seconds / 60) # timedelta in minutes output['meta']['duration']['concatenation'] = dt logging.info(f'{alignment_index} Performed concatenation. dt = {dt} min.') for idx, row in df.iterrows(): df.loc[idx, 'alignment_output'] = str(output) df.loc[idx, 'alignment_parameters'] = str(parameters) ## modify all motion correction file to the aligned version data_dir = os.environ['DATA_DIR'] + 'data/interim/motion_correction/main/' for i in range(len(input_mmap_file_list)): row = df.iloc[i].copy() motion_correction_output_list.append(motion_correction_output) aligned_movie = movie[timepoints[i]:timepoints[i+1]] file_name = db.create_file_name(2, selected_rows.iloc[i].name) motion_correction_output_aligned = aligned_movie.save(data_dir + file_name + '_els' + '.mmap', order='C') new_output= {'main' : motion_correction_output_aligned } new_dict = eval(row['motion_correction_output']) new_dict.update(new_output) row['motion_correction_output'] = str(new_dict) df = db.append_to_or_merge_with_states_df(df, row) # # Delete the motion corrected movies # for fname in mc.fname_tot_rig: # os.remove(fname) return df
splits_rig=splits_rig, strides=strides, overlaps=overlaps, splits_els=splits_els, upsample_factor_grid=upsample_factor_grid, max_deviation_rigid=max_deviation_rigid, shifts_opencv=True, nonneg_movie=True) # note that the file is not loaded in memory #%% Run piecewise-rigid motion correction using NoRMCorre mc.motion_correct_pwrigid(save_movie=True) m_els = cm.load(mc.fname_tot_els) bord_px_els = np.ceil(np.maximum(np.max(np.abs(mc.x_shifts_els)), np.max(np.abs(mc.y_shifts_els)))).astype(np.int) # maximum shift to be used for trimming against NaNs #%% compare with original movie moviehandle = cm.concatenate([m_orig.resize(1, 1, downsample_ratio) + offset_mov, m_els.resize(1, 1, downsample_ratio)], axis=2) display_images = False if display_images: moviehandle.play(fr=60, q_max=99.5, magnification=2, offset=0) # press q to exit #%% MEMORY MAPPING # memory map the file in order 'C' fnames = mc.fname_tot_els # name of the pw-rigidly corrected file. border_to_0 = bord_px_els # number of pixels to exclude fname_new = cm.save_memmap(fnames, base_name='memmap_', order='C', border_to_0=bord_px_els) # exclude borders # now load the file Yr, dims, T = cm.load_memmap(fname_new) d1, d2 = dims
def create_video(row, time_cropping, session_wise = False): ''' This fuction creates a complete video with raw movie (motion corrected), source extracted cells and source extraction + background. :param row: pandas dataframe containing the desired processing information to create the video. It can use the session_wise or trial_wise video. :return: ''' if session_wise: input_mmap_file_path = eval(row.loc['alignment_output'])['main'] else: input_mmap_file_path = eval(row.loc['motion_correction_output'])['main'] #load the mmap file Yr, dims, T = cm.load_memmap(input_mmap_file_path) logging.debug(f'{row.name} Loaded movie. dims = {dims}, T = {T}.') #create a caiman movie with the mmap file images = Yr.T.reshape((T,) + dims, order='F') images = cm.movie(images) #load source extraction result output = eval(row.loc['source_extraction_output']) cnm_file_path = output['main'] cnm = load_CNMF(db.get_file(cnm_file_path)) #estimate the background from the extraction W, b0 = cm.source_extraction.cnmf.initialization.compute_W(Yr, cnm.estimates.A.toarray(), cnm.estimates.C, cnm.estimates.dims, 1.4 * 5, ssub=2) cnm.estimates.W = W cnm.estimates.b0 = b0 # this part could be use with the lastest caiman version # movie_dir = '/home/sebastian/Documents/Melisa/calcium_imaging_analysis/data/processed/movies/' # file_name = db.create_file_name(5,row.name) # cnm.estimates.play_movie(cnm.estimates, images, movie_name= movie_dir + file_name + '.avi') frame_range = slice(None, None, None) # create a movie with the model : estimated A and C matrix Y_rec = cnm.estimates.A.dot(cnm.estimates.C[:, frame_range]) Y_rec = Y_rec.reshape(dims + (-1,), order='F') Y_rec = Y_rec.transpose([2, 0, 1]) # convert the variable to a caiman movie type Y_rec = cm.movie(Y_rec) ## this part of the function is a copy from a caiman version ssub_B = int(round(np.sqrt(np.prod(dims) / W.shape[0]))) B = images[frame_range].reshape((-1, np.prod(dims)), order='F').T - \ cnm.estimates.A.dot(cnm.estimates.C[:, frame_range]) if ssub_B == 1: B = b0[:, None] + W.dot(B - b0[:, None]) else: B = b0[:, None] + (np.repeat(np.repeat(W.dot( downscale(B.reshape(dims + (B.shape[-1],), order='F'), (ssub_B, ssub_B, 1)).reshape((-1, B.shape[-1]), order='F') - downscale(b0.reshape(dims, order='F'), (ssub_B, ssub_B)).reshape((-1, 1), order='F')) .reshape( ((dims[0] - 1) // ssub_B + 1, (dims[1] - 1) // ssub_B + 1, -1), order='F'), ssub_B, 0), ssub_B, 1)[:dims[0], :dims[1]].reshape( (-1, B.shape[-1]), order='F')) B = B.reshape(dims + (-1,), order='F').transpose([2, 0, 1]) Y_rec_2 = Y_rec + B Y_res = images[frame_range] - Y_rec - B images_np = np.zeros((time_cropping[1]-time_cropping[0],images.shape[1],images.shape[2])) images_np = images[time_cropping[0]:time_cropping[1],:,:] images_np = images_np / np.max(images_np) images_np = cm.movie(images_np) Y_rec_np = np.zeros((time_cropping[1]-time_cropping[0],images.shape[1],images.shape[2])) Y_rec_np = Y_rec[time_cropping[0]:time_cropping[1],:,:] Y_rec_np = Y_rec_np / np.max(Y_rec_np) Y_rec_np = cm.movie(Y_rec_np) Y_res_np = np.zeros((time_cropping[1]-time_cropping[0],images.shape[1],images.shape[2])) Y_res_np = Y_res[time_cropping[0]:time_cropping[1],:,:] Y_res_np = Y_res_np / np.max(Y_res_np) Y_res_np = cm.movie(Y_res_np) B_np = np.zeros((time_cropping[1]-time_cropping[0],images.shape[1],images.shape[2])) B_np = B[time_cropping[0]:time_cropping[1],:,:] B_np = B_np / np.max(B_np) B_np = cm.movie(B_np) mov1 = cm.concatenate((images_np, Y_rec_np), axis=2) mov2 = cm.concatenate((B_np, Y_res_np), axis=2) mov = cm.concatenate((mov1, mov2), axis=1) figure_path = '/home/sebastian/Documents/Melisa/calcium_imaging_analysis/data/interim/movies/' figure_name = db.create_file_name(5,row.name) #mov.save(figure_path+figure_name+'.tif') mov.save(figure_path+figure_name+'_'+f'{time_cropping[0]}' + '_' + f'{time_cropping[1]}'+'.tif') return
# use one every 200 frames temporal_stride = 200 # use one every 8 patches (patches are 8x8 by default) spatial_stride = 8 movie_train = movie[::temporal_stride] t = timeit.default_timer() estimation_res = est.estimate_vst_movie(movie_train, stride=spatial_stride) print('\tTime', timeit.default_timer() - t) alpha = estimation_res.alpha sigma_sq = estimation_res.sigma_sq movie_gat = compute_gat(movie, sigma_sq, alpha=alpha) # save movie_gat here movie_gat_inv = compute_inverse_gat(movie_gat, sigma_sq, alpha=alpha, method='asym') # save movie_gat_inv here return movie, movie_gat_inv #%% movie, movie_gat_inv = main() #%% cm.concatenate([movie, movie_gat_inv], axis=1).play(gain=10, magnification=4)
splits_rig=splits_rig, strides=strides, overlaps=overlaps, splits_els=splits_els, upsample_factor_grid=upsample_factor_grid, max_deviation_rigid=max_deviation_rigid, shifts_opencv=True, nonneg_movie=True) # note that the file is not loaded in memory #%% Run piecewise-rigid motion correction using NoRMCorre mc.motion_correct_rigid(save_movie=True) #%% m_els = cm.load(mc.fname_tot_rig) bord_px_els = np.max(np.ceil(np.abs(mc.shifts_rig))).astype(np.int) # maximum shift to be used for trimming against NaNs #%% compare with original movie cm.concatenate([m_orig.resize(1, 1, downsample_ratio) + offset_mov, m_els.resize(1, 1, downsample_ratio)], axis=2).play(fr=60, gain=1, magnification=1, offset=0) # press q to exit #%% MEMORY MAPPING # memory map the file in order 'C' fnames = mc.fname_tot_rig # name of the pw-rigidly corrected file. border_to_0 = bord_px_els # number of pixels to exclude fname_new = cm.save_memmap(fnames, base_name='memmap_', order='C', border_to_0=border_to_0) # exclude borders # now load the file Yr, dims, T = cm.load_memmap(fname_new) d1, d2 = dims images = np.reshape(Yr.T, [T] + list(dims), order='F') # load frames in python format (T x X x Y)
for i in ds_list: if 'mask' in i: m = np.load((dr + i), allow_pickle=True)['arr_0'] print(i) """ if 'IVQ' in i: print(len(m)/16) l.append(len(m)/16) else: """ print(len(m)) l.append(len(m)) #%% Video m2 = m2 * 1.2 mm = cm.concatenate([m1, m2], axis=2) mm = cm.concatenate([mm, lcm1], axis=2) m = cm.load('/home/nel/Code/VolPy/Mask_RCNN/videos & imgs/neurons_mc/403106_3min_d1_128_d2_512_d3_1_order_C_frames_20000_.mmap') lcm = cm.load('/home/nel/Code/VolPy/Paper/pic_paper/video/corr_video.tif') m = m[:19871,:,:] m.shape lcm.shape m = m.transpose([0,2,1]) lcm = lcm.transpose([0,2,1]) m.shape lcm.shape m.play() lcm.play() lcm.play(fr=30)
def play_movie(self, imgs, q_max=99.75, q_min=2, gain_res=1, magnification=1, include_bck=True, frame_range=slice(None, None, None), bpx=0, thr=0.): """Displays a movie with three panels (original data (left panel), reconstructed data (middle panel), residual (right panel)) Args: imgs: np.array (possibly memory mapped, t,x,y[,z]) Imaging data q_max: float (values in [0, 100]) percentile for maximum plotting value q_min: float (values in [0, 100]) percentile for minimum plotting value gain_res: float amplification factor for residual movie magnification: float magnification factor for whole movie include_bck: bool flag for including background in original and reconstructed movie frame_rage: range or slice or list display only a subset of frames bpx: int number of pixels to exclude on each border thr: float (values in [0, 1[) threshold value for contours, no contours if thr=0 Returns: self (to stop the movie press 'q') """ dims = imgs.shape[1:] if 'movie' not in str(type(imgs)): imgs = caiman.movie(imgs) Y_rec = self.A.dot(self.C[:, frame_range]) Y_rec = Y_rec.reshape(dims + (-1, ), order='F') Y_rec = Y_rec.transpose([2, 0, 1]) if self.W is not None: ssub_B = int(round(np.sqrt(np.prod(dims) / self.W.shape[0]))) B = imgs[frame_range].reshape((-1, np.prod(dims)), order='F').T - \ self.A.dot(self.C[:, frame_range]) if ssub_B == 1: B = self.b0[:, None] + self.W.dot(B - self.b0[:, None]) else: B = self.b0[:, None] + (np.repeat( np.repeat( self.W.dot( downscale( B.reshape(dims + (B.shape[-1], ), order='F'), (ssub_B, ssub_B, 1)).reshape( (-1, B.shape[-1]), order='F') - downscale(self.b0.reshape(dims, order='F'), (ssub_B, ssub_B)).reshape( (-1, 1), order='F')).reshape( ((dims[0] - 1) // ssub_B + 1, (dims[1] - 1) // ssub_B + 1, -1), order='F'), ssub_B, 0), ssub_B, 1)[:dims[0], :dims[1]].reshape( (-1, B.shape[-1]), order='F')) B = B.reshape(dims + (-1, ), order='F').transpose([2, 0, 1]) elif self.b is not None and self.f is not None: B = self.b.dot(self.f[:, frame_range]) if 'matrix' in str(type(B)): B = B.toarray() B = B.reshape(dims + (-1, ), order='F').transpose([2, 0, 1]) else: B = np.zeros_like(Y_rec) if bpx > 0: B = B[:, bpx:-bpx, bpx:-bpx] Y_rec = Y_rec[:, bpx:-bpx, bpx:-bpx] imgs = imgs[:, bpx:-bpx, bpx:-bpx] Y_res = imgs[frame_range] - Y_rec - B mov = caiman.concatenate( (imgs[frame_range] - (not include_bck) * B, Y_rec + include_bck * B, Y_res * gain_res), axis=2) if thr > 0: import cv2 contours = [] for a in self.A.T.toarray(): a = a.reshape(dims, order='F') if bpx > 0: a = a[bpx:-bpx, bpx:-bpx] if magnification != 1: a = cv2.resize(a, None, fx=magnification, fy=magnification, interpolation=cv2.INTER_LINEAR) ret, thresh = cv2.threshold(a, thr * np.max(a), 1., 0) im2, contour, hierarchy = cv2.findContours( thresh.astype('uint8'), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) contours.append(contour) contours.append( list([c + np.array([[a.shape[1], 0]]) for c in contour])) contours.append( list([ c + np.array([[2 * a.shape[1], 0]]) for c in contour ])) maxmov = np.nanpercentile(mov[0:10], q_max) if q_max < 100 else np.nanmax(mov) minmov = np.nanpercentile(mov[0:10], q_min) if q_min > 0 else np.nanmin(mov) for frame in mov: if magnification != 1: frame = cv2.resize(frame, None, fx=magnification, fy=magnification, interpolation=cv2.INTER_LINEAR) frame = np.clip((frame - minmov) * 255. / (maxmov - minmov), 0, 255) frame = np.repeat(frame[..., None], 3, 2) for contour in contours: cv2.drawContours(frame, contour, -1, (0, 255, 255), 1) cv2.imshow('frame', frame.astype('uint8')) if cv2.waitKey(30) & 0xFF == ord('q'): break cv2.destroyAllWindows() else: mov.play(q_min=q_min, q_max=q_max, magnification=magnification) return self
# %% inspect movie downsample_ratio = params_display['downsample_ratio'] # TODO: todocument offset_mov = -np.min(m_orig[:100]) m_rig.resize(1, 1, downsample_ratio).play(gain=10, offset=offset_mov * .25, fr=30, magnification=2, bord_px=bord_px_rig) # %% visualize raw, rigid and pw-rigid motion correted moviews downsample_factor = params_display['downsample_ratio'] # TODO : todocument cm.concatenate([ m_orig.resize(1, 1, downsample_factor) + offset_mov, m_rig.resize(1, 1, downsample_factor) ], axis=2).play(fr=60, gain=5, magnification=4, offset=0) # TODO: show screenshot 8 # %% restart cluster to clean up memory # TODO: todocument c, dview, n_processes = cm.cluster.setup_cluster(backend='local', n_processes=None, single_thread=False) # %% save each chunk in F format t1 = time.time() if not 'max_shifts' in params_movie: fnames = params_movie['fname'] border_to_0 = 0 else: # elif not params_movie.has_key('overlaps'): fnames = [mc.fname_tot_rig]
int), max_deviation_rigid + 1)[::-1] for num_splits_to_process in [28, None]: fname_tot_els, total_template_wls, templates_els, x_shifts_els, y_shifts_els, coord_shifts_els = cm.motion_correction.motion_correct_batch_pwrigid(fname, max_shifts_els, strides, overlaps, add_to_movie, newoverlaps=None, newstrides=None, dview=dview, upsample_factor_grid=upsample_factor_grid, max_deviation_rigid=max_deviation_rigid, splits=splits, num_splits_to_process=num_splits_to_process, num_iter=num_iter, template=new_templ, shifts_opencv=shifts_opencv, save_movie=save_movie) new_templ = total_template_wls #%% pl.subplot(2, 1, 1) pl.plot(x_shifts_els) pl.subplot(2, 1, 2) pl.plot(y_shifts_els) #%% m_els = cm.load(fname_tot_els) cm.concatenate([m_rig.resize(1, 1, .2), m_els.resize(1, 1, .2)], axis=1).play( fr=50, gain=20, magnification=2, offset=add_to_movie) #%% compute metrics for the results final_size = np.subtract(new_templ.shape, max_shifts_els) winsize = 75 swap_dim = False resize_fact_flow = .2 tmpl, correlations, flows_orig, norms, smoothness = cm.motion_correction.compute_metrics_motion_correction( fname_tot_els, final_size[0], final_size[1], swap_dim, winsize=winsize, play_flow=False, resize_fact_flow=resize_fact_flow) tmpl, correlations, flows_orig, norms, smoothness = cm.motion_correction.compute_metrics_motion_correction( fname_tot_rig, final_size[0], final_size[1], swap_dim, winsize=winsize, play_flow=False, resize_fact_flow=resize_fact_flow) tmpl, correlations, flows_orig, norms, smoothness = cm.motion_correction.compute_metrics_motion_correction( fname, final_size[0], final_size[1], swap_dim, winsize=winsize, play_flow=False, resize_fact_flow=resize_fact_flow) #%% plot the results of metrics fls = [fname_tot_els[:-4] + '_metrics.npz', fname_tot_rig[:-4] + '_metrics.npz', fname[:-4] + '_metrics.npz']
def main(): pass # For compatibility between running under Spyder and the CLI #%% Select file(s) to be processed (download if not present) fnames = ['Sue_2x_3000_40_-46.tif'] # filename to be processed if fnames[0] in ['Sue_2x_3000_40_-46.tif', 'demoMovie.tif']: fnames = [download_demo(fnames[0])] #%% First setup some parameters for data and motion correction # dataset dependent parameters fr = 30 # imaging rate in frames per second decay_time = 0.4 # length of a typical transient in seconds dxy = (2., 2.) # spatial resolution in x and y in (um per pixel) # note the lower than usual spatial resolution here max_shift_um = (12., 12.) # maximum shift in um patch_motion_um = (100., 100.) # patch size for non-rigid correction in um # motion correction parameters pw_rigid = True # flag to select rigid vs pw_rigid motion correction # maximum allowed rigid shift in pixels max_shifts = [int(a/b) for a, b in zip(max_shift_um, dxy)] # start a new patch for pw-rigid motion correction every x pixels strides = tuple([int(a/b) for a, b in zip(patch_motion_um, dxy)]) # overlap between pathes (size of patch in pixels: strides+overlaps) overlaps = (24, 24) # maximum deviation allowed for patch with respect to rigid shifts max_deviation_rigid = 3 mc_dict = { 'fnames': fnames, 'fr': fr, 'decay_time': decay_time, 'dxy': dxy, 'pw_rigid': pw_rigid, 'max_shifts': max_shifts, 'strides': strides, 'overlaps': overlaps, 'max_deviation_rigid': max_deviation_rigid, 'border_nan': 'copy' } opts = params.CNMFParams(params_dict=mc_dict) # %% play the movie (optional) # playing the movie using opencv. It requires loading the movie in memory. # To close the video press q display_images = True if display_images: m_orig = cm.load_movie_chain(fnames) ds_ratio = 0.2 moviehandle = m_orig.resize(1, 1, ds_ratio) moviehandle.play(q_max=99.5, fr=60, magnification=2) # %% start a cluster for parallel processing c, dview, n_processes = cm.cluster.setup_cluster( backend='local', n_processes=None, single_thread=False) # %%% MOTION CORRECTION # first we create a motion correction object with the specified parameters mc = MotionCorrect(fnames, dview=dview, **opts.get_group('motion')) # note that the file is not loaded in memory # %% Run (piecewise-rigid motion) correction using NoRMCorre mc.motion_correct(save_movie=True) # %% compare with original movie if display_images: m_orig = cm.load_movie_chain(fnames) m_els = cm.load(mc.mmap_file) ds_ratio = 0.2 moviehandle = cm.concatenate([m_orig.resize(1, 1, ds_ratio) - mc.min_mov*mc.nonneg_movie, m_els.resize(1, 1, ds_ratio)], axis=2) moviehandle.play(fr=60, q_max=99.5, magnification=2) # press q to exit # %% MEMORY MAPPING border_to_0 = 0 if mc.border_nan is 'copy' else mc.border_to_0 # you can include the boundaries of the FOV if you used the 'copy' option # during motion correction, although be careful about the components near # the boundaries # memory map the file in order 'C' fname_new = cm.save_memmap(mc.mmap_file, base_name='memmap_', order='C', border_to_0=border_to_0) # exclude borders # now load the file Yr, dims, T = cm.load_memmap(fname_new) images = np.reshape(Yr.T, [T] + list(dims), order='F') # load frames in python format (T x X x Y) # %% restart cluster to clean up memory cm.stop_server(dview=dview) c, dview, n_processes = cm.cluster.setup_cluster( backend='local', n_processes=None, single_thread=False) # %% parameters for source extraction and deconvolution p = 1 # order of the autoregressive system gnb = 2 # number of global background components merge_thr = 0.85 # merging threshold, max correlation allowed rf = 15 # half-size of the patches in pixels. e.g., if rf=25, patches are 50x50 stride_cnmf = 6 # amount of overlap between the patches in pixels K = 4 # number of components per patch gSig = [4, 4] # expected half size of neurons in pixels # initialization method (if analyzing dendritic data using 'sparse_nmf') method_init = 'greedy_roi' ssub = 2 # spatial subsampling during initialization tsub = 2 # temporal subsampling during intialization # parameters for component evaluation opts_dict = {'fnames': fnames, 'p': p, 'fr': fr, 'nb': gnb, 'rf': rf, 'K': K, 'gSig': gSig, 'stride': stride_cnmf, 'method_init': method_init, 'rolling_sum': True, 'merge_thr': merge_thr, 'n_processes': n_processes, 'only_init': True, 'ssub': ssub, 'tsub': tsub} opts.change_params(params_dict=opts_dict); # %% RUN CNMF ON PATCHES # First extract spatial and temporal components on patches and combine them # for this step deconvolution is turned off (p=0). If you want to have # deconvolution within each patch change params.patch['p_patch'] to a # nonzero value #opts.change_params({'p': 0}) cnm = cnmf.CNMF(n_processes, params=opts, dview=dview) cnm = cnm.fit(images) # %% ALTERNATE WAY TO RUN THE PIPELINE AT ONCE # you can also perform the motion correction plus cnmf fitting steps # simultaneously after defining your parameters object using # cnm1 = cnmf.CNMF(n_processes, params=opts, dview=dview) # cnm1.fit_file(motion_correct=True) # %% plot contours of found components Cns = local_correlations_movie_offline(mc.mmap_file[0], remove_baseline=True, window=1000, stride=1000, winSize_baseline=100, quantil_min_baseline=10, dview=dview) Cn = Cns.max(axis=0) Cn[np.isnan(Cn)] = 0 cnm.estimates.plot_contours(img=Cn) plt.title('Contour plots of found components') #%% save results cnm.estimates.Cn = Cn cnm.save(fname_new[:-5]+'_init.hdf5') # %% RE-RUN seeded CNMF on accepted patches to refine and perform deconvolution cnm2 = cnm.refit(images, dview=dview) # %% COMPONENT EVALUATION # the components are evaluated in three ways: # a) the shape of each component must be correlated with the data # b) a minimum peak SNR is required over the length of a transient # c) each shape passes a CNN based classifier min_SNR = 2 # signal to noise ratio for accepting a component rval_thr = 0.85 # space correlation threshold for accepting a component cnn_thr = 0.99 # threshold for CNN based classifier cnn_lowest = 0.1 # neurons with cnn probability lower than this value are rejected cnm2.params.set('quality', {'decay_time': decay_time, 'min_SNR': min_SNR, 'rval_thr': rval_thr, 'use_cnn': True, 'min_cnn_thr': cnn_thr, 'cnn_lowest': cnn_lowest}) cnm2.estimates.evaluate_components(images, cnm2.params, dview=dview) # %% PLOT COMPONENTS cnm2.estimates.plot_contours(img=Cn, idx=cnm2.estimates.idx_components) # %% VIEW TRACES (accepted and rejected) if display_images: cnm2.estimates.view_components(images, img=Cn, idx=cnm2.estimates.idx_components) cnm2.estimates.view_components(images, img=Cn, idx=cnm2.estimates.idx_components_bad) #%% update object with selected components cnm2.estimates.select_components(use_object=True) #%% Extract DF/F values cnm2.estimates.detrend_df_f(quantileMin=8, frames_window=250) #%% Show final traces cnm2.estimates.view_components(img=Cn) #%% cnm2.estimates.Cn = Cn cnm2.save(cnm2.mmap_file[:-4] + 'hdf5') #%% reconstruct denoised movie (press q to exit) if display_images: cnm2.estimates.play_movie(images, q_max=99.9, gain_res=2, magnification=2, bpx=border_to_0, include_bck=False) # background not shown #%% STOP CLUSTER and clean up log files cm.stop_server(dview=dview) log_files = glob.glob('*_LOG_*') for log_file in log_files: os.remove(log_file)
for counter, mm in enumerate(mc[:num_frames]): print(counter) t1 = time() new_img = cv2.remap( mm, mapX_res[counter], mapY_res[counter], cv2.INTER_CUBIC, None, cv2.BORDER_CONSTANT) new_ms[counter] = new_img # cv2.imshow('frame',(new_img-bl)*1./fact*5.) # if cv2.waitKey(1) & 0xFF == ord('q'): # break times.append(time() - t1) # cv2.destroyAllWindows() #%% cm.movie(np.array(mc[:num_frames] - new_ms[:num_frames]) ).play(gain=50., magnification=1) #%% cm.concatenate([cm.movie(np.array(new_ms[:num_frames]), fr=mc.fr), mc[:num_frames]], axis=2).resize(1, 1, .5).play(gain=2, magnification=3, fr=30, offset=-100) #%% pl.subplot(1, 3, 1) pl.imshow(np.mean(m[:2000], 0), cmap='gray', vmax=200) pl.subplot(1, 3, 2) pl.imshow(np.mean(new_ms[:2000], 0), cmap='gray', vmax=200) pl.subplot(1, 3, 3) pl.imshow(np.mean(new_ms[:2000], 0) - np.mean(m[:2000], 0), cmap='gray') #%% cm.movie(np.array(m[:2000] - new_ms[:2000])).play() #%% from multiprocessing import Pool pl = Pool(processes=5) def my_fun(X):
def main(): pass # For compatibility between running under Spyder and the CLI #%% Select file(s) to be processed (download if not present) root = '/Users/hheiser/Desktop/testing data/chronic_M2N3/0d_baseline/channel1' fnames = [r'W:\Neurophysiology-Storage1\Wahl\Hendrik\PhD\Data\Batch2\M22\20191208\N1\1\file_00003.tif', r'W:\Neurophysiology-Storage1\Wahl\Hendrik\PhD\Data\Batch2\M22\20191208\N1\2\file_00004.tif', r'W:\Neurophysiology-Storage1\Wahl\Hendrik\PhD\Data\Batch2\M22\20191208\N1\3\file_00005.tif', r'W:\Neurophysiology-Storage1\Wahl\Hendrik\PhD\Data\Batch2\M22\20191208\N1\4\file_00006.tif', r'W:\Neurophysiology-Storage1\Wahl\Hendrik\PhD\Data\Batch2\M22\20191208\N1\5\file_00007.tif', r'W:\Neurophysiology-Storage1\Wahl\Hendrik\PhD\Data\Batch2\M22\20191208\N1\6\file_00008.tif'] fnames = ['Sue_2x_3000_40_-46.tif'] # filename to be processed if fnames[0] in ['Sue_2x_3000_40_-46.tif', 'demoMovie.tif']: fnames = [download_demo(fnames[0])] #%% First setup some parameters for data and motion correction # dataset dependent parameters fr = 30 # imaging rate in frames per second decay_time = 0.4 # length of a typical transient in seconds dxy = (2., 2.) # spatial resolution in x and y in (um per pixel) # note the lower than usual spatial resolution here max_shift_um = (12., 12.) # maximum shift in um patch_motion_um = (100., 100.) # patch size for non-rigid correction in um # motion correction parameters pw_rigid = True # flag to select rigid vs pw_rigid motion correction # maximum allowed rigid shift in pixels max_shifts = [int(a/b) for a, b in zip(max_shift_um, dxy)] # start a new patch for pw-rigid motion correction every x pixels strides = tuple([int(a/b) for a, b in zip(patch_motion_um, dxy)]) # overlap between pathes (size of patch in pixels: strides+overlaps) overlaps = (24, 24) # maximum deviation allowed for patch with respect to rigid shifts max_deviation_rigid = 3 mc_dict = { 'fnames': fnames, 'fr': fr, 'decay_time': decay_time, 'dxy': dxy, 'pw_rigid': pw_rigid, 'max_shifts': max_shifts, 'strides': strides, 'overlaps': overlaps, 'max_deviation_rigid': max_deviation_rigid, 'border_nan': 'copy' } opts = params.CNMFParams(params_dict=mc_dict) #%% play the movie (optional) # playing the movie using opencv. It requires loading the movie in memory. # To close the video press q display_images = True if display_images: m_orig = cm.load_movie_chain(fnames) ds_ratio = 0.2 moviehandle = m_orig.resize(1, 1, ds_ratio) moviehandle.play(q_max=99.5, fr=30, magnification=1, do_loop=False) #%% start a cluster for parallel processing c, dview, n_processes = cm.cluster.setup_cluster( backend='local', n_processes=None, single_thread=False) #%% MOTION CORRECTION # first we create a motion correction object with the specified parameters mc = MotionCorrect(fnames, dview=dview, **opts.get_group('motion')) # note that the file is not loaded in memory #%% Run (piecewise-rigid motion) correction using NoRMCorre mc.motion_correct(save_movie=True) #%% compare with original movie if display_images: m_orig = cm.load_movie_chain(fnames[:3]) m_els = cm.load(mmap_file[:3]) ds_ratio = 0.2 moviehandle = cm.concatenate([m_orig.resize(1, 1, ds_ratio) - mc.min_mov*mc.nonneg_movie, m_els.resize(1, 1, ds_ratio)], axis=2) moviehandle.play(fr=15, q_max=99.5, magnification=2) # press q to exit #%% MEMORY MAPPING border_to_0 = 0 if mc.border_nan == 'copy' else mc.border_to_0 # you can include the boundaries of the FOV if you used the 'copy' option # during motion correction, although be careful about the components near # the boundaries mmap_file = [r'W:\Neurophysiology-Storage1\Wahl\Hendrik\PhD\Data\Batch2\M19\20191219\N2\1\file_00001_els__d1_512_d2_512_d3_1_order_F_frames_1147_.mmap', r'W:\Neurophysiology-Storage1\Wahl\Hendrik\PhD\Data\Batch2\M19\20191219\N2\2\file_00002_els__d1_512_d2_512_d3_1_order_F_frames_2520_.mmap', r'W:\Neurophysiology-Storage1\Wahl\Hendrik\PhD\Data\Batch2\M19\20191219\N2\3\file_00003_els__d1_512_d2_512_d3_1_order_F_frames_3814_.mmap', r'W:\Neurophysiology-Storage1\Wahl\Hendrik\PhD\Data\Batch2\M19\20191219\N2\4\file_00004_els__d1_512_d2_512_d3_1_order_F_frames_5154_.mmap', r'W:\Neurophysiology-Storage1\Wahl\Hendrik\PhD\Data\Batch2\M19\20191219\N2\5\file_00005_els__d1_512_d2_512_d3_1_order_F_frames_2677_.mmap', r'W:\Neurophysiology-Storage1\Wahl\Hendrik\PhD\Data\Batch2\M19\20191219\N2\6\file_00006_els__d1_512_d2_512_d3_1_order_F_frames_3685_.mmap'] fname_new = r'E:\PhD\Data\DG\M14_20191014\N1\memmap__d1_512_d2_512_d3_1_order_C_frames_34939_.mmap' # memory map the file in order 'C' fname_new = cm.save_memmap(mc.mmap_file, base_name='memmap_', order='C', border_to_0=0) # exclude borders # now load the file Yr, dims, T = cm.load_memmap(fname_new) images = np.reshape(Yr.T, [T] + list(dims), order='F') # load frames in python format (T x X x Y) #%% restart cluster to clean up memory cm.stop_server(dview=dview) c, dview, n_processes = cm.cluster.setup_cluster( backend='local', n_processes=None, single_thread=False) #%% parameters for source extraction and deconvolution p = 1 # order of the autoregressive system gnb = 2 # number of global background components merge_thr = 0.85 # merging threshold, max correlation allowed rf = 15 # half-size of the patches in pixels. e.g., if rf=25, patches are 50x50 stride_cnmf = 6 # amount of overlap between the patches in pixels K = 4 # number of components per patch gSig = [4, 4] # expected half size of neurons in pixels # initialization method (if analyzing dendritic data using 'sparse_nmf') method_init = 'greedy_roi' ssub = 2 # spatial subsampling during initialization tsub = 2 # temporal subsampling during intialization # parameters for component evaluation opts_dict = {'fnames': fnames, 'fr': fr, 'nb': gnb, 'rf': rf, 'K': K, 'gSig': gSig, 'stride': stride_cnmf, 'method_init': method_init, 'rolling_sum': True, 'merge_thr': merge_thr, 'n_processes': n_processes, 'only_init': True, 'ssub': ssub, 'tsub': tsub} opts.change_params(params_dict=opts_dict) #%% RUN CNMF ON PATCHES # First extract spatial and temporal components on patches and combine them # for this step deconvolution is turned off (p=0) #opts.change_params({'p': 1,'rf':None, 'only_init':False}) opts.change_params({'p': 0}) cnm = cnmf.CNMF(n_processes, params=opts, dview=dview) cnm = cnm.fit(images) #%% RUN CNMF SEEDED WITH MANUAL MASK # load mask mask = np.asarray(imageio.imread('/Users/hheiser/Desktop/testing data/file_00020_no_motion/avg_mask_fixed.png'), dtype=bool) # get component ROIs from the mask and plot them Ain, labels, mR = cm.base.rois.extract_binary_masks(mask) # plot original mask and extracted labels to check mask fig, ax = plt.subplots(1,2) ax[0].imshow(-mR,cmap='binary') ax[0].set_title('Original mask') ax[1].imshow(labels) ax[1].set_title('Extracted labelled ROIs') """" plt.figure() crd = cm.utils.visualization.plot_contours( Ain.astype('float32'), mR, thr=0.99, display_numbers=True) # todo check if this is important for the pipeline plt.title('Contour plots of detected ROIs in the structural channel') """ opts.change_params({'rf': None, 'only_init': False}) # run CNMF seeded with this mask cnm_corr = cnmf.CNMF(n_processes, params=opts, dview=dview, Ain=Ain) cnm_corr_seed = cnm_corr_seed.fit(images) #cnm_seed = cnm_seed.fit_file(motion_correct=False) #%% ALTERNATE WAY TO RUN THE PIPELINE AT ONCE # you can also perform the motion correction plus cnmf fitting steps # simultaneously after defining your parameters object using # cnm1 = cnmf.CNMF(n_processes, params=opts, dview=dview) # cnm1.fit_file(motion_correct=True) #%% plot contours of found components Cn = cm.local_correlations(images, swap_dim=False) Cn[np.isnan(Cn)] = 0 cnm.estimates.plot_contours(img=Cn) plt.title('Contour plots of found components') #%% RE-RUN seeded CNMF on accepted patches to refine and perform deconvolution cnm.params.change_params({'p': p}) cnm2 = cnm.refit(images, dview=dview) #%% COMPONENT EVALUATION # the components are evaluated in three ways: # a) the shape of each component must be correlated with the data # b) a minimum peak SNR is required over the length of a transient # c) each shape passes a CNN based classifier min_SNR = 2 # signal to noise ratio for accepting a component rval_thr = 0.85 # space correlation threshold for accepting a component cnn_thr = 0.99 # threshold for CNN based classifier cnn_lowest = 0.1 # neurons with cnn probability lower than this value are rejected cnm2.params.set('quality', {'decay_time': decay_time, 'min_SNR': min_SNR, 'rval_thr': rval_thr, 'use_cnn': True, 'min_cnn_thr': cnn_thr, 'cnn_lowest': cnn_lowest}) cnm2.estimates.evaluate_components(images, params=cnm2.params, dview=dview) #%% PLOT COMPONENTS cnm2.estimates.plot_contours(img=Cn, idx=cnm2.estimates.idx_components) #%% VIEW TRACES (accepted and rejected) if display_images: cnm2.estimates.view_components(images, img=Cn, idx=cnm2.estimates.idx_components) cnm2.estimates.view_components(images, img=Cn, idx=cnm2.estimates.idx_components_bad) #%% update object with selected components #### -> will delete rejected components! cnm2.estimates.select_components(use_object=True) #%% Extract DF/F values cnm2.estimates.detrend_df_f(quantileMin=8, frames_window=250) #%% Show final traces cnm2.estimates.view_components(img=Cn) #%% reconstruct denoised movie (press q to exit) if display_images: cnm2.estimates.play_movie(images, q_max=99.9, gain_res=2, magnification=1, bpx=border_to_0, include_bck=True) # background not shown #%% STOP CLUSTER and clean up log files cm.stop_server(dview=dview) log_files = glob.glob('*_LOG_*') for log_file in log_files: os.remove(log_file) #%% save results dirname = fnames[0][:-4] + "_results.hdf5" cnm2.estimates.Cn = Cn cnm2.save(dirname) #load results cnm2 = cnmf.load_CNMF(dirname) mov_name = fnames[0][:-4] + "_movie_restored_2_gain.tif" helper.save_movie(cnm2.estimates,images,mov_name,frame_range=range(200),include_bck=True)
import psutil import sys from ipyparallel import Client from skimage.external.tifffile import TiffFile import scipy #%% from caiman.motion_correction import tile_and_correct, motion_correction_piecewise from caiman.source_extraction.cnmf import cnmf as cnmf from caiman.components_evaluation import evaluate_components from caiman.utils.visualization import plot_contours, view_patches_bar from caiman.base.rois import extract_binary_masks_blob #%% m = cm.load('example_movies/demoMovie.tif') cm.concatenate([m.resize(1, 1, .2), m.resize(1, 1, .2)], axis=1).play(fr=20, gain=3., magnification=3) #%% set parameters and create template by RIGID MOTION CORRECTION # params_movie = {'fname':'example_movies/Sue_2x_3000_40_-46.tif', # 'max_shifts':(6,6), # maximum allow rigid shift # 'splits_rig':56, # for parallelization split the movies in num_splits chuncks across time # 'num_splits_to_process_rig':None, # if none all the splits are processed and the movie is saved # 'strides': (48,48), # intervals at which patches are laid out for motion correction # 'overlaps': (24,24), # overlap between pathes (size of patch strides+overlaps) # 'splits_els':56, # for parallelization split the movies in num_splits chuncks across time # 'num_splits_to_process_els':[28,None], # if none all the splits are processed and the movie is saved # 'upsample_factor_grid':4, # upsample factor to avoid smearing when merging patches # 'max_deviation_rigid':3, #maximum deviation allowed for patch with respect to rigid shift # 'p': 1, # order of the autoregressive system # 'merge_thresh' : 0.8, # merging threshold, max correlation allowed # 'rf' : 15, # half-size of the patches in pixels. rf=25, patches are 50x50 # 'stride_cnmf' : 6, # amounpl.it of overlap between the patches in pixels
# # total_shifts.append(total_shift) # start_steps.append(start_step) # xy_grids.append(xy_grid) #mc = cm.load('M_FLUO_4_d1_64_d2_128_d3_1_order_F_frames_4620_.mmap') #mc = cm.load('M_FLUO_t_d1_64_d2_128_d3_1_order_F_frames_6764_.mmap') #%% mc.resize(1,1,.2).play(gain=30,fr = 30, offset = 300,magnification=1.) #%% m.resize(1,1,.2).play(gain=10,fr = 30, offset = 0,magnification=1.) #%% cm.concatenate([mr.resize(1,1,.5),mc.resize(1,1,.5)],axis=1).play(gain=10,fr = 100, offset = 300,magnification=1.) #%% import h5py with h5py.File('sueann_pw_rigid_movie.mat') as f: mef = np.array(f['M2']) mef = cm.movie(mef.transpose([0,2,1])) #%% cm.concatenate([mef.resize(1,1,.15),mc.resize(1,1,.15)],axis=1).play(gain=30,fr = 40, offset = 300,magnification=1.) #%% (mef-mc).resize(1,1,.1).play(gain=50,fr = 20, offset = 0,magnification=1.) #%% (mc-mef).resize(1,1,.1).play(gain=50,fr = 20, offset = 0,magnification=1.) #%%
#%% pl.imshow(img,interpolation='none') #%% cc=scipy.sparse.csgraph.connected_components(img,directed=False) #distanceMatrix=distanceMatrix/np.max(distanceMatrix) #%% m=load(fname,fr=30).resize(1,1,.2) #%% shifts_,xcorrs_,template_ = m.motion_correction_online(init_frames_template=100,max_shift_h=5,max_shift_w=5) #%% shifts,xcorrs,template = m.motion_correction_online(template=template_,min_count=len(m),max_shift_h=5,max_shift_w=5) #%% mov_path=m.apply_shifts_online(shifts,save_base_name='/tmp/test') #%% m=load(mov_path,fr=30*.2) m1=cm.concatenate([m.resize(1,1,1),mh.resize(1,1,1)],axis=1) (m1-np.percentile(m1,8)).play(backend='opencv',fr=100,gain=3.,magnification=5) #%% #mc=m.motion_correct(5,5) #%% #m1=mc[0].resize(1,1,.2) #%% (m1-np.percentile(m1,8)).play(backend='opencv',fr=100,gain=10.,magnification=5) #%% final_frate=15 is_patches=True is_dendrites=False
def run_alignment(mouse, sessions, motion_correction_v, cropping_v, dview): """ This is the main function for the alignment step. It applies methods from the CaImAn package used originally in motion correction to do alignment. """ for session in sessions: # Update the database file_name = f"mouse_{mouse}_session_{session}_alignment" sql1 = "UPDATE Analysis SET alignment_main=? WHERE mouse = ? AND session=? AND motion_correction_v =? AND cropping_v=? " val1 = [file_name, mouse, session, motion_correction_v, cropping_v] cursor.execute(sql1, val1) # Determine the output .mmap file name output_mmap_file_path = os.environ[ 'DATA_DIR_LOCAL'] + f'data/interim/alignment/main/{file_name}.mmap' sql = "SELECT motion_correction_main FROM Analysis WHERE mouse = ? AND session=? AND motion_correction_v =? AND cropping_v=? " val = [mouse, session, motion_correction_v, cropping_v] cursor.execute(sql, val) result = cursor.fetchall() input_mmap_file_list = [] inter = [] for x in result: inter += x for y in inter: input_mmap_file_list.append(y) sql = "SELECT motion_correction_cropping_points_x1 FROM Analysis WHERE mouse = ? AND session=?AND motion_correction_v =? AND cropping_v=? " val = [mouse, session, motion_correction_v, cropping_v] cursor.execute(sql, val) result = cursor.fetchall() x_ = [] inter = [] for i in result: inter += i for j in range(0, len(inter)): x_.append(inter[j]) sql = "SELECT motion_correction_cropping_points_x2 FROM Analysis WHERE mouse = ? AND session=? AND motion_correction_v =? AND cropping_v=? " val = [mouse, session, motion_correction_v, cropping_v] cursor.execute(sql, val) result = cursor.fetchall() _x = [] inter = [] for i in result: inter += i for j in range(0, len(inter)): _x.append(inter[j]) sql = "SELECT motion_correction_cropping_points_y1 FROM Analysis WHERE mouse = ? AND session=? AND motion_correction_v =? AND cropping_v=?" val = [mouse, session, motion_correction_v, cropping_v] cursor.execute(sql, val) result = cursor.fetchall() _y = [] inter = [] for i in result: inter += i for j in range(0, len(inter)): _y.append(inter[j]) sql = "SELECT motion_correction_cropping_points_y2 FROM Analysis WHERE mouse = ? AND session=? AND motion_correction_v =? AND cropping_v=?" val = [mouse, session, motion_correction_v, cropping_v] cursor.execute(sql, val) result = cursor.fetchall() y_ = [] inter = [] for i in result: inter += i for j in range(0, len(inter)): y_.append(inter[j]) new_x1 = max(x_) new_x2 = max(_x) new_y1 = max(y_) new_y2 = max(_y) m_list = [] for i in range(len(input_mmap_file_list)): m = cm.load(input_mmap_file_list[i]) m = m.crop(new_x1 - x_[i], new_x2 - _x[i], new_y1 - y_[i], new_y2 - _y[i], 0, 0) m_list.append(m) # Concatenate them using the concat function m_concat = cm.concatenate(m_list, axis=0) fname = m_concat.save(output_mmap_file_path, order='C') # MOTION CORRECTING EACH INDIVIDUAL MOVIE WITH RESPECT TO A TEMPLATE MADE OF THE FIRST MOVIE logging.info( 'Performing motion correction on all movies with respect to a template made of the first movie.' ) t0 = datetime.datetime.today() # parameters alignment sql5 = "SELECT make_template_from_trial,gSig_filt,max_shifts,niter_rig,strides,overlaps,upsample_factor_grid,num_frames_split,max_deviation_rigid,shifts_opencv,use_conda,nonneg_movie, border_nan FROM Analysis WHERE alignment_main=? " val5 = [ file_name, ] cursor.execute(sql5, val5) myresult = cursor.fetchall() para = [] aux = [] for x in myresult: aux = x for y in aux: para.append(y) parameters = { 'make_template_from_trial': para[0], 'gSig_filt': (para[1], para[1]), 'max_shifts': (para[2], para[2]), 'niter_rig': para[3], 'strides': (para[4], para[4]), 'overlaps': (para[5], para[5]), 'upsample_factor_grid': para[6], 'num_frames_split': para[7], 'max_deviation_rigid': para[8], 'shifts_opencv': para[9], 'use_cuda': para[10], 'nonneg_movie': para[11], 'border_nan': para[12] } # Create a template of the first movie template_index = parameters['make_template_from_trial'] m0 = cm.load(input_mmap_file_list[1]) [x1, x2, y1, y2] = [x_, _x, y_, _y] for i in range(len(input_mmap_file_list)): m0 = m0.crop(new_x1 - x_[i], new_x2 - _x[i], new_y1 - y_[i], new_y2 - _y[i], 0, 0) m0_filt = cm.movie( np.array([ high_pass_filter_space(m_, parameters['gSig_filt']) for m_ in m0 ])) template0 = cm.motion_correction.bin_median( m0_filt.motion_correct( 5, 5, template=None)[0]) # may be improved in the future # Setting the parameters opts = params.CNMFParams(params_dict=parameters) # Create a motion correction object mc = MotionCorrect(fname, dview=dview, **opts.get_group('motion')) # Perform non-rigid motion correction mc.motion_correct(template=template0, save_movie=True) # Cropping borders x_ = math.ceil( abs(np.array(mc.shifts_rig)[:, 1].max() ) if np.array(mc.shifts_rig)[:, 1].max() > 0 else 0) _x = math.ceil( abs(np.array(mc.shifts_rig)[:, 1].min() ) if np.array(mc.shifts_rig)[:, 1].min() < 0 else 0) y_ = math.ceil( abs(np.array(mc.shifts_rig)[:, 0].max() ) if np.array(mc.shifts_rig)[:, 0].max() > 0 else 0) _y = math.ceil( abs(np.array(mc.shifts_rig)[:, 0].min() ) if np.array(mc.shifts_rig)[:, 0].min() < 0 else 0) # Load the motion corrected movie into memory movie = cm.load(mc.fname_tot_rig[0]) # Crop all movies to those border pixels movie.crop(x_, _x, y_, _y, 0, 0) sql1 = "UPDATE Analysis SET alignment_x1=?, alignment_x2 =?, alignment_y1=?, alignment_y2=? WHERE mouse = ? AND session=? AND motion_correction_v =? AND cropping_v=?" val1 = [ x_, _x, y_, _y, mouse, session, motion_correction_v, cropping_v ] cursor.execute(sql1, val1) # save motion corrected and cropped movie output_mmap_file_path_tot = movie.save( os.environ['DATA_DIR_LOCAL'] + f'data/interim/alignment/main/{file_name}.mmap', order='C') logging.info( f' Cropped and saved rigid movie as {output_mmap_file_path_tot}') # Remove the remaining non-cropped movie os.remove(mc.fname_tot_rig[0]) # Create a timeline and store it sql = "SELECT trial FROM Analysis WHERE mouse = ? AND session=? AND motion_correction_v =? AND cropping_v=?" val = [mouse, session, motion_correction_v, cropping_v] cursor.execute(sql, val) result = cursor.fetchall() trial_index_list = [] inter = [] for i in result: inter += i for j in range(0, len(inter)): trial_index_list.append(inter[j]) timeline = [[trial_index_list[0], 0]] timepoints = [0] for i in range(1, len(m_list)): m = m_list[i] timeline.append( [trial_index_list[i], timeline[i - 1][1] + m.shape[0]]) timepoints.append(timepoints[i - 1] + m.shape[0]) timeline_pkl_file_path = os.environ[ 'DATA_DIR'] + f'data/interim/alignment/meta/timeline/{file_name}.pkl' with open(timeline_pkl_file_path, 'wb') as f: pickle.dump(timeline, f) sql1 = "UPDATE Analysis SET alignment_timeline=? WHERE mouse = ? AND session=?AND motion_correction_v =? AND cropping_v=? " val1 = [ timeline_pkl_file_path, mouse, session, motion_correction_v, cropping_v ] cursor.execute(sql1, val1) timepoints.append(movie.shape[0]) dt = int((datetime.datetime.today() - t0).seconds / 60) # timedelta in minutes sql1 = "UPDATE Analysis SET alignment_duration_concatenation=? WHERE mouse = ? AND session=?AND motion_correction_v =? AND cropping_v=? " val1 = [dt, mouse, session, motion_correction_v, cropping_v] cursor.execute(sql1, val1) logging.info(f' Performed concatenation. dt = {dt} min.') ## modify all motion correction file to the aligned version data_dir = os.environ[ 'DATA_DIR'] + 'data/interim/motion_correction/main/' for i in range(len(input_mmap_file_list)): aligned_movie = movie[timepoints[i]:timepoints[i + 1]] motion_correction_output_aligned = aligned_movie.save( data_dir + file_name + '_els' + '.mmap', order='C') sql1 = "UPDATE Analysis SET motion_correct_align=? WHERE motion_correction_meta=? AND motion_correction_v" val1 = [ motion_correction_output_aligned, input_mmap_file_list[i], motion_correction_v ] cursor.execute(sql1, val1) database.commit() return
# makes estimation numerically better: movie -= movie.mean() # use one every 200 frames temporal_stride = 200 # use one every 8 patches (patches are 8x8 by default) spatial_stride = 8 movie_train = movie[::temporal_stride] t = timeit.default_timer() estimation_res = est.estimate_vst_movie(movie_train, stride=spatial_stride) print('\tTime', timeit.default_timer() - t) alpha = estimation_res.alpha sigma_sq = estimation_res.sigma_sq movie_gat = compute_gat(movie, sigma_sq, alpha=alpha) # save movie_gat here movie_gat_inv = compute_inverse_gat(movie_gat, sigma_sq, alpha=alpha, method='asym') # save movie_gat_inv here return movie, movie_gat_inv #%% movie, movie_gat_inv = main() #%% cm.concatenate([movie,movie_gat_inv],axis=1).play(gain = 10, magnification=4)
B = b0[:, None] + (np.repeat( np.repeat( W.dot( downscale(B.reshape(dims + (B.shape[-1], ), order='F'), ( ssub_B, ssub_B, 1)).reshape((-1, B.shape[-1]), order='F') - downscale(b0.reshape(dims, order='F'), ( ssub_B, ssub_B)).reshape((-1, 1), order='F')).reshape( ((dims[0] - 1) // ssub_B + 1, (dims[1] - 1) // ssub_B + 1, -1), order='F'), ssub_B, 0), ssub_B, 1)[:dims[0], :dims[1]].reshape((-1, B.shape[-1]), order='F')) B = B.reshape(dims + (-1, ), order='F').transpose([2, 0, 1]) Y_res = images[frame_range] - Y_rec - B mov = cm.concatenate((images, Y_rec), axis=2) mov = cm.concatenate((images, images), axis=2) #%% Select the row to be source extracted using current versions of cropping and motion correction selected_rows = db.select(states_df, 'source_extraction', mouse=mouse_number, session=session, is_rest=is_rest, cropping_v=cropping_version, motion_correction_v=motion_correction_version) gSig = 5 gSiz = 4 * gSig + 1
#distanceMatrix=distanceMatrix/np.max(distanceMatrix) #%% m = load(fname, fr=30).resize(1, 1, .2) #%% shifts_, xcorrs_, template_ = m.motion_correction_online( init_frames_template=100, max_shift_h=5, max_shift_w=5) #%% shifts, xcorrs, template = m.motion_correction_online(template=template_, min_count=len(m), max_shift_h=5, max_shift_w=5) #%% mov_path = m.apply_shifts_online(shifts, save_base_name='/tmp/test') #%% m = load(mov_path, fr=30 * .2) m1 = cm.concatenate([m.resize(1, 1, 1), mh.resize(1, 1, 1)], axis=1) (m1 - np.percentile(m1, 8)).play(backend='opencv', fr=100, gain=3., magnification=5) #%% #mc=m.motion_correct(5,5) #%% #m1=mc[0].resize(1,1,.2) #%% (m1 - np.percentile(m1, 8)).play(backend='opencv', fr=100, gain=10., magnification=5)
#m_denoised_now=(m_denoised_now- np.mean(m_denoised_now, axis=0)) #m_denoised_now =np.diff(m_denoised_now,axis = 0) #m_denoised_now = (m_denoised_now - np.mean(m_denoised_now, axis=(1,2))[:,np.newaxis,np.newaxis]) if baselinesubtract : m_denoised_baseline = voltage_imaging_utils.moving_average(m_denoised_now, n=baseline_window) m_denoised_now = m_denoised_now/m_denoised_baseline #% m_mocorr_denoised_now = m_mocorr_denoised[startframe:startframe+framenum,:,:]#.copy() #m_mocorr_denoised_now=(m_mocorr_denoised_now- np.mean(m_mocorr_denoised_now, axis=0)) #m_mocorr_denoised_now =np.diff(m_mocorr_denoised_now,axis = 0) if baselinesubtract : m_mocorr_denoised_baseline = voltage_imaging_utils.moving_average(m_mocorr_denoised_now, n=baseline_window) m_mocorr_denoised_now = m_mocorr_denoised_now/m_mocorr_denoised_baseline #% m_now = cm.concatenate([m_orig_now,m_volpy_now, m_denoised_now,m_mocorr_denoised_now], axis=1)# #%% #%% m_now = cm.concatenate([m_orig_now,m_volpy_now], axis=1)# #%% m_now.play(fr=900, magnification=2,q_max=99.9, q_min=0.1,save_movie = True) #m_orig = cm.load(allfnames[0:3]) #m_volpy_now.play(fr=400, magnification=1,q_max=99.5, q_min=0.5,save_movie = False) #%% Szar van a palacsintaban subject_ids,movie_names,frame_times,sessions,movie_numbers = (imaging.Movie*imaging.MovieFrameTimes()).fetch('subject_id','movie_name','frame_times','session','movie_number') for subject_id,movie_name,frame_time,session,movie_number in zip(subject_ids,movie_names,frame_times,sessions,movie_numbers): frametimediff = np.diff(frame_time) if np.min(frametimediff)<.5*np.median(frametimediff): key = {'subject_id':subject_id,'movie_number':movie_number,'session':session} fig=plt.figure() ax = fig.add_axes([0,0,1,1])
def main(): pass # For compatibility between running under Spyder and the CLI # %% Load demo movie and ROIs fnames = download_demo('demo_voltage_imaging.hdf5', 'volpy') # file path to movie file (will download if not present) path_ROIs = download_demo('demo_voltage_imaging_ROIs.hdf5', 'volpy') # file path to ROIs file (will download if not present) # %% Setup some parameters for data and motion correction # dataset parameters fr = 400 # sample rate of the movie ROIs = None # Region of interests index = None # index of neurons weights = None # reuse spatial weights by # opts.change_params(params_dict={'weights':vpy.estimates['weights']}) # motion correction parameters pw_rigid = False # flag for pw-rigid motion correction gSig_filt = (3, 3) # size of filter, in general gSig (see below), # change this one if algorithm does not work max_shifts = (5, 5) # maximum allowed rigid shift strides = (48, 48) # start a new patch for pw-rigid motion correction every x pixels overlaps = (24, 24) # overlap between pathes (size of patch strides+overlaps) max_deviation_rigid = 3 # maximum deviation allowed for patch with respect to rigid shifts border_nan = 'copy' opts_dict = { 'fnames': fnames, 'fr': fr, 'index': index, 'ROIs': ROIs, 'weights': weights, 'pw_rigid': pw_rigid, 'max_shifts': max_shifts, 'gSig_filt': gSig_filt, 'strides': strides, 'overlaps': overlaps, 'max_deviation_rigid': max_deviation_rigid, 'border_nan': border_nan } opts = volparams(params_dict=opts_dict) # %% play the movie (optional) # playing the movie using opencv. It requires loading the movie in memory. # To close the video press q display_images = False if display_images: m_orig = cm.load(fnames) ds_ratio = 0.2 moviehandle = m_orig.resize(1, 1, ds_ratio) moviehandle.play(q_max=99.5, fr=60, magnification=2) # %% start a cluster for parallel processing c, dview, n_processes = cm.cluster.setup_cluster( backend='local', n_processes=None, single_thread=False) # %%% MOTION CORRECTION # Create a motion correction object with the specified parameters mc = MotionCorrect(fnames, dview=dview, **opts.get_group('motion')) # Run piecewise rigid motion correction mc.motion_correct(save_movie=True) dview.terminate() # %% motion correction compared with original movie display_images = False if display_images: m_orig = cm.load(fnames) m_rig = cm.load(mc.mmap_file) ds_ratio = 0.2 moviehandle = cm.concatenate([m_orig.resize(1, 1, ds_ratio) - mc.min_mov * mc.nonneg_movie, m_rig.resize(1, 1, ds_ratio)], axis=2) moviehandle.play(fr=60, q_max=99.5, magnification=2) # press q to exit # % movie subtracted from the mean m_orig2 = (m_orig - np.mean(m_orig, axis=0)) m_rig2 = (m_rig - np.mean(m_rig, axis=0)) moviehandle1 = cm.concatenate([m_orig2.resize(1, 1, ds_ratio), m_rig2.resize(1, 1, ds_ratio)], axis=2) moviehandle1.play(fr=60, q_max=99.5, magnification=2) # %% Memory Mapping c, dview, n_processes = cm.cluster.setup_cluster( backend='local', n_processes=None, single_thread=False) border_to_0 = 0 if mc.border_nan == 'copy' else mc.border_to_0 fname_new = cm.save_memmap_join(mc.mmap_file, base_name='memmap_', add_to_mov=border_to_0, dview=dview, n_chunks=10) dview.terminate() # %% change fnames to the new motion corrected one opts.change_params(params_dict={'fnames': fname_new}) # %% SEGMENTATION # Create mean and correlation image use_maskrcnn = True # set to True to predict the ROIs using the mask R-CNN if not use_maskrcnn: # use manual annotations with h5py.File(path_ROIs, 'r') as fl: ROIs = fl['mov'][()] # load ROIs opts.change_params(params_dict={'ROIs': ROIs, 'index': list(range(ROIs.shape[0])), 'method': 'SpikePursuit'}) else: m = cm.load(mc.mmap_file[0], subindices=slice(0, 20000)) m.fr = fr img = m.mean(axis=0) img = (img-np.mean(img))/np.std(img) m1 = m.computeDFF(secsWindow=1, in_place=True)[0] m = m - m1 Cn = m.local_correlations(swap_dim=False, eight_neighbours=True) img_corr = (Cn-np.mean(Cn))/np.std(Cn) summary_image = np.stack([img, img, img_corr], axis=2).astype(np.float32) del m del m1 # %% # Mask R-CNN config = neurons.NeuronsConfig() class InferenceConfig(config.__class__): # Run detection on one image at a time GPU_COUNT = 1 IMAGES_PER_GPU = 1 DETECTION_MIN_CONFIDENCE = 0.7 IMAGE_RESIZE_MODE = "pad64" IMAGE_MAX_DIM = 512 RPN_NMS_THRESHOLD = 0.7 POST_NMS_ROIS_INFERENCE = 1000 config = InferenceConfig() config.display() model_dir = os.path.join(caiman_datadir(), 'model') DEVICE = "/cpu:0" # /cpu:0 or /gpu:0 with tf.device(DEVICE): model = modellib.MaskRCNN(mode="inference", model_dir=model_dir, config=config) weights_path = download_model('mask_rcnn') model.load_weights(weights_path, by_name=True) results = model.detect([summary_image], verbose=1) r = results[0] ROIs_mrcnn = r['masks'].transpose([2, 0, 1]) # %% visualize the result display_result = False if display_result: _, ax = plt.subplots(1,1, figsize=(16,16)) visualize.display_instances(summary_image, r['rois'], r['masks'], r['class_ids'], ['BG', 'neurons'], r['scores'], ax=ax, title="Predictions") # %% set rois opts.change_params(params_dict={'ROIs':ROIs_mrcnn, 'index':list(range(ROIs_mrcnn.shape[0])), 'method':'SpikePursuit'}) # %% Trace Denoising and Spike Extraction c, dview, n_processes = cm.cluster.setup_cluster( backend='local', n_processes=None, single_thread=False, maxtasksperchild=1) vpy = VOLPY(n_processes=n_processes, dview=dview, params=opts) vpy.fit(n_processes=n_processes, dview=dview) # %% some visualization print(np.where(vpy.estimates['passedLocalityTest'])[0]) # neurons that pass locality test n = 0 # Processed signal and spikes of neurons plt.figure() plt.plot(vpy.estimates['trace'][n]) plt.plot(vpy.estimates['spikeTimes'][n], np.max(vpy.estimates['trace'][n]) * np.ones(vpy.estimates['spikeTimes'][n].shape), color='g', marker='o', fillstyle='none', linestyle='none') plt.title('signal and spike times') plt.show() # Location of neurons by Mask R-CNN or manual annotation plt.figure() if use_maskrcnn: plt.imshow(ROIs_mrcnn[n]) else: plt.imshow(ROIs[n]) mv = cm.load(fname_new) plt.imshow(mv.mean(axis=0),alpha=0.5) # Spatial filter created by algorithm plt.figure() plt.imshow(vpy.estimates['spatialFilter'][n]) plt.colorbar() plt.title('spatial filter') plt.show() # %% STOP CLUSTER and clean up log files cm.stop_server(dview=dview) log_files = glob.glob('*_LOG_*') for log_file in log_files: os.remove(log_file)
def play_movie(self, imgs, q_max=99.75, q_min=2, gain_res=1, magnification=1, include_bck=True, frame_range=slice(None, None, None), bpx=0): """Displays a movie with three panels (original data (left panel), reconstructed data (middle panel), residual (right panel)) Args: imgs: np.array (possibly memory mapped, t,x,y[,z]) Imaging data q_max: float (values in [0, 100]) percentile for maximum plotting value q_min: float (values in [0, 100]) percentile for minimum plotting value gain_res: float amplification factor for residual movie magnification: float magnification factor for whole movie include_bck: bool flag for including background in original and reconstructed movie frame_rage: range or slice or list display only a subset of frames bpx: int number of pixels to exclude on each border Returns: self (to stop the movie press 'q') """ dims = imgs.shape[1:] if 'movie' not in str(type(imgs)): imgs = caiman.movie(imgs) Y_rec = self.A.dot(self.C[:, frame_range]) Y_rec = Y_rec.reshape(dims + (-1, ), order='F') Y_rec = Y_rec.transpose([2, 0, 1]) if self.b is not None and self.f is not None: B = self.b.dot(self.f[:, frame_range]) if 'matrix' in str(type(B)): B = B.toarray() B = B.reshape(dims + (-1, ), order='F').transpose([2, 0, 1]) elif self.W is not None: ssub_B = int(round(np.sqrt(np.prod(dims) / self.W.shape[0]))) B = imgs[frame_range].reshape((-1, np.prod(dims)), order='F').T - \ self.A.dot(self.C[:, frame_range]) if ssub_B == 1: B = self.b0[:, None] + self.W.dot(B - self.b0[:, None]) else: B = self.b0[:, None] + (np.repeat( np.repeat( self.W.dot( downscale( B.reshape(dims + (B.shape[-1], ), order='F'), (ssub_B, ssub_B, 1)).reshape( (-1, B.shape[-1]), order='F') - downscale(self.b0.reshape(dims, order='F'), (ssub_B, ssub_B)).reshape( (-1, 1), order='F')).reshape( ((dims[0] - 1) // ssub_B + 1, (dims[1] - 1) // ssub_B + 1, -1), order='F'), ssub_B, 0), ssub_B, 1)[:dims[0], :dims[1]].reshape( (-1, B.shape[-1]), order='F')) B = B.reshape(dims + (-1, ), order='F').transpose([2, 0, 1]) else: B = np.zeros_like(Y_rec) if bpx > 0: B = B[:, bpx:-bpx, bpx:-bpx] Y_rec = Y_rec[:, bpx:-bpx, bpx:-bpx] imgs = imgs[:, bpx:-bpx, bpx:-bpx] Y_res = imgs[frame_range] - Y_rec - B caiman.concatenate( (imgs[frame_range] - (not include_bck) * B, Y_rec + include_bck * B, Y_res * gain_res), axis=2).play(q_min=q_min, q_max=q_max, magnification=magnification) return self
def play_movie(estimates, imgs, q_max=99.75, q_min=2, gain_res=1, magnification=1, include_bck=True, frame_range=slice(None, None, None), bpx=0, thr=0., save_movie=False, movie_name='results_movie.avi'): dims = imgs.shape[1:] if 'movie' not in str(type(imgs)): imgs = cm.movie(imgs) Y_rec = estimates.A.dot(estimates.C[:, frame_range]) Y_rec = Y_rec.reshape(dims + (-1,), order='F') Y_rec = Y_rec.transpose([2, 0, 1]) if estimates.W is not None: ssub_B = int(round(np.sqrt(np.prod(dims) / estimates.W.shape[0]))) B = imgs[frame_range].reshape((-1, np.prod(dims)), order='F').T - \ estimates.A.dot(estimates.C[:, frame_range]) if ssub_B == 1: B = estimates.b0[:, None] + estimates.W.dot(B - estimates.b0[:, None]) else: B = estimates.b0[:, None] + (np.repeat(np.repeat(estimates.W.dot( downscale(B.reshape(dims + (B.shape[-1],), order='F'), (ssub_B, ssub_B, 1)).reshape((-1, B.shape[-1]), order='F') - downscale(estimates.b0.reshape(dims, order='F'), (ssub_B, ssub_B)).reshape((-1, 1), order='F')) .reshape(((dims[0] - 1) // ssub_B + 1, (dims[1] - 1) // ssub_B + 1, -1), order='F'), ssub_B, 0), ssub_B, 1)[:dims[0], :dims[1]].reshape((-1, B.shape[-1]), order='F')) B = B.reshape(dims + (-1,), order='F').transpose([2, 0, 1]) elif estimates.b is not None and estimates.f is not None: B = estimates.b.dot(estimates.f[:, frame_range]) if 'matrix' in str(type(B)): B = B.toarray() B = B.reshape(dims + (-1,), order='F').transpose([2, 0, 1]) else: B = np.zeros_like(Y_rec) if bpx > 0: B = B[:, bpx:-bpx, bpx:-bpx] Y_rec = Y_rec[:, bpx:-bpx, bpx:-bpx] imgs = imgs[:, bpx:-bpx, bpx:-bpx] Y_res = imgs[frame_range] - Y_rec - B mov = cm.concatenate((imgs[frame_range] - (not include_bck) * B, Y_rec, Y_rec + include_bck * B, Y_res * gain_res), axis=2) if thr > 0: if save_movie: import cv2 #fourcc = cv2.VideoWriter_fourcc('8', 'B', 'P', 'S') #fourcc = cv2.VideoWriter_fourcc(*'XVID') fourcc = cv2.VideoWriter_fourcc(*'MP4V') out = cv2.VideoWriter(movie_name, fourcc, 30.0, tuple([int(magnification*s) for s in mov.shape[1:][::-1]])) contours = [] for a in estimates.A.T.toarray(): a = a.reshape(dims, order='F') if bpx > 0: a = a[bpx:-bpx, bpx:-bpx] if magnification != 1: a = cv2.resize(a, None, fx=magnification, fy=magnification, interpolation=cv2.INTER_LINEAR) ret, thresh = cv2.threshold(a, thr * np.max(a), 1., 0) contour, hierarchy = cv2.findContours( thresh.astype('uint8'), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) contours.append(contour) contours.append(list([c + np.array([[a.shape[1], 0]]) for c in contour])) contours.append(list([c + np.array([[2 * a.shape[1], 0]]) for c in contour])) maxmov = np.nanpercentile(mov[0:10], q_max) if q_max < 100 else np.nanmax(mov) minmov = np.nanpercentile(mov[0:10], q_min) if q_min > 0 else np.nanmin(mov) for frame in mov: if magnification != 1: frame = cv2.resize(frame, None, fx=magnification, fy=magnification, interpolation=cv2.INTER_LINEAR) frame = np.clip((frame - minmov) * 255. / (maxmov - minmov), 0, 255) frame = np.repeat(frame[..., None], 3, 2) for contour in contours: cv2.drawContours(frame, contour, -1, (0, 255, 255), 1) cv2.imshow('frame', frame.astype('uint8')) if save_movie: out.write(frame.astype('uint8')) if cv2.waitKey(30) & 0xFF == ord('q'): break if save_movie: out.release() cv2.destroyAllWindows() cv2.destroyAllWindows() else: mov.play(q_min=q_min, q_max=q_max, magnification=magnification, save_movie=save_movie, movie_name=movie_name) return
pl.plot(mc.shifts_rig) pl.legend(['x shifts', 'y shifts']) pl.xlabel('frames') pl.ylabel('pixels') # %% inspect movie downsample_ratio = params_display['downsample_ratio'] # TODO: todocument offset_mov = -np.min(m_orig[:100]) m_rig.resize(1, 1, downsample_ratio).play( gain=10, offset=offset_mov * .25, fr=30, magnification=2, bord_px=bord_px_rig) # %% visualize raw, rigid and pw-rigid motion correted moviews downsample_factor = params_display['downsample_ratio'] # TODO : todocument cm.concatenate( [m_orig.resize(1, 1, downsample_factor) + offset_mov, m_rig.resize(1, 1, downsample_factor)], axis=2).play(fr=60, gain=5, magnification=4, offset=0) # TODO: show screenshot 8 # %% restart cluster to clean up memory # TODO: todocument c, dview, n_processes = cm.cluster.setup_cluster( backend='local', n_processes=None, single_thread=False) # %% save each chunk in F format t1 = time.time() if not 'max_shifts' in params_movie: fnames = params_movie['fname'] border_to_0 = 0 else: # elif not params_movie.has_key('overlaps'): fnames = [mc.fname_tot_rig] border_to_0 = bord_px_rig m_els = m_rig # else:
max_deviation_rigid=max_deviation_rigid, shifts_opencv=True, nonneg_movie=True) # note that the file is not loaded in memory #%% Run piecewise-rigid motion correction using NoRMCorre mc.motion_correct_pwrigid(save_movie=True) m_els = cm.load(mc.fname_tot_els) bord_px_els = np.ceil( np.maximum(np.max(np.abs(mc.x_shifts_els)), np.max(np.abs(mc.y_shifts_els)))).astype(np.int) # maximum shift to be used for trimming against NaNs #%% compare with original movie cm.concatenate([ m_orig.resize(1, 1, downsample_ratio) + offset_mov, m_els.resize(1, 1, downsample_ratio) ], axis=2).play(fr=60, gain=15, magnification=2, offset=0) # press q to exit #%% MEMORY MAPPING # memory map the file in order 'C' fnames = [mc.fname_tot_els] # name of the pw-rigidly corrected file. border_to_0 = bord_px_els # number of pixels to exclude fname_new = cm.save_memmap(fnames, base_name='memmap_', order='C', border_to_0=bord_px_els) # exclude borders # now load the file Yr, dims, T = cm.load_memmap(fname_new) d1, d2 = dims
def main(): pass # For compatibility between running under Spyder and the CLI #%% Select file(s) to be processed (download if not present) # fnames = [os.path.join(caiman_datadir(), 'example_movies/sampled3dMovieRigid.nwb')] fnames = [ os.path.join(caiman_datadir(), 'example_movies/sampled3dMovie2.nwb') ] # filename to be created or processed # dataset dependent parameters fr = 5 # imaging rate in frames per second decay_time = 0.4 # length of a typical transient in seconds starting_time = 0. #%% load the file and save it in the NWB format (if it doesn't exist already) if not os.path.exists(fnames[0]): # fnames_orig = [os.path.join(caiman_datadir(), 'example_movies/sampled3dMovieRigid.h5')] # filename to be processed fnames_orig = [ os.path.join(caiman_datadir(), 'example_movies/sampled3dMovie2.h5') ] # filename to be processed orig_movie = cm.load(fnames_orig, fr=fr, is3D=True) # orig_movie = cm.load_movie_chain(fnames_orig,fr=fr,is3D=True) # save file in NWB format with various additional info orig_movie.save(fnames[0], sess_desc='test', identifier='demo 3d', exp_desc='demo movie', imaging_plane_description='multi plane', emission_lambda=520.0, indicator='none', location='visual cortex', starting_time=starting_time, experimenter='NAOMi', lab_name='Tank Lab', institution='Princeton U', experiment_description='Experiment Description', session_id='Session 1', var_name_hdf5='TwoPhotonSeries') #%% First setup some parameters for data and motion correction # motion correction parameters dxy = (1., 1., 5.) # spatial resolution in x, y, and z in (um per pixel) # note the lower than usual spatial resolution here max_shift_um = (10., 10., 10.) # maximum shift in um patch_motion_um = (50., 50., 30. ) # patch size for non-rigid correction in um # pw_rigid = False # flag to select rigid vs pw_rigid motion correction niter_rig = 1 pw_rigid = True # flag to select rigid vs pw_rigid motion correction # maximum allowed rigid shift in pixels max_shifts = [int(a / b) for a, b in zip(max_shift_um, dxy)] # start a new patch for pw-rigid motion correction every x pixels strides = tuple([int(a / b) for a, b in zip(patch_motion_um, dxy)]) # overlap between pathes (size of patch in pixels: strides+overlaps) overlaps = (24, 24, 4) # maximum deviation allowed for patch with respect to rigid shifts max_deviation_rigid = 3 is3D = True mc_dict = { 'fnames': fnames, 'fr': fr, 'decay_time': decay_time, 'dxy': dxy, 'pw_rigid': pw_rigid, 'niter_rig': niter_rig, 'max_shifts': max_shifts, 'strides': strides, 'overlaps': overlaps, 'max_deviation_rigid': max_deviation_rigid, 'border_nan': 'copy', 'var_name_hdf5': 'acquisition/TwoPhotonSeries', 'is3D': is3D, 'splits_els': 12, 'splits_rig': 12 } opts = params.CNMFParams( params_dict=mc_dict ) #NOTE: default adjustments of parameters are not set yet, manually setting them now # %% play the movie (optional) # playing the movie using opencv. It requires loading the movie in memory. # To close the video press q display_images = True if display_images: m_orig = cm.load_movie_chain(fnames, var_name_hdf5=opts.data['var_name_hdf5'], is3D=True) T, h, w, z = m_orig.shape # Time, plane, height, weight m_orig = np.reshape(np.transpose(m_orig, (3, 0, 1, 2)), (T * z, h, w)) ds_ratio = 0.2 moviehandle = m_orig.resize(1, 1, ds_ratio) moviehandle.play(q_max=99.5, fr=60, magnification=2) # %% start a cluster for parallel processing # NOTE: ignore dview right now for debugging purposes # c, dview, n_processes = cm.cluster.setup_cluster( # backend='local', n_processes=None, single_thread=False) # %%% MOTION CORRECTION # first we create a motion correction object with the specified parameters mc = MotionCorrect(fnames, dview=None, var_name_hdf5=opts.data['var_name_hdf5'], **opts.get_group('motion')) # mc = MotionCorrect(fnames, dview=dview, var_name_hdf5=opts.data['var_name_hdf5'], **opts.get_group('motion')) # note that the file is not loaded in memory # %% Run (piecewise-rigid motion) correction using NoRMCorre mc.motion_correct(save_movie=True) # %% compare with original movie if display_images: m_orig = cm.load_movie_chain(fnames, var_name_hdf5=opts.data['var_name_hdf5'], is3D=True) T, h, w, z = m_orig.shape # Time, plane, height, weight m_orig = np.reshape(np.transpose(m_orig, (3, 0, 1, 2)), (T * z, h, w)) m_els = cm.load(mc.mmap_file, is3D=True) m_els = np.reshape(np.transpose(m_els, (3, 0, 1, 2)), (T * z, h, w)) ds_ratio = 0.2 moviehandle = cm.concatenate([ m_orig.resize(1, 1, ds_ratio) - mc.min_mov * mc.nonneg_movie, m_els.resize(1, 1, ds_ratio) ], axis=2) moviehandle.play(fr=60, q_max=99.5, magnification=2) # press q to exit
def main(): pass # For compatibility between running under Spyder and the CLI #%% First setup some parameters # dataset dependent parameters display_images = False # Set this to true to show movies and plots fname = ['Sue_2x_3000_40_-46.tif'] # filename to be processed fr = 30 # imaging rate in frames per second decay_time = 0.4 # length of a typical transient in seconds # motion correction parameters niter_rig = 1 # number of iterations for rigid motion correction max_shifts = (6, 6) # maximum allow rigid shift # for parallelization split the movies in num_splits chuncks across time splits_rig = 56 # start a new patch for pw-rigid motion correction every x pixels strides = (48, 48) # overlap between pathes (size of patch strides+overlaps) overlaps = (24, 24) # for parallelization split the movies in num_splits chuncks across time splits_els = 56 upsample_factor_grid = 4 # upsample factor to avoid smearing when merging patches # maximum deviation allowed for patch with respect to rigid shifts max_deviation_rigid = 3 # parameters for source extraction and deconvolution p = 1 # order of the autoregressive system gnb = 2 # number of global background components merge_thresh = 0.8 # merging threshold, max correlation allowed # half-size of the patches in pixels. e.g., if rf=25, patches are 50x50 rf = 15 stride_cnmf = 6 # amount of overlap between the patches in pixels K = 4 # number of components per patch gSig = [4, 4] # expected half size of neurons # initialization method (if analyzing dendritic data using 'sparse_nmf') init_method = 'greedy_roi' is_dendrites = False # flag for analyzing dendritic data # sparsity penalty for dendritic data analysis through sparse NMF alpha_snmf = None # parameters for component evaluation min_SNR = 2.5 # signal to noise ratio for accepting a component rval_thr = 0.8 # space correlation threshold for accepting a component cnn_thr = 0.8 # threshold for CNN based classifier #%% download the dataset if it's not present in your folder if fname[0] in ['Sue_2x_3000_40_-46.tif', 'demoMovie.tif']: fname = [download_demo(fname[0])] #%% play the movie # playing the movie using opencv. It requires loading the movie in memory. To # close the video press q m_orig = cm.load_movie_chain(fname[:1]) downsample_ratio = 0.2 offset_mov = -np.min(m_orig[:100]) moviehandle = m_orig.resize(1, 1, downsample_ratio) if display_images: moviehandle.play(gain=10, offset=offset_mov, fr=30, magnification=2) #%% start a cluster for parallel processing c, dview, n_processes = cm.cluster.setup_cluster(backend='local', n_processes=None, single_thread=False) #%%% MOTION CORRECTION # first we create a motion correction object with the parameters specified min_mov = cm.load(fname[0], subindices=range(200)).min() # this will be subtracted from the movie to make it non-negative mc = MotionCorrect(fname[0], min_mov, dview=dview, max_shifts=max_shifts, niter_rig=niter_rig, splits_rig=splits_rig, strides=strides, overlaps=overlaps, splits_els=splits_els, upsample_factor_grid=upsample_factor_grid, max_deviation_rigid=max_deviation_rigid, shifts_opencv=True, nonneg_movie=True) # note that the file is not loaded in memory #%% Run piecewise-rigid motion correction using NoRMCorre mc.motion_correct_pwrigid(save_movie=True) m_els = cm.load(mc.fname_tot_els) bord_px_els = np.ceil( np.maximum(np.max(np.abs(mc.x_shifts_els)), np.max(np.abs(mc.y_shifts_els)))).astype(np.int) # maximum shift to be used for trimming against NaNs #%% compare with original movie moviehandle = cm.concatenate([ m_orig.resize(1, 1, downsample_ratio) + offset_mov, m_els.resize(1, 1, downsample_ratio) ], axis=2) display_images = False if display_images: moviehandle.play(fr=60, q_max=99.5, magnification=2, offset=0) # press q to exit #%% MEMORY MAPPING # memory map the file in order 'C' fnames = mc.fname_tot_els # name of the pw-rigidly corrected file. border_to_0 = bord_px_els # number of pixels to exclude fname_new = cm.save_memmap(fnames, base_name='memmap_', order='C', border_to_0=bord_px_els) # exclude borders # now load the file Yr, dims, T = cm.load_memmap(fname_new) d1, d2 = dims images = np.reshape(Yr.T, [T] + list(dims), order='F') # load frames in python format (T x X x Y) #%% restart cluster to clean up memory cm.stop_server(dview=dview) c, dview, n_processes = cm.cluster.setup_cluster(backend='local', n_processes=None, single_thread=False) #%% RUN CNMF ON PATCHES # First extract spatial and temporal components on patches and combine them # for this step deconvolution is turned off (p=0) t1 = time.time() cnm = cnmf.CNMF(n_processes=1, k=K, gSig=gSig, merge_thresh=merge_thresh, p=0, dview=dview, rf=rf, stride=stride_cnmf, memory_fact=1, method_init=init_method, alpha_snmf=alpha_snmf, only_init_patch=False, gnb=gnb, border_pix=bord_px_els) cnm = cnm.fit(images) #%% plot contours of found components Cn = cm.local_correlations(images.transpose(1, 2, 0)) Cn[np.isnan(Cn)] = 0 plt.figure() crd = plot_contours(cnm.A, Cn, thr=0.9) plt.title('Contour plots of found components') #%% COMPONENT EVALUATION # the components are evaluated in three ways: # a) the shape of each component must be correlated with the data # b) a minimum peak SNR is required over the length of a transient # c) each shape passes a CNN based classifier idx_components, idx_components_bad, SNR_comp, r_values, cnn_preds = \ estimate_components_quality_auto(images, cnm.A, cnm.C, cnm.b, cnm.f, cnm.YrA, fr, decay_time, gSig, dims, dview=dview, min_SNR=min_SNR, r_values_min=rval_thr, use_cnn=False, thresh_cnn_min=cnn_thr) #%% PLOT COMPONENTS if display_images: plt.figure() plt.subplot(121) crd_good = cm.utils.visualization.plot_contours(cnm.A[:, idx_components], Cn, thr=.8, vmax=0.75) plt.title('Contour plots of accepted components') plt.subplot(122) crd_bad = cm.utils.visualization.plot_contours( cnm.A[:, idx_components_bad], Cn, thr=.8, vmax=0.75) plt.title('Contour plots of rejected components') #%% VIEW TRACES (accepted and rejected) if display_images: view_patches_bar(Yr, cnm.A.tocsc()[:, idx_components], cnm.C[idx_components], cnm.b, cnm.f, dims[0], dims[1], YrA=cnm.YrA[idx_components], img=Cn) view_patches_bar(Yr, cnm.A.tocsc()[:, idx_components_bad], cnm.C[idx_components_bad], cnm.b, cnm.f, dims[0], dims[1], YrA=cnm.YrA[idx_components_bad], img=Cn) #%% RE-RUN seeded CNMF on accepted patches to refine and perform deconvolution A_in, C_in, b_in, f_in = cnm.A[:, idx_components], cnm.C[ idx_components], cnm.b, cnm.f cnm2 = cnmf.CNMF(n_processes=1, k=A_in.shape[-1], gSig=gSig, p=p, dview=dview, merge_thresh=merge_thresh, Ain=A_in, Cin=C_in, b_in=b_in, f_in=f_in, rf=None, stride=None, gnb=gnb, method_deconvolution='oasis', check_nan=True) cnm2 = cnm2.fit(images) #%% Extract DF/F values F_dff = detrend_df_f(cnm2.A, cnm2.b, cnm2.C, cnm2.f, YrA=cnm2.YrA, quantileMin=8, frames_window=250) #%% Show final traces cnm2.view_patches(Yr, dims=dims, img=Cn) #%% STOP CLUSTER and clean up log files cm.stop_server(dview=dview) log_files = glob.glob('*_LOG_*') for log_file in log_files: os.remove(log_file) #%% reconstruct denoised movie denoised = cm.movie(cnm2.A.dot(cnm2.C) + cnm2.b.dot(cnm2.f)).reshape( dims + (-1, ), order='F').transpose([2, 0, 1]) #%% play along side original data moviehandle = cm.concatenate([ m_els.resize(1, 1, downsample_ratio), denoised.resize(1, 1, downsample_ratio) ], axis=2) if display_images: moviehandle.play(fr=60, gain=15, magnification=2, offset=0) # press q to exit
def run_caiman_pipeline(movie, fr, fnames, savedir, usematlabroi): #%% cpu_num = 7 cpu_num_spikepursuit = 2 #gsig_filt_micron = (4, 4) #max_shifts_micron = (6,6) #strides_micron = (60,60) #overlaps_micron = (30, 30) gsig_filt_micron = (4, 4) max_shifts_micron = (6, 6) strides_micron = (30, 30) overlaps_micron = (15, 15) max_deviation_rigid_micron = 4 pixel_size = movie['movie_pixel_size'] ROIs = None # Region of interests index = None # index of neurons weights = None # reuse spatial weights by # opts.change_params(params_dict={'weights':vpy.estimates['weights']}) # motion correction parameters pw_rigid = False # flag for pw-rigid motion correction gSig_filt = tuple( np.asarray(np.round(np.asarray(gsig_filt_micron) / float(pixel_size)), int)) # size of filter, in general gSig (see below), # change this one if algorithm does not work max_shifts = tuple( np.asarray(np.round(np.asarray(max_shifts_micron) / float(pixel_size)), int)) strides = tuple( np.asarray(np.round(np.asarray(strides_micron) / float(pixel_size)), int) ) # start a new patch for pw-rigid motion correction every x pixels overlaps = tuple( np.asarray(np.round(np.asarray(overlaps_micron) / float(pixel_size)), int) ) # start a new patch for pw-rigid motion correction every x pixels # overlap between pathes (size of patch strides+overlaps) max_deviation_rigid = int( round(max_deviation_rigid_micron / pixel_size) ) # maximum deviation allowed for patch with respect to rigid shifts border_nan = 'copy' opts_dict = { 'fnames': fnames, 'fr': fr, 'index': index, 'ROIs': ROIs, 'weights': weights, 'pw_rigid': pw_rigid, 'max_shifts': max_shifts, 'gSig_filt': gSig_filt, 'strides': strides, 'overlaps': overlaps, 'max_deviation_rigid': max_deviation_rigid, 'border_nan': border_nan } opts = volparams(params_dict=opts_dict) # %% play the movie (optional) # playing the movie using opencv. It requires loading the movie in memory. # To close the video press q display_images = False if display_images: m_orig = cm.load(fnames) ds_ratio = 0.2 moviehandle = m_orig.resize(1, 1, ds_ratio) moviehandle.play(q_max=99.5, fr=60, magnification=2) # %% start a cluster for parallel processing c, dview, n_processes = cm.cluster.setup_cluster(backend='local', n_processes=cpu_num, single_thread=False) # % MOTION CORRECTION # Create a motion correction object with the specified parameters mcrig = MotionCorrect(fnames, dview=dview, **opts.get_group('motion')) # Run piecewise rigid motion correction #% mcrig.motion_correct(save_movie=True) dview.terminate() # % MOTION CORRECTION2 opts.change_params({'pw_rigid': True}) c, dview, n_processes = cm.cluster.setup_cluster(backend='local', n_processes=cpu_num, single_thread=False) # Create a motion correction object with the specified parameters mc = MotionCorrect(mcrig.mmap_file, dview=dview, **opts.get_group('motion')) # Run piecewise rigid motion correction mc.motion_correct(save_movie=True) dview.terminate() # %% motion correction compared with original movie display_images = False if display_images: m_orig = cm.load(fnames) m_rig = cm.load(mcrig.mmap_file) m_pwrig = cm.load(mc.mmap_file) ds_ratio = 0.2 moviehandle = cm.concatenate([ m_orig.resize(1, 1, ds_ratio) - mc.min_mov * mc.nonneg_movie, m_rig.resize(1, 1, ds_ratio), m_pwrig.resize(1, 1, ds_ratio) ], axis=2) moviehandle.play(fr=60, q_max=99.5, magnification=2) # press q to exit # % movie subtracted from the mean m_orig2 = (m_orig - np.mean(m_orig, axis=0)) m_rig2 = (m_rig - np.mean(m_rig, axis=0)) m_pwrig2 = (m_pwrig - np.mean(m_pwrig, axis=0)) moviehandle1 = cm.concatenate([ m_orig2.resize(1, 1, ds_ratio), m_rig2.resize(1, 1, ds_ratio), m_pwrig2.resize(1, 1, ds_ratio) ], axis=2) moviehandle1.play(fr=60, q_max=99.5, magnification=2) # %% Memory Mapping c, dview, n_processes = cm.cluster.setup_cluster(backend='local', n_processes=cpu_num, single_thread=False) border_to_0 = 0 if mc.border_nan == 'copy' else mc.border_to_0 fname_new = cm.save_memmap_join(mc.mmap_file, base_name='memmap_', add_to_mov=border_to_0, dview=dview, n_chunks=10) dview.terminate() # %% change fnames to the new motion corrected one opts.change_params(params_dict={'fnames': fname_new}) # %% SEGMENTATION roidir = savedir[:savedir.find('VolPy')] + 'Spikepursuit' + savedir[ savedir.find('VolPy') + len('Volpy'):] try: files = os.listdir(roidir) except: files = [] if usematlabroi and 'ROIs.mat' in files: ROIs = loadmat(os.path.join(roidir, 'ROIs.mat'))['ROIs'] if len(np.shape(ROIs)) == 3: ROIs = np.moveaxis(np.asarray(ROIs, bool), 2, 0) else: ROIs = np.asarray([ROIs]) all_rois = ROIs opts.change_params( params_dict={ 'ROIs': ROIs, 'index': list(range(ROIs.shape[0])), 'method': 'SpikePursuit' }) else: #% print('WTF') # Create mean and correlation image use_maskrcnn = True # set to True to predict the ROIs using the mask R-CNN if not use_maskrcnn: # use manual annotations with h5py.File(path_ROIs, 'r') as fl: ROIs = fl['mov'][()] # load ROIs opts.change_params( params_dict={ 'ROIs': ROIs, 'index': list(range(ROIs.shape[0])), 'method': 'SpikePursuit' }) else: try: m = cm.load(mc.mmap_file[0], subindices=slice(0, 20000)) except: m = cm.load( '/home/rozmar/Data/Voltage_imaging/Voltage_rig_1P/rozsam/20200120/40x_1xtube_10A_7_000_rig__d1_128_d2_512_d3_1_order_F_frames_2273_._els__d1_128_d2_512_d3_1_order_F_frames_2273_.mmap', subindices=slice(0, 20000)) m.fr = fr img = m.mean(axis=0) img = (img - np.mean(img)) / np.std(img) m1 = m.computeDFF(secsWindow=1, in_place=True)[0] m = m - m1 Cn = m.local_correlations(swap_dim=False, eight_neighbours=True) img_corr = (Cn - np.mean(Cn)) / np.std(Cn) summary_image = np.stack([img, img, img_corr], axis=2).astype(np.float32) del m del m1 # % # Mask R-CNN config = neurons.NeuronsConfig() class InferenceConfig(config.__class__): # Run detection on one image at a time GPU_COUNT = 1 IMAGES_PER_GPU = 1 DETECTION_MIN_CONFIDENCE = 0.7 IMAGE_RESIZE_MODE = "pad64" IMAGE_MAX_DIM = 512 RPN_NMS_THRESHOLD = 0.7 POST_NMS_ROIS_INFERENCE = 1000 config = InferenceConfig() config.display() model_dir = os.path.join(caiman_datadir(), 'model') DEVICE = "/cpu:0" # /cpu:0 or /gpu:0 with tf.device(DEVICE): model = modellib.MaskRCNN(mode="inference", model_dir=model_dir, config=config) weights_path = download_model('mask_rcnn') model.load_weights(weights_path, by_name=True) results = model.detect([summary_image], verbose=1) r = results[0] ROIs_mrcnn = r['masks'].transpose([2, 0, 1]) # %% visualize the result display_result = False if display_result: _, ax = plt.subplots(1, 1, figsize=(16, 16)) visualize.display_instances(summary_image, r['rois'], r['masks'], r['class_ids'], ['BG', 'neurons'], r['scores'], ax=ax, title="Predictions") # %% set rois opts.change_params( params_dict={ 'ROIs': ROIs_mrcnn, 'index': list(range(ROIs_mrcnn.shape[0])), 'method': 'SpikePursuit' }) #all_rois = ROIs_mrcnn # %% Trace Denoising and Spike Extraction c, dview, n_processes = cm.cluster.setup_cluster( backend='local', n_processes=cpu_num_spikepursuit, single_thread=False, maxtasksperchild=1) #dview=None vpy = VOLPY(n_processes=n_processes, dview=dview, params=opts) vpy.fit(n_processes=n_processes, dview=dview) #%% print('saving parameters') parameters = dict() parameters['motion'] = opts.motion parameters['data'] = opts.data parameters['volspike'] = opts.volspike with open(os.path.join(savedir, 'parameters.pickle'), 'wb') as outfile: pickle.dump(parameters, outfile) #%% volspikedata = dict() volspikedata['estimates'] = vpy.estimates volspikedata['params'] = vpy.params.data with open(os.path.join(savedir, 'spikepursuit.pickle'), 'wb') as outfile: pickle.dump(volspikedata, outfile) #%% for mcidx, mc_now in enumerate([mcrig, mc]): motioncorr = dict() motioncorr['fname'] = mc_now.fname motioncorr['fname_tot_rig'] = mc_now.fname_tot_rig motioncorr['mmap_file'] = mc_now.mmap_file motioncorr['min_mov'] = mc_now.min_mov motioncorr['shifts_rig'] = mc_now.shifts_rig motioncorr['shifts_opencv'] = mc_now.shifts_opencv motioncorr['niter_rig'] = mc_now.niter_rig motioncorr['min_mov'] = mc_now.min_mov motioncorr['templates_rig'] = mc_now.templates_rig motioncorr['total_template_rig'] = mc_now.total_template_rig try: motioncorr['x_shifts_els'] = mc_now.x_shifts_els motioncorr['y_shifts_els'] = mc_now.y_shifts_els except: pass with open( os.path.join(savedir, 'motion_corr_' + str(mcidx) + '.pickle'), 'wb') as outfile: pickle.dump(motioncorr, outfile) #%% saving stuff print('moving files') for mmap_file in mcrig.mmap_file: fname = pathlib.Path(mmap_file).name os.remove(mmap_file) #shutil.move(mmap_file, os.path.join(savedir,fname)) for mmap_file in mc.mmap_file: fname = pathlib.Path(mmap_file).name os.remove(mmap_file) #shutil.move(mmap_file, os.path.join(savedir,fname)) fname = pathlib.Path(fname_new).name shutil.move(fname_new, os.path.join(savedir, fname)) #print('waiting') #time.sleep(1000) # %% some visualization plotstuff = False if plotstuff: print(np.where(vpy.estimates['passedLocalityTest']) [0]) # neurons that pass locality test n = 0 # Processed signal and spikes of neurons plt.figure() plt.plot(vpy.estimates['trace'][n]) plt.plot(vpy.estimates['spikeTimes'][n], np.max(vpy.estimates['trace'][n]) * np.ones(vpy.estimates['spikeTimes'][n].shape), color='g', marker='o', fillstyle='none', linestyle='none') plt.title('signal and spike times') plt.show() # Location of neurons by Mask R-CNN or manual annotation plt.figure() if use_maskrcnn: plt.imshow(ROIs_mrcnn[n]) else: plt.imshow(ROIs[n]) mv = cm.load(fname_new) plt.imshow(mv.mean(axis=0), alpha=0.5) # Spatial filter created by algorithm plt.figure() plt.imshow(vpy.estimates['spatialFilter'][n]) plt.colorbar() plt.title('spatial filter') plt.show() # %% STOP CLUSTER and clean up log files cm.stop_server(dview=dview) log_files = glob.glob('*_LOG_*') for log_file in log_files: os.remove(log_file)
def main(): pass # For compatibility between running under Spyder and the CLI #%% """ General parameters """ play_movie = 1 plot_extras = 1 plot_extras_cell = 1 compute_mc_metrics = 1 #%% Select file(s) to be processed (download if not present) """ Load file """ #fnames = ['Sue_2x_3000_40_-46.tif'] # filename to be processed #if fnames[0] in ['Sue_2x_3000_40_-46.tif', 'demoMovie.tif']: # fnames = [download_demo(fnames[0])] #fnames = ['/home/yuriy/Desktop/Data/rest1_5_9_19_cut.tif'] #f_dir = 'C:\\Users\\rylab_dataPC\\Desktop\\Yuriy\\caiman_data\\short\\' f_dir = 'G:\\analysis\\190828-calcium_voltage\\soma_dendrites\\pCAG_jREGECO1a_ASAP3_anesth_001\\' f_name = 'Ch1' f_ext = 'tif' fnames = [f_dir + f_name + '.' + f_ext] #fnames = ['C:/Users/rylab_dataPC/Desktop/Yuriy/caiman_data/rest1_5_9_19_2_cut_ca.hdf5'] #%% First setup some parameters for data and motion correction """ Parameters """ # dataset dependent parameters fr = 30 # imaging rate in frames per second decay_time = 1 #0.4 # length of a typical transient in seconds dxy = (2., 2.) # spatial resolution in x and y in (um per pixel) # note the lower than usual spatial resolution here max_shift_um = (12., 12.) # maximum shift in um patch_motion_um = (100., 100.) # patch size for non-rigid correction in um # motion correction parameters pw_rigid = True # flag to select rigid vs pw_rigid motion correction # maximum allowed rigid shift in pixels #max_shifts = [int(a/b) for a, b in zip(max_shift_um, dxy)] max_shifts = [6, 6] # start a new patch for pw-rigid motion correction every x pixels #strides = tuple([int(a/b) for a, b in zip(patch_motion_um, dxy)]) strides = [48, 48] # overlap between pathes (size of patch in pixels: strides+overlaps) overlaps = (24, 24) # maximum deviation allowed for patch with respect to rigid shifts max_deviation_rigid = 3 mc_dict = { 'fnames': fnames, 'fr': fr, 'decay_time': decay_time, 'dxy': dxy, 'pw_rigid': pw_rigid, 'max_shifts': max_shifts, 'strides': strides, 'overlaps': overlaps, 'max_deviation_rigid': max_deviation_rigid, 'border_nan': 'copy' } opts = params.CNMFParams(params_dict=mc_dict) # %% play the movie (optional) # playing the movie using opencv. It requires loading the movie in memory. # To close the video press q if play_movie: m_orig = cm.load_movie_chain(fnames) ds_ratio = 0.2 moviehandle = m_orig.resize(1, 1, ds_ratio) moviehandle.play(q_max=99.5, fr=60, magnification=2) # %% start a cluster for parallel processing c, dview, n_processes = cm.cluster.setup_cluster(backend='local', n_processes=None, single_thread=False) # %%% MOTION CORRECTION # first we create a motion correction object with the specified parameters mc = MotionCorrect(fnames, dview=dview, **opts.get_group('motion')) # note that the file is not loaded in memory # %% Run (piecewise-rigid motion) correction using NoRMCorre mc.motion_correct(save_movie=True) # type "mc."and press TAB to see all interesting associated variables and self. outputs # interesting outputs # saved file is mc.fname_tot_els / mc.fname_tot_rig # mc.x_shifts_els / mc.y_shifts_els: shifts in x/y per frame per patch # mc.coord_shifts_els: coordinates associated to patches with shifts # mc.total_template_els: updated template for pw # mc.total_template_rig: updated template for rigid # mc.templates_rig: templates for each iteration in rig #%% # compute metrics for the results (TAKES TIME!!) if compute_mc_metrics: # not finished bord_px_els = np.ceil( np.maximum(np.max(np.abs(mc.x_shifts_els)), np.max(np.abs(mc.y_shifts_els)))).astype(np.int) final_size = np.subtract( mc.total_template_els.shape, 2 * bord_px_els) # remove pixels in the boundaries winsize = 100 swap_dim = False resize_fact_flow = .2 # downsample for computing ROF tmpl_rig, correlations_orig, flows_orig, norms_orig, crispness_orig = cm.motion_correction.compute_metrics_motion_correction( fnames[0], final_size[0], final_size[1], swap_dim, winsize=winsize, play_flow=False, resize_fact_flow=resize_fact_flow) plt.figure() plt.plot(correlations_orig) # %% compare with original movie if play_movie: m_orig = cm.load_movie_chain(fnames) m_els = cm.load(mc.mmap_file) ds_ratio = 0.2 moviehandle = cm.concatenate([ m_orig.resize(1, 1, ds_ratio) - mc.min_mov * mc.nonneg_movie, m_els.resize(1, 1, ds_ratio) ], axis=2) moviehandle.play(fr=60, q_max=99.5, magnification=2) # press q to exit del m_orig del m_els if plot_extras: # plot total template plt.figure() plt.imshow(mc.total_template_els) plt.title('Template after iteration') # plot x and y corrections plt.figure() plt.plot(mc.shifts_rig) plt.title('Rigid motion correction xy movement') plt.legend(['x shift', 'y shift']) plt.xlabel('frames') # %% MEMORY MAPPING border_to_0 = 0 if mc.border_nan is 'copy' else mc.border_to_0 # you can include the boundaries of the FOV if you used the 'copy' option # during motion correction, although be careful about the components near # the boundaries # memory map the file in order 'C' fname_new = cm.save_memmap(mc.mmap_file, base_name='memmap_', order='C', border_to_0=border_to_0) # exclude borders # now load the file Yr, dims, T = cm.load_memmap(fname_new) images = np.reshape(Yr.T, [T] + list(dims), order='F') # load frames in python format (T x X x Y) # %% restart cluster to clean up memory cm.stop_server(dview=dview) c, dview, n_processes = cm.cluster.setup_cluster(backend='local', n_processes=None, single_thread=False) # %% parameters for source extraction and deconvolution p = 2 # order of the autoregressive system gnb = 2 # number of global background components merge_thr = 0.85 # merging threshold, max correlation allowed rf = 15 # half-size of the patches in pixels. e.g., if rf=25, patches are 50x50 stride_cnmf = 6 # amount of overlap between the patches in pixels K = 2 # number of components per patch gSig = [15, 15] # expected half size of neurons in pixels # initialization method (if analyzing dendritic data using 'sparse_nmf') method_init = 'greedy_roi' ssub = 1 # spatial subsampling during initialization tsub = 1 # temporal subsampling during intialization # parameters for component evaluation opts_dict = { 'fnames': fnames, 'fr': fr, 'nb': gnb, 'rf': rf, 'K': K, 'gSig': gSig, 'stride': stride_cnmf, 'method_init': method_init, 'rolling_sum': True, 'merge_thr': merge_thr, 'n_processes': n_processes, 'only_init': True, 'ssub': ssub, 'tsub': tsub } opts.change_params(params_dict=opts_dict) # %% RUN CNMF ON PATCHES # First extract spatial and temporal components on patches and combine them # for this step deconvolution is turned off (p=0) opts.change_params({'p': 0}) cnm = cnmf.CNMF(n_processes, params=opts, dview=dview) cnm = cnm.fit(images) if plot_extras_cell: num_cell_plot = 51 plt.figure() plt.plot(cnm.estimates.C[num_cell_plot, :]) plt.title('Temporal component') plt.legend(['Cell ' + str(num_cell_plot)]) # plot component sptial profile A # first convert back to dense components plot_spat_A = cnm.estimates.A[:, num_cell_plot].toarray().reshape( list(dims)) plt.figure() plt.imshow(plot_spat_A) plt.title('Spatial component cell ' + str(num_cell_plot)) # %% ALTERNATE WAY TO RUN THE PIPELINE AT ONCE # you can also perform the motion correction plus cnmf fitting steps # simultaneously after defining your parameters object using # cnm1 = cnmf.CNMF(n_processes, params=opts, dview=dview) # cnm1.fit_file(motion_correct=True) # %% plot contours of found components Cn = cm.local_correlations(images, swap_dim=False) Cn[np.isnan(Cn)] = 0 cnm.estimates.plot_contours(img=Cn) plt.title('Contour plots of found components') if plot_extras: plt.figure() plt.imshow(Cn) plt.title('Local correlations') # %% RE-RUN seeded CNMF on accepted patches to refine and perform deconvolution cnm.params.change_params({'p': p}) cnm2 = cnm.refit(images, dview=dview) # %% COMPONENT EVALUATION # the components are evaluated in three ways: # a) the shape of each component must be correlated with the data # b) a minimum peak SNR is required over the length of a transient # c) each shape passes a CNN based classifier min_SNR = 2 # signal to noise ratio for accepting a component rval_thr = 0.90 # space correlation threshold for accepting a component cnn_thr = 0.99 # threshold for CNN based classifier cnn_lowest = 0.1 # neurons with cnn probability lower than this value are rejected cnm2.params.set( 'quality', { 'decay_time': decay_time, 'min_SNR': min_SNR, 'rval_thr': rval_thr, 'use_cnn': True, 'min_cnn_thr': cnn_thr, 'cnn_lowest': cnn_lowest }) cnm2.estimates.evaluate_components(images, cnm2.params, dview=dview) # %% PLOT COMPONENTS cnm2.estimates.plot_contours(img=Cn, idx=cnm2.estimates.idx_components) plt.suptitle('Component selection: min_SNR=' + str(min_SNR) + '; rval_thr=' + str(rval_thr) + '; cnn prob range=[' + str(cnn_lowest) + ' ' + str(cnn_thr) + ']') # %% VIEW TRACES (accepted and rejected) if plot_extras: cnm2.estimates.view_components(images, img=Cn, idx=cnm2.estimates.idx_components) plt.suptitle('Accepted') cnm2.estimates.view_components(images, img=Cn, idx=cnm2.estimates.idx_components_bad) plt.suptitle('Rejected') #plt.figure(); #plt.plot(cnm2.estimates.YrA[0,:]+cnm2.estimates.C[0,:]) # # # # #plt.figure(); #plt.plot(cnm2.estimates.R[0,:]-cnm2.estimates.YrA[0,:]); #plt.plot(); #plt.show(); # # #plt.figure(); #plt.plot(cnm2.estimates.detrend_df_f[1,:]) # these store the good and bad components, and next step sorts them # cnm2.estimates.idx_components # cnm2.estimates.idx_components_bad #%% update object with selected components #cnm2.estimates.select_components(use_object=True) #%% Extract DF/F values cnm2.estimates.detrend_df_f(quantileMin=8, frames_window=250) #%% Show final traces cnm2.estimates.view_components(img=Cn) plt.suptitle("Final results") #%% Save the mc data as in cmn struct as well ## #mc_out = dict( # pw_rigid = mc.pw_rigid, # fname = mc.fname, # mmap_file = mc.mmap_file, # total_template_els = mc.total_template_els, # total_template_rig = mc.total_template_rig, # border_nan = mc.border_nan, # border_to_0 = mc.border_to_0, # x_shifts_els = mc.x_shifts_els, # y_shifts_els = mc.y_shifts_els, # Cn = Cn # ) # # #deepdish.io.save(fnames[0] + '_mc_data.hdf5', mc_out) #%% reconstruct denoised movie (press q to exit) if play_movie: cnm2.estimates.play_movie(images, q_max=99.9, gain_res=2, magnification=2, bpx=border_to_0, include_bck=False) # background not shown #%% STOP CLUSTER and clean up log files cm.stop_server(dview=dview) log_files = glob.glob('*_LOG_*') for log_file in log_files: os.remove(log_file) save_results = True if save_results: cnm2.save(fnames[0][:-4] + '_results.hdf5')
total_template_rig, 5), vmax=np.percentile(total_template_rig, 95)) #%% pl.close() pl.plot(shifts_rig) #%% m_rig = cm.load(fname_tot_rig) #%% add_to_movie = - np.min(total_template_rig) + 1 print(add_to_movie) #%% visualize movies m_rig.resize(1, 1, .2).play( fr=20, gain=5, magnification=1, offset=add_to_movie) #%% downs = .2 cm.concatenate([m_rig.resize(1, 1, downs), m_orig.resize(1, 1, downs)], axis=1).play( fr=30, gain=2, magnification=1, offset=add_to_movie) #%% visualize templates cm.movie(np.array(templates_rig)).play( fr=10, gain=2, magnification=1, offset=add_to_movie) #%% PIECEWISE RIGID MOTION CORRECTION t1 = time.time() new_templ = total_template_rig.copy() strides = params_movie['strides'] overlaps = params_movie['overlaps'] shifts_opencv = False save_movie = True splits = params_movie['splits_els'] num_splits_to_process_list = params_movie['num_splits_to_process_els'] upsample_factor_grid = params_movie['upsample_factor_grid'] max_deviation_rigid = params_movie['max_deviation_rigid']
# newstrides = newstrides, upsample_factor_grid=upsample_factor_grid,\ # upsample_factor_fft=10,show_movie=False,max_deviation_rigid=2,add_to_movie=add_to_movie) # # total_shifts.append(total_shift) # start_steps.append(start_step) # xy_grids.append(xy_grid) #mc = cm.load('M_FLUO_4_d1_64_d2_128_d3_1_order_F_frames_4620_.mmap') #mc = cm.load('M_FLUO_t_d1_64_d2_128_d3_1_order_F_frames_6764_.mmap') #%% mc.resize(1, 1, .1).play(gain=10., fr=30, offset=100, magnification=1.) #%% m.resize(1, 1, .2).play(gain=10, fr=30, offset=0, magnification=1.) #%% cm.concatenate([mr.resize(1, 1, .5), mc.resize(1, 1, .5)], axis=1).play(gain=10, fr=100, offset=300, magnification=1.) #%% import h5py with h5py.File('sueann_pw_rigid_movie.mat') as f: mef = np.array(f['M2']) mef = cm.movie(mef.transpose([0, 2, 1])) #%% cm.concatenate( [mef.resize(1, 1, .15), mc.resize(1, 1, .15)], axis=1).play(gain=30, fr=40, offset=300, magnification=1.) #%% (mef - mc).resize(1, 1, .1).play(gain=50, fr=20, offset=0, magnification=1.) #%%
plt.subplot(len(fls), 3, 1 + 3 * cnt) plt.ylabel(metr) try: mean_img = np.mean(movie, 0)[12:-12, 12:-12] except: try: mean_img = np.mean(movie, 0)[12:-12, 12:-12] except: mean_img = np.mean(cm.load(fl[:-12] + 'hdf5'), 0)[12:-12, 12:-12] lq, hq = np.nanpercentile(mean_img, [.5, 99.5]) plt.imshow(mean_img, vmin=lq, vmax=hq) plt.title('Mean') plt.subplot(len(fls), 3, 3 * cnt + 2) plt.imshow(ld['img_corr'], vmin=0, vmax=.35) plt.title('Corr image') plt.subplot(len(fls), 3, 3 * cnt + 3) # plt.plot(ld['norms']) # plt.xlabel('frame') # plt.ylabel('norm opt flow') # plt.subplot(len(fls), 3, 3 * cnt + 3) flows = ld['flows'] mean_flow_img = np.mean( np.sqrt(flows[:, :, :, 0]**2 + flows[:, :, :, 1]**2), 0) plt.imshow(mean_flow_img, vmin=0, vmax=0.1) plt.colorbar() plt.title('Mean optical flow') C = cm.concatenate([m_orig, m_rig, m_els], axis=2) #C.play(fr=60)
pl.ylabel('y_shifts (pixels)') pl.xlabel('frames') # TODO: show screenshot 6 # %% play corrected and downsampled movie downsample_ratio = 0.2 m_els.resize(1, 1, downsample_ratio).play( gain=3, offset=0, fr=100, magnification=1, bord_px=bord_px_els) # %% local correlation _Cn = m_els.local_correlations(eight_neighbours=True, swap_dim=False) pl.imshow(_Cn) # TODO: show screenshot 7 # %% visualize raw, rigid and pw-rigid motion correted moviews downsample_factor = params_display['downsample_ratio'] # TODO : todocument cm.concatenate( [m_orig.resize(1, 1, downsample_factor) + offset_mov, m_rig.resize(1, 1, downsample_factor), m_els.resize( 1, 1, downsample_factor)], axis=2)[700:1000].play(fr=60, gain=3, magnification=1, offset=0) # TODO: show screenshot 8 # %% compute metrics for the results, just to check that motion correction worked properly final_size = np.subtract(mc.total_template_els.shape, 2 * bord_px_els) winsize = 100 swap_dim = False resize_fact_flow = params_display['downsample_ratio'] # computationnaly intensive # TODO: todocument tmpl, correlations, flows_orig, norms, smoothness = cm.motion_correction.compute_metrics_motion_correction( mc.fname_tot_els, final_size[0], final_size[1], swap_dim, winsize=winsize, play_flow=False, resize_fact_flow=resize_fact_flow) tmpl, correlations, flows_orig, norms, smoothness = cm.motion_correction.compute_metrics_motion_correction( mc.fname_tot_rig, final_size[0], final_size[1], swap_dim, winsize=winsize, play_flow=False, resize_fact_flow=resize_fact_flow)