"""Figures for the protocol-013 comparison, from retained data only:
  results/vm-synchrony-results.json.gz (the model side, run 011), the digitized Yu & Ferster (2010)
  Figure 4 blank values and the declared limits (../protocol-013/targets and contract.json), and
  the Azouz & Gray (1999) value recorded in the contract. Writes figures/synchrony.png/.svg and
  figures/time-scales.png/.svg (1920 x 1200).
"""
import csv, gzip, json
from pathlib import Path
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

ROOT = Path(__file__).resolve().parent
PROTOCOL = ROOT.parent / 'protocol-013'
res = json.load(gzip.open(ROOT / 'results/vm-synchrony-results.json.gz', 'rt'))
contract = json.loads((PROTOCOL / 'contract.json').read_text())
digitized = list(csv.DictReader((PROTOCOL / 'targets/yu-ferster-fig4-digitized.csv').open()))
PAPER, INK, MUTED, LINE = '#fcfaf7', '#28222d', '#776e7d', '#d8cfdf'
MODEL, DATA = '#6240ab', '#b26a00'            # validated pair: CVD dE 27.7, both pass contrast on PAPER
MODEL_LIGHT, DATA_LIGHT = '#d9cdee', '#f3dfc0'
plt.rcParams.update({'font.family': 'STIXGeneral', 'text.color': INK, 'font.size': 14, 'svg.fonttype': 'none',
                     'axes.edgecolor': LINE, 'xtick.color': MUTED, 'ytick.color': INK})
rng = np.random.default_rng(1)


def header(f, title, subtitle):
    f.text(.055, .94, 'felisyn  /  research note 17', color=MODEL, fontsize=15)
    f.text(.055, .872, title, fontsize=28)
    f.text(.055, .823, subtitle, color=MUTED, fontsize=14.5)


def box(ax, y, values, colour, height=.5, label=None):
    v = np.asarray(values, float)
    p10, q25, med, q75, p90 = np.percentile(v, [10, 25, 50, 75, 90])
    ax.plot([p10, q25], [y, y], color=colour, lw=1.6, solid_capstyle='round')
    ax.plot([q75, p90], [y, y], color=colour, lw=1.6, solid_capstyle='round')
    ax.add_patch(plt.Rectangle((q25, y - height / 2), q75 - q25, height, facecolor=MODEL_LIGHT, edgecolor=colour, lw=1.4))
    ax.plot([med, med], [y - height / 2, y + height / 2], color=colour, lw=2.6)
    if label:
        ax.text(p90 + .015, y, label, va='center', fontsize=11.5, color=MUTED)


# ---------------------------------------------------------------- figure 1: synchrony
primary = res['groups']['L23E-L23E']
pairs = primary['per_pair']
f = plt.figure(figsize=(12.8, 8), dpi=150, facecolor=PAPER)
header(f, 'Synchrony of spontaneous Vm: model and cat recordings',
       'Peak Vm cross-correlation of nearby layer 2/3 neurons, blank screen. Shaded: the declared "within the data" range.')
rows = ['Cat, Yu & Ferster 2010\n(digitized symbols)', 'Model, all pairs\n0 to 500 µm'] + [f'Model, {a} to {b} µm' for a, b in zip(range(0, 500, 100), range(100, 501, 100))]
panels = [('peak', 'A_unfiltered', 'fig4A_blank_bounds_p25_low_p75_high', 'Unfiltered', 'unfiltered_peak'),
          ('highpass_peak', 'B_highpass_20Hz', 'fig4B_blank_bounds_p25_low_p75_high', 'Above 20 Hz', 'highpass_peak')]
for k, (key, panel, bounds_key, title, outcome_key) in enumerate(panels):
    ax = f.add_axes([.215 + k * .395, .2, .355, .55], facecolor=PAPER)
    lo, hi = contract['data_targets'][bounds_key]
    ax.axvspan(lo, hi, color=DATA_LIGHT, alpha=.55, lw=0)
    n = len(rows)
    ys = list(range(n - 1, -1, -1))
    data = np.array([float(r['blank_value']) for r in digitized if r['panel'] == panel])
    ax.scatter(data, ys[0] + rng.uniform(-.22, .22, len(data)), s=46, facecolors='none', edgecolors=DATA, linewidths=1.4, zorder=3)
    ax.plot([np.median(data)] * 2, [ys[0] - .32, ys[0] + .32], color=DATA, lw=2.6, zorder=4)
    box(ax, ys[1], [p[key] for p in pairs], MODEL, label=f'n = {len(pairs):,}')
    for i, (a, b) in enumerate(zip(range(0, 500, 100), range(100, 501, 100))):
        sel = [p[key] for p in pairs if a < p['distance_um'] <= b]
        box(ax, ys[2 + i], sel, MODEL, height=.36, label=f'{len(sel):,}')
    ax.set_xlim(0, 1.0)
    ax.set_ylim(-.7, n - .3)
    ax.set_yticks(ys, rows if k == 0 else [''] * n, fontsize=12)
    ax.tick_params(axis='y', length=0)
    for side in ['top', 'right', 'left']:
        ax.spines[side].set_visible(False)
    ax.set_xlabel('peak correlation', fontsize=12.5, color=MUTED)
    reading = primary['declared_outcomes'][outcome_key]
    ax.set_title(f'{title}: model median {primary[key]["median"]:.2f}, data median {np.median(data):.2f}\ndeclared reading: {reading}',
                 fontsize=13, loc='left', color=INK)
