"""The two conductance averages of run 011 against the values in the paper's text, from
conductance-averages.json. Computed values only; no recordings are plotted.
Writes figures/conductance-averages.png (1920 x 1200) and .svg.
"""
import json
from pathlib import Path
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

ROOT = Path(__file__).resolve().parent
data = json.loads((ROOT / 'conductance-averages.json').read_text())
rules = {r['rule']: r for r in data['rules']}
PAPER, INK, VIOLET, MUTED, LINE, LIGHT = '#fcfaf7', '#28222d', '#6240ab', '#776e7d', '#d8cfdf', '#b9a6dc'
plt.rcParams.update({'font.family': 'STIXGeneral', 'text.color': INK, 'font.size': 14, 'svg.fonttype': 'none',
                     'axes.edgecolor': LINE, 'xtick.color': MUTED, 'ytick.color': INK})
f = plt.figure(figsize=(12.8, 8), dpi=150, facecolor=PAPER)
f.text(.065, .94, 'felisyn  /  research note 16', color=VIOLET, fontsize=15)
f.text(.065, .869, 'Two ways to average the conductances', fontsize=29)
f.text(.065, .817, "Our run's four population means, weighted by population size or pooled over the recorded neurons,",
       color=MUTED, fontsize=15)
f.text(.065, .782, "against the averages given in the paper's text (shaded: values that round to them). The panels use different scales.", color=MUTED, fontsize=15)
PANELS = [('exc', 'Excitatory conductance', (1.10, 1.25), .50), ('inh', 'Inhibitory conductance', (3.30, 4.30), .22)]
for key, title, (lo, hi), bottom in PANELS:
    ax = f.add_axes([.065, bottom, .87, .17], facecolor=PAPER)
    text_value = float(data['text_values'][key]['value_nS'])
    ax.axvspan(text_value - .005, text_value + .005, color=LIGHT, alpha=.6, linewidth=0)
    ax.axvline(text_value, color=VIOLET, linewidth=1.2, linestyle=(0, (3, 2)))
    ax.text(text_value, 1.32, f"paper's text: {data['text_values'][key]['value_nS']} nS", ha='center', va='bottom', fontsize=13, color=VIOLET)
    size = rules['size_weighted'][key]['run_011_nS']
    pooled = rules['recorded_pooled'][key]['run_011_nS']
    gap = (hi - lo) * .012
    ax.plot([size], [.5], marker='o', markersize=12, color=VIOLET, linestyle='none')
    ax.text(size + gap, .5, f'weighted by population size\n(the code\'s logged mean): {size:.3f} nS', ha='left', va='center', fontsize=12.5, color=INK)
    ax.plot([pooled], [.5], marker='s', markersize=11, color=INK, linestyle='none')
    ax.text(text_value - .005 - gap, .5, f'pooled over the recorded\nneurons: {pooled:.3f} nS', ha='right', va='center', fontsize=12.5, color=INK)
    ax.set_xlim(lo, hi)
    ax.set_ylim(0, 1.3)
    ax.set_yticks([])
    for side in ['top', 'right', 'left']:
        ax.spines[side].set_visible(False)
    ax.tick_params(axis='x', labelsize=12)
    ax.set_xlabel('nS', fontsize=12, color=MUTED, labelpad=2)
    f.text(.065, bottom + .205, title, fontsize=17, color=INK)
unweighted = rules['unweighted']
f.text(.065, .085, f"The unweighted mean of the four populations gives {unweighted['exc']['run_011_nS']:.3f} and {unweighted['inh']['run_011_nS']:.3f} nS. "
       'Only the averages that ignore population size match both text values.', fontsize=13)
f.text(.065, .045, 'Inference: the code behind the text values is not public. Text: Antolík et al. 2024 (CC BY 4.0). '
       'Code: LSV1M v1.0 Figure 4 code, lines 538 and 549 (MIT). Means: run 011.', fontsize=12, color=MUTED)
(ROOT / 'figures').mkdir(exist_ok=True)
for ext in ['png', 'svg']:
    f.savefig(ROOT / f'figures/conductance-averages.{ext}', facecolor=PAPER)
print('ok')
