def get_centroids(masks): """ Calculate the centroids of each mask (calls caiman's plot_contours). :param np.array masks: Masks (image_height x image_width x num_components) :returns: Centroids (num_components x 2) in y, x pixels of each component. """ # Reshape masks image_height, image_width, num_components = masks.shape masks = masks.reshape(-1, num_components, order='F') # Get centroids fake_background = np.empty([image_height, image_width]) # needed for plot contours coordinates = visualization.plot_contours(masks, fake_background) import matplotlib.pyplot as plt; plt.close() centroids = np.array([coordinate['CoM'] for coordinate in coordinates]) return centroids
#idx_blobs = np.intersect1d(idx_components, idx_blobs) idx_components_bad = np.setdiff1d(list(range(len(r_values))), idx_components) print(' ***** ') print((len(r_values))) print((len(idx_components))) #%% try: A_off = A_off.toarray()[:, idx_components] except: A_off = A_off[:, idx_components] C_off = C_off[idx_components] # OASISinstances = OASISinstances[()] #%% pl.figure() crd = plot_contours(scipy.sparse.coo_matrix(A_off), Cn, thr=0.9) #%% view_patches_bar(None, scipy.sparse.coo_matrix( A_off[:, :]), C_off[:, :], b_off, f_off, dims_off[0], dims_off[1], YrA=YrA[:, :], img=Cn) #%% A_off_thr = cm.source_extraction.cnmf.spatial.threshold_components(A_off[:, :], dims_off, medw=None, thr_method='max', maxthr=0.2, nrgthr=0.99, extract_cc=True, se=None, ss=None, dview=dview) A_off_thr = A_off_thr > 0 size_neurons = A_off_thr.sum(0) A_off_thr = A_off_thr[:, (size_neurons > min_size_neuro) & (size_neurons < max_size_neuro)] C_off_thr = C_off[(size_neurons > min_size_neuro) & (size_neurons < max_size_neuro), :116000]
cnm = cnm.fit(images) A_tot = cnm.A C_tot = cnm.C YrA_tot = cnm.YrA b_tot = cnm.b f_tot = cnm.f sn_tot = cnm.sn t2 = time.time() - t1 print(('Number of components:' + str(A_tot.shape[-1]))) #%% np.savez(os.path.join(os.path.split(fname_new)[0], os.path.split(fname_new)[ 1][:-4] + 'results_analysis_ZEBRA_patch.npz'), Cn=Cn, A=A_tot, C=C_tot, b=b_tot, f=f_tot, YrA=YrA_tot, sn=sn_tot, d1=d1, d2=d2) #%% pl.figure() crd = plot_contours(A_tot, Cn, thr=0.9) #%% DISCARD LOW QUALITY COMPONENT t1 = time.time() final_frate = params_movie['final_frate'] r_values_min = .5 # threshold on space consistency fitness_min = -10 # threshold on time variability # threshold on time variability (if nonsparse activity) fitness_delta_min = -10 Npeaks = 10 traces = C_tot + YrA_tot idx_components, idx_components_bad, fitness_raw, fitness_delta, r_values = cm.components_evaluation.estimate_components_quality( traces, Y, A_tot, C_tot, b_tot, f_tot, final_frate=final_frate, Npeaks=Npeaks, r_values_min=r_values_min, fitness_min=fitness_min, fitness_delta_min=fitness_delta_min, return_all=True) #idx_components = np.where(r_values>=r_values_min)[0] #idx_components_bad = np.where(r_values < r_values_min)[0] t2 = time.time() - t1
cnm = cnmf.CNMF(n_processes, k=K, gSig=gSig, merge_thresh=0.8, p=0, dview=dview, Ain=None, rf=rf, stride=stride, memory_fact=memory_fact, method_init=init_method, alpha_snmf=alpha_snmf, only_init_patch=True, gnb=1, method_deconvolution='oasis') cnm = cnm.fit(images) A_tot = cnm.A C_tot = cnm.C YrA_tot = cnm.YrA b_tot = cnm.b f_tot = cnm.f sn_tot = cnm.sn print(('Number of components:' + str(A_tot.shape[-1]))) #%% pl.figure() crd = plot_contours(A_tot, Cn, thr=0.9) #%% final_frate = 16 # approx final rate (after eventual downsampling ) Npeaks = 10 traces = C_tot + YrA_tot # traces_a=traces-scipy.ndimage.percentile_filter(traces,8,size=[1,np.shape(traces)[-1]/5]) # traces_b=np.diff(traces,axis=1) fitness_raw, fitness_delta, erfc_raw, erfc_delta, r_values, significant_samples = evaluate_components( Y, traces, A_tot, C_tot, b_tot, f_tot, final_frate, remove_baseline=True, N=5, robust_std=False, Athresh=0.1, Npeaks=Npeaks, thresh_C=0.3) idx_components_r = np.where(r_values >= .5)[0] idx_components_raw = np.where(fitness_raw < -40)[0] idx_components_delta = np.where(fitness_delta < -20)[0] idx_components = np.union1d(idx_components_r, idx_components_raw) idx_components = np.union1d(idx_components, idx_components_delta)
p_tsub=2, block_size=block_size, check_nan=False) cnm = cnm.fit(images) A_tot = cnm.A C_tot = cnm.C YrA_tot = cnm.YrA b_tot = cnm.b f_tot = cnm.f sn_tot = cnm.sn t_patch_cnmf = time.time() - t1 print(('Number of components:' + str(A_tot.shape[-1]))) #%% pl.figure() crd = plot_contours(A_tot, Cn, thr=0.9) #%% DISCARD LOW QUALITY COMPONENT t1 = time.time() final_frate = params_movie['final_frate'] r_values_min = .6 # threshold on space consistency fitness_min = -30 # threshold on time variability # threshold on time variability (if nonsparse activity) fitness_delta_min = -30 Npeaks = 10 traces = C_tot + YrA_tot idx_components, idx_components_bad = cm.components_evaluation.estimate_components_quality( traces, Y, A_tot, C_tot, b_tot,
border_pix=border_pix, low_rank_background=params_movie['low_rank_background']) cnm = cnm.fit(images) A_tot = cnm.A C_tot = cnm.C YrA_tot = cnm.YrA b_tot = cnm.b f_tot = cnm.f sn_tot = cnm.sn print(('Number of components:' + str(A_tot.shape[-1]))) # %% pl.figure() # TODO: show screenshot 12` # TODO : change the way it is used crd = plot_contours(A_tot, Cn, thr=params_display['thr_plot']) # %% DISCARD LOW QUALITY COMPONENT final_frate = params_movie['final_frate'] r_values_min = params_movie[ 'r_values_min_patch'] # threshold on space consistency fitness_min = params_movie[ 'fitness_delta_min_patch'] # threshold on time variability # threshold on time variability (if nonsparse activity) fitness_delta_min = params_movie['fitness_delta_min_patch'] Npeaks = params_movie['Npeaks'] traces = C_tot + YrA_tot # TODO: todocument idx_components, idx_components_bad = estimate_components_quality( traces, Y, A_tot,
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
'only_init_patch'], gnb=params_movie['gnb'], method_deconvolution='oasis', border_pix=params_movie['crop_pix'], low_rank_background=params_movie['low_rank_background']) cnm = cnm.fit(images) A_tot = cnm.A C_tot = cnm.C YrA_tot = cnm.YrA b_tot = cnm.b f_tot = cnm.f sn_tot = cnm.sn print(('Number of components:' + str(A_tot.shape[-1]))) # %% pl.figure() # TODO: show screenshot 12` # TODO : change the way it is used crd = plot_contours(A_tot, Cn, thr=params_display['thr_plot']) # %% DISCARD LOW QUALITY COMPONENT final_frate = params_movie['final_frate'] # threshold on space consistency r_values_min = params_movie['r_values_min_patch'] # threshold on time variability fitness_min = params_movie['fitness_delta_min_patch'] # threshold on time variability (if nonsparse activity) fitness_delta_min = params_movie['fitness_delta_min_patch'] Npeaks = params_movie['Npeaks'] traces = C_tot + YrA_tot # TODO: todocument idx_components, idx_components_bad = estimate_components_quality( traces, Y, A_tot, C_tot, b_tot, f_tot, final_frate=final_frate, Npeaks=Npeaks, r_values_min=r_values_min, fitness_min=fitness_min, fitness_delta_min=fitness_delta_min) print(('Keeping ' + str(len(idx_components)) +
plt.imshow(Cn,vmax = np.percentile(Cn,95)) #%% 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, nb_patch=2, low_rank_background=False) cnm = cnm.fit(images) #%% plot contours of found components plt.figure() crd = plot_contours(cnm.A, Cn, thr=0.9) plt.title('Contour plots of found components') #%% RE-RUN seeded CNMF on accepted patches to refine and perform deconvolution A_in, C_in, b_in, f_in = cnm.A[:, :], cnm.C[:], 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, low_rank_background=False) cnm2 = cnm2.fit(images) #%% COMPONENT EVALUATION # the components are evaluated in three ways: # a) the shape of each component must be correlated with the data
#roi_cons = np.concatenate([roi_cons, caiman.base.rois.nf_read_roi_zip('/mnt/ceph/neuro/labeling/neurofinder.03.00.test/regions/intermediate_regions/ben_active_regions_nd_sonia_active_regions_nd__lindsey_active_regions_nd_1_mismatches.zip',dims)],0) print(roi_cons.shape) pl.imshow(roi_cons.sum(0)) if params_movie['kernel'] is not None: # kernel usually two kernel = np.ones( (radius // params_movie['kernel'], radius // params_movie['kernel']), np.uint8) roi_cons = np.vstack([cv2.dilate(rr, kernel, iterations=1)[ np.newaxis, :, :] > 0 for rr in roi_cons]) * 1. pl.imshow(roi_cons.sum(0), alpha=0.5) A_in = np.reshape(roi_cons.transpose( [2, 1, 0]), (-1, roi_cons.shape[0]), order='C') pl.figure() crd = plot_contours(A_in, Cn, thr=.99999) # %% 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'] # merging threshold, max correlation allowed merge_thresh = params_movie['merge_thresh'] # %% Extract spatial and temporal components on patches # TODO: todocument if images.shape[0] > 10000: check_nan = False else: check_nan = True cnm = cnmf.CNMF(check_nan=check_nan, n_processes=1, k=A_in.shape[-1], gSig=[radius, radius], merge_thresh=params_movie['merge_thresh'], p=params_movie['p'], Ain=A_in.astype(np.bool),
#%% 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, SNR_comp_delta, 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_lowest = cnn_thr)
raise Exception('Movie contains nan! You did not remove enough borders') #%% Cn = cm.local_correlations(Y[:, :, :1000]) pl.imshow(Cn, cmap='gray') #%% if not is_patches: #%% K = 35 # number of neurons expected per patch gSig = [7, 7] # expected half size of neurons merge_thresh = 0.8 # merging threshold, max correlation allowed p = 2 # order of the autoregressive system cnm = cnmf.CNMF(n_processes, method_init=init_method, k=K, gSig=gSig, merge_thresh=merge_thresh, p=p, dview=dview, Ain=None, method_deconvolution='oasis', skip_refinement=False) cnm = cnm.fit(images) crd = plot_contours(cnm.A, Cn, thr=0.9) #%% else: #%% rf = 100 # half-size of the patches in pixels. rf=25, patches are 50x50 stride = 10 # amounpl.it of overlap between the patches in pixels K = None # number of neurons expected per patch gSig = None # expected half size of neurons merge_thresh = 0.8 # merging threshold, max correlation allowed p = 1 # order of the autoregressive system save_results = False #%% RUN ALGORITHM ON PATCHES options_local_NMF = { 'NumCent': 400, # Define CNMF parameters 'mbs': [10], # temporal downsampling of data in intial phase of NMF
cm.stop_server(dview=dview) #%% cross-session alignment groundtruth export Anna from caiman.utils import visualization paths = [r'20191122a',r'20191125',r'20191126b', '20191127a',r'20191204',r'20191205',r'20191206', r'20191207',r'20191208',r'20191219'] target = r'W:\Neurophysiology-Storage1\Wahl\Hendrik\PhD\Data\Batch2\batch_analysis\alignment_session_data' files = os.listdir(target) for i, sess in enumerate(paths): pcf = pipe.load_pcf(r'E:\Batch2\M19\{}\N2'.format(sess)) data = np.load(os.path.join(target, files[i+1]), allow_pickle=True) del data['contours'] out = visualization.plot_contours(pcf.cnmf.estimates.A, pcf.cnmf.estimates.Cn, display_numbers=False, colors='r', verbose=False) plt.close() data['template'] = pcf.cnmf.estimates.Cn data['CoM'] = np.vstack([x['CoM'] for x in out]) np.save(os.path.join(target, os.path.splitext(files[i+1])[0]), data, allow_pickle=True) #%% Dimensionality reduction target = r'W:\Neurophysiology-Storage1\Wahl\Hendrik\PhD\Data\Batch3\batch_processing\PCA' mice = [r'W:\Neurophysiology-Storage1\Wahl\Hendrik\PhD\Data\Batch3\M32', r'W:\Neurophysiology-Storage1\Wahl\Hendrik\PhD\Data\Batch3\M33', r'W:\Neurophysiology-Storage1\Wahl\Hendrik\PhD\Data\Batch3\M38', r'W:\Neurophysiology-Storage1\Wahl\Hendrik\PhD\Data\Batch3\M39', r'W:\Neurophysiology-Storage1\Wahl\Hendrik\PhD\Data\Batch3\M40',
dummy = np.ones((512, 512)) dummy[0, 0] = 0 fig, ax = plt.subplots(1, 3) ax[0].imshow(pcf_objects[0].cnmf.estimates.Cn, cmap='gray') ax[1].text(0.5, 0.5, 'No Matches test', ha='center', va='center', transform=ax[1].transAxes) ax[1].imshow(dummy, cmap='gray') ax[2].imshow(pcf_objects[2].cnmf.estimates.Cn, cmap='gray') plt.sca(ax[0, 1]) visualization.plot_contours(spatial[1], templates[1]) plt.sca(ax[0, 2]) visualization.plot_contours(spatial[2], templates[2]) plt.sca(ax[1, 0]) visualization.plot_contours(spatial[3], templates[3]) plt.sca(ax[1, 1]) visualization.plot_contours(spatial[4], templates[4]) plt.sca(ax[1, 2]) visualization.plot_contours(spatial[5], templates[5]) fig.tight_layout() fig.show() mean = np.mean((templates[0], templates[1])) plt.figure() visualization.plot_contours(spatial_union, templates[0])
def main(): pass # For compatibility between running under Spyder and the CLI #%% load data fname = os.path.join(caiman_datadir(), 'example_movies', 'demoMovie.tif') Y = cm.load(fname).astype(np.float32) # # used as a background image Cn = cm.local_correlations(Y.transpose(1, 2, 0)) #%% set up some parameters # frame rate (Hz) fr = 10 # approximate length of transient event in seconds decay_time = 0.5 # expected half size of neurons gSig = [6, 6] # order of AR indicator dynamics p = 1 # minimum SNR for accepting new components min_SNR = 3.5 # correlation threshold for new component inclusion rval_thr = 0.90 # number of background components gnb = 3 # set up some additional supporting parameters needed for the algorithm (these are default values but change according to dataset characteristics) # number of shapes to be updated each time (put this to a finite small value to increase speed) max_comp_update_shape = np.inf # maximum number of expected components used for memory pre-allocation (exaggerate here) expected_comps = 50 # number of timesteps to consider when testing new neuron candidates N_samples = np.ceil(fr * decay_time) # exceptionality threshold thresh_fitness_raw = log_ndtr(-min_SNR) * N_samples # total length of file T1 = Y.shape[0] # set up CNMF initialization parameters # merging threshold, max correlation allowed merge_thresh = 0.8 # number of frames for initialization (presumably from the first file) initbatch = 400 # size of patch patch_size = 32 # amount of overlap between patches stride = 3 # max number of components in each patch K = 4 #%% obtain initial batch file used for initialization # memory map file (not needed) fname_new = Y[:initbatch].save(os.path.join(caiman_datadir(), 'example_movies', 'demo.mmap'), order='C') Yr, dims, T = cm.load_memmap(fname_new) images = np.reshape(Yr.T, [T] + list(dims), order='F') Cn_init = cm.local_correlations(np.reshape(Yr, dims + (T, ), order='F')) #%% RUN (offline) CNMF algorithm on the initial batch pl.close('all') cnm_init = cnmf.CNMF(2, k=K, gSig=gSig, merge_thresh=merge_thresh, fr=fr, p=p, rf=patch_size // 2, stride=stride, skip_refinement=False, normalize_init=False, options_local_NMF=None, minibatch_shape=100, minibatch_suff_stat=5, update_num_comps=True, rval_thr=rval_thr, thresh_fitness_delta=-50, gnb=gnb, decay_time=decay_time, thresh_fitness_raw=thresh_fitness_raw, batch_update_suff_stat=False, max_comp_update_shape=max_comp_update_shape, expected_comps=expected_comps, dview=None, min_SNR=min_SNR) cnm_init = cnm_init.fit(images) print(('Number of components:' + str(cnm_init.estimates.A.shape[-1]))) pl.figure() crd = plot_contours(cnm_init.estimates.A.tocsc(), Cn_init, thr=0.9) #%% run (online) OnACID algorithm cnm = deepcopy(cnm_init) cnm.params.data['dims'] = (60, 80) cnm._prepare_object(np.asarray(Yr), T1) t = initbatch Y_ = cm.load(fname)[initbatch:].astype(np.float32) for frame_count, frame in enumerate(Y_): cnm.fit_next(t, frame.copy().reshape(-1, order='F')) t += 1 #%% extract the results C, f = cnm.estimates.C_on[gnb:cnm.M], cnm.estimates.C_on[:gnb] A, b = cnm.estimates.Ab[:, gnb:cnm.M], cnm.estimates.Ab[:, :gnb] print(('Number of components:' + str(A.shape[-1]))) #%% pass through the CNN classifier with a low threshold (keeps clearer neuron shapes and excludes processes) use_CNN = True if use_CNN: # threshold for CNN classifier thresh_cnn = 0.1 from caiman.components_evaluation import evaluate_components_CNN predictions, final_crops = evaluate_components_CNN( A, dims, gSig, model_name=os.path.join(caiman_datadir(), 'model', 'cnn_model')) A_exclude, C_exclude = A[:, predictions[:, 1] < thresh_cnn], C[ predictions[:, 1] < thresh_cnn] A, C = A[:, predictions[:, 1] >= thresh_cnn], C[predictions[:, 1] >= thresh_cnn] noisyC = cnm.estimates.noisyC[gnb:cnm.M] YrA = noisyC[predictions[:, 1] >= thresh_cnn] - C else: YrA = cnm.estimates.noisyC[gnb:cnm.M] - C #%% plot results pl.figure() crd = cm.utils.visualization.plot_contours(A, Cn, thr=0.9) view_patches_bar(Yr, A, C, b, f, dims[0], dims[1], YrA, img=Cn)
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=border_to_0) 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_lowest=cnn_thr)
evaluate_components(Y, traces, A_tot, C_tot, b_tot, f_tot, final_frate, remove_baseline=True, N=5, robust_std=False, Athresh=0.1, Npeaks=Npeaks, thresh_C=0.3) idx_components_r = np.where(r_values >= .5)[0] idx_components_raw = np.where(fitness_raw < -20)[0] idx_components_delta = np.where(fitness_delta < -20)[0] idx_components = np.union1d(idx_components_r, idx_components_raw) idx_components = np.union1d(idx_components, idx_components_delta) idx_components_bad = np.setdiff1d(list(range(len(traces))), idx_components) print(('Keeping ' + str(len(idx_components)) + ' and discarding ' + str(len(idx_components_bad)))) #%% # pl.figure() crd = plot_contours(A_tot.tocsc()[:, idx_components], Cn2, thr=0.9) #%% view_patches_bar(Yr, scipy.sparse.coo_matrix(A_tot.tocsc()[:, idx_components_bad]), C_tot[ idx_components_bad, :], b_tot, f_tot, dims[0], dims[1], YrA=YrA_tot[idx_components_bad, :], img=Cn2) #%% A_tot = A_tot.tocsc()[:, idx_components] C_tot = C_tot[idx_components] #%% #%% # cnm2 = cnmf.CNMF(n_processes, k=A_tot.shape, gSig=gSig, merge_thresh=merge_thresh, p=p, dview=dview, Ain=A_tot, Cin=C_tot, # f_in=f_tot, rf=None, stride=None, method_deconvolution='oasis') cnm_refine = cnmf.CNMF(n_processes, method_init='greedy_roi', k=A_tot.shape, gSig=gSig, merge_thresh=merge_thresh, rf=None, stride=None, p=p, dview=dview, Ain=A_tot, Cin=C_tot, f_in=f_tot, method_deconvolution='oasis', skip_refinement=True, normalize_init=False, options_local_NMF=None,
evaluate_components(Y, traces, A_tot, C_tot, b_tot, f_tot, final_frate, remove_baseline=True, N=5, robust_std=False, Athresh=0.1, Npeaks=Npeaks, thresh_C=0.3) idx_components_r = np.where(r_values >= .5)[0] idx_components_raw = np.where(fitness_raw < -20)[0] idx_components_delta = np.where(fitness_delta < -20)[0] idx_components = np.union1d(idx_components_r, idx_components_raw) idx_components = np.union1d(idx_components, idx_components_delta) idx_components_bad = np.setdiff1d(list(range(len(traces))), idx_components) print(('Keeping ' + str(len(idx_components)) + ' and discarding ' + str(len(idx_components_bad)))) #%% # pl.figure() crd = plot_contours(A_tot.tocsc()[:, idx_components], Cn2, thr=0.9) #%% view_patches_bar(Yr, scipy.sparse.coo_matrix(A_tot.tocsc()[:, idx_components_bad]), C_tot[idx_components_bad, :], b_tot, f_tot, dims[0], dims[1], YrA=YrA_tot[idx_components_bad, :], img=Cn2) #%% A_tot = A_tot.tocsc()[:, idx_components] C_tot = C_tot[idx_components] #%%
border_pix=params_movie['crop_pix'], low_rank_background=params_movie['low_rank_background']) cnm = cnm.fit(images) A_tot = cnm.A C_tot = cnm.C YrA_tot = cnm.YrA b_tot = cnm.b f_tot = cnm.f sn_tot = cnm.sn print(('Number of components:' + str(A_tot.shape[-1]))) # %% pl.figure() # TODO: show screenshot 12` # TODO : change the way it is used crd = plot_contours(A_tot, Cn, thr=params_display['thr_plot']) # %% DISCARD LOW QUALITY COMPONENT final_frate = params_movie['final_frate'] # threshold on space consistency r_values_min = params_movie['r_values_min_patch'] # threshold on time variability fitness_min = params_movie['fitness_delta_min_patch'] # threshold on time variability (if nonsparse activity) fitness_delta_min = params_movie['fitness_delta_min_patch'] Npeaks = params_movie['Npeaks'] traces = C_tot + YrA_tot # TODO: todocument idx_components, idx_components_bad = estimate_components_quality( traces, Y, A_tot,
bnd_Y = np.percentile(Y,(0.001,100-0.001)) # plotting boundaries for Y #%% initialize OnACID with bare initialization cnm_init = bare_initialization(Y[:initbatch].transpose(1, 2, 0), init_batch=initbatch, k=K, gnb=gnb, gSig=gSig, p=p, minibatch_shape=100, minibatch_suff_stat=5, update_num_comps=True, rval_thr=rval_thr, thresh_fitness_raw=thresh_fitness_raw, batch_update_suff_stat=True, max_comp_update_shape=max_comp_update_shape, deconv_flag=False, use_dense=False, simultaneously=False, n_refit=0) #%% Plot initialization results crd = plot_contours(cnm_init.A.tocsc(), Cn_init, thr=0.9) A, C, b, f, YrA, sn = cnm_init.A, cnm_init.C, cnm_init.b, cnm_init.f, cnm_init.YrA, cnm_init.sn view_patches_bar(Yr, scipy.sparse.coo_matrix( A.tocsc()[:, :]), C[:, :], b, f, dims[0], dims[1], YrA=YrA[:, :], img=Cn_init) bnd_AC = np.percentile(A.dot(C),(0.001,100-0.005)) bnd_BG = np.percentile(b.dot(f),(0.001,100-0.001)) #%% create a function for plotting results in real time if needed def create_frame(cnm2, img_norm, captions): A, b = cnm2.Ab[:, cnm2.gnb:], cnm2.Ab[:, :cnm2.gnb].toarray() C, f = cnm2.C_on[cnm2.gnb:cnm2.M, :], cnm2.C_on[:cnm2.gnb, :] # inferred activity due to components (no background) frame_plot = (frame_cor.copy() - bnd_Y[0])/np.diff(bnd_Y) comps_frame = A.dot(C[:, t - 1]).reshape(cnm2.dims, order='F')
thresh_fitness_raw_online=-50, rval_thr_init=.5, thresh_fitness_delta_init=-20, thresh_fitness_raw_init=-20, rval_thr_refine=0.995, thresh_fitness_delta_refine=-200, thresh_fitness_raw_refine=-200, final_frate=2, Npeaks=10, single_thread=False, dview=dview) cnms.append(cnm) Cns.append(Cn2) base_names.append(fname_new) A, C, b, f, YrA, sn = cnm.A, cnm.C, cnm.b, cnm.f, cnm.YrA, cnm.sn crd = plot_contours(scipy.sparse.coo_matrix(A), Cns[-1], thr=0.9) pl.pause(1) cm.stop_server() log_files = glob.glob('Yr*_LOG_*') for log_file in log_files: os.remove(log_file) #%% vompute all the Cns Cns_all = [] templates_all = [] shifts_all = [] corrs_all = [] for (x0, x1, y0, y1, _) in sliding_window_new(m[0], overlaps, strides): print([x0, y0]) mc, shifts, template, corrs = m[:, x0:x1, y0:y1].copy().motion_correct( max_shifts[0], max_shifts[1])
K = 35 # number of neurons expected per patch gSig = [7, 7] # expected half size of neurons merge_thresh = 0.8 # merging threshold, max correlation allowed p = 2 # order of the autoregressive system cnm = cnmf.CNMF(n_processes, method_init=init_method, k=K, gSig=gSig, merge_thresh=merge_thresh, p=p, dview=dview, Ain=None, method_deconvolution='oasis', skip_refinement=False) cnm = cnm.fit(images) crd = plot_contours(cnm.A, Cn, thr=0.9) #%% else: #%% rf = 100 # half-size of the patches in pixels. rf=25, patches are 50x50 stride = 10 # amounpl.it of overlap between the patches in pixels K = None # number of neurons expected per patch gSig = None # expected half size of neurons merge_thresh = 0.8 # merging threshold, max correlation allowed p = 1 # order of the autoregressive system save_results = False #%% RUN ALGORITHM ON PATCHES options_local_NMF = { 'NumCent': 400, # Define CNMF parameters 'mbs': [10], # temporal downsampling of data in intial phase of NMF
A_tot = cnm.A C_tot = cnm.C YrA_tot = cnm.YrA b_tot = cnm.b f_tot = cnm.f sn_tot = cnm.sn print(('Number of components:' + str(A_tot.shape[-1]))) print(time.time() - t1) t_c = time.time() - t1 # %% if isscreen: pl.figure() # TODO: show screenshot 12` # TODO : change the way it is used crd = plot_contours(A_tot, Cn, thr=params_display['thr_plot']) # %% DISCARD LOW QUALITY COMPONENT t1 = time.time() final_frate = params_movie['final_frate'] # threshold on space consistency r_values_min = params_movie['r_values_min_patch'] # threshold on time variability fitness_min = params_movie['fitness_delta_min_patch'] # threshold on time variability (if nonsparse activity) fitness_delta_min = params_movie['fitness_delta_min_patch'] Npeaks = params_movie['Npeaks'] traces = C_tot + YrA_tot # TODO: todocument idx_components, idx_components_bad = estimate_components_quality( traces, Y,
A_thr_online[:, :].reshape([dims[0], dims[1], -1], order='F').transpose( [2, 0, 1]) * 1., thresh_cost=.8, min_dist=10, print_assignment=False, plot_results=False, Cn=None, labels=['GT', 'Offline']) # %% pl.figure('Figure 4a top') pl.subplot(2, 2, 1) a1 = plot_contours(A.tocsc()[:, tp_comp], Cn, thr=0.9, colors='yellow', vmax=0.75, display_numbers=False, cmap='gray') a2 = plot_contours(A_gt.tocsc()[:, tp_gt], Cn, thr=0.9, vmax=0.85, colors='r', display_numbers=False, cmap='gray') pl.subplot(2, 2, 2) a3 = plot_contours(A.tocsc()[:, fp_comp], Cn, thr=0.9, colors='yellow',
t1 = time() Yr,sn,g,psx = cm.source_extraction.cnmf.pre_processing.preprocess_data(Yr,dview=dview,**options['preprocess_params']) print((time() - t1)) #%% t1 = time() Atmp, Ctmp, b_in, f_in, center=cm.source_extraction.cnmf.initialization.initialize_components(Y, normalize=True, **options['init_params']) print((time() - t1)) #%% Refine manually component by clicking on neurons refine_components=False if refine_components: Ain,Cin = cm.source_extraction.cnmf.utilities.manually_refine_components(Y,options['init_params']['gSig'],coo_matrix(Atmp),Ctmp,Cn,thr=0.9) else: Ain,Cin = Atmp, Ctmp #%% plot estimated component pl.figure() crd = plot_contours(coo_matrix(Ain),Cn) pl.show() #%% UPDATE SPATIAL COMPONENTS #pl.close() t1 = time() A,b,Cin = cm.source_extraction.cnmf.spatial.update_spatial_components(Yr, Cin, f_in, Ain, sn=sn, dview=dview,**options['spatial_params']) t_elSPATIAL = time() - t1 pl.figure() crd = plot_contours(A,Cn) #%% update_temporal_components #pl.close() t1 = time() options['temporal_params']['p'] = 0 # set this to zero for fast updating without deconvolution C,f,S,bl,c1,neurons_sn,g,YrA = cm.source_extraction.cnmf.temporal.update_temporal_components(Yr,A,b,Cin,f_in,dview=dview,bl=None,c1=None,sn=None,g=None,**options['temporal_params']) t_elTEMPORAL = time() - t1
cnm = cnm.fit(images) A_tot = cnm.A C_tot = cnm.C YrA_tot = cnm.YrA b_tot = cnm.b f_tot = cnm.f sn_tot = cnm.sn comp.comparison['cnmf_on_patch']['timer'] = time.time() - t1 comp.comparison['cnmf_on_patch']['ourdata'] = [cnm.A.copy(), cnm.C.copy()] print(('Number of components:' + str(A_tot.shape[-1]))) # %% # pl.figure() # TODO: show screenshot 12` # TODO : change the way it is used crd = plot_contours(A_tot, Cn, thr=params_display['thr_plot']) # %% DISCARD LOW QUALITY COMPONENT final_frate = params_movie['final_frate'] # threshold on space consistency r_values_min = params_movie['r_values_min_patch'] # threshold on time variability fitness_min = params_movie['fitness_delta_min_patch'] # threshold on time variability (if nonsparse activity) fitness_delta_min = params_movie['fitness_delta_min_patch'] Npeaks = params_movie['Npeaks'] traces = C_tot + YrA_tot # TODO: todocument idx_components, idx_components_bad = estimate_components_quality( traces, Y, A_tot, C_tot, b_tot, f_tot, final_frate=final_frate, Npeaks=Npeaks, r_values_min=r_values_min, fitness_min=fitness_min, fitness_delta_min=fitness_delta_min) print(('Keeping ' + str(len(idx_components)) +
pl.imshow(roi_cons.sum(0)) if params_movie['kernel'] is not None: # kernel usually two kernel = np.ones( (radius // params_movie['kernel'], radius // params_movie['kernel']), np.uint8) roi_cons = np.vstack([ cv2.dilate(rr, kernel, iterations=1)[np.newaxis, :, :] > 0 for rr in roi_cons ]) * 1. pl.imshow(roi_cons.sum(0), alpha=0.5) A_in = np.reshape(roi_cons.transpose([2, 1, 0]), (-1, roi_cons.shape[0]), order='C') pl.figure() crd = plot_contours(A_in, Cn, thr=.99999) # %% 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'] # merging threshold, max correlation allowed merge_thresh = params_movie['merge_thresh'] # %% Extract spatial and temporal components on patches # TODO: todocument if images.shape[0] > 10000: check_nan = False else: check_nan = True cnm = cnmf.CNMF(check_nan=check_nan,
A=scipy.sparse.coo_matrix(A) Yr, dims, T = cm.load_memmap(os.path.join(folder,file_mmap)) d1, d2 = dims images = np.reshape(Yr.T, [T] + list(dims), order='F') Y = np.reshape(Yr, dims + (T,), order='F') gSig = [8, 8] # expected half size of neurons final_frate=1 with np.load(os.path.join(folder,'results_blobs.npz')) as ld: print((list(ld.keys()))) locals().update(ld) masks_cnmf=masks[idx_blob] #%% crd = plot_contours(A.tocsc()[:, idx_components], Cn, thr=0.9) #%% masks_ben=cm.base.rois.nf_read_roi_zip(ben_zip,dims) if '.mat' in princeton_zip: aa = scipy.io.loadmat(princeton_zip) try: masks_princeton = aa['allROIs'].transpose([2,0,1])*1. except: masks_princeton = aa['M'].transpose([2,0,1])*1. else: masks_princeton=cm.base.rois.nf_read_roi_zip(princeton_zip,dims)*1. #masks_ben_ci=cm.base.rois.nf_read_roi_zip(os.path.join(folder,ben_zip_ci),dims) ##%% #pl.imshow(masks_ben.sum(0)) #pl.imshow(masks_cnmf.sum(0),cmap='hot',alpha=.3)
def main(): pass # For compatibility between running under Spyder and the CLI #%% download and list all files to be processed # folder inside ./example_movies where files will be saved fld_name = 'Mesoscope' download_demo('Tolias_mesoscope_1.hdf5', fld_name) download_demo('Tolias_mesoscope_2.hdf5', fld_name) download_demo('Tolias_mesoscope_3.hdf5', fld_name) # folder where files are located folder_name = os.path.join(caiman_datadir(), 'example_movies', fld_name) extension = 'hdf5' # extension of files # read all files to be processed fls = glob.glob(folder_name + '/*' + extension) # your list of files should look something like this print(fls) #%% Set up some parameters # frame rate (Hz) fr = 15 # approximate length of transient event in seconds decay_time = 0.5 # expected half size of neurons gSig = (3, 3) # order of AR indicator dynamics p = 1 # minimum SNR for accepting new components min_SNR = 2.5 # correlation threshold for new component inclusion rval_thr = 0.85 # spatial downsampling factor (increases speed but may lose some fine structure) ds_factor = 1 # number of background components gnb = 2 # recompute gSig if downsampling is involved gSig = tuple(np.ceil(np.array(gSig) / ds_factor).astype('int')) # flag for online motion correction mot_corr = True # maximum allowed shift during motion correction max_shift = np.ceil(10. / ds_factor).astype('int') # set up some additional supporting parameters needed for the algorithm (these are default values but change according to dataset characteristics) # number of shapes to be updated each time (put this to a finite small value to increase speed) max_comp_update_shape = np.inf # number of files used for initialization init_files = 1 # number of files used for online online_files = len(fls) - 1 # number of frames for initialization (presumably from the first file) initbatch = 200 # maximum number of expected components used for memory pre-allocation (exaggerate here) expected_comps = 300 # initial number of components K = 2 # number of timesteps to consider when testing new neuron candidates N_samples = np.ceil(fr * decay_time) # exceptionality threshold thresh_fitness_raw = scipy.special.log_ndtr(-min_SNR) * N_samples # number of passes over the data epochs = 2 # upper bound for number of frames in each file (used right below) len_file = 1000 # total length of all files (if not known use a large number, then truncate at the end) T1 = len(fls) * len_file * epochs #%% Initialize movie # load only the first initbatch frames and possibly downsample them if ds_factor > 1: Y = cm.load(fls[0], subindices=slice(0, initbatch, None)).astype( np.float32).resize(1. / ds_factor, 1. / ds_factor) else: Y = cm.load(fls[0], subindices=slice(0, initbatch, None)).astype(np.float32) if mot_corr: # perform motion correction on the first initbatch frames mc = Y.motion_correct(max_shift, max_shift) Y = mc[0].astype(np.float32) borders = np.max(mc[1]) else: Y = Y.astype(np.float32) # minimum value of movie. Subtract it to make the data non-negative img_min = Y.min() Y -= img_min img_norm = np.std(Y, axis=0) # normalizing factor to equalize the FOV img_norm += np.median(img_norm) Y = Y / img_norm[None, :, :] # normalize data _, d1, d2 = Y.shape dims = (d1, d2) # dimensions of FOV Yr = Y.to_2D().T # convert data into 2D array Cn_init = Y.local_correlations(swap_dim=False) # compute correlation image #pl.imshow(Cn_init) #pl.title('Correlation Image on initial batch') #pl.colorbar() bnd_Y = np.percentile(Y, (0.001, 100 - 0.001)) # plotting boundaries for Y #%% initialize OnACID with bare initialization cnm_init = bare_initialization(Y[:initbatch].transpose(1, 2, 0), init_batch=initbatch, k=K, gnb=gnb, gSig=gSig, p=p, minibatch_shape=100, minibatch_suff_stat=5, update_num_comps=True, rval_thr=rval_thr, thresh_fitness_raw=thresh_fitness_raw, batch_update_suff_stat=True, max_comp_update_shape=max_comp_update_shape, deconv_flag=False, use_dense=False, simultaneously=False, n_refit=0) #%% Plot initialization results crd = plot_contours(cnm_init.A.tocsc(), Cn_init, thr=0.9) A, C, b, f, YrA, sn = cnm_init.A, cnm_init.C, cnm_init.b, cnm_init.f, cnm_init.YrA, cnm_init.sn view_patches_bar(Yr, scipy.sparse.coo_matrix(A.tocsc()[:, :]), C[:, :], b, f, dims[0], dims[1], YrA=YrA[:, :], img=Cn_init) bnd_AC = np.percentile(A.dot(C), (0.001, 100 - 0.005)) bnd_BG = np.percentile(b.dot(f), (0.001, 100 - 0.001)) #%% create a function for plotting results in real time if needed def create_frame(cnm2, img_norm, captions): A, b = cnm2.Ab[:, cnm2.gnb:], cnm2.Ab[:, :cnm2.gnb].toarray() C, f = cnm2.C_on[cnm2.gnb:cnm2.M, :], cnm2.C_on[:cnm2.gnb, :] # inferred activity due to components (no background) frame_plot = (frame_cor.copy() - bnd_Y[0]) / np.diff(bnd_Y) comps_frame = A.dot(C[:, t - 1]).reshape(cnm2.dims, order='F') bgkrnd_frame = b.dot(f[:, t - 1]).reshape( cnm2.dims, order='F') # denoised frame (components + background) denoised_frame = comps_frame + bgkrnd_frame denoised_frame = (denoised_frame.copy() - bnd_Y[0]) / np.diff(bnd_Y) comps_frame = (comps_frame.copy() - bnd_AC[0]) / np.diff(bnd_AC) if show_residuals: #all_comps = np.reshape(cnm2.Yres_buf.mean(0), cnm2.dims, order='F') all_comps = np.reshape(cnm2.mean_buff, cnm2.dims, order='F') all_comps = np.minimum(np.maximum(all_comps, 0) * 2 + 0.25, 255) else: all_comps = np.array(A.sum(-1)).reshape(cnm2.dims, order='F') # spatial shapes frame_comp_1 = cv2.resize( np.concatenate([frame_plot, all_comps * 1.], axis=-1), (2 * np.int(cnm2.dims[1] * resize_fact), np.int(cnm2.dims[0] * resize_fact))) frame_comp_2 = cv2.resize( np.concatenate([comps_frame, denoised_frame], axis=-1), (2 * np.int(cnm2.dims[1] * resize_fact), np.int(cnm2.dims[0] * resize_fact))) frame_pn = np.concatenate([frame_comp_1, frame_comp_2], axis=0).T vid_frame = np.repeat(frame_pn[:, :, None], 3, axis=-1) vid_frame = np.minimum((vid_frame * 255.), 255).astype('u1') if show_residuals and cnm2.ind_new: add_v = np.int(cnm2.dims[1] * resize_fact) for ind_new in cnm2.ind_new: cv2.rectangle(vid_frame, (int(ind_new[0][1] * resize_fact), int(ind_new[1][1] * resize_fact) + add_v), (int(ind_new[0][0] * resize_fact), int(ind_new[1][0] * resize_fact) + add_v), (255, 0, 255), 2) cv2.putText(vid_frame, captions[0], (5, 20), fontFace=5, fontScale=0.8, color=(0, 255, 0), thickness=1) cv2.putText(vid_frame, captions[1], (np.int(cnm2.dims[0] * resize_fact) + 5, 20), fontFace=5, fontScale=0.8, color=(0, 255, 0), thickness=1) cv2.putText(vid_frame, captions[2], (5, np.int(cnm2.dims[1] * resize_fact) + 20), fontFace=5, fontScale=0.8, color=(0, 255, 0), thickness=1) cv2.putText(vid_frame, captions[3], (np.int(cnm2.dims[0] * resize_fact) + 5, np.int(cnm2.dims[1] * resize_fact) + 20), fontFace=5, fontScale=0.8, color=(0, 255, 0), thickness=1) cv2.putText(vid_frame, 'Frame = ' + str(t), (vid_frame.shape[1] // 2 - vid_frame.shape[1] // 10, vid_frame.shape[0] - 20), fontFace=5, fontScale=0.8, color=(0, 255, 255), thickness=1) return vid_frame #%% Prepare object for OnACID cnm2 = deepcopy(cnm_init) save_init = False # flag for saving initialization object. Useful if you want to check OnACID with different parameters but same initialization if save_init: cnm_init.dview = None save_object(cnm_init, fls[0][:-4] + '_DS_' + str(ds_factor) + '.pkl') cnm_init = load_object(fls[0][:-4] + '_DS_' + str(ds_factor) + '.pkl') path_to_cnn_residual = os.path.join(caiman_datadir(), 'model', 'cnn_model_online.h5') cnm2._prepare_object(np.asarray(Yr), T1, expected_comps, idx_components=None, min_num_trial=3, max_num_added=3, path_to_model=path_to_cnn_residual, sniper_mode=False, use_peak_max=False, q=0.5) cnm2.thresh_CNN_noisy = 0.5 #%% Run OnACID and optionally plot results in real time epochs = 1 cnm2.Ab_epoch = [] # save the shapes at the end of each epoch t = cnm2.initbatch # current timestep tottime = [] Cn = Cn_init.copy() # flag for removing components with bad shapes remove_flag = False T_rm = 650 # remove bad components every T_rm frames rm_thr = 0.1 # CNN classifier removal threshold # flag for plotting contours of detected components at the end of each file plot_contours_flag = False # flag for showing results video online (turn off flags for improving speed) play_reconstr = True # flag for saving movie (file could be quite large..) save_movie = False movie_name = os.path.join( folder_name, 'sniper_meso_0.995_new.avi') # name of movie to be saved resize_fact = 1.2 # image resizing factor if online_files == 0: # check whether there are any additional files process_files = fls[:init_files] # end processing at this file init_batc_iter = [initbatch] # place where to start end_batch = T1 else: process_files = fls[:init_files + online_files] # additional files # where to start reading at each file init_batc_iter = [initbatch] + [0] * online_files shifts = [] show_residuals = True if show_residuals: caption = 'Mean Residual Buffer' else: caption = 'Identified Components' captions = ['Raw Data', 'Inferred Activity', caption, 'Denoised Data'] if save_movie and play_reconstr: fourcc = cv2.VideoWriter_fourcc('8', 'B', 'P', 'S') # fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter( movie_name, fourcc, 30.0, tuple([int(2 * x * resize_fact) for x in cnm2.dims])) for iter in range(epochs): if iter > 0: # if not on first epoch process all files from scratch process_files = fls[:init_files + online_files] init_batc_iter = [0] * (online_files + init_files) # np.array(fls)[np.array([1,2,3,4,5,-5,-4,-3,-2,-1])]: for file_count, ffll in enumerate(process_files): print('Now processing file ' + ffll) Y_ = cm.load(ffll, subindices=slice(init_batc_iter[file_count], T1, None)) # update max-correlation (and perform offline motion correction) just for illustration purposes if plot_contours_flag: if ds_factor > 1: Y_1 = Y_.resize(1. / ds_factor, 1. / ds_factor, 1) else: Y_1 = Y_.copy() if mot_corr: templ = (cnm2.Ab.data[:cnm2.Ab.indptr[1]] * cnm2.C_on[0, t - 1]).reshape(cnm2.dims, order='F') * img_norm newcn = (Y_1 - img_min).motion_correct( max_shift, max_shift, template=templ)[0].local_correlations(swap_dim=False) Cn = np.maximum(Cn, newcn) else: Cn = np.maximum(Cn, Y_1.local_correlations(swap_dim=False)) old_comps = cnm2.N # number of existing components for frame_count, frame in enumerate(Y_): # now process each file if np.isnan(np.sum(frame)): raise Exception('Frame ' + str(frame_count) + ' contains nan') if t % 100 == 0: print( 'Epoch: ' + str(iter + 1) + '. ' + str(t) + ' frames have beeen processed in total. ' + str(cnm2.N - old_comps) + ' new components were added. Total number of components is ' + str(cnm2.Ab.shape[-1] - gnb)) old_comps = cnm2.N t1 = time() # count time only for the processing part frame_ = frame.copy().astype(np.float32) # if ds_factor > 1: frame_ = cv2.resize(frame_, img_norm.shape[::-1]) # downsampling frame_ -= img_min # make data non-negative if mot_corr: # motion correct templ = cnm2.Ab.dot(cnm2.C_on[:cnm2.M, t - 1]).reshape( cnm2.dims, order='F') * img_norm frame_cor, shift = motion_correct_iteration_fast( frame_, templ, max_shift, max_shift) shifts.append(shift) else: templ = None frame_cor = frame_ frame_cor = frame_cor / img_norm # normalize data-frame cnm2.fit_next(t, frame_cor.reshape( -1, order='F')) # run OnACID on this frame # store time tottime.append(time() - t1) t += 1 if t % T_rm == 0 and remove_flag: prd, _ = evaluate_components_CNN(cnm2.Ab[:, gnb:], dims, gSig) ind_rem = np.where(prd[:, 1] < rm_thr)[0].tolist() cnm2.remove_components(ind_rem) print('Removing ' + str(len(ind_rem)) + ' components') if t % 1000 == 0 and plot_contours_flag: pl.cla() A = cnm2.Ab[:, cnm2.gnb:] # update the contour plot every 1000 frames crd = cm.utils.visualization.plot_contours(A, Cn, thr=0.9) pl.pause(1) if play_reconstr: # generate movie with the results vid_frame = create_frame(cnm2, img_norm, captions) if save_movie: out.write(vid_frame) if t - initbatch < 100: #for rp in np.int32(np.ceil(np.exp(-np.arange(1,100)/30)*20)): for rp in range(len(cnm2.ind_new) * 2): out.write(vid_frame) cv2.imshow('frame', vid_frame) if t - initbatch < 100: for rp in range(len(cnm2.ind_new) * 2): cv2.imshow('frame', vid_frame) if cv2.waitKey(1) & 0xFF == ord('q'): break print('Cumulative processing speed is ' + str((t - initbatch) / np.sum(tottime))[:5] + ' frames per second.') # save the shapes at the end of each epoch cnm2.Ab_epoch.append(cnm2.Ab.copy()) if save_movie: out.release() cv2.destroyAllWindows() #%% save results (optional) save_results = False if save_results: np.savez('results_analysis_online_MOT_CORR.npz', Cn=Cn, Ab=cnm2.Ab, Cf=cnm2.C_on, b=cnm2.b, f=cnm2.f, dims=cnm2.dims, tottime=tottime, noisyC=cnm2.noisyC, shifts=shifts) #%% extract results from the objects and do some plotting A, b = cnm2.Ab[:, cnm2.gnb:], cnm2.Ab[:, :cnm2.gnb].toarray() C, f = cnm2.C_on[cnm2.gnb:cnm2.M, t - t // epochs:t], cnm2.C_on[:cnm2.gnb, t - t // epochs:t] noisyC = cnm2.noisyC[:, t - t // epochs:t] b_trace = [osi.b for osi in cnm2.OASISinstances] if hasattr( cnm2, 'OASISinstances') else [0] * C.shape[0] pl.figure() crd = cm.utils.visualization.plot_contours(A, Cn, thr=0.9) view_patches_bar(Yr, scipy.sparse.coo_matrix(A.tocsc()[:, :]), C[:, :], b, f, dims[0], dims[1], YrA=noisyC[cnm2.gnb:cnm2.M] - C, img=Cn)
#idx_blobs = np.intersect1d(idx_components, idx_blobs) idx_components_bad = np.setdiff1d(list(range(len(r_values))), idx_components) print(' ***** ') print((len(r_values))) print((len(idx_components))) #%% try: A_off = A_off.toarray()[:, idx_components] except: A_off = A_off[:, idx_components] C_off = C_off[idx_components] # OASISinstances = OASISinstances[()] #%% pl.figure() crd = plot_contours(scipy.sparse.coo_matrix(A_off), Cn, thr=0.9) #%% view_patches_bar(None, scipy.sparse.coo_matrix(A_off[:, :]), C_off[:, :], b_off, f_off, dims_off[0], dims_off[1], YrA=YrA[:, :], img=Cn) #%% A_off_thr = cm.source_extraction.cnmf.spatial.threshold_components( A_off[:, :],
print((time() - t1)) #%% t1 = time() Atmp, Ctmp, b_in, f_in, center = cm.source_extraction.cnmf.initialization.initialize_components( Y, **options['init_params']) print((time() - t1)) #%% Refine manually component by clicking on neurons refine_components = False if refine_components: Ain, Cin = cm.source_extraction.cnmf.utilities.manually_refine_components( Y, options['init_params']['gSig'], coo_matrix(Atmp), Ctmp, Cn, thr=0.9) else: Ain, Cin = Atmp, Ctmp #%% plot estimated component pl.figure() crd = plot_contours(coo_matrix(Ain), Cn) pl.show() #%% UPDATE SPATIAL COMPONENTS #pl.close() t1 = time() A, b, Cin, f_in = cm.source_extraction.cnmf.spatial.update_spatial_components( Yr, Cin, f_in, Ain, sn=sn, dview=dview, **options['spatial_params']) t_elSPATIAL = time() - t1 pl.figure() crd = plot_contours(A, Cn) #%% update_temporal_components #pl.close() t1 = time() options['temporal_params'][ 'p'] = 0 # set this to zero for fast updating without deconvolution
import subprocess subprocess.run( ['mkdir', '-p', os.path.join(session['result_dir'], 'filtered')]) cnm_obj_fpath = os.path.join(session['result_dir'], 'filtered', 'analysis_results_filtered.hdf5') cnm_obj.save(cnm_obj_fpath) gdrive_upload_dir = os.path.join( session['result_dir'][session['result_dir'].find(downsample_subpath):], 'filtered') if upload_results: gdrive_upload_file(cnm_obj_fpath, gdrive_upload_dir, rclone_config) # Spatial footprint of filtered components plt.figure() plot_contours(cnm_obj.estimates.A, np.zeros(cnm_obj.dims), thr=0.9) plt.imshow(np.amax(readSFP(cnm_obj, False), axis=2)) plt.savefig(os.path.join(session['result_dir'], 'filtered', 'sfp.svg'), edgecolor='w', format='svg', transparent=False) msresult = load_msresult(session['result_dir'], session['gdrive_result_dir'], rclone_config) msobj = msresult['ms'][0, 0] ms_fpath, sfp_fpath = save_matlab(cnm_obj, session_info, os.path.join(session['result_dir'], 'filtered'), [], msobj['time'], msobj['camNumber'],
img_norm = 1 mc_init = mc_init / img_norm imgs_norm.append(img_norm) # pl.figure() cnm, Cn2, fname_new = initialize_movie(mc_init, K, gSig, rf, stride, base_name, merge_thresh=1.1, rval_thr_online=0.9, thresh_fitness_delta_online=-30, thresh_fitness_raw_online=-50, rval_thr_init=.5, thresh_fitness_delta_init=-20, thresh_fitness_raw_init=-20, rval_thr_refine=0.995, thresh_fitness_delta_refine=-200, thresh_fitness_raw_refine=-200, final_frate=2, Npeaks=10, single_thread=False, dview=dview) cnms.append(cnm) Cns.append(Cn2) base_names.append(fname_new) A, C, b, f, YrA, sn = cnm.A, cnm.C, cnm.b, cnm.f, cnm.YrA, cnm.sn crd = plot_contours(scipy.sparse.coo_matrix(A), Cns[-1], thr=0.9) pl.pause(1) cm.stop_server() log_files = glob.glob('Yr*_LOG_*') for log_file in log_files: os.remove(log_file) #%% vompute all the Cns Cns_all = [] templates_all = [] shifts_all = [] corrs_all = [] for (x0, x1, y0, y1, _) in sliding_window_new(m[0], overlaps, strides): print([x0, y0]) mc, shifts, template, corrs = m[:, x0:x1, y0:y1].copy(
print(roi_cons.shape) pl.imshow(roi_cons.sum(0)) if params_movie['kernel'] is not None: # kernel usually two kernel = np.ones((radius // params_movie['kernel'], radius // params_movie['kernel']), np.uint8) roi_cons = np.vstack([ cv2.dilate(rr, kernel, iterations=1)[np.newaxis, :, :] > 0 for rr in roi_cons ]) * 1. pl.imshow(roi_cons.sum(0), alpha=0.5) A_in = np.reshape(roi_cons.transpose([2, 1, 0]), (-1, roi_cons.shape[0]), order='C') pl.figure() crd = plot_contours(A_in, Cn, thr=.99999) # %% 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'] # merging threshold, max correlation allowed merge_thresh = params_movie['merge_thresh'] # %% Extract spatial and temporal components # TODO: todocument t1 = time.time() if images.shape[0] > 10000: check_nan = False else: check_nan = True cnm = cnmf.CNMF(check_nan=check_nan,
p=p, minibatch_shape=100, minibatch_suff_stat=5, update_num_comps=True, rval_thr=rval_thr, thresh_fitness_raw=thresh_fitness_raw, batch_update_suff_stat=True, max_comp_update_shape=max_comp_update_shape, deconv_flag=False, use_dense=True, simultaneously=False, n_refit=0) #%% Plot initialization results crd = plot_contours(cnm_init.A.tocsc(), Cn_init, thr=0.9) A, C, b, f, YrA, sn = cnm_init.A, cnm_init.C, cnm_init.b, cnm_init.f, cnm_init.YrA, cnm_init.sn view_patches_bar(Yr, scipy.sparse.coo_matrix(A.tocsc()[:, :]), C[:, :], b, f, dims[0], dims[1], YrA=YrA[:, :], img=Cn_init) #%% Prepare object for OnACID save_init = False # flag for saving initialization object. Useful if you want to check OnACID with different parameters but same initialization if save_init:
cnm = cnmf.CNMF(n_processes, k=K, gSig=gSig, merge_thresh=0.8, p=0, dview=dview, Ain=None, rf=rf, stride=stride, memory_fact=1, method_init=init_method, alpha_snmf=alpha_snmf, only_init_patch=True, gnb=1, method_deconvolution='oasis', n_pixels_per_process=n_pixels_per_process, p_ssub=2, p_tsub=2, block_size=block_size, check_nan=False) cnm = cnm.fit(images) A_tot = cnm.A C_tot = cnm.C YrA_tot = cnm.YrA b_tot = cnm.b f_tot = cnm.f sn_tot = cnm.sn t_patch_cnmf = time.time() - t1 print(('Number of components:' + str(A_tot.shape[-1]))) #%% pl.figure() crd = plot_contours(A_tot, Cn, thr=0.9) #%% DISCARD LOW QUALITY COMPONENT t1 = time.time() final_frate = params_movie['final_frate'] r_values_min = .6 # threshold on space consistency fitness_min = -30 # threshold on time variability # threshold on time variability (if nonsparse activity) fitness_delta_min = -30 Npeaks = 10 traces = C_tot + YrA_tot idx_components, idx_components_bad = cm.components_evaluation.estimate_components_quality( traces, Y, A_tot, C_tot, b_tot, f_tot, final_frate=final_frate, Npeaks=Npeaks, r_values_min=r_values_min, fitness_min=fitness_min, fitness_delta_min=fitness_delta_min) t_comps_quality = time.time() - t1 print(('Keeping ' + str(len(idx_components)) + ' and discarding ' + str(len(idx_components_bad)))) #%%