f.text(.055, .1, 'Not a declared reading: unfiltered, model pairs under 100 µm apart reach the recorded median; above 20 Hz, all of them exceed every visible recorded pair.',
       fontsize=12)
f.text(.055, .068, 'Recorded separations are not reported (at most 500 µm). Recordings: anaesthetized, paralysed cats; electrode noise lowers their above-20 Hz values.',
       fontsize=11.5, color=MUTED)
f.text(.055, .036, f'Data: Yu & Ferster 2010, Neuron, Fig. 4A/B blank values digitized by us ({len([r for r in digitized if r["panel"] == "A_unfiltered"])} and '
       f'{len([r for r in digitized if r["panel"] == "B_highpass_20Hz"])} of 44 symbols visible). Model: protocol 013 on run 011. Boxes: IQR, lines 10th-90th percentile.',
       fontsize=11.5, color=MUTED)
(ROOT / 'figures').mkdir(exist_ok=True)
for ext in ['png', 'svg']:
    f.savefig(ROOT / f'figures/synchrony.{ext}', facecolor=PAPER)
plt.close(f)

# ---------------------------------------------------------------- figure 2: time scales
f = plt.figure(figsize=(12.8, 8), dpi=150, facecolor=PAPER)
header(f, 'Time scales of spontaneous Vm: model and cat recordings',
       'Left: autocorrelation time constant of each neuron. Right: mean coherence of nearby layer 2/3 excitatory pairs.')
ax = f.add_axes([.205, .22, .36, .5], facecolor=PAPER)
ag = contract['data_targets']['azouz_gray_1999']
mean, disp = ag['tau_spontaneous_ms_mean'], ag['dispersion_ms']
ax.plot([mean - disp, mean + disp], [2, 2], color=DATA, lw=3, solid_capstyle='round')
ax.plot([mean], [2], marker='o', markersize=11, markerfacecolor=PAPER, markeredgecolor=DATA, markeredgewidth=2)
exc = [t for p in ('V1_Exc_L4', 'V1_Exc_L2/3') for t in res['populations'][p]['tau_per_neuron_ms'] if t is not None]
inh = [t for p in ('V1_Inh_L4', 'V1_Inh_L2/3') for t in res['populations'][p]['tau_per_neuron_ms'] if t is not None]
for y, vals in [(1, exc), (0, inh)]:
    ax.scatter(vals, y + rng.uniform(-.2, .2, len(vals)), s=7, color=MODEL, alpha=.35, lw=0)
    ax.plot([np.median(vals)] * 2, [y - .3, y + .3], color=MODEL, lw=2.6)
ax.set_xscale('log')
ax.set_xlim(2, 100)
ax.set_xticks([2, 5, 10, 20, 50, 100], ['2', '5', '10', '20', '50', '100'])
ax.set_yticks([2, 1, 0], [f'Cat V1, Azouz & Gray 1999\nmean ± dispersion, {ag["cells"]} cells', f'Model, excitatory\n{len(exc)} neurons',
                          f'Model, inhibitory\n{len(inh)} neurons'], fontsize=12)
ax.set_ylim(-.7, 2.7)
ax.tick_params(axis='y', length=0)
for side in ['top', 'right', 'left']:
    ax.spines[side].set_visible(False)
ax.set_xlabel('τ (ms, log scale)', fontsize=12.5, color=MUTED)
ax.set_title(f'Model median {res["tau"]["primary_ms"]["median"]:.1f} ms (excitatory); data {mean} ± {disp} ms\ndeclared reading: {res["tau"]["declared_outcome"]}',
             fontsize=13, loc='left')
ax2 = f.add_axes([.655, .22, .3, .5], facecolor=PAPER)
spec = primary['mean_coherence_spectrum']
ax2.plot(spec['freq_hz'][1:], spec['coherence'][1:], color=MODEL, lw=2)
ax2.set_xlim(0, 100)
ax2.set_ylim(0, 1)
for side in ['top', 'right']:
    ax2.spines[side].set_visible(False)
ax2.set_xlabel('frequency (Hz)', fontsize=12.5, color=MUTED)
ax2.set_ylabel('coherence', fontsize=12.5, color=MUTED)
peak_f = spec['freq_hz'][int(np.argmax(spec['coherence']))]
ax2.set_title(f'Model: coherence peaks near {peak_f:.0f} Hz', fontsize=13, loc='left')
ax2.text(3, .9, 'Recordings (Yu & Ferster 2010): spontaneous\ncoherence highest at low frequency and\nfalling with frequency (not digitized)', fontsize=11.5, color=DATA, va='top')
f.text(.055, .1, 'The model\'s spontaneous Vm is dominated by fast fluctuations; the anaesthetized recordings by slow ones.', fontsize=12.5)
f.text(.055, .068, 'Model τ: unbiased autocorrelation over 500 ms epochs, fitted a + b·exp(−t/τ); every fit succeeded, and the declared variants agree within 0.2 ms.',
       fontsize=12, color=MUTED)
f.text(.055, .036, 'Data: Azouz & Gray 1999, J Neurosci (Fig. 4D text value); Yu & Ferster 2010, Neuron. Model: protocol 013 on run 011. Measurements only.',
       fontsize=11.5, color=MUTED)
for ext in ['png', 'svg']:
    f.savefig(ROOT / f'figures/time-scales.{ext}', facecolor=PAPER)
print('ok')
