def mult_join_plots(var1,var2,r,cbool,bins=50): if type(bins)==int: bins = [np.linspace(np.nanmin(var1), np.nanmax(var1), bins), np.linspace(np.nanmin(var2), np.nanmax(var2), bins)] R = {} xx=[] yy=[] for ii in range(var1.shape[-1]): R[ii] = varTuning.joint_response_hist(var1[:,ii],var2[:,ii],r,cbool,bins=bins)[0] for mesh in R.values(): rows = np.where(np.any(~mesh.mask, axis=0))[0] cols = np.where(np.any(~mesh.mask, axis=1))[0] xx.append([rows[0],rows[-1]]) yy.append([cols[0], cols[-1]]) xx = [np.min(xx), np.max(xx)] yy = [np.min(yy), np.max(yy)] return(R,bins,xx,yy)
def plot_pca_spaces(fname, unit_num, p_smooth=None, deriv_smooth=[9], n_dims=3): """ Plot the PCA tuning spaces :param fname: Filename of the neo data :param unit_num: unit number to use :param p_smooth: [optional] If using derivative, this is where the smooth data live :param deriv_smooth: If using derivative, tells us what derivative smoothing to use :return: """ # Get the standard data, from which the PCA will be computed blk = neoUtils.get_blk(fname) cbool = neoUtils.get_Cbool(blk) varlist = ['M', 'F', 'TH', 'PHIE'] X = GLM.create_design_matrix(blk, varlist) sp = neoUtils.concatenate_sp(blk)['cell_{}'.format(unit_num)] r = neoUtils.get_rate_b(blk, unit_num)[0] # If smoothing directory is given, then add the derivative data if p_smooth is not None: blk_smooth = GLM.get_blk_smooth(fname, p_smooth) Xdot = GLM.get_deriv(blk, blk_smooth, varlist, deriv_smooth)[0] X = np.concatenate([X, Xdot], axis=1) X[np.isnan(X)] = 0 else: print('\tNot using derivative information') scaler = sklearn.preprocessing.StandardScaler(with_mean=False) X_scale = np.zeros_like(X) X_scale[cbool, :] = scaler.fit_transform(X[cbool, :]) pca = sklearn.decomposition.PCA() X_pcs = np.zeros_like(X) X_pcs[cbool, :] = pca.fit_transform(X_scale[cbool, :]) for ii in range(n_dims): var = X_pcs[:, ii] response, edges = varTuning.stim_response_hist(var, sp, cbool) response, edges1, edges2 = varTuning.joint_response_hist( X_pcs[:, 0], X_pcs[:, 1], sp, cbool, 40) response, edges1, edges2 = varTuning.joint_response_hist( X_pcs[:, -1], X_pcs[:, -2], sp, cbool, 40)
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 plot_single_joint_space(var1, var2, r, cbool, bins, bin_stretch=True, ax=None): idx = np.logical_and(np.isfinite(var1), np.isfinite(var2)).ravel() if bin_stretch: var1s = np.empty(var1.shape[0]) var2s = np.empty(var2.shape[0]) var1s[idx], logit_y = nl(var1[idx], 90) var2s[idx], logit_z = nl(var2[idx], 90) else: var1s = var1 var2s = var2 response, var1_edges, var2_edges = varTuning.joint_response_hist( var1s, var2s, r, cbool, bins=bins, min_obs=10) if bin_stretch: var1_edges = logit_y(var1_edges) var2_edges = logit_z(var2_edges) else: pass h = plt.pcolormesh(var1_edges[:-1], var2_edges[:-1], response, cmap='OrRd') if ax is None: ax = plt.gca() 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()]) return (h)
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')
vard_names = [item for sublist in vard_names for item in sublist] return (var_names + vard_names) spt = neo.SpikeTrain(np.where(y)[0] * pq.ms, sampling_rate=pq.kHz, t_stop=y.shape[0] * pq.ms) if kernel is not None: kernel = elephant.kernels.GaussianKernel(kernel * pq.ms) rate = elephant.statistics.instantaneous_rate( spt, sampling_period=pq.ms, kernel=kernel).magnitude.ravel() rate = rate / 1000. else: rate = y pred_response, edges1, edges2 = varTuning.joint_response_hist( X[:, 6], X[:, 7], yhat, cbool) obs_response, edges1, edges2 = varTuning.joint_response_hist( X[:, 6], X[:, 7], rate, cbool) max_r = np.max([np.max(pred_response), np.max(obs_response)]) # ================== plt.figure() plt.subplot(121) h = plt.pcolormesh(edges1[:-1], edges2[:-1], pred_response, vmin=0, vmax=max_r) plt.ylabel('$\\Delta\\phi$') plt.xlabel('$\\Delta\\theta$') plt.axvline(color='k') plt.axhline(color='k') plt.axis('square') plt.title('Predicted') plt.subplot(122) plt.pcolormesh(edges1[:-1], edges2[:-1], obs_response, vmin=0, vmax=max_r)