added python files for figures
This commit is contained in:
199
modelsusceptlown.py
Normal file
199
modelsusceptlown.py
Normal file
@@ -0,0 +1,199 @@
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from scipy.stats import pearsonr, linregress, gaussian_kde
|
||||
from thunderlab.tabledata import TableData
|
||||
from pathlib import Path
|
||||
from plotstyle import plot_style, labels_params, significance_str
|
||||
|
||||
|
||||
data_path = Path('newdata3')
|
||||
|
||||
|
||||
def sort_files(cell_name, all_files, n):
|
||||
files = [fn for fn in all_files if '-'.join(fn.stem.split('-')[2:-n]) == cell_name]
|
||||
if len(files) == 0:
|
||||
return None, 0
|
||||
nums = [int(fn.stem.split('-')[-1]) for fn in files]
|
||||
idxs = np.argsort(nums)
|
||||
files = [files[i] for i in idxs]
|
||||
nums = [nums[i] for i in idxs]
|
||||
return files, nums
|
||||
|
||||
|
||||
def plot_chi2(ax, s, data_file):
|
||||
data = np.load(data_file)
|
||||
n = data['n']
|
||||
alpha = data['alpha']
|
||||
freqs = data['freqs']
|
||||
pss = data['pss']
|
||||
dt_fix = 1 # 0.0005
|
||||
prss = np.abs(data['prss'])/dt_fix*0.5/np.sqrt(pss.reshape(1, -1)*pss.reshape(-1, 1))
|
||||
ax.set_visible(True)
|
||||
ax.set_aspect('equal')
|
||||
i0 = np.argmin(freqs < -300)
|
||||
i0 = np.argmin(freqs < 0)
|
||||
i1 = np.argmax(freqs > 300)
|
||||
if i1 == 0:
|
||||
i1 = len(freqs)
|
||||
freqs = freqs[i0:i1]
|
||||
prss = prss[i0:i1, i0:i1]
|
||||
vmax = np.quantile(prss, 0.996)
|
||||
ten = 10**np.floor(np.log10(vmax))
|
||||
for fac, delta in zip([1, 2, 3, 4, 6, 8, 10],
|
||||
[0.5, 1, 1, 2, 3, 4, 5]):
|
||||
if fac*ten >= vmax:
|
||||
vmax = fac*ten
|
||||
ten *= delta
|
||||
break
|
||||
pc = ax.pcolormesh(freqs, freqs, prss, vmin=0, vmax=vmax,
|
||||
cmap='viridis', rasterized=True)
|
||||
ns = f'$N={n}$' if n <= 100 else f'$N=10^{np.log10(n):.0f}$'
|
||||
if 'noise_frac' in data:
|
||||
ax.set_title(f'$c$=0\\,\\%, {ns}', fontsize='medium')
|
||||
else:
|
||||
ax.set_title(f'$c$={100*alpha:g}\\,\\%, {ns}', fontsize='medium')
|
||||
ax.set_xlim(0, 300)
|
||||
ax.set_ylim(0, 300)
|
||||
ax.set_xticks_delta(100)
|
||||
ax.set_yticks_delta(100)
|
||||
ax.set_xlabel('$f_1$', 'Hz')
|
||||
ax.set_ylabel('$f_2$', 'Hz')
|
||||
cax = ax.inset_axes([1.04, 0, 0.05, 1])
|
||||
cax.set_spines_outward('lrbt', 0)
|
||||
if alpha == 0.1:
|
||||
cb = fig.colorbar(pc, cax=cax, label=r'$|\chi_2|$ [Hz]')
|
||||
else:
|
||||
cb = fig.colorbar(pc, cax=cax)
|
||||
cb.outline.set_color('none')
|
||||
cb.outline.set_linewidth(0)
|
||||
cax.set_yticks_delta(ten)
|
||||
|
||||
|
||||
def plot_chi2_contrasts(axs, s, cell_name, n=None):
|
||||
print(cell_name)
|
||||
files, nums = sort_files(cell_name,
|
||||
data_path.glob(f'chi2-split-{cell_name}-*.npz'), 1)
|
||||
idx = -1 if n is None else nums.index(n)
|
||||
plot_chi2(axs[0], s, files[idx])
|
||||
for k, alphastr in enumerate(['010', '030', '100']):
|
||||
files, nums = sort_files(cell_name,
|
||||
data_path.glob(f'chi2-noisen-{cell_name}-{alphastr}-*.npz'), 2)
|
||||
idx = -1 if n is None else nums.index(n)
|
||||
plot_chi2(axs[k + 1], s, files[idx])
|
||||
|
||||
|
||||
def plot_nli_diags(ax, s, data, alphax, alphay, xthresh, ythresh, cell_name):
|
||||
datax = data[data('contrast') == alphax, :]
|
||||
datay = data[data('contrast') == alphay, :]
|
||||
nlix = datax('dnli')
|
||||
nliy = datay('dnli100')
|
||||
nfp = np.sum((nliy > ythresh) & (nlix < xthresh))
|
||||
ntp = np.sum((nliy > ythresh) & (nlix > xthresh))
|
||||
ntn = np.sum((nliy < ythresh) & (nlix < xthresh))
|
||||
nfn = np.sum((nliy < ythresh) & (nlix > xthresh))
|
||||
print(f' {ntp:2d} ({100*ntp/len(nlix):2.0f}%) true positive')
|
||||
print(f' {nfp:2d} ({100*nfp/len(nlix):2.0f}%) false positive')
|
||||
print(f' {ntn:2d} ({100*ntn/len(nlix):2.0f}%) true negative')
|
||||
print(f' {nfn:2d} ({100*nfn/len(nlix):2.0f}%) false negative')
|
||||
r, p = pearsonr(nlix, nliy)
|
||||
l = linregress(nlix, nliy)
|
||||
x = np.linspace(0, 10, 10)
|
||||
ax.set_visible(True)
|
||||
ax.set_title(f'$c$={100*alphay:g}\\,\\%', fontsize='medium')
|
||||
ax.plot(x, x, **s.lsLine)
|
||||
ax.plot(x, l.slope*x + l.intercept, **s.lsGrid)
|
||||
ax.axhline(ythresh, **s.lsLine)
|
||||
ax.axvline(xthresh, 0, 0.5, **s.lsLine)
|
||||
if alphax == 0:
|
||||
mask = datax('triangle') > 0.5
|
||||
ax.plot(nlix[mask], nliy[mask], zorder=30, label='strong', **s.psA1m)
|
||||
mask = datax('border') > 0.5
|
||||
ax.plot(nliy[mask], nliy[mask], zorder=20, label='weak', **s.psA2m)
|
||||
ax.plot(nlix, nliy, zorder=10, label='none', **s.psB1m)
|
||||
# mark cell:
|
||||
mask = datax('cell') == cell_name
|
||||
color = s.psB1m['color']
|
||||
if alphax == 0:
|
||||
if datax[mask, 'border']:
|
||||
color = s.psA2m['color']
|
||||
elif datax[mask, 'triangle']:
|
||||
color = s.psA1m['color']
|
||||
ax.plot(nlix[mask], nliy[mask], zorder=40, marker='o',
|
||||
ms=s.psB1m['markersize'], mfc=color, mec='k', mew=0.8)
|
||||
|
||||
box = dict(boxstyle='square,pad=0.1', fc='white', ec='none')
|
||||
ax.text(1.0, 0.0, f'{ntn}', ha='right', fontsize='small', bbox=box)
|
||||
ax.text(7.5, 0.0, f'{nfn}', ha='right', fontsize='small', bbox=box)
|
||||
ax.text(1.0, 3.7, f'{nfp}', ha='right', fontsize='small', bbox=box)
|
||||
ax.text(7.5, 3.7, f'{ntp}', ha='right', fontsize='small', bbox=box)
|
||||
ax.set_ylim(0, 9)
|
||||
ax.set_xlim(0, 9)
|
||||
n = datax[0, 'nsegs']
|
||||
if alphax == 0:
|
||||
ax.set_xlabel(f'SI, $c=0$, $N=10^{np.log10(n):.0f}$')
|
||||
else:
|
||||
ax.set_xlabel(f'SI, $N=10^{np.log10(n):.0f}$')
|
||||
ax.set_ylabel('SI, $N=100$')
|
||||
ax.set_xticks_delta(4)
|
||||
ax.set_yticks_delta(4)
|
||||
ax.set_minor_xticks_delta(1)
|
||||
ax.set_minor_yticks_delta(1)
|
||||
ax.text(0, 0.9, f'$r={r:.2f}$', transform=ax.transAxes, fontsize='small')
|
||||
ax.text(0, 0.7, significance_str(p), transform=ax.transAxes,
|
||||
fontsize='small')
|
||||
if alphax == 0 and alphay == 0.01:
|
||||
ax.legend(loc='upper left', bbox_to_anchor=(-1.5, 1),
|
||||
title='triangle', handlelength=0.5,
|
||||
handletextpad=0.5, labelspacing=0.2)
|
||||
|
||||
kde = gaussian_kde(nliy, 0.15/np.std(nliy, ddof=1))
|
||||
nli = np.linspace(0, 8, 100)
|
||||
pdf = kde(nli)
|
||||
dax = ax.inset_axes([1.04, 0, 0.3, 1])
|
||||
dax.show_spines('')
|
||||
dax.fill_betweenx(nli, pdf, **s.fsB1a)
|
||||
dax.plot(pdf, nli, clip_on=False, **s.lsB1m)
|
||||
|
||||
|
||||
def plot_summary_contrasts(axs, s, xthresh, ythresh, cell_name):
|
||||
print(f'against contrast with thresholds: x={xthresh} and y={ythresh}')
|
||||
data = TableData('summarychi2noise.csv')
|
||||
for i, a in enumerate([0.01, 0.03, 0.1]):
|
||||
print(f'contrast {100*a:2g}%:')
|
||||
plot_nli_diags(axs[1 + i], s, data, a, a, xthresh, ythresh, cell_name)
|
||||
print()
|
||||
|
||||
|
||||
def plot_summary_diags(axs, s, xthresh, ythresh, cell_name):
|
||||
print(f'against split with thresholds: x={xthresh} and y={ythresh}')
|
||||
data = TableData('summarychi2noise.csv')
|
||||
for i, a in enumerate([0.01, 0.03, 0.1]):
|
||||
print(f'contrast {100*a:2g}%:')
|
||||
plot_nli_diags(axs[1 + i], s, data, 0, a, xthresh, ythresh, cell_name)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
xthresh = 1.2
|
||||
ythresh = 1.8
|
||||
s = plot_style()
|
||||
fig, axs = plt.subplots(6, 4, cmsize=(s.plot_width, 0.85*s.plot_width),
|
||||
height_ratios=[1, 1, 0, 1, 0, 1])
|
||||
fig.subplots_adjust(leftm=7, rightm=8, topm=2, bottomm=3.5,
|
||||
wspace=1, hspace=1)
|
||||
for ax in axs.flat:
|
||||
ax.set_visible(False)
|
||||
cell_name = '2012-12-21-ak-invivo-1'
|
||||
plot_chi2_contrasts(axs[0], s, cell_name)
|
||||
plot_chi2_contrasts(axs[1], s, cell_name, 10)
|
||||
for k in range(2):
|
||||
fig.common_yticks(axs[k, :])
|
||||
for k in range(4):
|
||||
fig.common_xticks(axs[:2, k])
|
||||
plot_summary_contrasts(axs[3], s, xthresh, ythresh, cell_name)
|
||||
plot_summary_diags(axs[5], s, xthresh, ythresh, cell_name)
|
||||
fig.common_yticks(axs[3, 1:])
|
||||
fig.common_yticks(axs[5, 1:])
|
||||
fig.tag(axs, xoffs=-4.5, yoffs=1.8)
|
||||
axs[1, 0].set_visible(False)
|
||||
#plt.show()
|
||||
fig.savefig()
|
||||
Reference in New Issue
Block a user