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) C_dff = extract_DF_F(Yr, cnm.A, cnm.C, cnm.bl, quantileMin=8, frames_window=200, dview=dview) pl.figure() pl.plot(C_dff.T) else: rf = 14 # half-size of the patches in pixels. rf=25, patches are 50x50 stride = 6 # amounpl.it of overlap between the patches in pixels K = 6 # number of neurons expected per patch gSig = [6, 6] # expected half size of neurons merge_thresh = 0.8 # merging threshold, max correlation allowed p = 1 # order of the autoregressive system save_results = False cnm = cnmf.CNMF(n_processes,
Yr,dims,T=cm.load_memmap(fname_new) Y=np.reshape(Yr,dims+(T,),order='F') nb_back=1 options = cnmf.utilities.CNMFSetParms(Y,n_processes,p=p,gSig=gSig,K=K,ssub=2,tsub=1,nb=nb_back) #options['preprocess_params']['max_num_samples_fft']=10000 Cn = cm.local_correlations(Y) Yr,sn,g,psx = cnmf.pre_processing.preprocess_data(Yr,dview=dview,**options['preprocess_params']) Ain,Cin, b_in, f_in, center=cnmf.initialization.initialize_components(Y, **options['init_params']) Ain,b_in,Cin, f_in = cnmf.spatial.update_spatial_components(Yr, Cin, f_in, Ain, sn=sn, dview=dview,**options['spatial_params']) if Cin.size > 0: options = cnmf.utilities.CNMFSetParms(Y,n_processes,p=p,gSig=gSig,K=K,tsub=1) options['temporal_params']['p'] = 0 # set this to zero for fast updating without deconvolution Cin,Ain,b_in,f_in,S,bl,c1,neurons_sn,g,YrA = cnmf.temporal.update_temporal_components(Yr,Ain,b_in,Cin,f_in,bl=None,c1=None,sn=None,g=None,**options['temporal_params']) Ain,Cin,nr_m,merged_ROIs,S_m,bl_m,c1_m,sn_m,g_m=cnmf.merging.merge_components(Yr,Ain,b_in,Cin,f_in,S,sn,options['temporal_params'], options['spatial_params'],dview=dview, bl=bl, c1=c1, sn=neurons_sn, g=g, thr=merge_thresh,mx=1000, fast_merge = True) Ain,b_in,Cin, f_in = cnmf.spatial.update_spatial_components(Yr, Cin, f_in, Ain, sn=sn, dview=dview,**options['spatial_params']) options['temporal_params']['p'] = p # set it back to original value to perform full deconvolution Cin,Ain,b_in,f_in,S,bl,c1,neurons_sn,g,YrA = cnmf.temporal.update_temporal_components(Yr,Ain,b_in,Cin,f_in,bl=None,c1=None,sn=None,g=None,**options['temporal_params']) traces=Cin+YrA tB = np.minimum(-2,np.floor(-5./30*final_frate)) tA = np.maximum(5,np.ceil(25./30*final_frate)) fitness_raw, fitness_delta, erfc_raw,erfc_delta,r_values,num_significant_samples = evaluate_components(Y,traces,Ain,Cin,bl,f_in, final_frate, remove_baseline = True, N = 5, robust_std = False, Athresh = 0.1, Npeaks = 5, thresh_C = 0.2) idx_components_r=np.where(r_values>=.6)[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) C_dff = extract_DF_F(Yr, Ain.tocsc()[:, idx_components], Cin[idx_components, :], bl[idx_components], quantileMin = 8, frames_window = 200, dview = dview) idx_components=np.union1d(idx_components,idx_components_delta) idx_components_bad=np.setdiff1d(range(len(traces)),idx_components) savemat(fnames[0][:-4]+'_output_analysis_matlab.mat',mdict={'ROIs':Ain,'DenoisedTraces':Cin,'Baseline':bl, 'Noise':YrA, 'Spikes': S,'DFF':C_dff , 'idx_components':idx_components, 'Correlation_image':Cn})
big_string = '' for substring in my_list: big_string = big_string + ' ' + substring A = pcf.cnmf.estimates.A F = pcf.cnmf.estimates.C + pcf.cnmf.estimates.YrA b = pcf.cnmf.estimates.b f= pcf.cnmf.estimates.f B = A.T.dot(b).dot(f) import scipy.ndimage as nd Df = nd.percentile_filter(B, 10, (1000,1)) plt.figure(); plt.plot(B[49]+pcf.cnmf.estimates.C[49]+pcf.cnmf.estimates.R[49]) #%% Flag auto as False, how does dFF look import caiman.source_extraction.cnmf.utilities as ut flag_dff = ut.detrend_df_f(A, b, pcf.cnmf.estimates.C, f, YrA=pcf.cnmf.estimates.YrA, quantileMin=8, frames_window=500, flag_auto=False, use_fast=False, detrend_only=False) slow_dff = ut.extract_DF_F(Yr, A, C, bl, quantileMin=8, frames_window=200, block_size=400, dview=None) fig, ax = plt.subplots(2) ax[0].plot(pcf.cnmf.estimates.F_dff[49]) ax[1].plot(flag_dff[49]) plt.figure() plt.plot(flag_dff[49], label='auto=False') plt.plot(pcf.cnmf.estimates.F_dff[49], label='auto=True') plt.legend()
raise Exception('Movie contains nan! You did not remove enough borders') #%% Cn = cm.local_correlations(Y[:, :, :3000]) 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) C_dff = extract_DF_F(Yr, cnm.A, cnm.C, cnm.bl, quantileMin=8, frames_window=200, dview=dview) pl.figure() pl.plot(C_dff.T) else: rf = 14 # half-size of the patches in pixels. rf=25, patches are 50x50 stride = 6 # amounpl.it of overlap between the patches in pixels K = 6 # number of neurons expected per patch gSig = [6, 6] # expected half size of neurons merge_thresh = 0.8 # merging threshold, max correlation allowed p = 1 # order of the autoregressive system save_results = False 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=2, method_deconvolution='oasis', low_rank_background=True) cnm = cnm.fit(images)
Y, A, C, b, f, 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) print(' ***** ') print((len(traces))) print((len(idx_components))) # %% save results Cdf = extract_DF_F(Yr=Yr, A=A, C=C, bl=cnm.bl) np.savez(os.path.join( os.path.split(fname_new)[0], os.path.split(fname_new)[1][:-4] + 'results_analysis.npz'), Cn=Cn, A=A, Cdf=Cdf, C=C, b=b, f=f, YrA=YrA, sn=sn, d1=d1, d2=d2, idx_components=idx_components, idx_components_bad=idx_components_bad,