def findHDCells(tuning_curves, ep, spikes, position, cut_off=0.49): """ Peak firing rate larger than 1 and Rayleigh test p<0.001 & z > 100 """ cond1 = pd.DataFrame(tuning_curves.max() > 1.0) angle = position.restrict(ep) from pycircstat.tests import rayleigh stat = pd.DataFrame(index=tuning_curves.columns, columns=['pval', 'z']) for k in tuning_curves: stat.loc[k] = rayleigh(tuning_curves[k].index.values, tuning_curves[k].values) #stat.loc[k]= rayleigh(position['ry'].restrict(ep).values , position['ry'].realign(spikes[k].restrict(ep))) rMean = pd.DataFrame(index=tuning_curves.columns, columns=['hd_score']) for k in tuning_curves: """computes the rayleigh vector length as hdScore. """ spk = spikes[k] spk = spk.restrict(ep) angle_spk = angle.realign(spk) C = np.sum(np.cos(angle_spk.values)) S = np.sum(np.sin(angle_spk.values)) Rmean = np.sqrt(C**2 + S**2) / len(angle_spk) rMean.loc[k] = Rmean stat['hd_score'] = rMean cond2 = pd.DataFrame(np.logical_and(stat['pval'] < 0.001, stat['z'] > 19)) cond3 = pd.DataFrame(rMean['hd_score'] >= cut_off) '''To Do Add cond 4 and set it to any value greater than 0.4''' #cond4=pd.DataFrame(stat['hd_info']) tokeep = (cond1[0] == True) & (cond2[0] == True) & (cond3['hd_score'] == True) stat['hd_cells'] = tokeep #tuning_curves have been normalised by occupancy, unlike the rvector #Rayleigh z test to test the null hypothesis that there is no sample mean direction '''I=pd.DataFrame(index=spikes.keys(),columns=['Ispk']) for i in spikes.keys(): lamda_i=tuning_curves[i].values bins=tuning_curves.index lamda=len(spikes[i].restrict(ep))/ep.tot_length('s') pos=position.restrict(ep) bins=linspace(0,2*pi,60) occu,a=np.histogram(pos, bins) occu= occu/sum(occu) bits_spk=sum(occu*(lamda_i/lamda)*np.log2(lamda_i/lamda)) I.loc[i,'Ispk']=bits_spk stat['hd_info']=I''' return stat
def findHDCells(tuning_curves): """ Peak firing rate larger than 1 and Rayleigh test p<0.001 & z > 100 """ cond1 = tuning_curves.max() > 1.0 from pycircstat.tests import rayleigh stat = pd.DataFrame(index=tuning_curves.columns, columns=['pval', 'z']) for k in tuning_curves: stat.loc[k] = rayleigh(tuning_curves[k].index.values, tuning_curves[k].values) cond2 = np.logical_and(stat['pval'] < 0.001, stat['z'] > 20) tokeep = np.where(np.logical_and(cond1, cond2))[0] return tokeep, stat
def findHDCells(tuning_curves, ep, spikes, position): """ Peak firing rate larger than 1 and Rayleigh test p<0.001 & z > 100 """ cond1 = pd.DataFrame(tuning_curves.max() > 1.0) angle = position.restrict(ep) from pycircstat.tests import rayleigh stat = pd.DataFrame(index=tuning_curves.columns, columns=['pval', 'z']) for k in tuning_curves: stat.loc[k] = rayleigh(tuning_curves[k].index.values, tuning_curves[k].values) #stat.loc[k]= rayleigh(position['ry'].restrict(ep).values , position['ry'].realign(spikes[k].restrict(ep))) rMean = pd.DataFrame(index=tuning_curves.columns, columns=['hd_score']) for k in tuning_curves: """computes the rayleigh vector length as hdScore. """ spk = spikes[k] spk = spk.restrict(ep) angle_spk = angle.realign(spk) C = np.sum(np.cos(angle_spk.values)) S = np.sum(np.sin(angle_spk.values)) Rmean = np.sqrt(C**2 + S**2) / len(angle_spk) rMean.loc[k] = Rmean stat['hd_score'] = rMean cond2 = pd.DataFrame(np.logical_and(stat['pval'] < 0.001, stat['z'] > 40)) cond3 = pd.DataFrame(rMean['hd_score'] >= 0.5) tokeep = (cond1[0] == True) & (cond2[0] == True) & (cond3['hd_score'] == True) stat['hd_cells'] = tokeep
def plot_correlogram(ax, df, tag, direct='A->B', overlap_key='overlap', color='gray', density=False, regress=True, x_dist=True, y_dist=True, ab_key='phaselag_AB', ba_key='phaselag_BA', alpha=0.2, markersize=8, linew=1): """ Parameters ---------- ax : matplotlib.axes._subplots.AxesSubplot df : Dataframe tag : str direct : str Either 'A->B', 'B->A' or 'combined' overlap_key : str Key of the overlap metric color : str Color of the scatter regress : bool x_dist : bool y_dist : bool ab_key : str ba_key : str alpha : float Returns ------- """ overlap = df[overlap_key].to_numpy() phaselag_AB = df[ab_key].to_numpy() phaselag_BA = df[ba_key].to_numpy() # Define x, y if direct == 'A->B': x = overlap y = phaselag_AB elif direct == 'B->A': x = overlap y = phaselag_BA elif direct == 'combined': x = np.concatenate([overlap, overlap]) y = np.concatenate([phaselag_AB, -phaselag_BA]) nan_mask = np.isnan(x) | np.isnan(y) x_nonan, y_nonan = x[~nan_mask], y[~nan_mask] # Scatter or density if density: xx, yy, zz = linear_circular_gauss_density(x_nonan, y_nonan, cir_kappa=3 * np.pi, lin_std=0.05, xbins=200, ybins=800, ybound=(-np.pi, 2 * np.pi)) ax.pcolormesh(xx, yy, zz) else: ax.scatter(x, y, c=color, alpha=alpha, s=markersize, marker='.') ax.scatter(x, y + (2 * np.pi), c=color, alpha=alpha, s=markersize, marker='.') # Plot marginal mean mean_y = circmean(y_nonan) ax.plot([0, 0.2], [mean_y, mean_y], c='k', linewidth=linew) # Regression regress_d = None if regress: regress_d = rcc(x_nonan, y_nonan, abound=[-2, 2]) m, c, r, pval = regress_d['aopt'], regress_d['phi0'], regress_d[ 'rho'], regress_d['p'] r_regress = np.linspace(0, 1, 20) lag_regress = 2 * np.pi * r_regress * m for tmpid, intercept in enumerate( [2 * mul_idx * np.pi + c for mul_idx in [-2, -1, 0, 1, 2]]): if tmpid == 0: ax.plot(r_regress, lag_regress + intercept, c='k', linewidth=linew, label='rho=%0.2f (p%s)' % (r, p2str(pval))) else: ax.plot(r_regress, lag_regress + intercept, c='k', linewidth=linew) # X-axis ax.plot([0, 1], [0, 0], c='k', alpha=1, linewidth=linew) # (y-axis) Interpolation and Smoothened histrogram if y_dist: p, _ = rayleigh(y) yax, yden = circular_density_1d(y_nonan, 10 * np.pi, 60, (-np.pi, 3 * np.pi)) ax.plot(yden / yden.max() * 0.2, yax, c='k', linewidth=linew) # (x-axis) Interpolation and Smoothened histrogram if x_dist: xax, xden = linear_density_1d(x_nonan, std=0.05, bins=100, bound=(0, 1)) ax.plot(xax, xden / xden.max() * (np.pi / 2) - np.pi, c='k', linewidth=linew) # Set title # ax.set_title("%s %s (n=%d)" % (tag, direct, num_pairs), fontsize=fontsize) ax.set_ylim(-np.pi, 2 * np.pi) ax.set_yticks([-np.pi, 0, np.pi, np.pi * 2]) _ = ax.set_yticklabels(['$-\pi$', '0', '$\pi$', '$2\pi$']) return ax, x, y, regress_d