templates=[];
for tif_file in tif_files:
    print(tif_file)
    m=cb.load(tif_file,fr=30,start_time=0,subindices=range(0,1500,20))
    min_val_add=np.percentile(m,.01)
    m=m-min_val_add
    movs.append(m)
    templ=np.nanmedian(m,axis=0);
    m,template,shifts,xcorrs=m.motion_correct(max_shift_w=5, max_shift_h=5, show_movie=False, template=templ, method='opencv')    
    templates.append(np.median(m,axis=0))

all_movs=cb.concatenate(movs)
m=cb.movie(np.array(templates),fr=1)
m=m.motion_correct(template=m[0])[0]
template=np.median(m,axis=0)
cb.matrixMontage(m,cmap=pl.cm.gray,vmin=0,vmax=1000)

#%%
all_shifts=[];
movs=[];
for tif_file in tif_files:
    print(tif_file)
    m=cb.load(tif_file,fr=30,start_time=0);   
    min_val_add=np.percentile(m,.01)
    m=m-min_val_add
    m,_,shifts,_=m.motion_correct(template=template, method='opencv')
    movs.append(m)    
    all_shifts.append(shifts)

all_movs=cb.concatenate(movs)
Exemple #2
0
pl.imshow(fovs)

#%% create a denoised version of the movie, nice to visualize
if False:
    
    m.IPCA_denoise(components = 100, batch = 10000)
    
    
#%%
spcomps=m.IPCA_io(n_components=50, fun='logcosh', max_iter=1000, tol=1e-20)
spcomps=np.rollaxis(spcomps,2)
#%% compute spatial components via NMF
initTime=time.time()
space_spcomps,time_comps=(m-np.min(m)).NonnegativeMatrixFactorization(n_components=20,beta=1,tol=5e-7);
print 'elapsed time:' + str(time.time()-initTime) 
cb.matrixMontage(np.asarray(space_spcomps),cmap=pl.cm.gray) # visualize components

#%% compute spatial components via ICA PCA
from scipy.stats import mode
initTime=time.time()
spcomps=m.IPCA_stICA(components=40,mu=.5,batch=100000);
print 'elapsed time:' + str(time.time()-initTime) 
#cb.matrixMontage(spcomps,cmap=pl.cm.gray) # visualize components
 
#%% extract ROIs from spatial components 
#_masks,masks_grouped=m.extractROIsFromPCAICA(spcomps, numSTD=6, gaussiansigmax=2 , gaussiansigmay=2)
_masks,_=cb.extractROIsFromPCAICA(spcomps, numSTD=20.0, gaussiansigmax=1 , gaussiansigmay=1,thresh=.005)
#cb.matrixMontage(np.asarray(_masks),cmap=pl.cm.gray)

#%%  extract single ROIs from each mask
minPixels=8;
m,mbl=m.computeDFF(secsWindow=10,quantilMin=50)
print 'elapsed time:' + str(time.time()-initTime) 

#%% denoise and local correlation. this makes the movie look much better
if False:
    loc_corrs=m.local_correlations(eight_neighbours=True)
    m=m.IPCA_denoise(components = 100, batch = 100000)
    m=m*loc_corrs


#%% compute spatial components via ICA PCA
print 'Computing PCA + ICA...'
initTime=time.time()
spcomps=m.IPCA_stICA(componentsPCA=70,componentsICA = 50, mu=1, batch=1000000, algorithm='parallel', whiten=True, ICAfun='logcosh', fun_args=None, max_iter=2000, tol=1e-8, w_init=None, random_state=None);
print 'elapsed time:' + str(time.time()-initTime) 
cb.matrixMontage(spcomps,cmap=pl.cm.gray) # visualize components
 
#%% extract ROIs from spatial components 
#_masks,masks_grouped=m.extractROIsFromPCAICA(spcomps, numSTD=6, gaussiansigmax=2 , gaussiansigmay=2)
_masks,_=cb.extractROIsFromPCAICA(spcomps, numSTD=4.0, gaussiansigmax=.1 , gaussiansigmay=.2)
#cb.matrixMontage(np.asarray(_masks),cmap=pl.cm.gray)

#%%  extract single ROIs from each mask
minPixels=30;
maxPixels=4000;
masks_tmp=[];
for mask in _masks:
    numPixels=np.sum(np.array(mask));        
    if (numPixels>minPixels and numPixels<maxPixels):
#        print numPixels
        masks_tmp.append(mask>0)