if save_results: np.savez(os.path.join(base_folder, 'results_analysis_patch.npz'), A_tot=A_tot.todense(), C_tot=C_tot, sn_tot=sn_tot, d1=d1, d2=d2, b=b, f=f) #%% if you have many components this might take long! pl.figure() crd = cse.utilities.plot_contours(A_tot, Cn, thr=0.9) #%% set parameters for full field of view analysis options = cse.utilities.CNMFSetParms( Y, n_processes, p=0, gSig=gSig, K=A_tot.shape[-1], thr=merge_thresh) pix_proc = np.minimum(np.int((d1 * d2) / n_processes / (old_div(T, 2000.))), np.int(old_div((d1 * d2), n_processes))) # regulates the amount of memory used options['spatial_params']['n_pixels_per_process'] = pix_proc options['temporal_params']['n_pixels_per_process'] = pix_proc #%% merge spatially overlaping and temporally correlated components if 1: A_m, C_m, nr_m, merged_ROIs, S_m, bl_m, c1_m, sn_m, g_m = cse.merge_components(Yr, A_tot, [], np.array(C_tot), [], np.array( C_tot), [], options['temporal_params'], options['spatial_params'], dview=dview, thr=options['merging']['thr'], mx=np.Inf) else: A_m, C_m, f_m = A_tot, C_tot, f cse.utilities.view_patches_bar(Yr, A_m, C_m, b, f_m, d1, d2, C_m, img=Cn) #%% update temporal to get Y_r options['temporal_params']['p'] = 0 # change ifdenoised traces time constant is wrong options['temporal_params']['fudge_factor'] = 0.96 options['temporal_params']['backend'] = 'ipyparallel' C_m, A_m, b, f_m, S_m, bl_m, c1_m, neurons_sn_m, g2_m, YrA_m = cse.temporal.update_temporal_components( Yr, A_m, np.atleast_2d(b).T, C_m, f, dview=dview, bl=None, c1=None, sn=None, g=None, **options['temporal_params']) #%% get rid of evenrually noisy components. # But check by visual inspection to have a feeling fot the threshold. Try to be loose, you will be able to get rid of more of them later!
print 'Number of components:' + str(A_tot.shape[-1]) times.append(time.time() - t0) #if save_results: # np.savez('results_analysis_patch.npz',A_tot=A_tot.todense(), C_tot=C_tot, sn_tot=sn_tot,d1=d1,d2=d2) # if you have many components this might take long! #pl.figure() #crd = cse.utilities.plot_contours(A_tot,Cn,thr=0.9) # set parameters for full field of view analysis options = cse.utilities.CNMFSetParms(Y,n_processes,p=0,gSig=gSig,K=A_tot.shape[-1],thr=merge_thresh) pix_proc=np.minimum(np.int((d1*d2)/n_processes/(T/2000.)),np.int((d1*d2)/n_processes)) # regulates the amount of memory used options['spatial_params']['n_pixels_per_process']=pix_proc options['temporal_params']['n_pixels_per_process']=pix_proc # merge spatially overlaping and temporally correlated components A_m,C_m,nr_m,merged_ROIs,S_m,bl_m,c1_m,sn_m,g_m=cse.merge_components(Yr,A_tot,[],np.array(C_tot),[],np.array(C_tot),[],options['temporal_params'],options['spatial_params'],thr=options['merging']['thr'],mx=np.Inf) #ti print 'Number of components:' + str(A_m.shape[-1]) # UPDATE SPATIAL COMPONENTS options['spatial_params']['backend']='ipyparallel' #parallelize with ipyparallel t1 = time.time() #% A2,b2,C2 = cse.spatial.update_spatial_components(Yr, C_m, f, A_m, sn=sn_tot, **options['spatial_params']) print time.time() - t1 times.append(time.time() - t1) # # UPDATE TEMPORAL COMPONENTS options['temporal_params']['p']= 2 options['temporal_params']['fudge_factor']=0.98 #change ifdenoised traces time constant is wrong options['temporal_params']['backend']='ipyparallel'
np.savez(os.path.join(folder_in,'images','results_analysis_patch_3.npz'),A_tot=A_tot.todense(), C_tot=C_tot, sn_tot=sn_tot,d1=d1,d2=d2,b=b,f=f,Cn=Cn) #%% if you have many components this might take long! # #pl.figure();crd = cse.utilities.plot_contours(A_tot,Cn,thr=0.9) #%% set parameters for full field of view analysis options = cse.utilities.CNMFSetParms(Y,n_processes,p=0,gSig=gSig,K=A_tot.shape[-1],thr=merge_thresh) pix_proc=np.minimum(np.int((d1*d2)/n_processes/(old_div(T,2000.))),np.int(old_div((d1*d2),n_processes))) # regulates the amount of memory used options['spatial_params']['n_pixels_per_process']=pix_proc options['temporal_params']['n_pixels_per_process']=pix_proc #%% merge spatially overlaping and temporally correlated components merged_ROIs=[0] A_m=scipy.sparse.coo_matrix(A_tot) C_m=C_tot while len(merged_ROIs)>0: A_m,C_m,nr_m,merged_ROIs,S_m,bl_m,c1_m,sn_m,g_m=cse.merge_components(Yr,A_m,[],np.array(C_m),[],np.array(C_m),[],options['temporal_params'],options['spatial_params'],dview=dview,thr=options['merging']['thr'],mx=np.Inf) # pl.figure();crd = cse.utilities.plot_contours(A_m,Cn,thr=0.9) #%% update temporal to get Y_r options['temporal_params']['p']=0 options['temporal_params']['fudge_factor']=0.96 #change ifdenoised traces time constant is wrong options['temporal_params']['backend']='ipyparallel' C_m,f_m,S_m,bl_m,c1_m,neurons_sn_m,g2_m,YrA_m = cse.temporal.update_temporal_components(Yr,A_m,np.atleast_2d(b).T,C_m,f,dview=dview,bl=None,c1=None,sn=None,g=None,**options['temporal_params']) #%% get rid of evenrually noisy components. # But check by visual inspection to have a feeling fot the threshold. Try to be loose, you will be able to get rid of more of them later! tB = np.minimum(-2,np.floor(-5./30*final_frate)) tA = np.maximum(5,np.ceil(25./30*final_frate)) Npeaks=10 traces=C_m+YrA_m # traces_a=traces-scipy.ndimage.percentile_filter(traces,8,size=[1,np.shape(traces)[-1]/5]) # traces_b=np.diff(traces,axis=1)
pix_proc = np.minimum( np.int((d1 * d2) / n_processes / (old_div(T, 2000.))), np.int(old_div((d1 * d2), n_processes))) # regulates the amount of memory used options['spatial_params']['n_pixels_per_process'] = pix_proc options['temporal_params']['n_pixels_per_process'] = pix_proc #%% merge spatially overlaping and temporally correlated components merged_ROIs = [0] A_m = scipy.sparse.coo_matrix(A_tot) C_m = C_tot while len(merged_ROIs) > 0: A_m, C_m, nr_m, merged_ROIs, S_m, bl_m, c1_m, sn_m, g_m = cse.merge_components( Yr, A_m, [], np.array(C_m), [], np.array(C_m), [], options['temporal_params'], options['spatial_params'], dview=dview, thr=options['merging']['thr'], mx=np.Inf) # pl.figure();crd = cse.utilities.plot_contours(A_m,Cn,thr=0.9) #%% update temporal to get Y_r options['temporal_params']['p'] = 0 # change ifdenoised traces time constant is wrong options['temporal_params']['fudge_factor'] = 0.96 options['temporal_params']['backend'] = 'ipyparallel' C_m, A_m, b, f_m, S_m, bl_m, c1_m, neurons_sn_m, g2_m, YrA_m = cse.temporal.update_temporal_components( Yr, A_m, np.atleast_2d(b).T, C_m,
def process_data(haussio_data, mask=None, p=2, nrois_init=200): fn_cnmf = haussio_data.dirname_comp + '_cnmf.mat' tiffs_to_cnmf(haussio_data, mask) sys.stdout.write('Loading from {0}... '.format( haussio_data.dirname_comp + '_Y*.npy')) Y = np.load(haussio_data.dirname_comp + '_Y.npy', mmap_mode='r') d1, d2, T = Y.shape if not os.path.exists(fn_cnmf): cse.utilities.stop_server() sys.stdout.flush() t0 = time.time() Yr = np.load(haussio_data.dirname_comp + '_Yr.npy', mmap_mode='r') sys.stdout.write('took {0:.2f} s\n'.format(time.time()-t0)) # how to subdivide the work among processes n_pixels_per_process = d1*d2/NCPUS options = cse.utilities.CNMFSetParms(Y, K=nrois_init, p=p, gSig=[9, 9]) options['preprocess_params']['n_processes'] = NCPUS options['preprocess_params'][ 'n_pixels_per_process'] = n_pixels_per_process options['init_params']['nIter'] = 10 options['init_params']['maxIter'] = 10 options['init_params']['use_hals'] = False options['spatial_params']['n_processes'] = NCPUS options['spatial_params'][ 'n_pixels_per_process'] = n_pixels_per_process options['temporal_params']['n_processes'] = NCPUS options['temporal_params'][ 'n_pixels_per_process'] = n_pixels_per_process cse.utilities.start_server(NCPUS) t0 = time.time() sys.stdout.write("Preprocessing... ") sys.stdout.flush() Yr, sn, g = cse.preprocess_data(Yr, **options['preprocess_params']) sys.stdout.write(' took {0:.2f} s\n'.format(time.time()-t0)) # 224.94s t0 = time.time() sys.stdout.write("Initializing components... ") sys.stdout.flush() Ain, Cin, b_in, f_in, center = cse.initialize_components( Y, **options['init_params']) sys.stdout.write(' took {0:.2f} s\n'.format(time.time()-t0)) # 2281.37s t0 = time.time() sys.stdout.write("Updating spatial components... ") sys.stdout.flush() A, b, Cin = cse.update_spatial_components( Yr, Cin, f_in, Ain, sn=sn, **options['spatial_params']) sys.stdout.write(' took {0:.2f} s\n'.format(time.time()-t0)) # 252.57s t0 = time.time() sys.stdout.write("Updating temporal components... ") sys.stdout.flush() C, f, S, bl, c1, neurons_sn, g, YrA = \ cse.update_temporal_components( Yr, A, b, Cin, f_in, bl=None, c1=None, sn=None, g=None, **options['temporal_params']) sys.stdout.write(' took {0:.2f} s\n'.format(time.time()-t0)) # 455.14s t0 = time.time() sys.stdout.write("Merging ROIs... ") sys.stdout.flush() A_m, C_m, nr_m, merged_ROIs, S_m, bl_m, c1_m, sn_m, g_m = \ cse.merge_components( Yr, A, b, C, f, S, sn, options['temporal_params'], options['spatial_params'], bl=bl, c1=c1, sn=neurons_sn, g=g, thr=0.7, mx=100, fast_merge=True) sys.stdout.write(' took {0:.2f} s\n'.format(time.time()-t0)) # 702.55s t0 = time.time() sys.stdout.write("Updating spatial components... ") sys.stdout.flush() A2, b2, C2 = cse.update_spatial_components( Yr, C_m, f, A_m, sn=sn, **options['spatial_params']) sys.stdout.write(' took {0:.2f} s\n'.format(time.time()-t0)) # 77.16s t0 = time.time() sys.stdout.write("Updating temporal components... ") sys.stdout.flush() C2, f2, S2, bl2, c12, neurons_sn2, g21, YrA = \ cse.update_temporal_components( Yr, A2, b2, C2, f, bl=None, c1=None, sn=None, g=None, **options['temporal_params']) sys.stdout.write(' took {0:.2f} s\n'.format(time.time()-t0)) # 483.41s # A: spatial components (ROIs) # C: denoised [Ca2+] # YrA: residuals ("noise") # S: Spikes savemat(fn_cnmf, {"A": A2, "C": C2, "YrA": YrA, "S": S2, "bl": bl2}) else: resdict = loadmat(fn_cnmf) A2 = resdict["A"] C2 = resdict["C"] YrA = resdict["YrA"] S2 = resdict["S"] bl2 = resdict["bl"] proj_fn = haussio_data.dirname_comp + "_proj.npy" if not os.path.exists(proj_fn): zproj = utils.zproject(np.transpose(Y, (2, 0, 1))) np.save(proj_fn, zproj) else: zproj = np.load(proj_fn) # DF_F, DF = cse.extract_DF_F(Y.reshape(d1*d2, T), A2, C2) t0 = time.time() sys.stdout.write("Ordering components... ") sys.stdout.flush() A_or, C_or, srt = cse.order_components(A2, C2) sys.stdout.write(' took {0:.2f} s\n'.format(time.time()-t0)) cse.utilities.stop_server() polygons = contour(A2, d1, d2, thr=0.9) rois = ROIList([sima.ROI.ROI(polygons=poly) for poly in polygons]) return rois, C2, haussio_data, zproj, S2, Y, YrA
def extract_rois_patch(file_name,d1,d2,rf=5,stride = 2): not_completed, in_progress rf=6 stride = 2 idx_flat,idx_2d=extract_patch_coordinates(d1, d2, rf=rf,stride = stride) perctl=95 n_components=2 tol=1e-6 max_iter=5000 args_in=[] for id_f,id_2d in zip(idx_flat,idx_2d): args_in.append((file_name, id_f,id_2d[0].shape, perctl,n_components,tol,max_iter)) st=time.time() try: if 1: c = Client() dview=c[:] file_res = dview.map_sync(nmf_patches, args_in) else: file_res = map(nmf_patches, args_in) finally: dview.results.clear() c.purge_results('all') c.purge_everything() c.close() print time.time()-st A1=lil_matrix((d1*d2,len(file_res))) C1=[] A2=lil_matrix((d1*d2,len(file_res))) C2=[] A_tot=lil_matrix((d1*d2,n_components*len(file_res))) C_tot=[]; count_out=0 for count,f in enumerate(file_res): idx_,flt,ca,d=f print count_out #flt,ca,_=cse.order_components(coo_matrix(flt),ca) # A1[idx_,count]=flt[:,0][:,np.newaxis]/np.sqrt(np.sum(flt[:,0]**2)) # A2[idx_,count]=flt[:,1][:,np.newaxis] /np.sqrt(np.sum(flt[:,1]**2)) # C1.append(ca[0,:]) # C2.append(ca[1,:]) for ccc in range(n_components): A_tot[idx_,count_out]=flt[:,ccc][:,np.newaxis]/np.sqrt(np.sum(flt[:,ccc]**2)) C_tot.append(ca[ccc,:]) count_out+=1 # pl.imshow(np.reshape(flt[:,0],d,order='F'),vmax=10) # pl.pause(.1) correlations=np.corrcoef(np.array(C_tot)) centers=cse.com(A_tot.todense(),d1,d2) distances=sklearn.metrics.pairwise.euclidean_distances(centers) pl.imshow((correlations>0.8) & (distances<10)) Yr=np.load('Yr.npy',mmap_mode='r') [d,T]=Yr.shape Y=np.reshape(Yr,(d1,d2,T),order='F') options=cse.utilities.CNMFSetParms(Y,p=0) res_merge=cse.merge_components(Yr,A_tot,[],np.array(C_tot),[],np.array(C_tot),[],options['temporal_params'],options['spatial_params'],thr=0.8) A_m,C_m,nr_m,merged_ROIs,S_m,bl_m,c1_m,sn_m,g_m=res_merge A_norm=np.array([A_m[:,rr].toarray()/np.sqrt(np.sum(A_m[:,rr].toarray()**2)) for rr in range(A_m.shape[-1])]).T options=cse.utilities.CNMFSetParms(Y,p=2,K=np.shape(A_m)[-1]) Yr,sn,g=cse.pre_processing.preprocess_data(Yr,**options['preprocess_params']) epsilon=1e-2 pixels_bckgrnd=np.nonzero(A_norm.sum(axis=-1)<epsilon)[0] f=np.sum(Yr[pixels_bckgrnd,:],axis=0) A2,b2,C2 = cse.spatial.update_spatial_components(Yr, C_m, f, A_m, sn=sn, **options['spatial_params']) A_or2, C_or2, srt2 = cse.utilities.order_components(A2,C2) A_norm2=np.array([A_or2[:,rr]/np.sqrt(np.sum(A_or2[:,rr]**2)) for rr in range(A_or2.shape[-1])]).T options['temporal_params']['p'] = 2 # set it back to original value to perform full deconvolution C2,f2,S2,bl2,c12,neurons_sn2,g21,YrA = cse.temporal.update_temporal_components(Yr,A2,b2,C2,f,bl=None,c1=None,sn=None,g=None,**options['temporal_params']) A_or, C_or, srt = cse.utilities.order_components(A2,C2) return A1,A2,C1
def process_data(haussio_data, mask=None, p=2, nrois_init=400): fn_cnmf = haussio_data.dirname_comp + '_cnmf.mat' tiffs_to_cnmf(haussio_data, mask) sys.stdout.write('Loading from {0}... '.format( haussio_data.dirname_comp + '_Y*.npy')) Y = np.load(haussio_data.dirname_comp + '_Y.npy', mmap_mode='r') d1, d2, T = Y.shape if not os.path.exists(fn_cnmf): cse.utilities.stop_server() sys.stdout.flush() t0 = time.time() Yr = np.load(haussio_data.dirname_comp + '_Yr.npy', mmap_mode='r') sys.stdout.write('took {0:.2f} s\n'.format(time.time()-t0)) # how to subdivide the work among processes n_pixels_per_process = d1*d2/NCPUS options = cse.utilities.CNMFSetParms( Y, NCPUS, K=nrois_init, p=p, gSig=[9, 9], ssub=2, tsub=2) options['preprocess_params']['n_processes'] = NCPUS options['preprocess_params'][ 'n_pixels_per_process'] = n_pixels_per_process options['init_params']['nIter'] = 10 options['init_params']['maxIter'] = 10 options['init_params']['use_hals'] = True options['spatial_params']['n_processes'] = NCPUS options['spatial_params'][ 'n_pixels_per_process'] = n_pixels_per_process options['temporal_params']['n_processes'] = NCPUS options['temporal_params'][ 'n_pixels_per_process'] = n_pixels_per_process cse.utilities.start_server() t0 = time.time() sys.stdout.write("Preprocessing... ") sys.stdout.flush() Yr, sn, g, psx = cse.preprocess_data(Yr, **options['preprocess_params']) sys.stdout.write(' took {0:.2f} s\n'.format(time.time()-t0)) # 224.94s # 2016-05-24: 146.30s t0 = time.time() sys.stdout.write("Initializing components... ") sys.stdout.flush() Ain, Cin, b_in, f_in, center = cse.initialize_components( Y, **options['init_params']) sys.stdout.write(' took {0:.2f} s\n'.format(time.time()-t0)) # 2281.37s # 2016-05-24: 1054.72s t0 = time.time() sys.stdout.write("Updating spatial components... ") sys.stdout.flush() A, b, Cin = cse.update_spatial_components( Yr, Cin, f_in, Ain, sn=sn, **options['spatial_params']) sys.stdout.write(' took {0:.2f} s\n'.format(time.time()-t0)) # 252.57s # 2016-05-24: 445.95s t0 = time.time() sys.stdout.write("Updating temporal components... ") sys.stdout.flush() C, f, S, bl, c1, neurons_sn, g, YrA = \ cse.update_temporal_components( Yr, A, b, Cin, f_in, bl=None, c1=None, sn=None, g=None, **options['temporal_params']) sys.stdout.write(' took {0:.2f} s\n'.format(time.time()-t0)) # 455.14s # 2016-05-24: 86.10s t0 = time.time() sys.stdout.write("Merging ROIs... ") sys.stdout.flush() A_m, C_m, nr_m, merged_ROIs, S_m, bl_m, c1_m, sn_m, g_m = \ cse.merge_components( Yr, A, b, C, f, S, sn, options['temporal_params'], options['spatial_params'], bl=bl, c1=c1, sn=neurons_sn, g=g, thr=0.7, mx=100, fast_merge=True) sys.stdout.write(' took {0:.2f} s\n'.format(time.time()-t0)) # 702.55s # 2016-05-24: 11.75s t0 = time.time() sys.stdout.write("Updating spatial components... ") sys.stdout.flush() A2, b2, C2 = cse.update_spatial_components( Yr, C_m, f, A_m, sn=sn, **options['spatial_params']) sys.stdout.write(' took {0:.2f} s\n'.format(time.time()-t0)) # 77.16s # 2016-05-24: 99.22s t0 = time.time() sys.stdout.write("Updating temporal components... ") sys.stdout.flush() C2, f2, S2, bl2, c12, neurons_sn2, g21, YrA = \ cse.update_temporal_components( Yr, A2, b2, C2, f, bl=None, c1=None, sn=None, g=None, **options['temporal_params']) sys.stdout.write(' took {0:.2f} s\n'.format(time.time()-t0)) # 483.41s # 2016-05-24: 74.81s # A: spatial components (ROIs) # C: denoised [Ca2+] # YrA: residuals ("noise") # S: Spikes savemat(fn_cnmf, {"A": A2, "C": C2, "YrA": YrA, "S": S2, "bl": bl2}) else: resdict = loadmat(fn_cnmf) A2 = resdict["A"] C2 = resdict["C"] YrA = resdict["YrA"] S2 = resdict["S"] bl2 = resdict["bl"] proj_fn = haussio_data.dirname_comp + "_proj.npy" if not os.path.exists(proj_fn): zproj = utils.zproject(np.transpose(Y, (2, 0, 1))) np.save(proj_fn, zproj) else: zproj = np.load(proj_fn) # DF_F, DF = cse.extract_DF_F(Y.reshape(d1*d2, T), A2, C2) t0 = time.time() sys.stdout.write("Ordering components... ") sys.stdout.flush() A_or, C_or, srt = cse.order_components(A2, C2) sys.stdout.write(' took {0:.2f} s\n'.format(time.time()-t0)) cse.utilities.stop_server() polygons = contour(A2, d1, d2, thr=0.9) rois = ROIList([sima.ROI.ROI(polygons=poly) for poly in polygons]) return rois, C2, zproj, S2, Y, YrA
def process_data_patches( haussio_data, mask=None, p=2, nrois_init=400, roi_iceberg=0.9): fn_cnmf = haussio_data.dirname_comp + '_cnmf.mat' tiffs_to_cnmf(haussio_data, mask) sys.stdout.write('Loading from {0}... '.format( haussio_data.dirname_comp + '_Y*.npy')) Y = np.load(haussio_data.dirname_comp + '_Y.npy', mmap_mode='r') d1, d2, T = Y.shape if not os.path.exists(fn_cnmf): cse.utilities.stop_server() sys.stdout.flush() t0 = time.time() fname_new = haussio_data.dirname_comp + '_Yr.npy' Yr = np.load(fname_new, mmap_mode='r') sys.stdout.write('took {0:.2f} s\n'.format(time.time()-t0)) # how to subdivide the work among processes n_pixels_per_process = d1*d2/NCPUS_PATCHES options = cse.utilities.CNMFSetParms( Y, NCPUS, K=np.max((int(nrois_init/NCPUS), 1)), p=p, gSig=[9, 9], ssub=1, tsub=1) options['preprocess_params']['n_processes'] = NCPUS options['preprocess_params'][ 'n_pixels_per_process'] = n_pixels_per_process options['init_params']['nIter'] = 10 options['init_params']['maxIter'] = 10 options['init_params']['use_hals'] = True options['spatial_params']['n_processes'] = NCPUS options['spatial_params'][ 'n_pixels_per_process'] = n_pixels_per_process options['temporal_params']['n_processes'] = NCPUS options['temporal_params'][ 'n_pixels_per_process'] = n_pixels_per_process options['temporal_params']['backend'] = 'ipyparallel' rf = 16 # half-size of the patches in pixels. rf=25, patches are 50x50 stride = 2 # amounpl of overlap between the patches in pixels cse.utilities.start_server() cl = Client() dview = cl[:len(cl)] t0 = time.time() sys.stdout.write("CNMF patches... ") sys.stdout.flush() A_tot, C_tot, b, f, sn_tot, opt_out = cse.map_reduce.run_CNMF_patches( fname_new, (d1, d2, T), options, rf=rf, stride=stride, n_processes=NCPUS_PATCHES, dview=dview, memory_fact=4.0) sys.stdout.write(' took {0:.2f} s\n'.format(time.time()-t0)) options = cse.utilities.CNMFSetParms( Y, NCPUS, K=A_tot.shape[-1], p=p, gSig=[9, 9], ssub=1, tsub=1) pix_proc = np.minimum( np.int((d1*d2)/NCPUS/(T/2000.)), np.int((d1*d2)/NCPUS)) # regulates the amount of memory used options['preprocess_params']['n_processes'] = NCPUS options['preprocess_params'][ 'n_pixels_per_process'] = n_pixels_per_process options['init_params']['nIter'] = 10 options['init_params']['maxIter'] = 10 options['init_params']['use_hals'] = True options['spatial_params']['n_processes'] = NCPUS options['temporal_params']['n_processes'] = NCPUS options['spatial_params']['n_pixels_per_process'] = pix_proc options['temporal_params']['n_pixels_per_process'] = pix_proc t0 = time.time() sys.stdout.write("Merging ROIs... ") sys.stdout.flush() A_m, C_m, nr_m, merged_ROIs, S_m, bl_m, c1_m, sn_m, g_m = \ cse.merge_components( Yr, A_tot, [], np.array(C_tot), [], np.array(C_tot), [], options['temporal_params'], options['spatial_params'], thr=options['merging']['thr'], mx=np.Inf) sys.stdout.write(' took {0:.2f} s\n'.format(time.time()-t0)) t0 = time.time() sys.stdout.write("Updating spatial components... ") sys.stdout.flush() A2, b2, C2 = cse.spatial.update_spatial_components( Yr, C_m, f, A_m, sn=sn_tot, **options['spatial_params']) sys.stdout.write(' took {0:.2f} s\n'.format(time.time()-t0)) # 77.16s # 2016-05-24: 99.22s t0 = time.time() sys.stdout.write("Updating temporal components... ") sys.stdout.flush() C2, f2, S2, bl2, c12, neurons_sn2, g21, YrA = \ cse.temporal.update_temporal_components( Yr, A2, b2, C2, f, bl=None, c1=None, sn=None, g=None, **options['temporal_params']) sys.stdout.write(' took {0:.2f} s\n'.format(time.time()-t0)) # 483.41s # 2016-05-24: 74.81s # A: spatial components (ROIs) # C: denoised [Ca2+] # YrA: residuals ("noise") # S: Spikes savemat(fn_cnmf, {"A": A2, "C": C2, "YrA": YrA, "S": S2, "bl": bl2}) else: resdict = loadmat(fn_cnmf) A2 = resdict["A"] C2 = resdict["C"] YrA = resdict["YrA"] S2 = resdict["S"] bl2 = resdict["bl"] proj_fn = haussio_data.dirname_comp + "_proj.npy" if not os.path.exists(proj_fn): zproj = utils.zproject(np.transpose(Y, (2, 0, 1))) np.save(proj_fn, zproj) else: zproj = np.load(proj_fn) # DF_F, DF = cse.extract_DF_F(Y.reshape(d1*d2, T), A2, C2) # t0 = time.time() # sys.stdout.write("Ordering components... ") # sys.stdout.flush() # A_or, C_or, srt = cse.order_components(A2, C2) # sys.stdout.write(' took {0:.2f} s\n'.format(time.time()-t0)) cse.utilities.stop_server() polygons = contour(A2, d1, d2, thr=roi_iceberg) rois = ROIList([sima.ROI.ROI(polygons=poly) for poly in polygons]) return rois, C2, zproj, S2, Y, YrA