def get_Xc_yc(fname,p_smooth,unit_num,binsize): varlist = ['M', 'F', 'TH', 'PHIE'] blk = neoUtils.get_blk(fname) blk_smooth = GLM.get_blk_smooth(fname,p_smooth) cbool = neoUtils.get_Cbool(blk) X = GLM.create_design_matrix(blk,varlist) Xdot = GLM.get_deriv(blk,blk_smooth,varlist,[0,5,9]) #maybe only want one derivative? X = np.concatenate([X,Xdot],axis=1) X = neoUtils.replace_NaNs(X,'pchip') X = neoUtils.replace_NaNs(X,'interp') Xbin = GLM.bin_design_matrix(X,binsize=binsize) scaler = sklearn.preprocessing.StandardScaler(with_mean=False) Xbin = scaler.fit_transform(Xbin) cbool_bin= GLM.bin_design_matrix(cbool[:,np.newaxis],binsize=binsize).ravel() y = neoUtils.concatenate_sp(blk)['cell_{}'.format(unit_num)] ybin = elephant.conversion.BinnedSpikeTrain(y,binsize=binsize*pq.ms).to_array().T.astype('f8') Xbin = Xbin[:ybin.shape[0],:] cbool_bin = cbool_bin[:ybin.shape[0]] yhat = np.zeros(ybin.shape[0]) Xc = Xbin[cbool_bin,:] yc = ybin[cbool_bin,:] return(Xc,yc,cbool_bin,yhat)
def create_design_matrix(blk,varlist,window=1,binsize=1,deriv_tgl=False,bases=None): ''' Takes a list of variables and turns it into a matrix. Sets the non-contact mechanics to zero, but keeps all the kinematics as NaN You can append the derivative or apply the pillow bases, or both. Scales, but does not center the output ''' X = [] if type(window)==pq.quantity.Quantity: window = int(window) if type(binsize)==pq.quantity.Quantity: binsize = int(binsize) Cbool = neoUtils.get_Cbool(blk,-1) use_flags = neoUtils.concatenate_epochs(blk) # ================================ # # GET THE CONCATENATED DESIGN MATRIX OF REQUESTED VARS # ================================ # for varname in varlist: if varname in ['MB','FB']: var = neoUtils.get_var(blk,varname[0],keep_neo=False)[0] var = neoUtils.get_MB_MD(var)[0] var[np.invert(Cbool)]=0 elif varname in ['MD','FD']: var = neoUtils.get_var(blk,varname[0],keep_neo=False)[0] var = neoUtils.get_MB_MD(var)[1] var[np.invert(Cbool)]=0 elif varname in ['ROT','ROTD']: TH = neoUtils.get_var(blk,'TH',keep_neo=False)[0] PH = neoUtils.get_var(blk,'PHIE',keep_neo=False)[0] TH = neoUtils.center_var(TH,use_flags=use_flags) PH = neoUtils.center_var(PH,use_flags=use_flags) TH[np.invert(Cbool)] = 0 PH[np.invert(Cbool)] = 0 if varname=='ROT': var = np.sqrt(TH**2+PH**2) else: var = np.arctan2(PH,TH) else: var = neoUtils.get_var(blk,varname, keep_neo=False)[0] if varname in ['M','F']: var[np.invert(Cbool),:]=0 if varname in ['TH','PHIE']: var = neoUtils.center_var(var,use_flags) var[np.invert(Cbool),:]=0 var = neoUtils.replace_NaNs(var,'pchip') var = neoUtils.replace_NaNs(var,'interp') X.append(var) X = np.concatenate(X, axis=1) return X
def get_X_y(fname, unit_num=0): varlist = ['M', 'FX', 'FY', 'TH'] blk = neoUtils.get_blk(fname) cbool = neoUtils.get_Cbool(blk) X = GLM.create_design_matrix(blk, varlist) Xdot, Xsmooth = GLM.get_deriv(blk, blk, varlist, [0, 5, 9]) X = np.concatenate([X, Xdot], axis=1) X = neoUtils.replace_NaNs(X, 'pchip') X = neoUtils.replace_NaNs(X, 'interp') scaler = sklearn.preprocessing.StandardScaler(with_mean=False) X = scaler.fit_transform(X) y = neoUtils.get_rate_b(blk, unit_num)[1][:, np.newaxis] yhat = np.zeros_like(y) return (X, y, cbool)
def get_X_y(fname,p_smooth,unit_num=0): varlist = ['M', 'F', 'TH', 'PHIE'] blk = neoUtils.get_blk(fname) blk_smooth = GLM.get_blk_smooth(fname,p_smooth) cbool = neoUtils.get_Cbool(blk) X = GLM.create_design_matrix(blk,varlist) Xdot,Xsmooth = GLM.get_deriv(blk,blk_smooth,varlist,[9]) X = np.concatenate([X,Xdot],axis=1) X = neoUtils.replace_NaNs(X,'pchip') X = neoUtils.replace_NaNs(X,'interp') scaler = sklearn.preprocessing.StandardScaler(with_mean=False) X = scaler.fit_transform(X) y = neoUtils.get_rate_b(blk,unit_num)[1][:,np.newaxis] y[np.invert(cbool)]=0 return(X,y,cbool)
def get_components(fname,p_smooth=None,smooth_idx=9): ''' Get the PCA comonents given a filename''' varlist = ['M', 'F', 'TH', 'PHIE'] blk = neoUtils.get_blk(fname) cbool = neoUtils.get_Cbool(blk) root = neoUtils.get_root(blk,0)[:-2] X = GLM.create_design_matrix(blk,varlist) if p_smooth is not None: blk_smooth = GLM.get_blk_smooth(fname,p_smooth) Xdot = GLM.get_deriv(blk,blk_smooth,varlist,smoothing=[smooth_idx])[0] X = np.concatenate([X,Xdot],axis=1) X[np.invert(cbool),:]=0 X = neoUtils.replace_NaNs(X,'pchip') X = neoUtils.replace_NaNs(X,'interp') scaler = sklearn.preprocessing.StandardScaler(with_mean=False) X[cbool,:] = scaler.fit_transform(X[cbool,:]) pca = sklearn.decomposition.PCA() pca.fit_transform(X[cbool,:]) return(pca,root)
def get_X_y(fname, p_smooth, unit_num, pca_tgl=False, n_pcs=3): varlist = ['M', 'F', 'TH', 'PHIE'] blk = neoUtils.get_blk(fname) blk_smooth = get_blk_smooth(fname, p_smooth) cbool = neoUtils.get_Cbool(blk) X = GLM.create_design_matrix(blk, varlist) Xdot, Xsmooth = GLM.get_deriv(blk, blk_smooth, varlist, [0, 5, 9]) # if using the PCA decomposition of the inputs: if pca_tgl: X = neoUtils.replace_NaNs(X, 'pchip') X = neoUtils.replace_NaNs(X, 'interp') Xsmooth = neoUtils.replace_NaNs(Xsmooth, 'pchip') Xsmooth = neoUtils.replace_NaNs(Xsmooth, 'interp') scaler = sklearn.preprocessing.StandardScaler(with_mean=False) X = scaler.fit_transform(X) scaler = sklearn.preprocessing.StandardScaler(with_mean=False) Xsmooth = scaler.fit_transform(Xsmooth) pca = sklearn.decomposition.PCA() X_pc = pca.fit_transform(X)[:, :n_pcs] pca = sklearn.decomposition.PCA() Xs_pc = pca.fit_transform(Xsmooth)[:, :n_pcs] zero_pad = np.zeros([1, n_pcs]) Xd_pc = np.diff(np.concatenate([zero_pad, Xs_pc], axis=0), axis=0) X = np.concatenate([X_pc, Xd_pc], axis=1) scaler = sklearn.preprocessing.StandardScaler(with_mean=False) X = scaler.fit_transform(X) else: X = np.concatenate([X, Xdot], axis=1) X = neoUtils.replace_NaNs(X, 'pchip') X = neoUtils.replace_NaNs(X, 'interp') scaler = sklearn.preprocessing.StandardScaler(with_mean=False) X = scaler.fit_transform(X) y = neoUtils.get_rate_b(blk, unit_num)[1][:, np.newaxis] # Xc = X[cbool,:] # yc = y[cbool] yhat = np.zeros_like(y) return (X, y, cbool)