#%% if False: m=cb.load(filename_py); #%% m=cb.load(filename_hdf5); #m=m.crop(crop_top=0,crop_bottom=1,crop_left=0,crop_right=0,crop_begin=0,crop_end=0); min_val_add=np.min(np.mean(m,axis=0)) #min_val_add=np.min(m)-1# movies must be strictly positive!! m=m-min_val_add #%% concatenate movies (it will add to the original movie) # you have to create another movie new_mov=XMovie(...) if False: conc_mov=cb.concatenate([m,m]) #%% quick and dirt m=cb.load(filename_hdf5); m,shifts,xcorrs,template=m.motion_correct(max_shift_w=5,max_shift_h=5, num_frames_template=None, template = None,method='opencv') #%% motion correct for template purpose. Just use a subset to compute the template if type_ == 'granule': submov=m[::1,:] max_shift_h=5; max_shift_w=5; else:
filename_hdf5 = filename[:-4]+'.hdf5' filename_mc = filename[:-4]+'_mc.npz' frameRate = 30 start_time = 0 # load movie # for loading only a portion of the movie or only some channels # you can use the option: subindices=range(0,1500,10) m = cb.load(filename, fr=frameRate, start_time=start_time) # red and green channels m_r=m[:,:,:,0] m=m[:,:,:,1] # backend='opencv' is much faster cb.concatenate([m,m_r],axis=1).resize(.5,.5,.1).play(fr=100, gain=1.0, magnification=1,backend='opencv') # automatic parameters motion correction max_shift_h = 20 # maximum allowed shifts in y max_shift_w = 20 # maximum allowed shifts in x m_r_mc, shifts, xcorrs, template = m_r.motion_correct(max_shift_w=max_shift_w, max_shift_h=max_shift_h, num_frames_template=None, template=None, remove_blanks=False, method='opencv') #%% pl.figure() pl.imshow(template,cmap='gray') pl.figure() pl.plot(shifts)
num_frames_median=1000; movs=[]; fr=30; start_time=0; 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)
# save movie hdf5 format. fastest m.save(filename_hdf5) # backend='opencv' is much faster m.play(fr=100, gain=15.0, magnification=1, backend='opencv') low_SNR = True if low_SNR: N = 1000000 mn1 = m.copy().bilateral_blur_2D(diameter=5, sigmaColor=10000, sigmaSpace=0) mn1, shifts, xcorrs, template = mn1.motion_correct() mn2 = mn1.apply_shifts(shifts) # mn1=cb.movie(np.transpose(np.array(Y_n),[2,0,1]),fr=30) mn = cb.concatenate([mn1, mn2], axis=1) # automatic parameters motion correction max_shift_h = 10 # maximum allowed shifts in y max_shift_w = 10 # maximum allowed shifts in x m = cb.load(filename_hdf5) m, shifts, xcorrs, template = m.motion_correct(max_shift_w=max_shift_w, max_shift_h=max_shift_h, num_frames_template=None, template=None, method='opencv') # removie the borders max_h, max_w = np.max(shifts, axis=0) min_h, min_w = np.min(shifts, axis=0) m = m.crop(crop_top=max_h,
X1 = mdl1.components_.T pl.figure() for idx, mm in enumerate(X1.T): pl.subplot(7, 7, idx + 1) pl.imshow(np.reshape(mm, (d1, d2), order='F'), cmap=pl.cm.gray, vmin=np.percentile(mm, 1), vmax=np.percentile(mm, 99)) pl.subplot(7, 7, idx + 2) mu = np.mean(m1, 0) pl.imshow(mu, cmap=pl.cm.gray, vmin=np.percentile( mu, 1), vmax=np.percentile(mu, 99)) #%% PLAY DENOISED MOVIE denoised = cb.movie(np.reshape(traces.dot( X1.T), [T, d1, d2], order='F'), fr=30) denoised = cb.concatenate([denoised, cb.movie(np.transpose( np.array(Y[:, :, N:N1]), [2, 0, 1]), fr=30)], axis=2) # denoised=cb.concatenate([denoised,m1],axis=1) (denoised - np.mean(denoised, 0)).play(gain=8., magnification=8, backend='opencv', fr=30) #%% PLAY DENOISED MOVIE ONLY UYSING SOME COMPONENTS idx = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]) idx = np.array([0, 12]) denoised = cb.movie(np.reshape(traces[:, idx].dot( X1[:, idx].T), [T, d1, d2], order='F'), fr=30) (denoised - np.mean(denoised, 0).min()).play(gain=5., magnification=8, backend='opencv', fr=30) #%% PLAY RESIDUAL MOVIE residual = m1 - denoised
filename_mc = filename[:-4] + '_mc.npz' frameRate = 30 start_time = 0 # load movie # for loading only a portion of the movie or only some channels # you can use the option: subindices=range(0,1500,10) m = cb.load(filename, fr=frameRate, start_time=start_time) # red and green channels m_r = m[:, :, :, 0] m = m[:, :, :, 1] # backend='opencv' is much faster cb.concatenate([m, m_r], axis=1).resize(.5, .5, .1).play(fr=100, gain=1.0, magnification=1, backend='opencv') # automatic parameters motion correction max_shift_h = 20 # maximum allowed shifts in y max_shift_w = 20 # maximum allowed shifts in x m_r_mc, shifts, xcorrs, template = m_r.motion_correct(max_shift_w=max_shift_w, max_shift_h=max_shift_h, num_frames_template=None, template=None, remove_blanks=False, method='opencv') #%% pl.figure() pl.imshow(template, cmap='gray') pl.figure()