updated model figures to new analysis with the right units

This commit is contained in:
2025-05-25 12:29:46 +02:00
parent e785d51b18
commit e87d63c46b
5 changed files with 219 additions and 351 deletions

View File

@@ -3,8 +3,9 @@ import matplotlib.pyplot as plt
from scipy.stats import pearsonr, linregress, gaussian_kde
from thunderlab.tabledata import TableData
from pathlib import Path
from spectral import whitenoise, diag_projection, peak_size
from plotstyle import plot_style, labels_params, significance_str
from plotstyle import plot_style, labels_params
from plotstyle import noise_files, plot_chi2, significance_str
from modelsusceptcontrasts import load_chi2
model_cell = '2012-12-21-ak-invivo-1'
@@ -13,103 +14,44 @@ data_path = Path('data')
sims_path = data_path / 'simulations'
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, rate):
fcutoff = 300
data = np.load(data_file)
n = data['n']
alpha = data['alpha']
freqs = data['freqs']
pss = data['pss']
prss = data['prss']
chi2 = np.abs(prss)*0.5/(pss.reshape(1, -1)*pss.reshape(-1, 1))
ax.set_visible(True)
ax.set_aspect('equal')
i0 = np.argmin(freqs < -fcutoff)
i0 = np.argmin(freqs < 0)
i1 = np.argmax(freqs > fcutoff)
if i1 == 0:
i1 = len(freqs)
freqs = freqs[i0:i1]
chi2 = chi2[i0:i1, i0:i1]
vmax = np.quantile(chi2, 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, chi2, vmin=0, vmax=vmax,
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, fcutoff)
ax.set_ylim(0, fcutoff)
ax.set_xticks_delta(100)
ax.set_yticks_delta(100)
ax.set_xlabel('$f_1$', 'Hz')
ax.set_ylabel('$f_2$', 'Hz')
dfreqs, diag = diag_projection(freqs, chi2, 2*fcutoff)
sinorm, sirel, sif = peak_size(dfreqs, diag, rate, median=False)
ax.text(0.95, 0.88, f'SI($r$)={sinorm:.1f}', ha='right', zorder=50,
color='white', fontsize='medium', transform=ax.transAxes)
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):
d = sims_path / f'baseline-{cell_name}.npz'
def plot_chi2_contrasts(axs, s, cell_name, nsegs=None):
d = sims_path / f'{cell_name}-baseline.npz'
data = np.load(d)
rate = float(data['rate'])
cv = float(data['cv'])
print(f' {cell_name}: r={rate:3.0f}Hz, CV={cv:4.2f}')
files, nums = sort_files(cell_name,
sims_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], rate)
for k, alphastr in enumerate(['010', '030', '100']):
files, nums = sort_files(cell_name,
sims_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], rate)
freqs, chi2, fcutoff, contrast, n = load_chi2(sims_path, cell_name,
None, nsegs)
ns = f'$N={n}$' if n < 1000 else f'$N=10^{np.log10(n):.0f}$'
cax = plot_chi2(axs[0], s, freqs, chi2, fcutoff, rate)
cax.set_ylabel('')
axs[0].set_title(f'$c$=0\\,\\%, {ns}', fontsize='medium')
for k, alpha in enumerate([0.01, 0.03, 0.1]):
freqs, chi2, fcutoff, contrast, n = load_chi2(sims_path, cell_name,
alpha, nsegs)
ns = f'$N={n}$' if n < 1000 else f'$N=10^{np.log10(n):.0f}$'
cax = plot_chi2(axs[k + 1], s, freqs, chi2, fcutoff, rate)
if alpha < 0.1:
cax.set_ylabel('')
axs[k + 1].set_title(f'$c$={100*alpha:g}\\,\\%, {ns}',
fontsize='medium')
def plot_nli_diags(ax, s, data, alphax, alphay, xthresh, ythresh, cell_name):
def plot_si_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)
six = datax['dsinorm']
siy = datay['dsinorm100']
nfp = np.sum((siy > ythresh) & (six < xthresh))
ntp = np.sum((siy > ythresh) & (six > xthresh))
ntn = np.sum((siy < ythresh) & (six < xthresh))
nfn = np.sum((siy < ythresh) & (six > xthresh))
print(f' {ntp:2d} ({100*ntp/len(six):2.0f}%) true positive')
print(f' {nfp:2d} ({100*nfp/len(six):2.0f}%) false positive')
print(f' {ntn:2d} ({100*ntn/len(six):2.0f}%) true negative')
print(f' {nfn:2d} ({100*nfn/len(six):2.0f}%) false negative')
r, p = pearsonr(six, siy)
l = linregress(six, siy)
x = np.linspace(0, 10, 10)
ax.set_visible(True)
ax.set_title(f'$c$={100*alphay:g}\\,\\%', fontsize='medium')
@@ -119,10 +61,10 @@ def plot_nli_diags(ax, s, data, alphax, alphay, xthresh, ythresh, cell_name):
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)
ax.plot(six[mask], siy[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)
ax.plot(siy[mask], siy[mask], zorder=20, label='weak', **s.psA2m)
ax.plot(six, siy, zorder=10, label='none', **s.psB1m)
# mark cell:
mask = datax['cell'] == cell_name
color = s.psB1m['color']
@@ -131,7 +73,7 @@ def plot_nli_diags(ax, s, data, alphax, alphay, xthresh, ythresh, cell_name):
color = s.psA2m['color']
elif datax[mask, 'triangle']:
color = s.psA1m['color']
ax.plot(nlix[mask], nliy[mask], zorder=40, marker='o',
ax.plot(six[mask], siy[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')
@@ -159,13 +101,13 @@ def plot_nli_diags(ax, s, data, alphax, alphay, xthresh, ythresh, cell_name):
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)
kde = gaussian_kde(siy, 0.15/np.std(siy, ddof=1))
si = np.linspace(0, 8, 100)
pdf = kde(si)
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)
dax.fill_betweenx(si, pdf, **s.fsB1a)
dax.plot(pdf, si, clip_on=False, **s.lsB1m)
def plot_summary_contrasts(axs, s, xthresh, ythresh, cell_name):
@@ -173,7 +115,7 @@ def plot_summary_contrasts(axs, s, xthresh, ythresh, cell_name):
data = TableData(data_path / 'Apteronotus_leptorhynchus-Punit-models.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)
plot_si_diags(axs[1 + i], s, data, a, a, xthresh, ythresh, cell_name)
print()
@@ -182,7 +124,7 @@ def plot_summary_diags(axs, s, xthresh, ythresh, cell_name):
data = TableData(data_path / 'Apteronotus_leptorhynchus-Punit-models.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)
plot_si_diags(axs[1 + i], s, data, 0, a, xthresh, ythresh, cell_name)
if __name__ == '__main__':
@@ -211,4 +153,3 @@ if __name__ == '__main__':
fig.tag(axs, xoffs=-4.5, yoffs=1.8)
axs[1, 0].set_visible(False)
fig.savefig()
print()