def initialize_components(Y, K=30, gSig=[5,5], gSiz=None, ssub=1, tsub=1, nIter = 5, maxIter=5, kernel = None, use_hals=True,Cn=None,sn=None): """Initalize components This method uses a greedy approach followed by hierarchical alternative least squares (HALS) NMF. Optional use of spatio-temporal downsampling to boost speed. Parameters ---------- Y: np.ndarray d1 x d2 x T movie, raw data. K: [optional] int number of neurons to extract (default value: 30). tau: [optional] list,tuple standard deviation of neuron size along x and y (default value: (5,5). gSiz: [optional] list,tuple size of kernel (default 2*tau + 1). nIter: [optional] int number of iterations for shape tuning (default 5). maxIter: [optional] int number of iterations for HALS algorithm (default 5). ssub: [optional] int spatial downsampling factor recommended for large datasets (default 1, no downsampling). tsub: [optional] int temporal downsampling factor recommended for long datasets (default 1, no downsampling). kernel: [optional] np.ndarray User specified kernel for greedyROI (default None, greedy ROI searches for Gaussian shaped neurons) use_hals: [bool] Whether to refine components with the hals method Returns -------- Ain: np.ndarray (d1*d2) x K , spatial filter of each neuron. Cin: np.ndarray T x K , calcium activity of each neuron. center: np.ndarray K x 2 , inferred center of each neuron. bin: np.ndarray (d1*d2) X nb, initialization of spatial background. fin: np.ndarray nb X T matrix, initalization of temporal background. """ if gSiz is None: gSiz=(2*gSig[0] + 1,2*gSig[1] + 1) d1,d2,T=np.shape(Y) # rescale according to downsampling factor gSig = np.round([gSig[0]/ssub,gSig[1]/ssub]).astype(np.int) gSiz = np.round([gSiz[0]/ssub,gSiz[1]/ssub]).astype(np.int) print 'Noise Normalization' if sn is not None: min_noise=np.percentile(sn,2) noise=np.maximum(sn,min_noise) Y=Y/np.reshape(noise,(d1,d2))[:,:,np.newaxis] # spatial downsampling mean_val=np.mean(Y) if ssub!=1 or tsub!=1: print "Spatial Downsampling ..." Y_ds = downscale_local_mean(Y,(ssub,ssub,tsub),cval=mean_val) if Cn is not None: Cn = downscale_local_mean(Cn,(ssub,ssub),cval=mean_val) else: Y_ds = Y print 'Roi Extraction...' Ain, Cin, _, b_in, f_in = greedyROI2d(Y_ds, nr = K, gSig = gSig, gSiz = gSiz, nIter=nIter, kernel = kernel) if use_hals: print 'Refining Components...' Ain, Cin, b_in, f_in = hals_2D(Y_ds, Ain, Cin, b_in, f_in,maxIter=maxIter); #center = ssub*com(Ain,d1s,d2s) d1s,d2s,Ts=np.shape(Y_ds) Ain = np.reshape(Ain, (d1s, d2s,K),order='F') Ain = resize(Ain, [d1, d2, K],order=1) Ain = np.reshape(Ain, (d1*d2, K),order='F') b_in=np.reshape(b_in,(d1s, d2s),order='F') b_in = resize(b_in, [d1, d2]); b_in = np.reshape(b_in, (d1*d2, 1),order='F') Cin = resize(Cin, [K, T]) f_in = resize(np.atleast_2d(f_in), [1, T]) center = com(Ain,d1,d2) if sn is not None: Ain=Ain*np.reshape(noise,(d1*d2,))[:,np.newaxis] b_in=b_in*np.reshape(noise,(d1*d2,)) Y=Y*np.reshape(noise,(d1,d2))[:,:,np.newaxis] return Ain, Cin, b_in, f_in, center
def initialize_components(Y, K=30, gSig=[5,5], gSiz=None, ssub=1, tsub=1, nIter = 5, maxIter=5, use_median = False, kernel = None): """Initalize components This method uses a greedy approach followed by hierarchical alternative least squares (HALS) NMF. Optional use of spatio-temporal downsampling to boost speed. Parameters ---------- Y: np.ndarray d1 x d2 x T movie, raw data. K: [optional] int number of neurons to extract (default value: 30). tau: [optional] list,tuple standard deviation of neuron size along x and y (default value: (5,5). gSiz: [optional] list,tuple size of kernel (default 2*tau + 1). nIter: [optional] int number of iterations for shape tuning (default 5). maxIter: [optional] int number of iterations for HALS algorithm (default 5). ssub: [optional] int spatial downsampling factor recommended for large datasets (default 1, no downsampling). tsub: [optional] int temporal downsampling factor recommended for long datasets (default 1, no downsampling). use_median: [optional] bool add back fluorescence median values or not during refinement. kernel: [optional] np.ndarray User specified kernel for greedyROI (default None, greedy ROI searches for Gaussian shaped neurons) Returns -------- Ain: np.ndarray (d1*d2) x K , spatial filter of each neuron. Cin: np.ndarray T x K , calcium activity of each neuron. center: np.ndarray K x 2 , inferred center of each neuron. bin: np.ndarray (d1*d2) X nb, initialization of spatial background. fin: np.ndarray nb X T matrix, initalization of temporal background. """ if gSiz is None: gSiz=(2*gSig[0] + 1,2*gSig[1] + 1) d1,d2,T=np.shape(Y) # rescale according to downsampling factor gSig = np.round([gSig[0]/ssub,gSig[1]/ssub]).astype(np.int) gSiz = np.round([gSiz[0]/ssub,gSiz[1]/ssub]).astype(np.int) # d1s = np.ceil(d1/ssub).astype(np.int) #size of downsampled image # d2s = np.ceil(d2/ssub).astype(np.int) # Ts = np.floor(T/tsub).astype(np.int) #reduced number of frames # spatial downsampling if ssub!=1 or tsub!=1: print "Spatial Downsampling ..." Y_ds = scipy.ndimage.zoom(Y, [1./ssub, 1./ssub, 1./tsub], order=0,mode='nearest') else: Y_ds = Y print 'Roi Extraction...' Ain, Cin, _, b_in, f_in = greedyROI2d(Y_ds, nr = K, gSig = gSig, gSiz = gSiz, use_median = use_median, nIter=nIter, kernel = kernel) print 'Refining Components...' Ain, Cin, b_in, f_in = hals_2D(Y_ds, Ain, Cin, b_in, f_in,maxIter=maxIter); #center = ssub*com(Ain,d1s,d2s) d1s,d2s,Ts=np.shape(Y_ds) Ain = np.reshape(Ain, (d1s, d2s,K),order='F') Ain = resize(Ain, [d1, d2],order=0) Ain = np.reshape(Ain, (d1*d2, K),order='F') b_in=np.reshape(b_in,(d1s, d2s),order='F') b_in = resize(b_in, [d1, d2]); b_in = np.reshape(b_in, (d1*d2, 1),order='F') Cin = resize(Cin, [K, T]) f_in = resize(f_in, [1, T]) center = com(Ain,d1,d2) return Ain, Cin, b_in, f_in, center
def initialize_components(Y, K=30, gSig=[5, 5], gSiz=None, ssub=1, tsub=1, nIter=5, maxIter=5, kernel=None, use_hals=True, Cn=None): """Initalize components This method uses a greedy approach followed by hierarchical alternative least squares (HALS) NMF. Optional use of spatio-temporal downsampling to boost speed. Parameters ---------- Y: np.ndarray d1 x d2 x T movie, raw data. K: [optional] int number of neurons to extract (default value: 30). tau: [optional] list,tuple standard deviation of neuron size along x and y (default value: (5,5). gSiz: [optional] list,tuple size of kernel (default 2*tau + 1). nIter: [optional] int number of iterations for shape tuning (default 5). maxIter: [optional] int number of iterations for HALS algorithm (default 5). ssub: [optional] int spatial downsampling factor recommended for large datasets (default 1, no downsampling). tsub: [optional] int temporal downsampling factor recommended for long datasets (default 1, no downsampling). kernel: [optional] np.ndarray User specified kernel for greedyROI (default None, greedy ROI searches for Gaussian shaped neurons) use_hals: [bool] Whether to refine components with the hals method Returns -------- Ain: np.ndarray (d1*d2) x K , spatial filter of each neuron. Cin: np.ndarray T x K , calcium activity of each neuron. center: np.ndarray K x 2 , inferred center of each neuron. bin: np.ndarray (d1*d2) X nb, initialization of spatial background. fin: np.ndarray nb X T matrix, initalization of temporal background. """ if gSiz is None: gSiz = (2 * gSig[0] + 1, 2 * gSig[1] + 1) d1, d2, T = np.shape(Y) # rescale according to downsampling factor gSig = np.round([gSig[0] / ssub, gSig[1] / ssub]).astype(np.int) gSiz = np.round([gSiz[0] / ssub, gSiz[1] / ssub]).astype(np.int) # spatial downsampling mean_val = np.mean(Y) if ssub != 1 or tsub != 1: print "Spatial Downsampling ..." Y_ds = downscale_local_mean(Y, (ssub, ssub, tsub), cval=mean_val) if Cn is not None: Cn = downscale_local_mean(Cn, (ssub, ssub), cval=mean_val) else: Y_ds = Y print 'Roi Extraction...' Ain, Cin, _, b_in, f_in = greedyROI2d(Y_ds, nr=K, gSig=gSig, gSiz=gSiz, nIter=nIter, kernel=kernel) if use_hals: print 'Refining Components...' Ain, Cin, b_in, f_in = hals_2D(Y_ds, Ain, Cin, b_in, f_in, maxIter=maxIter) #center = ssub*com(Ain,d1s,d2s) d1s, d2s, Ts = np.shape(Y_ds) Ain = np.reshape(Ain, (d1s, d2s, K), order='F') Ain = resize(Ain, [d1, d2, K], order=1) Ain = np.reshape(Ain, (d1 * d2, K), order='F') b_in = np.reshape(b_in, (d1s, d2s), order='F') b_in = resize(b_in, [d1, d2]) b_in = np.reshape(b_in, (d1 * d2, 1), order='F') Cin = resize(Cin, [K, T]) f_in = resize(np.atleast_2d(f_in), [1, T]) center = com(Ain, d1, d2) return Ain, Cin, b_in, f_in, center