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 calc_MSE(fname, p_smooth, unit_num): blk = neoUtils.get_blk(fname) blk_smooth = GLM.get_blk_smooth(fname, p_smooth) varlist = ['M', 'F', 'TH', 'PHIE'] root = neoUtils.get_root(blk, unit_num) print('Working on {}'.format(root)) Xdot = GLM.get_deriv(blk, blk_smooth, varlist)[0] Xdot = np.reshape(Xdot, [-1, 8, 10]) sp = neoUtils.concatenate_sp(blk)['cell_{}'.format(0)] cbool = neoUtils.get_Cbool(blk) mse = [] for ii in range(Xdot.shape[1]): var_in = Xdot[:, ii, :].copy() mse.append(tuning_curve_MSE(var_in, sp, cbool, bins=50)) return (mse)
def MB_curve(blk,unit_num,save_tgl=False,im_ext='svg',dpi_res=300): root = neoUtils.get_root(blk, unit_num) M = neoUtils.get_var(blk) use_flags = neoUtils.get_Cbool(blk) MB = mechanics.get_MB_MD(M)[0].magnitude.ravel() MB[np.invert(use_flags)]=0 sp = neoUtils.concatenate_sp(blk)['cell_{}'.format(unit_num)] r, b = neoUtils.get_rate_b(blk, unit_num, sigma=5 * pq.ms) MB_bayes,edges = varTuning.stim_response_hist(MB*1e6,r,use_flags,nbins=100,min_obs=5) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(edges[:-1],MB_bayes,'o',color='k') ax.set_ylabel('Spike Rate (sp/s)') ax.set_xlabel('Bending Moment ($\mu$N-m)') plt.tight_layout() if save_tgl: plt.savefig('./figs/{}_MB_tuning.{}'.format(root,im_ext),dpi=dpi_res) plt.close('all')
def mymz_space(blk,unit_num,bin_stretch=False,save_tgl=False,p_save=None,im_ext='png',dpi_res=300): root = neoUtils.get_root(blk,unit_num) use_flags = neoUtils.get_Cbool(blk) M = neoUtils.get_var(blk).magnitude sp = neoUtils.concatenate_sp(blk)['cell_{}'.format(unit_num)] idx = np.all(np.isfinite(M),axis=1) if bin_stretch: MY = np.empty(M.shape[0]) MZ = np.empty(M.shape[0]) MY[idx], logit_y = nl(M[idx, 1],90) MZ[idx], logit_z = nl(M[idx, 2],90) else: MY = M[:,1]*1e-6 MZ = M[:,2]*1e-6 response, var1_edges,var2_edges = varTuning.joint_response_hist(MY,MZ,sp,use_flags,bins = 100,min_obs=15) if bin_stretch: var1_edges = logit_y(var1_edges) var2_edges = logit_z(var2_edges) else: pass ax = varTuning.plot_joint_response(response,var1_edges,var2_edges,contour=False) ax.axvline(color='k',linewidth=1) ax.axhline(color='k',linewidth=1) ax.patch.set_color([0.6,0.6,0.6]) mask = response.mask.__invert__() if not mask.all(): ax.set_ylim(var2_edges[np.where(mask)[0].min()], var2_edges[np.where(mask)[0].max()]) ax.set_xlim(var1_edges[np.where(mask)[1].min()], var1_edges[np.where(mask)[1].max()]) ax.set_xlabel('M$_y$ ($\mu$N-m)') ax.set_ylabel('M$_z$ ($\mu$N-m)') plt.draw() plt.tight_layout() if save_tgl: if p_save is None: raise ValueError("figure save location is required") else: plt.savefig(os.path.join(p_save,'{}_mymz.{}'.format(root,im_ext)),dpi=dpi_res) plt.close('all')
def FX_plots(blk,unit_num,save_tgl=False,im_ext='svg',dpi_res=300): root = neoUtils.get_root(blk, unit_num) F = neoUtils.get_var(blk,'F') Fx = F.magnitude[:,0] use_flags = neoUtils.get_Cbool(blk) sp = neoUtils.concatenate_sp(blk)['cell_{}'.format(unit_num)] r, b = neoUtils.get_rate_b(blk, unit_num, sigma=5 * pq.ms) Fx[np.invert(use_flags)] = 0 Fx_bayes, edges = varTuning.stim_response_hist(Fx * 1e6, r, use_flags, nbins=50, min_obs=5) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(edges[:-1], Fx_bayes*1000, 'o', color='k') ax.set_ylabel('Spike Rate (sp/s)') ax.set_xlabel('Axial Force ($\mu$N-m)') plt.tight_layout() if save_tgl: plt.savefig('./figs/{}_Fx_tuning.{}'.format(root,im_ext), dpi=dpi_res) plt.close('all')
def smoothed(smooth_idx=9): smooth_vals = np.arange(5, 100, 10) sub_p_save = os.path.join( p_save, '{}ms_smoothing_deriv'.format(smooth_vals[smooth_idx])) if not os.path.isdir(sub_p_save): os.mkdir(sub_p_save) for f in glob.glob(os.path.join(p_load, '*NEO.h5')): try: blk = neoUtils.get_blk(f) blk_smooth = GLM.get_blk_smooth(f, p_smooth) num_units = len(blk.channel_indexes[-1].units) for unit_num in range(num_units): varlist = ['M', 'F', 'TH', 'PHIE'] root = neoUtils.get_root(blk, unit_num) print('Working on {}'.format(root)) outname = os.path.join( sub_p_save, '{}ms_{}_pillowX.mat'.format(smooth_vals[smooth_idx], root)) X = GLM.create_design_matrix(blk, varlist) Xdot = GLM.get_deriv(blk, blk_smooth, varlist, [smooth_idx])[0] X = np.concatenate([X, Xdot], axis=1) sp = neoUtils.concatenate_sp(blk)['cell_{}'.format(unit_num)] y = neoUtils.get_rate_b(blk, unit_num)[1] cbool = neoUtils.get_Cbool(blk) arclengths = get_arclength_bool(blk, unit_num) sio.savemat(outname, { 'X': X, 'y': y, 'cbool': cbool, 'arclengths': arclengths }) except Exception as ex: print('Problem with {}:{}'.format(os.path.basename(f), ex))
def calc_corr(fname, p_smooth, unit_num): blk = neoUtils.get_blk(fname) blk_smooth = GLM.get_blk_smooth(fname, p_smooth) varlist = ['M', 'F', 'TH', 'PHIE'] component_list = [ '{}_dot'.format(x) for x in ['Mx', 'My', 'Mz', 'Fx', 'Fy', 'Fz', 'TH', 'PHI'] ] root = neoUtils.get_root(blk, unit_num) Xdot = GLM.get_deriv(blk, blk_smooth, varlist)[0] Xdot = np.reshape(Xdot, [-1, 8, 10]) windows = np.arange(5, 100, 10) sp = neoUtils.concatenate_sp(blk)['cell_{}'.format(0)] cbool = neoUtils.get_Cbool(blk) corr = [] R = [] # loop over variables for ii in range(Xdot.shape[1]): var_in = Xdot[:, ii, :].copy() # loop over smoothing r = [] for jj in range(var_in.shape[1]): kernel = elephant.kernels.GaussianKernel(pq.ms * windows[jj]) FR = elephant.statistics.instantaneous_rate(sp, pq.ms, kernel=kernel) idx = np.isfinite(var_in[:, jj]) r.append( scipy.corrcoef(var_in[:, jj].ravel()[idx], FR.magnitude.ravel()[idx])[0, 1]) R.append(r) R = np.array(R) df = pd.DataFrame(data=R, columns=['{}ms'.format(x) for x in windows]) df.index = component_list return (df)
def phase_plots(blk,unit_num,save_tgl=False,bin_stretch=False,p_save=None,im_ext='png',dpi_res=300): ''' Plot Phase planes for My and Mz''' root = neoUtils.get_root(blk, unit_num) M = neoUtils.get_var(blk).magnitude sp = neoUtils.concatenate_sp(blk)['cell_{}'.format(unit_num)] r, b = neoUtils.get_rate_b(blk, unit_num, sigma=5 * pq.ms) use_flags = neoUtils.get_Cbool(blk) Mdot = mechanics.get_deriv(M) if bin_stretch: raise Exception('Not finished with use_flags') # MY, logit_y = nl(M[idx, 1], 90) # MZ, logit_z = nl(M[idx, 2], 90) # MY_dot, logit_ydot = nl(Mdot[idx, 1], 95) # MZ_dot, logit_zdot = nl(Mdot[idx, 2], 95) else: MY = M[:, 1] * 1e-6 MZ = M[:, 2] * 1e-6 MY_dot = Mdot[:, 1] * 1e-6 MZ_dot = Mdot[:, 2] * 1e-6 My_response,My_edges,Mydot_edges = varTuning.joint_response_hist(MY, MY_dot, r, use_flags, [100,30],min_obs=15) Mz_response,Mz_edges,Mzdot_edges = varTuning.joint_response_hist(MZ, MZ_dot, r, use_flags, [100,30],min_obs=15) if bin_stretch: My_edges = logit_y(My_edges) Mz_edges = logit_z(Mz_edges) Mydot_edges = logit_ydot(Mydot_edges) Mzdot_edges = logit_zdot(Mzdot_edges) else: pass axy = varTuning.plot_joint_response(My_response,My_edges,Mydot_edges,contour=False) axz = varTuning.plot_joint_response(Mz_response,Mz_edges,Mzdot_edges,contour=False) # Set bounds y_mask = My_response.mask.__invert__() if not y_mask.all(): axy.set_ylim(Mydot_edges[np.where(y_mask)[0].min()], Mydot_edges[np.where(y_mask)[0].max()]) axy.set_xlim(My_edges[np.where(y_mask)[1].min()], My_edges[np.where(y_mask)[1].max()]) z_mask = Mz_response.mask.__invert__() if not z_mask.all(): axz.set_ylim(Mzdot_edges[np.where(z_mask)[0].min()], Mzdot_edges[np.where(z_mask)[0].max()]) axz.set_xlim(Mz_edges[np.where(z_mask)[1].min()], Mz_edges[np.where(z_mask)[1].max()]) # other annotations axy.set_title('M$_y$ Phase Plane') axz.set_title('M$_z$ Phase Plane') axy.set_xlabel('M$_y$ ($\mu$N-m)') axy.set_ylabel('M$_\dot{y}$ ($\mu$N-m/ms)') axz.set_xlabel('M$_z$ ($\mu$N-m)') axz.set_ylabel('M$_\dot{z}$ ($\mu$N-m/ms)') axy.grid('off') axy.set_facecolor([0.6, 0.6, 0.6]) axy.axvline(color='k',linewidth=1) axy.axhline(color='k',linewidth=1) axz.grid('off') axz.set_facecolor([0.6, 0.6, 0.6]) axz.axvline(color='k', linewidth=1) axz.axhline(color='k', linewidth=1) plt.sca(axy) plt.tight_layout() if save_tgl: if p_save is None: raise ValueError("figure save location is required") else: plt.savefig(os.path.join(p_save,'{}_My_phaseplane.{}'.format(root,im_ext)),dpi=dpi_res) plt.sca(axz) plt.tight_layout() if save_tgl: if p_save is None: raise ValueError("figure save location is required") else: plt.savefig(os.path.join(p_save,'{}_Mz_phaseplane.{}'.format(root,im_ext)),dpi=dpi_res) plt.close('all')
def calc_world_geom_hist(p_load,p_save,n_bins=100): """ Since calculation takes so long on getting the histograms (mostly loading of data) we want to calculate them once and save the data. This calculates the Geometry. :param p_load: Location where all the neo h5 files live :param p_save: Location to save the output data files :param n_bins: Number of bins in with which to split the data :return None: Saves a 'world_geom_hists.npz' file. """ # init ID = [] all_S_bayes = [] all_TH_bayes = [] all_PHIE_bayes = [] all_ZETA_bayes = [] all_S_edges = [] all_TH_edges = [] all_PHIE_edges = [] all_ZETA_edges = [] # loop files for f in glob.glob(os.path.join(p_load,'rat*.h5')): # load in print(os.path.basename(f)) blk = neoUtils.get_blk(f) # get contact Cbool = neoUtils.get_Cbool(blk) use_flags = neoUtils.concatenate_epochs(blk) # get vars S = neoUtils.get_var(blk, 'S').magnitude TH = neoUtils.get_var(blk, 'TH').magnitude neoUtils.center_var(TH, use_flags) PHIE = neoUtils.get_var(blk, 'PHIE').magnitude neoUtils.center_var(PHIE, use_flags) ZETA = neoUtils.get_var(blk, 'ZETA').magnitude neoUtils.center_var(ZETA, use_flags) # loop units for unit in blk.channel_indexes[-1].units: # get unit info unit_num = int(unit.name[-1]) r, b = neoUtils.get_rate_b(blk, unit_num, sigma=5 * pq.ms) sp = neoUtils.concatenate_sp(blk)['cell_{}'.format(unit_num)] root = neoUtils.get_root(blk,unit_num) ID.append(root) # Create hists S_bayes, S_edges = varTuning.stim_response_hist(S.ravel(), r, Cbool, nbins=n_bins, min_obs=5) TH_bayes, TH_edges = varTuning.stim_response_hist(TH.ravel(), r, Cbool, nbins=n_bins, min_obs=5) PHIE_bayes, PHIE_edges = varTuning.stim_response_hist(PHIE.ravel(), r, Cbool, nbins=n_bins,min_obs=5) ZETA_bayes, ZETA_edges = varTuning.stim_response_hist(ZETA.ravel(), r, Cbool, nbins=n_bins,min_obs=5) # append outputs plt.close('all') all_S_bayes.append(S_bayes) all_TH_bayes.append(TH_bayes) all_PHIE_bayes.append(PHIE_bayes) all_ZETA_bayes.append(ZETA_bayes) all_S_edges.append(S_edges) all_TH_edges.append(TH_edges) all_PHIE_edges.append(PHIE_edges) all_ZETA_edges.append(ZETA_edges) np.savez(os.path.join(p_save, 'world_geom_hists.npz'), all_S_bayes=all_S_bayes, all_TH_bayes=all_TH_bayes, all_PHIE_bayes=all_PHIE_bayes, all_ZETA_bayes=all_ZETA_bayes, all_S_edges=all_S_edges, all_TH_edges=all_TH_edges, all_PHIE_edges=all_PHIE_edges, all_ZETA_edges=all_ZETA_edges, ID=ID )
def calc_all_mech_hists(p_load,p_save,n_bins=100): """ Since calculation takes so long on getting the histograms (mostly loading of data) we want to calculate them once and save the data. This calculates the mechanics. :param p_load: Location where all the neo h5 files live :param p_save: Location to save the output data files :param n_bins: Number of bins in with which to split the data :return None: Saves a 'mech_histograms.npz' file. """ # TODO: This is currently pretty gross, it is really too hardcoded (I wrote it in a car). Do better. # TODO: Combine with geometry # Case in point: all_F_edges = [] all_M_edges = [] all_F_bayes = [] all_M_bayes = [] all_MB_edges = [] all_MD_edges = [] all_MD_bayes = [] all_MB_bayes = [] ID = [] # Loop all neo files for f in glob.glob(os.path.join(p_load,'rat*.h5')): print(os.path.basename(f)) blk = neoUtils.get_blk(f) Cbool = neoUtils.get_Cbool(blk) # Loop all units for unit in blk.channel_indexes[-1].units: unit_num = int(unit.name[-1]) # grab needed variables r, b = neoUtils.get_rate_b(blk, unit_num, sigma=5 * pq.ms) sp = neoUtils.concatenate_sp(blk)['cell_{}'.format(unit_num)] root = neoUtils.get_root(blk,unit_num) M = neoUtils.get_var(blk).magnitude F = neoUtils.get_var(blk,'F').magnitude MB, MD = neoUtils.get_MB_MD(M) # init histograms M_bayes = np.empty([n_bins,3]) F_bayes = np.empty([n_bins, 3]) M_edges = np.empty([n_bins+1, 3]) F_edges = np.empty([n_bins+1, 3]) #calculate tuning curves (seperately on each dimension) for ii in range(3): F_bayes[:, ii], F_edges[:, ii] = varTuning.stim_response_hist(F[:, ii] * 1e6, r, Cbool, nbins=n_bins, min_obs=5) M_bayes[:, ii], M_edges[:, ii] = varTuning.stim_response_hist(M[:, ii] * 1e6, r, Cbool, nbins=n_bins, min_obs=5) MB_bayes, MB_edges = varTuning.stim_response_hist(MB.squeeze() * 1e6, r, Cbool, nbins=n_bins, min_obs=5) MD_bayes, MD_edges,_,_ = varTuning.angular_response_hist(MD.squeeze(), r, Cbool, nbins=n_bins) plt.close('all') # append to output lists all_F_edges.append(F_edges) all_M_edges.append(M_edges) all_MB_edges.append(MB_edges) all_MD_edges.append(MD_edges) all_F_bayes.append(F_bayes) all_M_bayes.append(M_bayes) all_MB_bayes.append(MB_bayes) all_MD_bayes.append(MD_bayes) ID.append(root) # save np.savez(os.path.join(p_save,'mech_histograms.npz'), all_F_bayes=all_F_bayes, all_F_edges=all_F_edges, all_M_bayes=all_M_bayes, all_M_edges=all_M_edges, all_MB_bayes=all_MB_bayes, all_MB_edges=all_MB_edges, all_MD_bayes=all_MD_bayes, all_MD_edges=all_MD_edges, ID=ID )
import sys import neoUtils import matplotlib.pyplot as plt import seaborn as sns import numpy as np sns.set() sns.set_style('ticks') blk = neoUtils.get_blk(sys.argv[1]) M = neoUtils.get_var(blk).magnitude sp = neoUtils.concatenate_sp(blk) cc = neoUtils.concatenate_epochs(blk, -1) Cbool = neoUtils.get_Cbool(blk) c_idx = np.where(Cbool)[0] # M[np.invert(Cbool),:] = 0 ymax = np.nanmax(M) / 4 ymin = np.nanmin(M) / 4 def shadeVector(cc, color='k'): ax = plt.gca() ylim = ax.get_ylim() for start, dur in zip(cc.times.magnitude, cc.durations.magnitude): ax.fill([start, start, start + dur, start + dur], [ylim[0], ylim[1], ylim[1], ylim[0]], color, alpha=0.1) for ii in xrange(len(sp)):
import os sns.set() sns.set_style('ticks') dpi_res, figsize, ext = plotVG3D.set_fig_style() p_load = os.path.join(os.environ['BOX_PATH'], r'__VG3D\_deflection_trials\_NEO') fname = os.path.join(p_load, r'rat2017_03_JAN10_VG_B1_NEO.h5') p_save = os.path.join(os.environ['BOX_PATH'], r'__VG3D\_deflection_trials\_NEO\results') starts = [224600, 298800] stops = [227000, 300750] starts = np.array(starts) * pq.ms stops = np.array(stops) * pq.ms blk = neoUtils.get_blk(fname) sp = neoUtils.concatenate_sp(blk)['cell_0'] cc = neoUtils.concatenate_epochs(blk) cbool = neoUtils.get_Cbool(blk) def shadeVector(cc, color='k', ax=None): if ax is None: ax = plt.gca() ylim = ax.get_ylim() for start, dur in zip(cc.times.magnitude, cc.durations.magnitude): ax.fill([start, start, start + dur, start + dur], [ylim[0], ylim[1], ylim[1], ylim[0]], color, alpha=0.1)