322 lines
12 KiB
Python
322 lines
12 KiB
Python
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
from pathlib import Path
|
|
from spectral import whitenoise, diag_projection, peakedness
|
|
from plotstyle import plot_style
|
|
|
|
|
|
#example_cell = ['2012-07-03-ak-invivo-1', 0]
|
|
example_cell = ['2017-07-18-ai-invivo-1', 1] # Take this! at 3% model, 5% data
|
|
model_cell = example_cell
|
|
|
|
base_path = Path('data')
|
|
data_path = base_path / 'cells'
|
|
sims_path = base_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, freqs, chi2, nsegs, rate):
|
|
fcutoff = 300
|
|
ax.set_aspect('equal')
|
|
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 = prev_fac*ten
|
|
ten *= prev_delta
|
|
break
|
|
prev_fac = fac
|
|
prev_delta = delta
|
|
pc = ax.pcolormesh(freqs, freqs, chi2, vmin=0, vmax=vmax,
|
|
rasterized=True)
|
|
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')
|
|
if nsegs < 10000:
|
|
ax.text(1, 1.1, f'$N={nsegs}$',
|
|
ha='right', transform=ax.transAxes)
|
|
else:
|
|
ax.text(1, 1.1, f'$N=10^{{{np.log10(nsegs):.0f}}}$',
|
|
ha='right', transform=ax.transAxes)
|
|
dfreqs, diag = diag_projection(freqs, chi2, 2*fcutoff)
|
|
nli, nlif = peakedness(dfreqs, diag, rate, median=False)
|
|
ax.text(0.95, 0.88, f'SI($r$)={nli:.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)
|
|
cb = fig.colorbar(pc, cax=cax)
|
|
cb.outline.set_color('none')
|
|
cb.outline.set_linewidth(0)
|
|
cax.set_ylabel(r'$|\chi_2|$ [Hz]')
|
|
cax.set_yticks_delta(ten)
|
|
return cax
|
|
|
|
|
|
def plot_overn(ax, s, files, nmax=1e6):
|
|
fcutoff = 300
|
|
ns = []
|
|
stats = []
|
|
for fname in files:
|
|
data = np.load(fname)
|
|
n = data['n']
|
|
if nmax is not None and n > nmax:
|
|
continue
|
|
alpha = data['alpha']
|
|
freqs = data['freqs']
|
|
pss = data['pss']
|
|
dt_fix = 1 # 0.0005
|
|
chi2 = np.abs(data['prss'])/dt_fix*0.5/np.sqrt(pss.reshape(1, -1)*pss.reshape(-1, 1))
|
|
ns.append(n)
|
|
i0 = np.argmin(freqs < 0)
|
|
i1 = np.argmax(freqs > fcutoff)
|
|
if i1 == 0:
|
|
i1 = len(freqs)
|
|
chi2 = chi2[i0:i1, i0:i1]
|
|
stats.append(np.quantile(chi2, [0, 0.001, 0.05, 0.25, 0.5,
|
|
0.75, 0.95, 0.998, 1.0]))
|
|
ns = np.array(ns)
|
|
stats = np.array(stats)
|
|
indx = np.argsort(ns)
|
|
ns = ns[indx]
|
|
stats = stats[indx]
|
|
ax.set_visible(True)
|
|
ax.plot(ns, stats[:, 7], zorder=50, label='99.8\\%', **s.lsMax)
|
|
ax.fill_between(ns, stats[:, 2], stats[:, 6], fc='0.85', zorder=40, label='5--95\\%')
|
|
ax.fill_between(ns, stats[:, 3], stats[:, 5], fc='0.5', zorder=45, label='25-75\\%')
|
|
ax.plot(ns, stats[:, 4], zorder=50, label='median', **s.lsMedian)
|
|
#ax.plot(ns, stats[:, 8], '0.0')
|
|
ax.set_xlim(1e2, nmax)
|
|
ax.set_xscale('log')
|
|
ax.set_yscale('log')
|
|
ax.set_yticks_log(numticks=5)
|
|
if nmax > 1e6:
|
|
ax.set_ylim(3e-1, 5e3)
|
|
ax.set_minor_yticks_log(numticks=5)
|
|
ax.set_xticks_log(numticks=4)
|
|
ax.set_minor_xticks_log(numticks=8)
|
|
else:
|
|
ax.set_ylim(4e0, 1.3e3)
|
|
#ax.set_minor_yticks_log(numticks=5)
|
|
ax.set_minor_yticks_off()
|
|
ax.set_xticks_log(numticks=5)
|
|
#ax.set_minor_xticks_log(numticks=6)
|
|
ax.set_xlabel('segments')
|
|
ax.set_ylabel('$|\\chi_2|$ [Hz]')
|
|
|
|
|
|
def plot_chi2_contrast(ax1, ax2, s, files, nums, nsmall, nlarge, rate):
|
|
for ax, n in zip([ax1, ax2], [nsmall, nlarge]):
|
|
i = nums.index(n)
|
|
data = np.load(files[i])
|
|
n = data['n']
|
|
alpha = data['alpha']
|
|
freqs = data['freqs']
|
|
pss = data['pss']
|
|
chi2 = np.abs(data['prss'])*0.5/np.sqrt(pss.reshape(1, -1)*pss.reshape(-1, 1))
|
|
cax = plot_chi2(ax, s, freqs, chi2, n, rate)
|
|
cax.set_ylabel('')
|
|
print(f'Modeled cell {"-".join(files[i].name.split("-")[2:-2])} at {100*alpha:4.1f}% contrast: noise_frac={1:3.1f}, nsegs={n}')
|
|
print()
|
|
|
|
|
|
def plot_chi2_split(ax1, ax2, s, files, nums, nsmall, nlarge, rate):
|
|
for ax, n in zip([ax1, ax2], [nsmall, nlarge]):
|
|
i = nums.index(n)
|
|
data = np.load(files[i])
|
|
n = data['n']
|
|
alpha = data['alpha']
|
|
noise_frac = data['noise_frac']
|
|
freqs = data['freqs']
|
|
pss = data['pss']
|
|
chi2 = np.abs(data['prss'])*0.5/np.sqrt(pss.reshape(1, -1)*pss.reshape(-1, 1))
|
|
cax = plot_chi2(ax, s, freqs, chi2, n, rate)
|
|
cax.set_ylabel('')
|
|
print(f'Modeled cell {"-".join(files[i].name.split("-")[2:-1])} at {100*alpha:4.1f}% contrast: noise_frac={noise_frac:3.1f}, nsegs={n}')
|
|
print()
|
|
return alpha, noise_frac
|
|
|
|
|
|
def plot_chi2_data(ax, s, cell_name, run):
|
|
data_file = data_path / f'{cell_name}-baseline.npz'
|
|
data = np.load(data_file)
|
|
eodf = float(data['eodf'])
|
|
ratebase = float(data['ratebase/Hz'])
|
|
cvbase = float(data['cvbase'])
|
|
data_file = data_path / f'{cell_name}-spectral-s{run:02d}.npz'
|
|
data = np.load(data_file)
|
|
n = data['n']
|
|
nfft = data['nfft']
|
|
deltat = data['deltat']
|
|
alpha = data['alpha']
|
|
freqs = data['freqs']
|
|
pss = data['pss']
|
|
chi2 = np.abs(data['prss'])*0.5/np.sqrt(pss.reshape(1, -1)*pss.reshape(-1, 1))
|
|
print(f'Measured cell {"-".join(data_file.name.split("-")[:-2])} at {100*alpha:4.1f}% contrast: r={ratebase:3.0f}Hz, CV={cvbase:4.2f}, dt={1000*deltat:4.2f}ms, nfft={nfft}, win={1000*deltat*nfft:6.1f}ms, nsegs={n}')
|
|
print()
|
|
plot_chi2(ax, s, freqs, chi2, n, ratebase)
|
|
return alpha, ratebase, eodf
|
|
|
|
|
|
def plot_ram(ax, contrast, eodf, wtime, wnoise):
|
|
tmax = 50
|
|
am = 1 + contrast*wnoise
|
|
eod = np.sin(2*np.pi*eodf*wtime)*am
|
|
|
|
ax.show_spines('l')
|
|
ax.plot(1e3*wtime, eod, clip_on=False, **s.lsEOD)
|
|
ax.plot(1e3*wtime, +am, clip_on=False, **s.lsAM)
|
|
ax.plot(1e3*wtime, -am, clip_on=False, **s.lsAM)
|
|
ax.set_xlim(0, tmax)
|
|
ax.set_ylim(-1.3, 1.3)
|
|
ax.set_yticks_delta(1)
|
|
ax.set_ylabel('EOD')
|
|
ax.text(1, 1, f'RAM ($c={100*contrast:.0f}$\\,\\%)', ha='right',
|
|
transform=ax.transAxes, color=s.lsAM['color'])
|
|
|
|
|
|
def plot_noise_split(ax, contrast, noise_contrast, noise_frac,
|
|
wtime, wnoise):
|
|
axr, axs, axn = ax.subplots(3, 1, hspace=0.2)
|
|
cmax = 26
|
|
cdelta = 20
|
|
tmax = 50
|
|
|
|
axr.show_spines('l')
|
|
axr.axhline(0, **s.lsGrid)
|
|
axr.plot(1e3*wtime, 100*contrast*wnoise, clip_on=False, **s.lsAM)
|
|
axr.set_xlim(0, tmax)
|
|
axr.set_ylim(-cmax, cmax)
|
|
axr.set_yticks_delta(cdelta)
|
|
axr.set_ylabel('\\%')
|
|
axr.text(1, 1, f'RAM ($c={100*contrast:.0f}$\\,\\%)', ha='right',
|
|
transform=axr.transAxes, color=s.lsAM['color'])
|
|
|
|
axs.show_spines('l')
|
|
axs.axhline(0, **s.lsGrid)
|
|
axs.plot(1e3*wtime, 100*noise_contrast*wnoise, clip_on=False, **s.lsAMsplit)
|
|
axs.set_xlim(0, tmax)
|
|
axs.set_ylim(-cmax, cmax)
|
|
axs.set_yticks_delta(cdelta)
|
|
axs.set_ylabel('\\%')
|
|
if noise_contrast > 0:
|
|
axs.text(1, 1, f'$s_{{\\xi}}(t)$ ($c={100*noise_contrast:.0f}$\\,\\%)',
|
|
ha='right', transform=axs.transAxes,
|
|
color=s.lsAMsplit['color'])
|
|
|
|
ntime = np.linspace(0, 1e-3*tmax, 800)
|
|
rng = np.random.default_rng(45432)
|
|
nnoise = rng.normal(size=len(ntime))
|
|
axn.show_spines('l')
|
|
axn.axhline(0, **s.lsGrid)
|
|
axn.plot(1e3*ntime, noise_frac*nnoise, clip_on=False, **s.lsNoise)
|
|
axn.set_ylim(-2, 2)
|
|
axn.set_xlim(0, tmax)
|
|
axn.set_yticks_delta(5)
|
|
axn.set_yticks_blank()
|
|
#axn.set_xticks_delta(25)
|
|
#axn.set_xlabel('Time', 'ms')
|
|
y = 0.8 if noise_frac < 1 else 1.2
|
|
axn.text(1, y, f'Intrinsic noise (${100*noise_frac:.0f}$\\,\\%)',
|
|
ha='right', transform=axn.transAxes, color=s.lsNoise['color'])
|
|
if noise_frac < 1:
|
|
axn.xscalebar(1, -0.1, 10, 'ms', ha='right')
|
|
|
|
return axr
|
|
|
|
|
|
if __name__ == '__main__':
|
|
nsmall = 100
|
|
nlarge = 1000000
|
|
contrast = 0.01
|
|
|
|
wdt = 0.0001
|
|
wnoise = whitenoise(0, 300, wdt, 0.05, rng=np.random.default_rng(51234))
|
|
wtime = np.arange(len(wnoise))*wdt
|
|
|
|
s = plot_style()
|
|
fig, axs = plt.subplots(4, 4, cmsize=(s.plot_width, 0.83*s.plot_width),
|
|
width_ratios=[1, 0, 1, 1, 0.15, 1])
|
|
fig.subplots_adjust(leftm=8, rightm=1.5, topm=3.5, bottomm=4,
|
|
wspace=0.25, hspace=0.6)
|
|
axs[0, 2].set_visible(False)
|
|
axs[0, 3].set_visible(False)
|
|
xt = -2.25
|
|
yt = 1.25
|
|
|
|
# data:
|
|
axss = axs[0]
|
|
axss[1].text(xt, yt, 'P-unit data', fontsize='large',
|
|
transform=axss[1].transAxes, color=s.punit_color1)
|
|
data_contrast, ratebase, eodf = plot_chi2_data(axss[1], s, example_cell[0],
|
|
example_cell[1])
|
|
plot_ram(axss[0], data_contrast, eodf, wtime, wnoise)
|
|
axss[1].text(xt + 0.9, yt, f'$r={ratebase:.0f}$\\,Hz',
|
|
transform=axss[1].transAxes, fontsize='large')
|
|
|
|
# model 5%:
|
|
axss = axs[1]
|
|
data_files = sims_path.glob(f'chi2-noisen-{example_cell[0]}-{1000*data_contrast:03.0f}-*.npz')
|
|
files, nums = sort_files(example_cell[0], data_files, 2)
|
|
axss[1].text(xt, yt, 'P-unit model', fontsize='large',
|
|
transform=axs[1, 1].transAxes, color=s.model_color1)
|
|
plot_chi2_contrast(axss[1], axss[2], s, files, nums, nsmall, nlarge, ratebase)
|
|
axr1 = plot_noise_split(axss[0], data_contrast, 0, 1, wtime, wnoise)
|
|
plot_overn(axss[3], s, files, nmax=1e6)
|
|
axss[3].legend(loc='lower center', bbox_to_anchor=(0.5, 1.2),
|
|
markerfirst=False, title='$|\\chi_2|$ percentiles')
|
|
|
|
# model 1%:
|
|
axss = axs[2]
|
|
data_files = sims_path.glob(f'chi2-noisen-{example_cell[0]}-{1000*contrast:03.0f}-*.npz')
|
|
files, nums = sort_files(example_cell[0], data_files, 2)
|
|
plot_chi2_contrast(axss[1], axss[2], s, files, nums, nsmall, nlarge, ratebase)
|
|
axr2 = plot_noise_split(axss[0], contrast, 0, 1, wtime, wnoise)
|
|
plot_overn(axss[3], s, files, nmax=1e6)
|
|
|
|
# model noise split:
|
|
axss = axs[3]
|
|
data_files = sims_path.glob(f'chi2-split-{example_cell[0]}-*.npz')
|
|
files, nums = sort_files(example_cell[0], data_files, 1)
|
|
axss[1].text(xt, yt, 'P-unit model', fontsize='large',
|
|
transform=axss[1].transAxes, color=s.model_color1)
|
|
axss[1].text(xt + 0.9, yt, f'(noise split)', fontsize='large',
|
|
transform=axss[1].transAxes)
|
|
noise_contrast, noise_frac = plot_chi2_split(axss[1], axss[2], s,
|
|
files, nums, nsmall, nlarge, ratebase)
|
|
axr3 = plot_noise_split(axss[0], 0, noise_contrast, noise_frac,
|
|
wtime, wnoise)
|
|
plot_overn(axss[3], s, files, nmax=1e6)
|
|
|
|
fig.common_xticks(axs[:, 1])
|
|
fig.common_xticks(axs[:, 2])
|
|
fig.common_xticks(axs[:, 3])
|
|
fig.common_yticks(axs[1, 1:3])
|
|
fig.common_yticks(axs[2, 1:3])
|
|
fig.common_yticks(axs[3, 1:3])
|
|
fig.tag([axs[0, :2],
|
|
[axr1] + axs[1, 1:].tolist(),
|
|
[axr2] + axs[2, 1:].tolist(),
|
|
[axr3] + axs[3, 1:].tolist()],
|
|
xoffs=[-4.5, 1, 1, -4.5], yoffs=2)
|
|
fig.savefig()
|