def extract_motor_components_OF(m, n_components, mask = None, resize_fact= .5, only_magnitude = False, max_iter = 1000, verbose = False): if mask is not None: mask = coo_matrix(np.array(mask).squeeze()) ms = [get_nonzero_subarray(mask.multiply(fr),mask) for fr in m] ms = np.dstack(ms) ms = cm.movie(ms.transpose([2,0,1])) else: ms = m of_or = compute_optical_flow(ms,do_show=False,polar_coord=False) of_or = np.concatenate([cm.movie(of_or[0]).resize(resize_fact,resize_fact,1)[np.newaxis,:,:,:],cm.movie(of_or[1]).resize(resize_fact,resize_fact,1)[np.newaxis,:,:,:]],axis = 0) if only_magnitude: of = np.sqrt(of[0]**2+of[1]**2) else: offset_removed = np.min(of_or) of = of_or - offset_removed spatial_filter_, time_trace_, norm_fact = extract_components(of,n_components=n_components,verbose = verbose ,normalize_std=False,max_iter=max_iter) return spatial_filter_, time_trace_, of_or
def extract_motor_components_OF(m, n_components, mask=None, resize_fact=.5, only_magnitude=False, max_iter=1000, verbose=False, method_factorization='nmf', max_iter_DL=-30): # todo todocument if mask is not None: mask = coo_matrix(np.array(mask).squeeze()) ms = [get_nonzero_subarray(mask.multiply(fr), mask) for fr in m] ms = np.dstack(ms) ms = cm.movie(ms.transpose([2, 0, 1])) else: ms = m of_or = compute_optical_flow(ms, do_show=False, polar_coord=False) of_or = np.concatenate([cm.movie(of_or[0]).resize(resize_fact, resize_fact, 1)[np.newaxis, :, :, :], cm.movie(of_or[1]).resize(resize_fact, resize_fact, 1)[np.newaxis, :, :, :]], axis=0) if only_magnitude: of = np.sqrt(of[0]**2 + of[1]**2) else: if method_factorization == 'nmf': offset_removed = np.min(of_or) of = of_or - offset_removed else: of = of_or spatial_filter_, time_trace_, _ = extract_components(of, n_components=n_components, verbose=verbose, normalize_std=False, max_iter=max_iter, method_factorization=method_factorization, max_iter_DL=max_iter_DL) return spatial_filter_, time_trace_, of_or
def save(): global all_ROIs, save_ROIs, summary_images print('Saving') ffll = F.getSaveFileName(filter='HDF5 (*.hdf5)') print(ffll[0]) save_ROIs = np.array(list(all_ROIs.values())).copy() save_ROIs = np.flip(save_ROIs, axis=1) if os.path.splitext(ffll[0])[1] == '.hdf5': cm.movie(save_ROIs).save(ffll[0]) summary_images = summary_images.transpose([0, 2, 1]) summary_images = np.flip(summary_images, axis=1)
def local_correlations_movie_offline(file_name, Tot_frames=None, fr: int = 10, window: int = 30, stride: int = 3, swap_dim: bool = True, eight_neighbours: bool = True, order_mean: int = 1, ismulticolor: bool = False, dview=None): if Tot_frames is None: Tot_frames = cm.load(file_name).shape[0] params: List = [[ file_name, range(j, j + window), eight_neighbours, swap_dim, order_mean, ismulticolor ] for j in range(0, Tot_frames - window, stride)] if dview is None: parallel_result = list(map(local_correlations_movie_parallel, params)) else: if 'multiprocessing' in str(type(dview)): parallel_result = dview.map_async( local_correlations_movie_parallel, params).get(4294967) else: parallel_result = dview.map_sync(local_correlations_movie_parallel, params) dview.results.clear() mm = cm.movie(np.concatenate(parallel_result, axis=0), fr=fr) return mm
def save_tif_to_mmap_online(movie_iterable, save_base_name='YrOL_', order='C', add_to_movie=0, data_type=np.uint16, border_to_0=0): # todo: todocument if isinstance(movie_iterable, basestring ): # Allow specifying a filename rather than its data rep with tifffile.TiffFile( movie_iterable) as tf: # And load it if that happens movie_iterable = cm.movie(tf) count = 0 new_mov = [] dims = (len(movie_iterable), ) + movie_iterable[0].shape fname_tot = (save_base_name + '_d1_' + str(dims[1]) + '_d2_' + str(dims[2]) + '_d3_' + str(1 if len(dims) == 3 else dims[3]) + '_order_' + str(order) + '_frames_' + str(dims[0]) + '_.mmap') big_mov = np.memmap(fname_tot, mode='w+', dtype=data_type, shape=prepare_shape((np.prod(dims[1:]), dims[0])), order=order) for page in movie_iterable: if count % 100 == 0: logging.debug(count) if 'tifffile' in str(type(movie_iterable[0])): page = page.asarray() img = np.array(page, dtype=data_type) new_img = img if save_base_name is not None: big_mov[:, count] = np.reshape(new_img, np.prod(dims[1:]), order='F') else: new_mov.append(new_img) if border_to_0 > 0: img[:border_to_0, :] = 0 img[:, :border_to_0] = 0 img[:, -border_to_0:] = 0 img[-border_to_0:, :] = 0 big_mov[:, count] = np.reshape(img + add_to_movie, np.prod(dims[1:]), order='F') count += 1 big_mov.flush() del big_mov return fname_tot
def extract_motor_components_OF( m, n_components, mask=None, resize_fact: float = .5, only_magnitude: bool = False, max_iter: int = 1000, verbose: bool = False, method_factorization: str = 'nmf', max_iter_DL=-30) -> Tuple[np.ndarray, np.ndarray, np.ndarray]: # todo todocument if mask is not None: mask = coo_matrix(np.array(mask).squeeze()) ms = [get_nonzero_subarray(mask.multiply(fr), mask) for fr in m] ms = np.dstack(ms) ms = cm.movie(ms.transpose([2, 0, 1])) else: ms = m of_or = compute_optical_flow(ms, do_show=False, polar_coord=False) of_or = np.concatenate([ cm.movie(of_or[0]).resize(resize_fact, resize_fact, 1)[np.newaxis, :, :, :], cm.movie(of_or[1]).resize(resize_fact, resize_fact, 1)[np.newaxis, :, :, :] ], axis=0) if only_magnitude: of = np.sqrt(of[0]**2 + of[1]**2) else: if method_factorization == 'nmf': offset_removed = np.min(of_or) of = of_or - offset_removed else: of = of_or spatial_filter_, time_trace_, _ = extract_components( of, n_components=n_components, verbose=verbose, normalize_std=False, max_iter=max_iter, method_factorization=method_factorization, max_iter_DL=max_iter_DL) return spatial_filter_, time_trace_, of_or
def save_tif_to_mmap_online(movie_iterable, save_base_name='YrOL_', order='C', add_to_movie=0, border_to_0=0) -> str: # todo: todocument if isinstance(movie_iterable, basestring ): # Allow specifying a filename rather than its data rep with tifffile.TiffFile( movie_iterable) as tf: # And load it if that happens movie_iterable = cm.movie(tf) count = 0 new_mov = [] dims = (len(movie_iterable), ) + movie_iterable[0].shape # TODO: Don't pack length into dims fname_tot = caiman.paths.fn_relocated( caiman.paths.memmap_frames_filename(save_base_name, dims[1:], dims[0], order)) big_mov = np.memmap(fname_tot, mode='w+', dtype=np.float32, shape=prepare_shape((np.prod(dims[1:]), dims[0])), order=order) for page in movie_iterable: if count % 100 == 0: logging.debug(count) if 'tifffile' in str(type(movie_iterable[0])): page = page.asarray() img = np.array(page, dtype=np.float32) new_img = img if save_base_name is not None: big_mov[:, count] = np.reshape(new_img, np.prod(dims[1:]), order='F') else: new_mov.append(new_img) if border_to_0 > 0: img[:border_to_0, :] = 0 img[:, :border_to_0] = 0 img[:, -border_to_0:] = 0 img[-border_to_0:, :] = 0 big_mov[:, count] = np.reshape(img + add_to_movie, np.prod(dims[1:]), order='F') count += 1 big_mov.flush() del big_mov return fname_tot
def write_avi(memmap_fpath, result_data_dir): images = load_images(memmap_fpath) # Write motion corrected video to drive w = cm.movie(images) mcwriter = skvideo.io.FFmpegWriter(result_data_dir + '/mc.avi', outputdict={'-vcodec': 'rawvideo'}) for iddxx, frame in enumerate(w): mcwriter.writeFrame(frame.astype('uint8')) mcwriter.close()
def save_tif_to_mmap_online(movie_iterable, save_base_name='YrOL_', order='C', add_to_movie=0, border_to_0=0): if isinstace(movie_iterable, basestring): with tifffile.TiffFile(movie_iterable) as tf: movie_iterable = cm.movie(tf) count = 0 new_mov = [] dims = (len(movie_iterable), ) + movie_iterable[0].shape fname_tot = save_base_name + '_d1_' + str(dims[1]) + '_d2_' + str( dims[2]) + '_d3_' + str(1 if len(dims) == 3 else dims[3] ) + '_order_' + str(order) + '_frames_' + str( dims[0]) + '_.mmap' big_mov = np.memmap(fname_tot, mode='w+', dtype=np.float32, shape=(np.prod(dims[1:]), dims[0]), order=order) for page in movie_iterable: if count % 100 == 0: print(count) if 'tifffile' in str(type(movie_iterable[0])): page = page.asarray() img = np.array(page, dtype=np.float32) new_img = img if save_base_name is not None: big_mov[:, count] = np.reshape(new_img, np.prod(dims[1:]), order='F') else: new_mov.append(new_img) if border_to_0 > 0: img[:border_to_0, :] = 0 img[:, :border_to_0] = 0 img[:, -border_to_0:] = 0 img[-border_to_0:, :] = 0 big_mov[:, count] = np.reshape(img + add_to_movie, np.prod(dims[1:]), order='F') count += 1 big_mov.flush() del big_mov return fname_tot
def preview_parameters_cropping_cropping_points_temporal_get_in_out_movie(m, cropping_points_temporal): clip_range = range(cropping_points_temporal[0],cropping_points_temporal[1]) text_width, text_height = cv2.getTextSize('EXCLUDE: Frame 100', fontFace=5, fontScale = 0.8, thickness=1)[0] m_with_in_out = [] for i, frame in enumerate(m): m_with_in_out.append(cv2.putText(frame, f'EXCLUDE: Frame {i}' if i in clip_range else f'INCLUDE: Frame {i}',((frame.shape[1] - text_width) // 2, frame.shape[0] - (text_height + 5)), fontFace=5, fontScale=0.8, color=(255, 255, 255), thickness=1)) m_with_in_out = cm.movie(np.array(m_with_in_out)) return m_with_in_out
def mean_image(file_name, Tot_frames=None, fr: float = 10., window: int = 100, dview=None): """ Efficient (parallel) computation of mean image in chunks Args: Y: str path to movie file Tot_frames: int Number of total frames considered fr: int (100) Frame rate (optional) window: int (100) Window length in frames dview: map object Use it for parallel computation Returns: mm: cm.movie (2D). mean image """ if Tot_frames is None: _, Tot_frames = get_file_size(file_name) params: List = [[file_name, range(j * window, (j + 1) * window)] for j in range(int(Tot_frames / window))] remain_frames = Tot_frames - int(Tot_frames / window) * window if remain_frames > 0: params.append([file_name, range(int(Tot_frames / window) * window, Tot_frames)]) if dview is None: parallel_result = list(map(mean_image_parallel, params)) else: if 'multiprocessing' in str(type(dview)): parallel_result = dview.map_async(mean_image_parallel, params).get(4294967) else: parallel_result = dview.map_sync(mean_image_parallel, params) dview.results.clear() mm = cm.movie(np.concatenate(parallel_result, axis=0), fr=fr/len(parallel_result)) if remain_frames > 0: mean_image = (mm[:-1].sum(axis=0) + (remain_frames / window) * mm[-1]) / (len(mm) - 1 + remain_frames / window) else: mean_image = mm.mean(axis=0) return mean_image
def save_tif_to_mmap_online(movie_iterable,save_base_name='YrOL_', order = 'C',add_to_movie=0,border_to_0=0): if isinstace(movie_iterable,basestring): with tifffile.TiffFile(movie_iterable) as tf: movie_iterable = cm.movie(tf) count=0 new_mov=[] dims=(len(movie_iterable),)+movie_iterable[0].shape fname_tot = save_base_name + '_d1_' + str(dims[1]) + '_d2_' + str(dims[2]) + '_d3_' + str( 1 if len(dims) == 3 else dims[3]) + '_order_' + str(order) + '_frames_' + str(dims[0]) + '_.mmap' big_mov = np.memmap(fname_tot, mode='w+', dtype=np.float32, shape=(np.prod(dims[1:]), dims[0]), order=order) for page in movie_iterable: if count%100 == 0: print(count) if 'tifffile' in str(type(movie_iterable[0])): page=page.asarray() img=np.array(page,dtype=np.float32) new_img = img if save_base_name is not None: big_mov[:,count] = np.reshape(new_img,np.prod(dims[1:]),order='F') else: new_mov.append(new_img) if border_to_0 > 0: img[:border_to_0,:]=0 img[:,:border_to_0]=0 img[:,-border_to_0:]=0 img[-border_to_0:,:]=0 big_mov[:,count] = np.reshape(img+add_to_movie,np.prod(dims[1:]),order='F') count+=1 big_mov.flush() del big_mov return fname_tot
def correlation_image(images): '''Compute the correlation image for the input video Args: images: The input video shape: (frames, dims, dims) Returns: Cn: The correlation image ''' Cn = cm.movie(images).local_correlations(swap_dim=False) return Cn
def pre_process_handle(args): # todo: todocument from scipy.ndimage import filters as ft import logging fil, resize_factors, diameter_bilateral_blur, median_filter_size = args name_log = fil[:-4] + '_LOG' logger = logging.getLogger(name_log) hdlr = logging.FileHandler(name_log) formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') hdlr.setFormatter(formatter) logger.addHandler(hdlr) logger.setLevel(logging.INFO) logger.info('START') logger.info(fil) mov = cm.load(fil, fr=30) logger.info('Read file') mov = mov.resize(1, 1, resize_factors[0]) logger.info('Resize') mov = mov.bilateral_blur_2D(diameter=diameter_bilateral_blur) logger.info('Bilateral') mov1 = cm.movie(ft.median_filter(mov, median_filter_size), fr=30) logger.info('Median filter') mov1 = mov1.resize(1, 1, resize_factors[1]) logger.info('Resize 2') mov1 = mov1 - cm.utils.stats.mode_robust(mov1, 0) logger.info('Mode') mov = mov.resize(1, 1, resize_factors[1]) logger.info('Resize') mov.save(fil[:-4] + '_compress_.tif') logger.info('Save 1') mov1.save(fil[:-4] + '_BL_compress_.tif') logger.info('Save 2') return 1
def memmap_file(fname, video, dview): '''This is for changing data format into caiman data and saving into several memmap files. Args: fname: Name of the video (This is only used for working with caiman support) video: The input video shape: (frames, dims, dims) dview: Direct View object for parallelization pruposes when using ipyparallel Returns: Yr: Memory mapped variable dims: Dimension of one frame shape: (521,521) T: Number of frames ''' video = np.squeeze(video) loaded = cm.movie(video, fr=30, start_time=0, file_name=fname[-1], meta_data=None) add_to_movie = -np.min(loaded).astype(float) add_to_movie = np.maximum(add_to_movie, 0) base_name = 'Yr' name_new = csp.save_memmap_each(video, fname, dview=dview, base_name=base_name, add_to_movie=add_to_movie) name_new.sort() fname_new = cm.save_memmap_join(name_new, base_name='Yr', dview=dview) Yr, dims, T = cm.load_memmap(fname_new) return Yr, dims, T
def main(): mmat = loadmat('mov_AG051514-01-060914 C.mat')['mov'] m = cm.movie(mmat.transpose((2, 0, 1)), fr=120) mask = select_roi(m[0]) if 1: mov_tot = compute_optical_flow(m[:3000], mask) else: mov_tot = compute_optical_flow(m[:3000], mask, polar_coord=False) sp_filt, t_trace, norm_fact = extract_components(mov_tot) plot_components(sp_filt, t_trace) id_comp = 1 pl.plot( old_div( np.sum(np.reshape(sp_filt[id_comp] > 1, [d1, d2]) * mov_tot[1], axis=(1, 2)), np.sum(sp_filt[id_comp] > 1))) pl.plot(t_trace[id_comp][:, 1])
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 denoise_mov(movie, ncomp=1, batch_size=1000): """ Denoises movie using PCA. """ num_frames, h, w = movie.shape frame_size = h * w frame_samples = np.reshape(movie, (num_frames, frame_size)).T ipca_f = sklearn.decomposition.IncrementalPCA(n_components=ncomp, batch_size=batch_size) ipca_f.fit(frame_samples) proj_frame_vectors = ipca_f.inverse_transform( ipca_f.transform(frame_samples)) eigenseries = ipca_f.components_.T eigenframes = np.dot(proj_frame_vectors, eigenseries) movie2 = caiman.movie( np.reshape(np.float32(proj_frame_vectors.T), movie.shape)) return movie2, eigenframes
def main(): #% mmat=loadmat('mov_AG051514-01-060914 C.mat')['mov'] m=cm.movie(mmat.transpose((2,0,1)),fr=120) mask=select_roi(m[0]) if 1: mov_tot=compute_optical_flow(m[:3000],mask) else: mov_tot=compute_optical_flow(m[:3000],mask,polar_coord=False) sp_filt,t_trace,norm_fact=extract_components(mov_tot) new_t_trace,coor_1,coor_2 = normalize_components(t_trace,sp_filt) plot_components(sp_filt,t_trace) #% id_comp=1 pl.plot(old_div(np.sum(np.reshape(sp_filt[id_comp]>1,[d1,d2])*mov_tot[1],axis=(1,2)),np.sum(sp_filt[id_comp]>1))) pl.plot(t_trace[id_comp][:,1])
def make_init_mmap(self): logger.debug('Making init memmap...') self.init_dir.mkdir(exist_ok=True, parents=True) self._validate_tiffs() mov = tiffs2array(movie_list=self.files, x_slice=self.xslice, y_slice=self.yslice, t_slice=self.tslice) self.frame_start = mov.shape[0] + 1 self.t = mov.shape[0] + 1 self.params.change_params(dict(init_batch=mov.shape[0])) m = cm.movie(mov.astype('float32')) save_path = f'initplane{self.plane}.mmap' init_mmap = m.save(save_path, order='C') logger.debug(f'Init mmap saved to {init_mmap}.') return init_mmap
def local_correlations_movie_offline(file_name, Tot_frames = None, fr = 10, window=30, stride = 3, swap_dim=True, eight_neighbours=True, order_mean = 1, ismulticolor = False, dview = None): import caiman as cm if Tot_frames is None: Tot_frames = cm.load(file_name).shape[0] params = [[file_name,range(j,j + window), eight_neighbours, swap_dim, order_mean, ismulticolor] for j in range(0,Tot_frames - window,stride)] if dview is None: # parallel_result = [self[j:j + window, :, :].local_correlations( # eight_neighbours=True,swap_dim=swap_dim, order_mean=order_mean)[np.newaxis, :, :] for j in range(T - window)] parallel_result = list(map(local_correlations_movie_parallel,params)) else: if 'multiprocessing' in str(type(dview)): parallel_result = dview.map_async( local_correlations_movie_parallel, params).get(4294967) else: parallel_result = dview.map_sync( local_correlations_movie_parallel, params) dview.results.clear() mm = cm.movie(np.concatenate(parallel_result, axis=0),fr=fr) return mm
def parameters_test_gSig(path, figname, gSig_filt_list=None): m_orig = cm.load(path) if gSig_filt_list == None: gSig_filt_list = [(2, 2), (4, 4), (6, 6), (8, 8), (10, 10), (20, 20), (30, 30)] m_filt_list = [] for i, gSig_filt in enumerate(gSig_filt_list): m_filt_list.append( cm.movie( np.array( [high_pass_filter_space(m_, gSig_filt) for m_ in m_orig]))) import matplotlib.pyplot as plt for i, mov in enumerate(m_filt_list): plt.figure() plt.imshow(mov[0], cmap='gray') gSig_size = gSig_filt_list[i] plt.title(f'{figname} \n gSig_size = {gSig_size}') plt.savefig( f'data/motion_correction/png/{figname}_gSig_experiment_{gSig_size}.png' ) return
def process_movie_parallel(arg_in): fname,fr,margins_out,template,max_shift_w, max_shift_h,remove_blanks,apply_smooth,save_hdf5=arg_in if template is not None: if isinstance(template,basestring): if os.path.exists(template): template=cm.load(template,fr=1) else: raise Exception('Path to template does not exist:'+template) # with open(fname[:-4]+'.stout', "a") as log: # print fname # sys.stdout = log # import pdb # pdb.set_trace() type_input = str(type(fname)) if 'movie' in type_input: print((type(fname))) Yr=fname elif ('ndarray' in type_input): Yr=cm.movie(np.array(fname,dtype=np.float32),fr=fr) elif isinstance(fname,basestring): Yr=cm.load(fname,fr=fr) else: raise Exception('Unkown input type:' + type_input) if Yr.ndim>1: print('loaded') if apply_smooth: print('applying smoothing') Yr=Yr.bilateral_blur_2D(diameter=10,sigmaColor=10000,sigmaSpace=0) # bl_yr=np.float32(np.percentile(Yr,8)) # Yr=Yr-bl_yr # needed to remove baseline print('Remove BL') if margins_out!=0: Yr=Yr[:,margins_out:-margins_out,margins_out:-margins_out] # borders create troubles print('motion correcting') Yr,shifts,xcorrs,template=Yr.motion_correct(max_shift_w=max_shift_w, max_shift_h=max_shift_h, method='opencv',template=template,remove_blanks=remove_blanks) # Yr = Yr + bl_yr if ('movie' in type_input) or ('ndarray' in type_input): print('Returning Values') return Yr, shifts, xcorrs, template else: print('median computing') template=Yr.bin_median() print('saving') idx_dot=len(fname.split('.')[-1]) if save_hdf5: Yr.save(fname[:-idx_dot]+'hdf5') print('saving 2') np.savez(fname[:-idx_dot]+'npz',shifts=shifts,xcorrs=xcorrs,template=template) print('deleting') del Yr print('done!') return fname[:-idx_dot] #sys.stdout = sys.__stdout__ else: return None
Y = np.reshape(Yr, dims + (T,), order='F') m_orig = np.array(images) patch_size = 50 half_crop = np.minimum(gSig[0]*4+1,patch_size)//2 idx_included = np.arange(A_gt.shape[-1]) #idx_included = idx_components_cnn[np.arange(50)] # neurons that are displayed all_pos_crops = [] all_neg_crops = [] all_dubious_crops = [] base_name = fname_new.split('/')[5] dims = (d1,d2) idx_included = np.array([x for x in idx_included if x is not None]) base_name = base_name + '_' + str(idx_included[0]) + '_' + str(idx_included[-1]) print(base_name) idx_excluded = np.setdiff1d(np.arange(A_gt.shape[-1]),idx_included) m_res = m_orig - cm.movie(np.reshape(A_gt.tocsc()[:,idx_excluded].dot(C_gt[idx_excluded]) + b_gt.dot(f_gt),dims+(-1,),order = 'F').transpose([2,0,1])) mean_proj = np.mean(m_res,0) std_mov = mean_proj.std() min_mov = np.median(mean_proj) #%% full_fov = False # will use either the overfeat like (full FOV, train_net_cifar_edge_cutter_FOV.py) network or the single patch one (train_net_cifar_edge_cutter.py) count_start = 30 dims = np.array(dims) #bin_ = 10 cms_total = [np.array(scipy.ndimage.center_of_mass(np.reshape(a.toarray(),dims,order = 'F'))).astype(np.int) for a in A_gt.tocsc()[:,idx_included].T] cms_total = np.maximum(cms_total ,half_crop) cms_total = np.array([np.minimum(cms,dims-half_crop) for cms in cms_total ]).astype(np.int) for count in range(count_start,T): if count%1000==0: print(count) print(count)
def save_memmap(filenames, base_name='Yr', resize_fact=(1, 1, 1), remove_init=0, idx_xy=None, order='F', xy_shifts=None, is_3D=False, add_to_movie=0, border_to_0=0, dview=None, n_chunks=100, slices=None): """ Efficiently write data from a list of tif files into a memory mappable file Args: filenames: list list of tif files or list of numpy arrays base_name: str the base used to build the file name. IT MUST NOT CONTAIN "_" resize_fact: tuple x,y, and z downsampling factors (0.5 means downsampled by a factor 2) remove_init: int number of frames to remove at the begining of each tif file (used for resonant scanning images if laser in rutned on trial by trial) idx_xy: tuple size 2 [or 3 for 3D data] for selecting slices of the original FOV, for instance idx_xy = (slice(150,350,None), slice(150,350,None)) order: string whether to save the file in 'C' or 'F' order xy_shifts: list x and y shifts computed by a motion correction algorithm to be applied before memory mapping is_3D: boolean whether it is 3D data add_to_movie: floating-point value to add to each image point, typically to keep negative values out. border_to_0: (undocumented) dview: (undocumented) n_chunks: (undocumented) slices: slice object or list of slice objects slice can be used to select portion of the movies in time and x,y directions. For instance slices = [slice(0,200),slice(0,100),slice(0,100)] will take the first 200 frames and the 100 pixels along x and y dimensions. Returns: fname_new: the name of the mapped file, the format is such that the name will contain the frame dimensions and the number of frames """ if type(filenames) is not list: raise Exception('input should be a list of filenames') if slices is not None: slices = [slice(0, None) if sl is None else sl for sl in slices] if len(filenames) > 1: recompute_each_memmap = False for file__ in filenames: if ('order_' + order not in file__) or ('.mmap' not in file__): recompute_each_memmap = True if recompute_each_memmap or (remove_init>0) or (idx_xy is not None)\ or (xy_shifts is not None) or (add_to_movie != 0) or (border_to_0>0)\ or slices is not None: logging.debug('Distributing memory map over many files') # Here we make a bunch of memmap files in the right order. Same parameters fname_new = cm.save_memmap_each(filenames, base_name=base_name, order=order, border_to_0=border_to_0, dview=dview, resize_fact=resize_fact, remove_init=remove_init, idx_xy=idx_xy, xy_shifts=xy_shifts, slices=slices, add_to_movie=add_to_movie) else: fname_new = filenames # The goal is to make a single large memmap file, which we do here if order == 'F': raise exception( 'You cannot merge files in F order, they must be in C order for CaImAn' ) fname_new = cm.save_memmap_join(fname_new, base_name=base_name, dview=dview, n_chunks=n_chunks) else: # TODO: can be done online Ttot = 0 for idx, f in enumerate(filenames): if isinstance(f, str): # Might not always be filenames. logging.debug(f) if is_3D: Yr = f if not (isinstance(f, basestring)) else tifffile.imread(f) if slices is not None: Yr = Yr[slices] else: if idx_xy is None: #todo remove if not used, superceded by the slices parameter Yr = Yr[remove_init:] elif len( idx_xy ) == 2: #todo remove if not used, superceded by the slices parameter Yr = Yr[remove_init:, idx_xy[0], idx_xy[1]] else: #todo remove if not used, superceded by the slices parameter Yr = Yr[remove_init:, idx_xy[0], idx_xy[1], idx_xy[2]] else: Yr = cm.load(f, fr=1, in_memory=True) if (isinstance( f, basestring) or isinstance(f, list)) else cm.movie( f) # TODO: Rewrite more legibly if xy_shifts is not None: Yr = Yr.apply_shifts(xy_shifts, interpolation='cubic', remove_blanks=False) if slices is not None: Yr = Yr[slices] else: if idx_xy is None: if remove_init > 0: Yr = Yr[remove_init:] elif len(idx_xy) == 2: Yr = Yr[remove_init:, idx_xy[0], idx_xy[1]] else: raise Exception( 'You need to set is_3D=True for 3D data)') Yr = np.array(Yr)[remove_init:, idx_xy[0], idx_xy[1], idx_xy[2]] if border_to_0 > 0: if slices is not None: if type(slices) is list: raise Exception( 'You cannot slice in x and y and then use add_to_movie: if you only want to slice in time do not pass in a list but just a slice object' ) min_mov = Yr.calc_min() Yr[:, :border_to_0, :] = min_mov Yr[:, :, :border_to_0] = min_mov Yr[:, :, -border_to_0:] = min_mov Yr[:, -border_to_0:, :] = min_mov fx, fy, fz = resize_fact if fx != 1 or fy != 1 or fz != 1: if 'movie' not in str(type(Yr)): Yr = cm.movie(Yr, fr=1) Yr = Yr.resize(fx=fx, fy=fy, fz=fz) T, dims = Yr.shape[0], Yr.shape[1:] Yr = np.transpose(Yr, list(range(1, len(dims) + 1)) + [0]) Yr = np.reshape(Yr, (np.prod(dims), T), order='F') Yr = np.ascontiguousarray(Yr, dtype=np.float32) + np.float32( 0.0001) + np.float32(add_to_movie) if idx == 0: fname_tot = base_name + '_d1_' + str(dims[0]) + '_d2_' + str( dims[1]) + '_d3_' + str( 1 if len(dims) == 2 else dims[2]) + '_order_' + str( order) # TODO: Rewrite more legibly if isinstance(f, str): fname_tot = os.path.join(os.path.split(f)[0], fname_tot) if len(filenames) > 1: big_mov = np.memmap(fname_tot, mode='w+', dtype=np.float32, shape=prepare_shape( (np.prod(dims), T)), order=order) big_mov[:, Ttot:Ttot + T] = Yr del big_mov else: logging.debug('SAVING WITH numpy.tofile()') Yr.tofile(fname_tot) else: big_mov = np.memmap(fname_tot, dtype=np.float32, mode='r+', shape=prepare_shape( (np.prod(dims), Ttot + T)), order=order) big_mov[:, Ttot:Ttot + T] = Yr del big_mov sys.stdout.flush() Ttot = Ttot + T fname_new = fname_tot + '_frames_' + str(Ttot) + '_.mmap' try: # need to explicitly remove destination on windows os.unlink(fname_new) except OSError: pass os.rename(fname_tot, fname_new) return fname_new
mc = MotionCorrect(fname, min_mov, dview=dview, max_shifts=max_shifts, niter_rig=niter_rig, splits_rig=splits_rig, num_splits_to_process_rig=num_splits_to_process_rig, strides=strides, overlaps=overlaps, splits_els=splits_els, num_splits_to_process_els=num_splits_to_process_els, upsample_factor_grid=upsample_factor_grid, max_deviation_rigid=max_deviation_rigid, shifts_opencv=True, nonneg_movie=True) #%% mc.motion_correct_rigid(save_movie=True) # load motion corrected movie #%% pl.imshow(mc.total_template_rig, cmap='gray') #%% visualize templates cm.movie(np.array(mc.templates_rig)).play( fr=10, gain=5, magnification=2, offset=offset_mov) #%% plot rigid shifts pl.close() pl.plot(mc.shifts_rig) pl.legend(['x shifts', 'y shifts']) pl.xlabel('frames') pl.ylabel('pixels') #%% inspect movie bord_px_rig = np.ceil(np.max(mc.shifts_rig)).astype(np.int) downsample_ratio = .2 m_rig.resize(1, 1, downsample_ratio).play( gain=10, offset=offset_mov * .25, fr=30, magnification=2, bord_px=bord_px_rig) #%% mc.motion_correct_pwrigid( save_movie=True, template=mc.total_template_rig, show_template=True) m_els = cm.load(mc.fname_tot_els)
'thr_plot': 0.8 } # TODO: do find&replace on those parameters and delete this paragrph # @params fname name of the movie fname_new = params_movie['fname'][0] # %% RUN ANALYSIS c, dview, n_processes = cm.cluster.setup_cluster( backend='local', n_processes=None, single_thread=False) # %% LOAD MEMMAP FILE # fname_new='Yr_d1_501_d2_398_d3_1_order_F_frames_369_.mmap' Yr, dims, T = cm.load_memmap(fname_new) d1, d2 = dims images = np.reshape(Yr.T, [T] + list(dims), order='F') Y = np.reshape(Yr, dims + (T,), order='F') m_images = cm.movie(images) # %% correlation image if m_images.shape[0] < 10000: Cn = m_images.local_correlations( swap_dim=params_movie['swap_dim'], frames_per_chunk=1500) Cn[np.isnan(Cn)] = 0 else: Cn = np.array(cm.load(('/'.join(params_movie['gtname'][0].split('/')[:-2] + [ 'projections', 'correlation_image_better.tif'])))).squeeze() pl.imshow(Cn, cmap='gray', vmax=.95) # TODO: show screenshot 11 #%% import cv2 if not '.mat' in params_movie['seed_name'][0]: roi_cons = np.load(params_movie['seed_name'][0]) else:
fname_new = params_movie['fname'] # %% RUN ANALYSIS c, dview, n_processes = cm.cluster.setup_cluster( backend='local', n_processes=None, single_thread=False) # %% LOAD MEMMAP FILE # fname_new='Yr_d1_501_d2_398_d3_1_order_F_frames_369_.mmap' Yr, dims, T = cm.load_memmap(fname_new) d1, d2 = dims images = np.reshape(Yr.T, [T] + list(dims), order='F') # TODO: needinfo Y = np.reshape(Yr, dims + (T,), order='F') m_images = cm.movie(images) # TODO: show screenshot 10 # %% correlation image if m_images.shape[0] < 10000: Cn = m_images.local_correlations( swap_dim=params_movie['swap_dim'], frames_per_chunk=1500) Cn[np.isnan(Cn)] = 0 else: Cn = np.array(cm.load(('/'.join(fname_new.split('/') [:-3] + ['projections', 'correlation_image_better.tif'])))).squeeze() pl.imshow(Cn, cmap='gray', vmax=.95) # %% some parameter settings # order of the autoregressive fit to calcium imaging in general one (slow gcamps) or two (fast gcamps fast scanning) p = params_movie['p']
# pl.figure(); pl.subplot(1, 3, 1) crd = plot_contours(A.tocsc()[:, idx_components], Cn, thr=0.9) pl.subplot(1, 3, 2) crd = plot_contours(A.tocsc()[:, idx_blobs], Cn, thr=0.9) pl.subplot(1, 3, 3) crd = plot_contours(A.tocsc()[:, idx_components_bad], Cn, thr=0.9) #%% #idx_very_nice=[2, 19, 23, 27,32,43,45,49,51,94,100] # idx_very_nice=np.array(idx_very_nice)[np.array([3,4,8,10])] # idx_very_nice=idx_blobs[idx_very_nice] idx_very_nice = idx_blobs view_patches_bar(Yr, scipy.sparse.coo_matrix(A.tocsc()[:, idx_very_nice]), C[ idx_very_nice, :], b, f, dims[0], dims[1], YrA=YrA[idx_very_nice, :], img=Cn) #%% new_m = cm.movie(np.reshape(A.tocsc()[ :, idx_blobs] * C[idx_blobs] + b.dot(f), dims + (-1,), order='F').transpose([2, 0, 1])) new_m.play(fr=30, backend='opencv', gain=7., magnification=3.) #%% new_m = cm.movie(np.reshape(A.tocsc()[:, idx_blobs] * C[idx_blobs] + b * np.median(f), dims + (-1,), order='F').transpose([2, 0, 1])) new_m.play(fr=30, backend='opencv', gain=7., magnification=3.) #%% new_m = cm.movie(np.reshape(A.tocsc()[ :, idx_blobs] * C[idx_blobs], dims + (-1,), order='F').transpose([2, 0, 1])) new_m.play(fr=30, backend='opencv', gain=30., magnification=3.) #%% # idx_to_show=[0,1,5,8,14,17,18,23,24,25,26,28,29,31,32,33,34,36,43,45,47,51,53,54,57,60,61,62,63,64,65,66,67,71,72,74,75,78,79,80,81,91,95,96,97,99,102] #cm.view_patches_bar(Yr,scipy.sparse.coo_matrix(A.tocsc()[:,sure_in_idx[idx_to_show]]),C[sure_in_idx[idx_to_show],:],b,f, dims[0],dims[1], YrA=YrA[sure_in_idx[idx_to_show],:],img=np.mean(Y,-1)) #%% # idx_to_show=[0,1,5,8,14,17,18,23,24,25,26,28,29,31,32,33,34,36,43,45,47,51,53,54,57,60,61,62,63,64,65,66,67,71,72,74,75,78,79,80,81,91,95,96,97,99,102] # idx_to_show=np.array(idx_to_show)[[2,19,23,26,34]]
tr_BL = scipy.ndimage.zoom(np.array(tr_BL, dtype=np.float32), [ downsampfact, 1], order=3, mode='constant', cval=0.0, prefilter=True) if padafter == 0: traces_gt -= tr_BL.T else: traces_gt -= tr_BL[padbefore:-padafter].T #traces_gt = scipy.signal.savgol_filter(traces_gt,5,2) #% fitness, exceptionality, sd_r, md = cm.components_evaluation.compute_event_exceptionality( traces_gt[idx_included], robust_std=False, N=5, use_mode_fast=False) fitness_d, exceptionality_d, sd_r_d, md_d = cm.components_evaluation.compute_event_exceptionality( np.diff(traces_gt[idx_included], axis=1), robust_std=False, N=3, use_mode_fast=False) #% m_res = m_orig - cm.movie(np.reshape(A_gt.tocsc()[:, idx_excluded].dot( C_gt[idx_excluded]) + b_gt.dot(f_gt), dims + (-1,), order='F').transpose([2, 0, 1])) #% # mean_proj = np.mean(m_res,0) #% # max_mov = mean_proj.max() #%% # min_mov = mean_proj.min() #%% # m_res = (m_res-min_mov)/(max_mov-min_mov) #%% # m_res.play() #%% # cnn = False # if cnn: # import json as simplejson # from keras.models import model_from_json
#a = cm.load('/mnt/ceph/neuro/labeling/yuste.Single_150u/images/tifs/Single_150um_024.tif') #a = cm.load('/Users/agiovann/example_movies_ALL/quietBlock_2_ds_2_2.hdf5') a = cm.load('/mnt/ceph/neuro/labeling/yuste.Single_150u/images/tifs/Yr_d1_200_d2_256_d3_1_order_C_frames_3000_.mmap') all_els = [] for it in range(1): print(it) # a = cm.movie(np.random.randn(*mns.shape).astype(np.float32)) Yr = remove_baseline_fast(np.array(cm.movie.to_2D(a)).T).T #% # norm = lambda(x): np.exp(-x**2/2)/np.sqrt(2*np.pi) fitness, res, sd_r, md = compute_event_exceptionality(Yr.T) Yr_c = -np.log(norm.sf(np.array((Yr - md) / sd_r, dtype=np.float))) mns = cm.movie(scipy.ndimage.convolve(np.reshape( Yr_c, [-1, a.shape[1], a.shape[2]], order='F'), np.ones([5, 3, 3]))) mns[mns < (38 * np.log10((mns.shape[0])))] = 0 all_els.append(np.sum(mns > 0)/Yr.size) print(all_els) #%% #m1 = cm.movie((np.array((Yr-md)/sd_r)).reshape([-1,60,80],order = 'F'))*(scipy.ndimage.convolve(mns>0,np.ones([5,3,3]))) m1 = cm.movie((np.array((Yr - md) / sd_r) ).reshape([-1, a.shape[1],a.shape[2]], order='F')) * (mns > 0) #%% m2 = cm.movie((np.array((Yr-md)/sd_r)).reshape(m1.shape,order = 'F'))*(scipy.ndimage.convolve(mns>0,np.ones([5,3,3]))) #%% if False: b1 = np.percentile(Yr,20,axis=0).reshape([a.shape[1],a.shape[2]], order='F')
#%% 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'] add_to_movie = - np.min(total_template_rig) + 1 num_iter = 1 for num_splits_to_process in num_splits_to_process_list[-1:]: 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, strides, overlaps, add_to_movie, newoverlaps=None, newstrides=None,
mc_list.append(mc) t_a = time.time() - t1 print(time.time() - t1) # %% # load motion corrected movie if isscreen: m_rig = cm.load(mc_list[-2].fname_tot_rig) m_rig.resize(1, 1, .2).play(gain=1, offset=offset_mov, fr=30) pl.imshow(mc.total_template_rig, cmap='gray') #%% cm.movie(np.array([mc_.total_template_rig for mc_ in mc_list])).play( fr=8, gain=1, offset=offset_mov) # %% visualize templates cm.movie(np.array(mc.templates_rig)).play( fr=10, gain=1, magnification=1, offset=offset_mov) # %% plot rigid shifts pl.close() pl.plot(np.vstack([mc.shifts_rig for mc in mc_list])) pl.legend(['x shifts', 'y shifts']) pl.xlabel('frames') pl.ylabel('pixels') # TODO: show screenshot 9 # %% restart cluster to clean up memory # TODO: todocument # THIS SOMETIMES GETS STUCK IN SCRIPT MODE c, dview, n_processes = cm.cluster.setup_cluster( backend='local', n_processes=None, single_thread=False)
cv2.waitKey(300) count_neuro += 1 print(count_neuro) break # crop_img = cv2.resize(crop_img,dsize=None,fx=2,fy=2) # newshape = np.array(crop_img.shape)//2 # crop_img = crop_img[newshape[0]-half_crop:newshape[0]+half_crop,newshape[0]-half_crop:newshape[0]+half_crop] # NOTE: its img[y: y + h, x: x + w] and *not* img[x: x + w, y: y + h] id_file += 1 all_masks_gt = np.vstack(all_masks_gt) #%% cm.movie(np.squeeze(all_masks_gt[labels_gt == 0])).play( gain=3., magnification=10) #%% np.savez('ground_truth_components_minions.npz', all_masks_gt=all_masks_gt, labels_gt=labels_gt, traces_gt=traces_gt) #%% import itertools def grouper(n, iterable, fillvalue=None): "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx" args = [iter(iterable)] * n return itertools.zip_longest(*args, fillvalue=fillvalue) #%% curate once more. Remove wrong negatives negatives = np.where(labels_gt == 1)[0]
def save_memmap(filenames, base_name='Yr', resize_fact=(1, 1, 1), remove_init=0, idx_xy=None, order='F', xy_shifts=None, is_3D=False, add_to_movie=0, border_to_0=0, dview = None, n_chunks=100): """ Efficiently write data from a list of tif files into a memory mappable file Parameters: ---------- filenames: list list of tif files or list of numpy arrays base_name: str the base used to build the file name. IT MUST NOT CONTAIN "_" resize_fact: tuple x,y, and z downsampling factors (0.5 means downsampled by a factor 2) remove_init: int number of frames to remove at the begining of each tif file (used for resonant scanning images if laser in rutned on trial by trial) idx_xy: tuple size 2 [or 3 for 3D data] for selecting slices of the original FOV, for instance idx_xy = (slice(150,350,None), slice(150,350,None)) order: string whether to save the file in 'C' or 'F' order xy_shifts: list x and y shifts computed by a motion correction algorithm to be applied before memory mapping is_3D: boolean whether it is 3D data add_to_movie: floating-point value to add to each image point, typically to keep negative values out. border_to_0: (undocumented) dview: (undocumented) n_chunks: (undocumented) Returns: ------- fname_new: the name of the mapped file, the format is such that the name will contain the frame dimensions and the number of frames """ if type(filenames) is not list: raise Exception('input should be a list of filenames') if len(filenames) > 1: is_inconsistent_order = False for file__ in filenames: if ('order_' + order not in file__) or ('.mmap' not in file__): is_inconsistent_order = True if is_inconsistent_order: # Here we make a bunch of memmap files in the right order. Same parameters fname_new = cm.save_memmap_each(filenames, base_name = base_name, order = order, border_to_0 = border_to_0, dview = dview, resize_fact = resize_fact, remove_init = remove_init, idx_xy = idx_xy, xy_shifts = xy_shifts, add_to_movie = add_to_movie) else: fname_new = filenames # The goal is to make a single large memmap file, which we do here if order == 'F': raise exception('You cannot merge files in F order, they must be in C order') fname_new = cm.save_memmap_join(fname_new, base_name=base_name, dview=dview, n_chunks=n_chunks, add_to_mov = add_to_movie) else: # TODO: can be done online Ttot = 0 for idx, f in enumerate(filenames): if isinstance(f, str): # Might not always be filenames. print(f) if is_3D: Yr = f if not(isinstance(f, basestring)) else tifffile.imread(f) if idx_xy is None: Yr = Yr[remove_init:] elif len(idx_xy) == 2: Yr = Yr[remove_init:, idx_xy[0], idx_xy[1]] else: Yr = Yr[remove_init:, idx_xy[0], idx_xy[1], idx_xy[2]] else: Yr = cm.load(f, fr=1, in_memory=True) if (isinstance(f, basestring) or isinstance(f, list)) else cm.movie(f) # TODO: Rewrite more legibly if xy_shifts is not None: Yr = Yr.apply_shifts(xy_shifts, interpolation='cubic', remove_blanks=False) if idx_xy is None: if remove_init > 0: Yr = Yr[remove_init:] elif len(idx_xy) == 2: Yr = Yr[remove_init:, idx_xy[0], idx_xy[1]] else: raise Exception('You need to set is_3D=True for 3D data)') Yr = np.array(Yr)[remove_init:, idx_xy[0], idx_xy[1], idx_xy[2]] if border_to_0 > 0: min_mov = Yr.calc_min() Yr[:, :border_to_0, :] = min_mov Yr[:, :, :border_to_0] = min_mov Yr[:, :, -border_to_0:] = min_mov Yr[:, -border_to_0:, :] = min_mov fx, fy, fz = resize_fact if fx != 1 or fy != 1 or fz != 1: if 'movie' not in str(type(Yr)): Yr = cm.movie(Yr, fr=1) Yr = Yr.resize(fx=fx, fy=fy, fz=fz) T, dims = Yr.shape[0], Yr.shape[1:] Yr = np.transpose(Yr, list(range(1, len(dims) + 1)) + [0]) Yr = np.reshape(Yr, (np.prod(dims), T), order='F') Yr = np.ascontiguousarray(Yr, dtype=np.float32) + 0.0001 + add_to_movie if idx == 0: fname_tot = base_name + '_d1_' + str(dims[0]) + '_d2_' + str(dims[1]) + '_d3_' + str( 1 if len(dims) == 2 else dims[2]) + '_order_' + str(order) # TODO: Rewrite more legibly if isinstance(f, str): fname_tot = os.path.join(os.path.split(f)[0], fname_tot) if len(filenames) > 1: big_mov = np.memmap(fname_tot, mode='w+', dtype=np.float32, shape=prepare_shape((np.prod(dims), T)), order=order) big_mov[:, Ttot:Ttot + T] = Yr del big_mov else: print('SAVING WITH numpy.tofile()') Yr.tofile(fname_tot) else: big_mov = np.memmap(fname_tot, dtype=np.float32, mode='r+', shape=prepare_shape((np.prod(dims), Ttot + T)), order=order) big_mov[:, Ttot:Ttot + T] = Yr del big_mov sys.stdout.flush() Ttot = Ttot + T fname_new = fname_tot + '_frames_' + str(Ttot) + '_.mmap' try: # need to explicitly remove destination on windows os.unlink(fname_new) except OSError: pass os.rename(fname_tot, fname_new) return fname_new
def save_memmap(filenames, base_name='Yr', resize_fact=(1, 1, 1), remove_init=0, idx_xy=None, order='F',xy_shifts=None,is_3D=False,add_to_movie=0,border_to_0=0): """ Saves efficiently a list of tif files into a memory mappable file Parameters ---------- filenames: list list of tif files base_name: str the base used to build the file name. IT MUST NOT CONTAIN "_" resize_fact: tuple x,y, and z downampling factors (0.5 means downsampled by a factor 2) remove_init: int number of frames to remove at the begining of each tif file (used for resonant scanning images if laser in rutned on trial by trial) idx_xy: tuple size 2 [or 3 for 3D data] for selecting slices of the original FOV, for instance idx_xy=(slice(150,350,None),slice(150,350,None)) order: string whether to save the file in 'C' or 'F' order xy_shifts: list x and y shifts computed by a motion correction algorithm to be applied before memory mapping is_3D: boolean whether it is 3D data Return ------- fname_new: the name of the mapped file, the format is such that the name will contain the frame dimensions and the number of f """ #TODO: can be done online Ttot = 0 for idx, f in enumerate(filenames): print(f) if is_3D: import tifffile print("Using tifffile library instead of skimage because of 3D") if idx_xy is None: Yr = tifffile.imread(f)[remove_init:] elif len(idx_xy) == 2: Yr = tifffile.imread(f)[remove_init:, idx_xy[0], idx_xy[1]] else: Yr = tifffile.imread(f)[remove_init:, idx_xy[0], idx_xy[1], idx_xy[2]] # elif : # # if xy_shifts is not None: # raise Exception('Calblitz not installed, you cannot motion correct') # # if idx_xy is None: # Yr = imread(f)[remove_init:] # elif len(idx_xy) == 2: # Yr = imread(f)[remove_init:, idx_xy[0], idx_xy[1]] # else: # raise Exception('You need to set is_3D=True for 3D data)') else: Yr=cm.load(f,fr=1) if xy_shifts is not None: Yr=Yr.apply_shifts(xy_shifts,interpolation='cubic',remove_blanks=False) if idx_xy is None: Yr = np.array(Yr)[remove_init:] elif len(idx_xy) == 2: Yr = np.array(Yr)[remove_init:, idx_xy[0], idx_xy[1]] else: raise Exception('You need to set is_3D=True for 3D data)') Yr = np.array(Yr)[remove_init:, idx_xy[0], idx_xy[1], idx_xy[2]] if border_to_0>0: min_mov=np.min(Yr) Yr[:,:border_to_0,:]=min_mov Yr[:,:,:border_to_0]=min_mov Yr[:,:,-border_to_0:]=min_mov Yr[:,-border_to_0:,:]=min_mov fx, fy, fz = resize_fact if fx != 1 or fy != 1 or fz != 1: Yr = cm.movie(Yr, fr=1) Yr = Yr.resize(fx=fx, fy=fy, fz=fz) T, dims = Yr.shape[0], Yr.shape[1:] Yr = np.transpose(Yr, list(range(1, len(dims) + 1)) + [0]) Yr = np.reshape(Yr, (np.prod(dims), T), order='F') if idx == 0: fname_tot = base_name + '_d1_' + str(dims[0]) + '_d2_' + str(dims[1]) + '_d3_' + str( 1 if len(dims) == 2 else dims[2]) + '_order_' + str(order) fname_tot = os.path.join(os.path.split(f)[0],fname_tot) big_mov = np.memmap(fname_tot, mode='w+', dtype=np.float32, shape=(np.prod(dims), T), order=order) else: big_mov = np.memmap(fname_tot, dtype=np.float32, mode='r+', shape=(np.prod(dims), Ttot + T), order=order) # np.save(fname[:-3]+'npy',np.asarray(Yr)) big_mov[:, Ttot:Ttot + T] = np.asarray(Yr, dtype=np.float32) + 1e-10 + add_to_movie big_mov.flush() del big_mov Ttot = Ttot + T fname_new = fname_tot + '_frames_' + str(Ttot) + '_.mmap' os.rename(fname_tot, fname_new) return fname_new
with open(json_path, "w") as json_file: json_file.write(simplejson.dumps(simplejson.loads(model_json), indent=4)) print('Saved trained model at %s ' % json_path) if not os.path.isdir(save_dir): os.makedirs(save_dir) model_path = os.path.join(save_dir, model_name + '.h5') model.save(model_path) print('Saved trained model at %s ' % model_path) #%% visualize_results predictions = model.predict(all_masks_gt, batch_size=32, verbose=1) cm.movie(np.squeeze(all_masks_gt[np.where(predictions[:, 0] >= 0.5)[0]])).play( gain=3., magnification=5, fr=10) #%% cm.movie(np.squeeze(all_masks_gt[np.where(predictions[:, 1] >= 0.5)[0]])).play( gain=3., magnification=5, fr=10) #%% cm.movie(np.squeeze(all_masks_gt[np.where(predictions[:, 2] >= 0.5)[0]])).play( gain=3., magnification=5, fr=10) #%% retrieve and test json_file = open(json_path, 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) loaded_model.load_weights(model_path) opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6) loaded_model.compile(loss=keras.losses.categorical_crossentropy,
max_shifts, dview=dview, splits=splits, num_splits_to_process=num_splits_to_process, num_iter=num_iter, template=total_template_rig, shifts_opencv=shifts_opencv, save_movie_rigid=save_movie_rigid, add_to_movie=add_to_movie) total_shifts.append(shifts_rig) templates_all.append(total_template_rig) t2 = time.time() - t1 print(t2) pl.imshow(total_template_rig, cmap='gray', vmax=np.percentile(total_template_rig, 95)) pl.pause(1) #%% pl.close() pl.plot(np.concatenate(total_shifts, 0)) #%% visualize all templates cm.movie(np.array(templates_all)).play(fr=2, gain=5) #%% PIECEWISE RIGID MOTION CORRECTION total_shifts_els = [] templates_all_els = [] new_templ = total_template_rig.copy() strides = params_movie['strides'] overlaps = params_movie['overlaps'] shifts_opencv = True 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['upsample_factor_grid'] for file_to_process in all_files:
spatial_filter_, time_trace_, of_or = cm.behavior.behavior.extract_motor_components_OF(m, n_components, mask=mask, resize_fact=resize_fact, only_magnitude=only_magnitude, verbose=True, method_factorization='dict_learn', max_iter_DL=max_iter_DL) #%% mags, dircts, dircts_thresh, spatial_masks_thrs = cm.behavior.behavior.extract_magnitude_and_angle_from_OF( spatial_filter_, time_trace_, of_or, num_std_mag_for_angle=num_std_mag_for_angle, sav_filter_size=3, only_magnitude=only_magnitude) #%% idd = 0 axlin = pl.subplot(n_components, 2, 2) for mag, dirct, spatial_filter in zip(mags, dircts_thresh, spatial_filter_): pl.subplot(n_components, 2, 1 + idd * 2) min_x, min_y = np.min(np.where(mask), 1) # max_x,max_y = np.max(np.where(mask),1) spfl = spatial_filter spfl = cm.movie(spfl[None, :, :]).resize( 1 / resize_fact, 1 / resize_fact, 1).squeeze() max_x, max_y = np.add((min_x, min_y), np.shape(spfl)) mask[min_x:max_x, min_y:max_y] = spfl mask[mask < np.nanpercentile(spfl, 70)] = np.nan # spfl = ld['spatial_filter'][idd] pl.imshow(m[0], cmap='gray') pl.imshow(mask, alpha=.5) pl.axis('off') axelin = pl.subplot(n_components, 2, 2 + idd * 2, sharex=axlin) pl.plot(mag / 10, 'k') dirct[mag < 0.5 * np.std(mag)] = np.nan pl.plot(dirct, 'r-', linewidth=2) # gt_pix_2 = scipy.signal.savgol_filter(np.diff(np.array(pts[8]).T,axis=0)[2:],3,1)
def get_behavior_traces(fname,t0,t1,freq,ISI,draw_rois=False,plot_traces=False,mov_filt_1d=True,window_hp=201,window_lp=3,interpolate=True,EXPECTED_ISI=.25): """ From hdf5 movies extract eyelid closure and wheel movement Parameters ---------- fname: str file name of the hdf5 file t0,t1: float. Times of beginning and end of trials (in general 0 and 8 for our dataset) to build the absolute time vector freq: float frequency used to build the final time vector ISI: float inter stimulu interval draw_rois: bool whether to manually draw the eyelid contour plot_traces: bool whether to plot the traces during extraction mov_filt_1d: bool whether to filter the movie after extracting the average or ROIs. The alternative is a 3D filter that can be very computationally expensive window_lp, window_hp: ints number of frames to be used to median filter the data. It is needed because of the light IR artifact coming out of the eye Returns ------- res: dict dictionary with fields 'eyelid': eyelid trace 'wheel': wheel trace 'time': absolute tim vector 'trials': corresponding indexes of the trials 'trial_info': for each trial it returns start trial, end trial, time CS, time US, trial type (CS:0 US:1 CS+US:2) 'idx_CS_US': idx trial CS US 'idx_US': idx trial US 'idx_CS': idx trial CS """ CS_ALONE=0 US_ALONE=1 CS_US=2 meta_inf = fname[:-7]+'data.h5' time_abs=np.linspace(t0,t1,freq*(t1-t0)) T=len(time_abs) t_us=0 t_cs=0 n_samples_ISI=np.int(ISI*freq) t_uss=[] ISIs=[] eye_traces=[] wheel_traces=[] trial_info=[] tims=[] with h5py.File(fname) as f: with h5py.File(meta_inf) as dt: rois=np.asarray(dt['roi'],np.float32) trials = list(f.keys()) trials.sort(key=lambda x: np.int(x.replace('trial_',''))) trials_idx=[np.int(x.replace('trial_',''))-1 for x in trials] trials_idx_=[] for tr,idx_tr in zip(trials[:],trials_idx[:]): if plot_traces: pl.cla() print(tr) trial=f[tr] mov=np.asarray(trial['mov']) if draw_rois: pl.imshow(np.mean(mov,0)) pl.xlabel('Draw eye') pts=pl.ginput(-1) pts = np.asarray(pts, dtype=np.int32) data = np.zeros(np.shape(mov)[1:], dtype=np.int32) # if CV_VERSION == 2: #lt = cv2.CV_AA # elif CV_VERSION == 3: lt = cv2.LINE_AA cv2.fillConvexPoly(data, pts, (1,1,1), lineType=lt) rois[0]=data pl.close() pl.imshow(np.mean(mov,0)) pl.xlabel('Draw wheel') pts=pl.ginput(-1) pts = np.asarray(pts, dtype=np.int32) data = np.zeros(np.shape(mov)[1:], dtype=np.int32) # if CV_VERSION == 2: #lt = cv2.CV_AA # elif CV_VERSION == 3: lt = cv2.LINE_AA cv2.fillConvexPoly(data, pts, (1,1,1), lineType=lt) rois[1]=data pl.close() # eye_trace=np.mean(mov*rois[0],axis=(1,2)) # mov_trace=np.mean((np.diff(np.asarray(mov,dtype=np.float32),axis=0)**2)*rois[1],axis=(1,2)) mov=np.transpose(mov,[0,2,1]) mov=mov[:,:,::-1] if mov.shape[0]>0: ts=np.array(trial['ts']) if np.size(ts)>0: assert np.std(np.diff(ts))<0.005, 'Time stamps of behaviour are unreliable' if interpolate: new_ts=np.linspace(0,ts[-1,0]-ts[0,0],np.shape(mov)[0]) if dt['trials'][idx_tr,-1] == US_ALONE: t_us=np.maximum(t_us,dt['trials'][idx_tr,3]-dt['trials'][idx_tr,0]) mmm=mov[:n_samples_ISI].copy() mov=mov[:-n_samples_ISI] mov=np.concatenate([mmm,mov]) elif dt['trials'][idx_tr,-1] == CS_US: t_cs=np.maximum(t_cs,dt['trials'][idx_tr,2]-dt['trials'][idx_tr,0]) t_us=np.maximum(t_us,dt['trials'][idx_tr,3]-dt['trials'][idx_tr,0]) t_uss.append(t_us) ISI=t_us-t_cs ISIs.append(ISI) n_samples_ISI=np.int(ISI*freq) else: t_cs=np.maximum(t_cs,dt['trials'][idx_tr,2]-dt['trials'][idx_tr,0]) new_ts=new_ts tims.append(new_ts) else: start,end,t_CS,t_US= dt['trials'][idx_tr,:-1]-dt['trials'][idx_tr,0] f_rate=np.median(np.diff(ts[:,0])) ISI=t_US-t_CS idx_US=np.int(old_div(t_US,f_rate)) idx_CS=np.int(old_div(t_CS,f_rate)) fr_before_US=np.int(old_div((t_US - start -.1),f_rate)) fr_after_US=np.int(old_div((end -.1 - t_US),f_rate)) idx_abs=np.arange(-fr_before_US,fr_after_US) time_abs=idx_abs*f_rate assert np.abs(ISI-EXPECTED_ISI)<.01, str(np.abs(ISI-EXPECTED_ISI)) + ':the distance form CS and US is different from what expected' # trig_US= # new_ts= mov_e=cb.movie(mov*rois[0][::-1].T,fr=old_div(1,np.mean(np.diff(new_ts)))) mov_w=cb.movie(mov*rois[1][::-1].T,fr=old_div(1,np.mean(np.diff(new_ts)))) x_max_w,y_max_w=np.max(np.nonzero(np.max(mov_w,0)),1) x_min_w,y_min_w=np.min(np.nonzero(np.max(mov_w,0)),1) x_max_e,y_max_e=np.max(np.nonzero(np.max(mov_e,0)),1) x_min_e,y_min_e=np.min(np.nonzero(np.max(mov_e,0)),1) mov_e=mov_e[:,x_min_e:x_max_e,y_min_e:y_max_e] mov_w=mov_w[:,x_min_w:x_max_w,y_min_w:y_max_w] # mpart=mov[:20].copy() # md=cse.utilities.mode_robust(mpart.flatten()) # N=np.sum(mpart<=md) # mpart[mpart>md]=md # mpart[mpart==0]=md # mpart=mpart-md # std=np.sqrt(np.sum(mpart**2)/N) # thr=md+10*std # # thr=np.minimum(255,thr) # return mov if mov_filt_1d: mov_e=np.mean(mov_e, axis=(1,2)) window_hp_=window_hp window_lp_=window_lp if plot_traces: pl.plot(old_div((mov_e-np.mean(mov_e)),(np.max(mov_e)-np.min(mov_e)))) else: window_hp_=(window_hp,1,1) window_lp_=(window_lp,1,1) bl=signal.medfilt(mov_e,window_hp_) mov_e=signal.medfilt(mov_e-bl,window_lp_) if mov_filt_1d: eye_=np.atleast_2d(mov_e) else: eye_=np.atleast_2d(np.mean(mov_e, axis=(1,2))) wheel_=np.concatenate([np.atleast_1d(0),np.nanmean(np.diff(mov_w,axis=0)**2,axis=(1,2))]) if np.abs(new_ts[-1] - time_abs[-1])>1: raise Exception('Time duration is significantly larger or smaller than reference time') wheel_=np.squeeze(wheel_) eye_=np.squeeze(eye_) f1=scipy.interpolate.interp1d(new_ts , eye_,bounds_error=False,kind='linear') eye_=np.array(f1(time_abs)) f1=scipy.interpolate.interp1d(new_ts , wheel_,bounds_error=False,kind='linear') wheel_=np.array(f1(time_abs)) if plot_traces: pl.plot( old_div((eye_), (np.nanmax(eye_)-np.nanmin(eye_))),'r') pl.plot( old_div((wheel_ -np.nanmin(wheel_)), np.nanmax(wheel_)),'k') pl.pause(.01) trials_idx_.append(idx_tr) eye_traces.append(eye_) wheel_traces.append(wheel_) trial_info.append(dt['trials'][idx_tr,:]) res=dict() res['eyelid'] = eye_traces res['wheel'] = wheel_traces res['time'] = time_abs - np.median(t_uss) res['trials'] = trials_idx_ res['trial_info'] = trial_info res['idx_CS_US'] = np.where(list(map(int,np.array(trial_info)[:,-1]==CS_US)))[0] res['idx_US'] = np.where(list(map(int,np.array(trial_info)[:,-1]==US_ALONE)))[0] res['idx_CS'] = np.where(list(map(int,np.array(trial_info)[:,-1]==CS_ALONE)))[0] return res
for i, j in zip(x, y): cv2.rectangle(data, (i * 8, j * 8), (i * 8 + 64, j * 8 + 64), (0, 0, 255), 5) return data annotated = locate(data) plt.title("Augmented") plt.imshow(annotated) plt.show() #%% visualize_results num_sampl = 30000 predictions = model.predict( all_masks_gt[:num_sampl, :, :, None], batch_size=32, verbose=1) cm.movie(np.squeeze(all_masks_gt[np.where(predictions[:num_sampl, 0] >= 0.95)[ 0]])).play(gain=3., magnification=5, fr=10) #%% cm.movie(np.squeeze(all_masks_gt[np.where(predictions[:num_sampl, 1] >= 0.95)[ 0]])).play(gain=3., magnification=5, fr=10) #%% pl.imshow(montage2d(all_masks_gt[np.where((labels_gt[:num_sampl] == 0) & ( predictions[:num_sampl, 1] > 0.95))[0]].squeeze())) #%% pl.imshow(montage2d(all_masks_gt[np.where((labels_gt[:num_sampl] == 1) & ( predictions[:num_sampl, 0] > 0.95))[0]].squeeze())) #%% pl.imshow(montage2d(all_masks_gt[np.where( (predictions[:num_sampl, 0] > 0.95))[0]].squeeze())) #%% retrieve and test json_file = open(json_path, 'r') loaded_model_json = json_file.read()
m_res = glob.glob('Sue*.tif') final_size = (512 - 24, 512 - 24) winsize = 100 resize_fact_flow = .2 for mv in m_res: tmpl, correlations, flows_orig, norms, smoothness = compute_metrics_motion_correction( mv, final_size[0], final_size[1], winsize=winsize, play_flow=False, resize_fact_flow=resize_fact_flow) #%% run comparisons SUITE2P for mvs in glob.glob('Sue*2000*16*.mat'): print(mvs) cm.movie(scipy.io.loadmat(mvs)['data'].transpose( [2, 0, 1])).save(mvs[:-3] + '.hdf5') #%% m_fluos = glob.glob('M_FLUO*.hdf5') final_size = (64 - 20, 128 - 20) winsize = 32 resize_fact_flow = 1 for mv in m_fluos: tmpl, correlations, flows_orig, norms, smoothness = compute_metrics_motion_correction( mv, final_size[0], final_size[1], winsize=winsize, play_flow=False, resize_fact_flow=resize_fact_flow) #% run comparisons resonant m_res = glob.glob('Sue_2000*16*.hdf5')
1, 1, downsample_factor), add_to_movie=add_to_movie) name_new.sort() fname_new = cm.save_memmap_join(name_new, base_name='Yr', dview=dview) #%% LOAD MEMORY MAPPABLE FILE Yr, dims, T = cm.load_memmap(fname_new) d1, d2 = dims images = np.reshape(Yr.T, [T] + list(dims), order='F') Y = np.reshape(Yr, dims + (T,), order='F') #%% play movie, press q to quit play_movie = False if play_movie: cm.movie(images).play(fr=50, magnification=3, gain=2.) #%% movie cannot be negative! if np.min(images) < 0: raise Exception('Movie too negative, add_to_movie should be larger') #%% correlation image. From here infer neuron size and density Cn = cm.movie(images)[:3000].local_correlations(swap_dim=False) #%% run seeded CNMF cnm = cnmf.CNMF(n_processes, method_init='greedy_roi', k=Ain.shape[1], gSig=gSig, merge_thresh=merge_thresh, p=p, dview=dview, Ain=Ain, method_deconvolution='oasis', rolling_sum=False, rf=None) cnm = cnm.fit(images)
# TODO : needinfo pl.imshow(new_templ, cmap='gray') pl.pause(.1) mc_list.append(mc) # we are going to keep this part because it helps the user understand what we need. # needhelp why it is not the same as in the notebooks ? # TODO: show screenshot 2,3 # %% # load motion corrected movie m_rig = cm.load(mc.fname_tot_rig) pl.imshow(mc.total_template_rig, cmap='gray') # %% visualize templates cm.movie(np.array(mc.templates_rig)).play( fr=5, gain=3, magnification=2, offset=offset_mov) # %% plot rigid shifts pl.close() 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=3, offset=offset_mov * .5, fr=100, magnification=1, bord_px=bord_px_rig) # %% restart cluster to clean up memory # TODO: todocument
pl.xlabel('rigid') pl.xlim([0, 1]) pl.ylim([0, 1]) #% pl.subplot(2, 2, 3) pl.title('rigid mean') pl.imshow(np.mean(mr, 0), cmap='gray', vmax=300) pl.axis('off') pl.subplot(2, 2, 4) pl.imshow(np.mean(mc, 0), cmap='gray', vmax=300) pl.title('pw-rigid mean') pl.axis('off') #%% mc = cm.movie(mc) mc[np.isnan(mc)] = 0 mc.resize(1, 1, .25).play(gain=10., fr=50) #%% ccimage = m.local_correlations(eight_neighbours=True, swap_dim=False) ccimage_rig = mr.local_correlations(eight_neighbours=True, swap_dim=False) ccimage_els = mc.local_correlations(eight_neighbours=True, swap_dim=False) #%% pl.subplot(3, 1, 1) pl.imshow(ccimage) pl.subplot(3, 1, 2) pl.imshow(ccimage_rig) pl.subplot(3, 1, 3) pl.imshow(ccimage_els) #%% pl.subplot(2, 1, 1)
max_size_neuro = max_radius**2*np.pi with np.load(gt_file, encoding = 'latin1') as ld: print(ld.keys()) d1_or = int(ld['d1']) d2_or = int(ld['d2']) dims_or = (d1_or,d2_or) A_gt = ld['A_gt'][()].toarray() C_gt = ld['C_gt'] Cn_orig = ld['Cn'] #locals().update(ld) #A_gt = scipy.sparse.coo_matrix(A_gt[()]) #dims = (d1,d2) if ds_factor > 1: A_gt = cm.movie(np.reshape(A_gt,dims_or+(-1,),order='F')).transpose(2,0,1).resize(1./ds_factor,1./ds_factor) if plot_on: pl.figure(); pl.imshow(A_gt.sum(0)) A_gt2 = np.array(np.reshape(A_gt,(A_gt.shape[0],-1),order='F')).T Cn_orig = cv2.resize(Cn_orig,None,fx=1./ds_factor,fy=1./ds_factor) else: A_gt2 = A_gt.copy() A_gt_thr = cm.source_extraction.cnmf.spatial.threshold_components(A_gt2, dims, medw=None, thr_method='max', maxthr=global_params['max_thr'], extract_cc=True, se=None, ss=None, dview=None) A_gt_thr_bin = A_gt_thr > 0 size_neurons_gt = A_gt_thr_bin.sum(0) idx_size_neurons_gt = np.where((size_neurons_gt>min_size_neuro) & (size_neurons_gt < max_size_neuro) )[0] print(A_gt_thr.shape) #%% filter for size found neurons
strides=strides, overlaps=overlaps, splits_els=splits_els, num_splits_to_process_els=num_splits_to_process_els, upsample_factor_grid=upsample_factor_grid, max_deviation_rigid=max_deviation_rigid, shifts_opencv=True, nonneg_movie=True) #%% mc.motion_correct_rigid(save_movie=True) #%% load motion corrected movie m_rig = cm.load(mc.fname_tot_rig) pl.imshow(mc.total_template_rig, cmap='gray') #%% visualize templates cm.movie(np.array(mc.templates_rig)).play(fr=20, gain=1, magnification=1, offset=offset_mov) #%% plot rigid shifts pl.close() pl.plot(mc.shifts_rig) pl.legend(['x shifts', 'y shifts']) pl.xlabel('frames') pl.ylabel('pixels') #%% inspect movie bord_px_rig = np.ceil(np.max(mc.shifts_rig)).astype(np.int) #%% downsample_ratio = .2 m_rig.resize(1, 1, downsample_ratio).play(gain=2, offset=offset_mov * .25, fr=30, magnification=1,
mags = ld['mags'] dircts_thresh = ld['dircts'] spatial_filter_ = ld['spatial_filter_'] mask_orig = ld['mask'] idd = 0 axlin = pl.subplot(n_components, 2, 2) for mag, dirct, spatial_filter in zip(mags, dircts_thresh, spatial_filter_): pl.subplot(n_components, 2, 1 + idd * 2) m = cm.load(os.path.join(os.path.split(e)[0], 'trial1.mat')) mask = mask_orig.astype(np.float32).copy() min_x, min_y = np.min(np.where(mask), 1) # max_x,max_y = np.max(np.where(mask),1) spfl = spatial_filter spfl = cm.movie(spfl[None, :, :]).resize( 1 / resize_fact, 1 / resize_fact, 1).squeeze() max_x, max_y = np.add((min_x, min_y), np.shape(spfl)) mask[min_x:max_x, min_y:max_y] = spfl mask[mask < np.nanpercentile(spfl, 70)] = np.nan # spfl = ld['spatial_filter'][idd] pl.imshow(m[0], cmap='gray') pl.imshow(mask, alpha=.5) pl.axis('off') axelin = pl.subplot(n_components, 2, 2 + idd * 2, sharex=axlin) pl.plot(mag, 'k') dirct[mag < 0.5 * np.std(mag)] = np.nan pl.plot(dirct, 'r-', linewidth=2) gt_pix_2 = scipy.signal.savgol_filter(
def run_equalizer(selected_rows, states_df, parameters, session_wise=False): ''' This function is meant to help with differences in contrast in different trials and session, to equalize general brightness or reduce photobleaching. It corrects the video and saves them in the corrected version. It can be run with the already aligned videos or trial by trial. for trial by trial, a template is required. params: selected_rows: pd.DataFrame -> A dataframe containing the analysis states you want to have equalized params: states_df: pd.DataFrame -> A dataframe containing all the analysis data base params: parameters: dict -> contains parameters concerning equalization returns : None ''' step_index = 4 # Sort the dataframe correctly df = selected_rows.sort_values(by=paths.multi_index_structure) # Determine the output path output_tif_file_path = os.environ[ 'DATA_DIR'] + f'data/interim/equalizer/main/' mouse, session, init_trial, *r = df.iloc[0].name #histogram_name = f'mouse_{mouse}_session_{session}_init_trial_{init_trial}' #output_steps_file_path = f'data/interim/equalizer/meta/figures/histograms/'+histogram_name 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 #creates an output dictionary for the data base output = { 'main': {}, 'meta': { 'analysis': { 'analyst': os.environ['ANALYST'], 'date': datetime.datetime.today().strftime("%m-%d-%Y"), 'time': datetime.datetime.today().strftime("%H:%M:%S") }, 'duration': {} } } if session_wise: row_local = df.iloc[0] input_tif_file_list = eval(row_local['alignment_output'])['main'] movie_original = cm.load( input_tif_file_list) # load video as 3d array already concatenated if parameters['make_template_from_trial'] == 0: movie_equalized = do_equalization(movie_original) else: movie_equalized = np.empty_like(movie_original) source = movie_original[0:100, :, :] # equalize all the videos loads in m_list_reshape with the histogram of source for j in range(int(movie_original.shape[0] / 100)): want_to_equalize = movie_original[j * 100:(j + 1) * 100, :, :] movie_equalized[j * 100:(j + 1) * 100, :, :] = do_equalization_from_template( reference=want_to_equalize, source=source) #Save the movie index = row_local.name new_index = db.replace_at_index1(index, 4 + 4, 2) ## version 2 is for session wise row_local.name = new_index equalized_path = movie_equalized.save( output_tif_file_path + db.create_file_name(4, row_local.name) + '.mmap', order='C') output['main'] = equalized_path #auxiliar = eval(row_local.loc['alignment_output']) #auxiliar.update({'equalizing_output' : output}) # row_local.loc['alignment_output'] = str(auxiliar) row_local.loc['equalization_output'] = output states_df = db.append_to_or_merge_with_states_df(states_df, row_local) else: # Get necessary parameters and create a list with the paths to the relevant files decoding_output_list = [] input_tif_file_list = [] trial_index_list = [] for idx, row in df.iterrows(): decoding_output = eval(row.loc['decoding_output']) decoding_output_list.append(decoding_output) input_tif_file_list.append(decoding_output['main']) trial_index_list.append(db.get_trial_name(idx[2], idx[3])) # this was something for ploting while testing, can be removed #colors = [] #for i in range(len(df)): # colors.append('#%06X' % randint(0, 0xFFFFFF)) #load the videos as np.array to be able to manipulate them m_list = [] legend = [] shape_list = [] h_step = parameters['histogram_step'] for i in range(len(input_tif_file_list)): im = io.imread(input_tif_file_list[i]) #load video as 3d array m_list.append(im) # and adds all the videos to a list shape_list.append( im.shape ) # list of sizes to cut the videos in time for making all of them having the same length #legend.append('trial = ' + f'{df.iloc[i].name[2]}') min_shape = min(shape_list) new_shape = (100 * int(min_shape[0] / 100), min_shape[1], min_shape[2] ) # new videos shape m_list_reshape = [] m_list_equalized = [] source = m_list[0][0:100, :, :] #equalize all the videos loaded in m_list_reshape with the histogram of source for i in range(len(input_tif_file_list)): video = m_list[i] if parameters['make_template_from_trial'] == 0: equalized_video = do_equalization(video) else: m_list_reshape.append(video[:new_shape[0], :, :]) equalized_video = np.empty_like(video[:new_shape[0], :, :]) for j in range(int(min_shape[0] / 100)): want_to_equalize = m_list_reshape[i][j * 100:(j + 1) * 100, :, :] equalized_video[j * 100:(j + 1) * 100, :, :] = do_equalization_from_template( reference=want_to_equalize, source=source) m_list_equalized.append(equalized_video) #convert the 3d np.array to a caiman movie and save it as a tif file, so it can be read by motion correction script. for i in range(len(input_tif_file_list)): # Save the movie row_local = df.iloc[i] movie_original = cm.movie(m_list_reshape[i]) movie_equalized = cm.movie(m_list_equalized[i]) # Write necessary variables to the trial index and row_local index = row_local.name new_index = db.replace_at_index1(index, 4 + 0, 1) ## version 1 is for trial wise row_local.name = new_index output['main'] = output_tif_file_path + db.create_file_name( 4, row_local.name) + '.tif' #auxiliar = eval(row_local.loc['decoding_output']) #auxiliar.update({'equalizing_output' : output}) #row_local.loc['decoding_output'] = str(auxiliar) row_local.loc['equalization_output'] = output movie_equalized.save(output_tif_file_path + db.create_file_name(4, row_local.name) + '.tif') states_df = db.append_to_or_merge_with_states_df( states_df, row_local) db.save_analysis_states_database(states_df, paths.analysis_states_database_path, paths.backup_path) return
# pl.xlim([0, dim_r[1]]) # pl.ylim([0, dim_r[0]]) pl.pause(.01) #%% final movie shifts size np.multiply(shape_grid, 2) movie_shifts_x = np.zeros(shape_grid + (T,)) movie_shifts_y = np.zeros(shape_grid + (T,)) for idx, r in enumerate(res_p): x1, x2 = np.unravel_index(idx, shape_grid) movie_shifts_x[x1, x2, :] = np.array(r[0][-1])[:, 0] #%% for idx, r in enumerate(res_p): mm = cm.movie(r[-1]) mm.play(fr=100, magnification=7, gain=2.) #%% #%% imgtot = np.zeros(np.prod(dim_r)) for idx, r in enumerate(res_p): # pl.subplot(2,5,idx+1) img = r[0][2] # lq,hq = np.percentile(img,[10,90]) # pl.imshow(img,cmap='gray',vmin=lq,vmax=hq) imgtot[r[1]] = np.maximum(img.T.flatten(), imgtot[r[1]]) m = cm.load('test_mmap_d1_49_d2_114_d3_1_order_C_frames_6764_.mmap') lq, hq = np.percentile(imgtot, [1, 97])
fn_parts = fn.split('_') d1 = int(fn_parts[fn_parts.index('d1') + 1]) # column, x d2 = int(fn_parts[fn_parts.index('d2') + 1]) # row, y d3 = int(fn_parts[fn_parts.index('d3') + 1]) # channel d4 = int(fn_parts[fn_parts.index('frames') + 1]) # frame, T order = fn_parts[fn_parts.index('order') + 1] mov = np.memmap(filename=os.path.join(data_folder, fn), shape=(d1, d2, d4), order=order, dtype=np.float32, mode='r') mov = mov.transpose((2, 1, 0)) print('movie shape: {}'.format(mov.shape)) f = plt.figure(figsize=(8, 5)) ax = f.add_subplot(111) fig = ax.imshow(np.mean(mov, axis=0), vmin=300, vmax=1500, cmap='inferno', interpolation='nearest') f.colorbar(fig) plt.show() input("Press enter to continue ...") print('playing {} ...'.format(fn)) cm.movie(mov).play(fr=30, magnification=1, gain=2.)
m_orig = np.array(images) patch_size = 50 half_crop = np.minimum(gSig[0] * 4 + 1, patch_size) // 2 idx_included = np.arange(A_gt.shape[-1]) # idx_included = idx_components_cnn[np.arange(50)] # neurons that are displayed all_pos_crops = [] all_neg_crops = [] all_dubious_crops = [] base_name = fname_new.split('/')[5] dims = (d1, d2) idx_included = np.array([x for x in idx_included if x is not None]) base_name = base_name + '_' + \ str(idx_included[0]) + '_' + str(idx_included[-1]) print(base_name) idx_excluded = np.setdiff1d(np.arange(A_gt.shape[-1]), idx_included) m_res = m_orig - cm.movie(np.reshape(A_gt.tocsc()[:, idx_excluded].dot( C_gt[idx_excluded]) + b_gt.dot(f_gt), dims + (-1,), order='F').transpose([2, 0, 1])) mean_proj = np.mean(m_res, 0) std_mov = mean_proj.std() min_mov = np.median(mean_proj) #%% # will use either the overfeat like (full FOV, train_net_cifar_edge_cutter_FOV.py) network or the single patch one (train_net_cifar_edge_cutter.py) full_fov = False count_start = 30 dims = np.array(dims) #bin_ = 10 cms_total = [np.array(scipy.ndimage.center_of_mass(np.reshape(a.toarray( ), dims, order='F'))).astype(np.int) for a in A_gt.tocsc()[:, idx_included].T] cms_total = np.maximum(cms_total, half_crop) cms_total = np.array([np.minimum(cms, dims - half_crop) for cms in cms_total]).astype(np.int) for count in range(count_start, T):
all_results = dict() ALL_CCs = [] for params_movie in np.array(params_movies)[ID]: # params_movie['gnb'] = 3 params_display = {'downsample_ratio': .2, 'thr_plot': 0.8} fname_new = os.path.join(base_folder, params_movie['fname']) print(fname_new) # %% LOAD MEMMAP FILE Yr, dims, T = cm.load_memmap(fname_new) d1, d2 = dims images = np.reshape(Yr.T, [T] + list(dims), order='F') # TODO: needinfo Y = np.reshape(Yr, dims + (T, ), order='F') m_images = cm.movie(images) if plot_on: if m_images.shape[0] < 5000: Cn = m_images.local_correlations( swap_dim=params_movie['swap_dim'], frames_per_chunk=1500) Cn[np.isnan(Cn)] = 0 else: Cn = np.array( cm.load(('/'.join(params_movie['gtname'].split('/')[:-2] + ['projections', 'correlation_image.tif']) ))).squeeze() check_nan = False # %% start cluster # TODO: show screenshot 10 try:
resize_fact_flow = 1 for mv in m_fluos: tmpl, correlations, flows_orig, norms, smoothness = compute_metrics_motion_correction( mv, final_size[0], final_size[1], winsize=winsize, play_flow=False, resize_fact_flow=resize_fact_flow) #% run comparisons resonant m_res = glob.glob('Sue*.tif') final_size = (512 - 24, 512 - 24) winsize = 100 resize_fact_flow = .2 for mv in m_res: tmpl, correlations, flows_orig, norms, smoothness = compute_metrics_motion_correction( mv, final_size[0], final_size[1], winsize=winsize, play_flow=False, resize_fact_flow=resize_fact_flow) #%% run comparisons SUITE2P for mvs in glob.glob('Sue*2000*16*.mat'): print(mvs) cm.movie(scipy.io.loadmat(mvs)['data'].transpose( [2, 0, 1])).save(mvs[:-3] + '.hdf5') #%% m_fluos = glob.glob('M_FLUO*.hdf5') final_size = (64 - 20, 128 - 20) winsize = 32 resize_fact_flow = 1 for mv in m_fluos: tmpl, correlations, flows_orig, norms, smoothness = compute_metrics_motion_correction( mv, final_size[0], final_size[1], winsize=winsize, play_flow=False, resize_fact_flow=resize_fact_flow) #% run comparisons resonant m_res = glob.glob('Sue_2000*16*.hdf5') final_size = (512 - 24, 512 - 24) winsize = 100 resize_fact_flow = .2 for mv in m_res: tmpl, correlations, flows_orig, norms, smoothness = compute_metrics_motion_correction(
cm.utils.visualization.view_patches_bar(Yr, cnm.A[:, idx_components], cnm.C[idx_components], cnm.b, cnm.f, dims[0], dims[1], YrA=cnm.YrA[idx_components], img=cn_filter) #%% cm.stop_server(dview=dview) #%% denoised movie cm.movie( np.reshape(cnm.A.tocsc()[:, idx_components].dot(cnm.C[idx_components]) + cnm.b.dot(cnm.f), dims + (-1, ), order='F').transpose(2, 0, 1)).play(magnification=3, gain=1.) #%% only neurons cm.movie( np.reshape(cnm.A.tocsc()[:, idx_components].dot(cnm.C[idx_components]), dims + (-1, ), order='F').transpose(2, 0, 1)).play(magnification=3, gain=10.) #%% only the background cm.movie( np.reshape(cnm.b.dot(cnm.f), dims + (-1, ), order='F').transpose(2, 0, 1)).play(magnification=3, gain=1.) #%% residuals cm.movie( np.array(Y) - np.reshape(cnm.A.tocsc()[:, :].dot(cnm.C[:]) + cnm.b.dot(cnm.f),
#%% VIEW TRACES (accepted and rejected) view_patches_bar(Yr, cnm2.A.tocsc()[:, idx_components], cnm2.C[idx_components], cnm2.b, cnm2.f, dims[0], dims[1], YrA=cnm2.YrA[idx_components], img=Cn) view_patches_bar(Yr, cnm2.A.tocsc()[:, idx_components_bad], cnm2.C[idx_components_bad], cnm2.b, cnm2.f, dims[0], dims[1], YrA=cnm2.YrA[idx_components_bad], img=Cn) #%% 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 cm.concatenate([m_els.resize(1, 1, downsample_ratio), denoised.resize(1, 1, downsample_ratio)], axis=2).play(fr=60, gain=15, magnification=2, offset=0) # press q to exit
bord_px_rig = np.ceil(np.max(mc.shifts_rig)).astype(np.int) # TODO : needinfo pl.imshow(new_templ, cmap='gray') pl.pause(.1) mc_list.append(mc) # we are going to keep this part because it helps the user understand what we need. # needhelp why it is not the same as in the notebooks ? # TODO: show screenshot 2,3 # %% # load motion corrected movie m_rig = cm.load(mc.fname_tot_rig) pl.imshow(mc.total_template_rig, cmap='gray') # %% visualize templates cm.movie(np.array(mc.templates_rig)).play( fr=10, gain=5, magnification=2, offset=offset_mov) # %% plot rigid shifts pl.close() 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) # %% # a computing intensive but parralellized part t1 = time.time()
#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.) #%% T,d1,d2 = np.shape(m) shape_mov = (d1*d2,m.shape[0]) Y = np.memmap('M_FLUO_4_d1_64_d2_128_d3_1_order_F_frames_4620_.mmap',mode = 'r',dtype=np.float32, shape=shape_mov, order='F') mc = cm.movie(np.reshape(Y,(d2,d1,T),order = 'F').transpose([2,1,0])) mc.resize(1,1,.25).play(gain=10.,fr=50) #%%