"""Redraw published Figure 4 mean labels, not a new Felisyn simulation.

Antolík et al. 2024, doi:10.1371/journal.pcbi.1012342.g004, CC BY.
No error bars: exact SDs and cohort sizes were not recovered from raw data.
"""
import csv
from pathlib import Path
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

ROOT = Path(__file__).resolve().parent
rows = list(csv.DictReader((ROOT/'reference-means.csv').open()))
paper, ink, violet, muted = '#fcfaf7', '#29232f', '#6240ab', '#766e7d'
plt.rcParams.update({'font.family': 'STIXGeneral', 'font.size': 12,
                     'axes.spines.top': False, 'axes.spines.right': False,
                     'axes.spines.left': False, 'axes.edgecolor': '#cec7d4',
                     'xtick.color': muted, 'ytick.color': ink, 'axes.labelcolor': ink})
fig, axes = plt.subplots(2, 3, figsize=(15, 9), dpi=150, facecolor=paper)
fig.subplots_adjust(left=.125, right=.975, bottom=.205, top=.745, hspace=.8, wspace=.62)
metrics = [('firing_rate_hz', 'Firing rate', 'spikes / second', (0, 9)),
           ('isi_cv', 'Spike irregularity', 'ISI coefficient of variation', (0, 1.15)),
           ('correlation_10ms', 'Pairwise synchrony', 'Pearson r · 10 ms bins', (0, .026)),
           ('voltage_mV', 'Membrane voltage', 'mV · signed mean', (-76, -60)),
           ('exc_conductance_nS', 'Excitatory conductance', 'nS', (0, 1.6)),
           ('inh_conductance_nS', 'Inhibitory conductance', 'nS', (0, 7))]
labels = ['L4 · E', 'L4 · I', 'L2/3 · E', 'L2/3 · I']
for ax, (key, title, unit, limits) in zip(axes.flat, metrics):
    ax.set_facecolor(paper)
    for i, row in enumerate(rows):
        value = float(row[key]); y = 3-i
        color = violet if i % 2 == 0 else muted
        ax.axhline(y, color='#e5dfea', lw=.8, zorder=0)
        ax.scatter(value, y, s=48, c=color, zorder=3)
        ax.annotate(format(value, '.2g'), (value, y), xytext=(8, 0),
                    textcoords='offset points', va='center', color=ink, fontsize=12)
    ax.set_yticks([3, 2, 1, 0], labels)
    ax.tick_params(axis='y', length=0, pad=9)
    ax.set_ylim(-.55, 3.55); ax.set_xlim(*limits)
    ax.set_title(title, loc='left', fontsize=16, pad=16)
    ax.set_xlabel(unit, labelpad=10)
fig.text(.07, .935, 'felisyn  /  research note 08', color=violet, fontsize=15)
fig.text(.07, .874, 'What the cat-circuit benchmark must measure', color=ink, fontsize=29)
fig.text(.07, .822, 'Published model reference · 24 rounded means · four cortical populations', color=ink, fontsize=17)
fig.text(.07, .125, 'E = excitatory cells    I = inhibitory cells    •    These are the authors’ results, not a new Felisyn run.', color=ink, fontsize=13)
fig.text(.07, .087, 'Means transcribed from printed labels. Exact SDs and sample counts unavailable; error bars are deliberately omitted.', color=muted, fontsize=12)
fig.text(.07, .049, 'Adapted from Antolík et al., PLOS Computational Biology (2024), Figure 4 · doi:10.1371/journal.pcbi.1012342 · CC BY', color=muted, fontsize=11)
fig.savefig(ROOT/'reference-means.png', dpi=150, facecolor=paper)
fig.savefig(ROOT/'reference-means.svg', facecolor=paper)
print(ROOT/'reference-means.png')
