"""Figures for protocol 022 revision 2, the plaids pilot, drawn only from the retained results/results.json.
Numbered as in journal note 22:

  1. plaids-suppression: layer 4 excitatory groups' responses to their preferred grating alone, the orthogonal
                         grating alone and both together, against the relay prediction (the sum of the two alone),
                         in the intact network and the plain lesion. Small dots are single steps.
  2. plaids-primaries:   the three primary measures (intact network) with their 90 % and 95 % intervals, against the
                         relay value 0 and the declared margin 0.10.
  3. plaids-readout:     the decoded direction of the two block B plaids (gratings 45 degrees apart) against the sum
                         of the readouts of their two gratings alone, in the intact network and the plain lesion.

  python3 draw_plaids.py RESULTS_JSON OUT_DIR [--font FILE ...]          (each figure 1920 x 1200 PNG, and SVG)

--font takes Latin Modern Roman font files (.ttf or .otf, or .woff if fontTools is installed). Without them the
figures fall back to STIXGeneral. Glyphs Latin Modern lacks (the minus sign) also come from STIXGeneral.
Every plotted number is read from RESULTS_JSON: condition means, single-step values and the declared measures.
The script recomputes the plotted XOS parts and readout shifts and stops if they differ from the declared values
in RESULTS_JSON. It imports nothing but the standard library and matplotlib.
"""
import argparse, cmath, json, math, os, sys, tempfile
from pathlib import Path

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib import font_manager
from matplotlib.lines import Line2D
from matplotlib.patches import FancyArrowPatch

PAPER, INK, MUTED, LINE, TINT = '#fcfaf7', '#28222d', '#776e7d', '#d8cfdf', '#efe9f7'
# categorical slots, validated together on PAPER (dataviz validator, all pairs, light mode: all checks pass)
VIOLET, TEAL, AMBER = '#5924d6', '#0b6e99', '#b26a00'
OWN, OTHER, PLAID, RELAY = VIOLET, TEAL, AMBER, INK
FOOT = ('Model: 2 mm reduction of LSV1M (Antolík et al. 2024, CC BY 4.0), simulated with Mozaik and NEST. '
        'Simulation only, one network instance: not a biological measurement.')


def fonts(paths):
    family = []
    for p in paths:
        p = Path(p)
        if p.suffix.lower() == '.woff':
            from fontTools.ttLib import TTFont   # optional: only to read the site's web font
            t = TTFont(str(p))
            t.flavor = None
            tmp = Path(tempfile.mkdtemp()) / (p.stem + '.ttf')
            t.save(str(tmp))
            p = tmp
        font_manager.fontManager.addfont(str(p))
        family.append(font_manager.FontProperties(fname=str(p)).get_name())
    family = list(dict.fromkeys(family)) + ['STIXGeneral']
    plt.rcParams.update({'font.family': family, 'text.color': INK, 'font.size': 14, 'svg.fonttype': 'none',
                         'svg.hashsalt': 'felisyn-plaids-022', 'axes.edgecolor': LINE, 'axes.linewidth': 1,
                         'xtick.color': MUTED, 'ytick.color': MUTED, 'axes.labelcolor': MUTED})
    return family[0]


def header(f, title, sub):
    f.text(.055, .94, 'felisyn  /  research note, protocol 022 (plaids pilot)', color=VIOLET, fontsize=15)
    f.text(.055, .872, title, fontsize=28)
    f.text(.055, .823, sub, color=MUTED, fontsize=14)
    f.text(.055, .014, FOOT, fontsize=11, color=MUTED)


def save(f, out, name):
    f.savefig(out / f'{name}.png', dpi=150, facecolor=PAPER)
    f.savefig(out / f'{name}.svg', facecolor=PAPER, metadata={'Date': None})
    plt.close(f)


def quiet(ax, keep=('bottom',)):
    for side in ('top', 'right', 'left', 'bottom'):
        ax.spines[side].set_visible(side in keep)


def signed(x, d=3):
    s = f'{abs(x):.{d}f}'
    return ('−' if x < 0 and float(s) != 0 else '+') + s


def interval(e, key='ci95'):
    lo, hi = e[key]
    return f'{signed(lo)} to {signed(hi)}'


def check(name, got, declared, tol=1e-9):
    if abs(got - declared) > tol:
        sys.exit(f'refused: recomputed {name} {got!r} differs from the declared value {declared!r}')


# ---------------------------------------------------------------- figure 2: the primaries (plaids-primaries)
def primaries(r, out):
    p, holm = r['measures']['intact']['primary'], r['holm']
    rows = [('XOS', 'cross-orientation suppression',
             'how far the plaid response falls below the relay\nprediction, in units of the preferred response'),
            ('Dc', 'dominance at unequal contrasts',
             'how much more of their single-grating response\nthe stronger grating\'s cells keep'),
            ('ds', 'readout shift',
             'toward the stronger grating, in the controller\'s\nsignal units (45° is 1)')]
    f = plt.figure(figsize=(12.8, 8), dpi=150, facecolor=PAPER)
    header(f, 'All three declared measures cleared the margin',
           'Intact network. Dot: estimate; thick bar: 90 % interval; thin line: 95 % interval. A relay of the '
           'single-grating responses gives 0.')
    ax = f.add_axes([.405, .22, .315, .50], facecolor=PAPER)
    n, delta = len(rows), r['declared']['delta']
    ax.set_ylim(-0.6, n - 0.4)
    ax.set_xlim(-0.25, 0.9)
    to_fig = ax.transData + f.transFigure.inverted()
    ax.axvspan(-delta, delta, color=TINT, zorder=0, lw=0)
    for x in (-delta, delta):
        ax.axvline(x, color=MUTED, lw=1, ls=(0, (4, 3)), zorder=1)
    ax.axvline(0, color=INK, lw=1.2, zorder=1)
    for i, (key, name, gloss) in enumerate(rows):
        y = n - 1 - i
        e = p[key]
        ax.plot(e['ci95'], [y, y], color=VIOLET, lw=2, solid_capstyle='round', zorder=3)
        ax.plot(e['ci90'], [y, y], color=VIOLET, lw=8, solid_capstyle='butt', zorder=3)
        ax.plot(e['estimate'], y, 'o', ms=12, color=VIOLET, mec=PAPER, mew=2, zorder=4)
        fy = to_fig.transform((0, y))[1]
        f.text(.055, fy + .026, f'{key}, {name}', fontsize=17, color=INK, va='center')
        f.text(.055, fy - .022, gloss, fontsize=12, color=MUTED, va='center', linespacing=1.3)
        f.text(.745, fy + .026, signed(e['estimate']), fontsize=17, color=INK, va='center')
        f.text(.745, fy - .010, f'95 %: {interval(e)}', fontsize=12, color=INK, va='center')
        f.text(.745, fy - .042, f'p {e["p"]:.4f}; Holm threshold {holm["thresholds"][key]:.3f}', fontsize=12,
               color=MUTED, va='center')
    ax.set_yticks([])
    ax.set_xticks([-0.2, 0, 0.2, 0.4, 0.6, 0.8])
    ax.set_xticklabels(['−0.2', '0', '0.2', '0.4', '0.6', '0.8'])
    ax.set_xlabel('estimate, each measure in its own unit')
    ax.text(0, n - 0.32, 'relay', ha='center', va='bottom', fontsize=12, color=INK)
    ax.text(-delta - .015, n - 0.32, '−0.10', ha='right', va='bottom', fontsize=11, color=MUTED)
    ax.text(delta + .015, n - 0.32, '+0.10', ha='left', va='bottom', fontsize=11, color=MUTED)
    ax.grid(axis='x', color=LINE, lw=0.6)
    ax.set_axisbelow(True)
    quiet(ax)
    f.text(.055, .11, f'Shaded: within ±{delta:.2f} of a relay, the declared margin for all three measures. The outcome '
           f'GO needs one measure that passes Holm', fontsize=12, color=MUTED)
    f.text(.055, .085, f'at family-wise 0.05 and lies at least {delta:.2f} from 0. Each p is the smallest a '
           '10,000-replicate bootstrap can give (2 / 10,001).', fontsize=12, color=MUTED)
    save(f, out, 'plaids-primaries')


# ---------------------------------------------------------------- figure 1: suppression in the groups (plaids-suppression)
def group_data(r, run):
    m = r['measures'][run]
    cm, ps, conds = m['condition_means'], m['per_step'], r['sequence']['conditions']
    cells = []
    for g, o in ((45, 135), (135, 45)):
        for c in (15, 30):
            key, B = f'G|L4E|{g}', cm[f'G|L4E|{g}']['blank']
            labels = {'own': f'{g}@{c}', 'other': f'{o}@{c}', 'plaid': f'45@{c}+135@{c}'}
            mean = {k: cm[key][lab] - B for k, lab in labels.items()}
            steps = {k: [y - B for y, cond in zip(ps[key], conds) if cond == lab] for k, lab in labels.items()}
            xos = (mean['own'] + mean['other'] - mean['plaid']) / mean['own']
            check(f'{run} XOS {g}@{c}', xos, m['S1']['XOS']['parts'][f'{g}@{c}']['estimate'])
            cells.append({'g': g, 'c': c, 'mean': mean, 'steps': steps, 'xos': xos,
                          'cross': mean['other'] / mean['own']})
    return cells


def suppression(r, out):
    f = plt.figure(figsize=(12.8, 8), dpi=150, facecolor=PAPER)
    header(f, 'An orthogonal grating lowered the response to the preferred one',
           'Layer 4 excitatory cells grouped by preferred orientation (within 15°): spikes per cell above the blank, '
           '20 to 230 ms after onset.')
    handles = [Line2D([], [], ls='', marker='o', ms=10, color=OWN, mec=PAPER, mew=2),
               Line2D([], [], ls='', marker='o', ms=10, color=OTHER, mec=PAPER, mew=2),
               Line2D([], [], ls='', marker='o', ms=10, color=PLAID, mec=PAPER, mew=2),
               Line2D([], [], color=RELAY, lw=2.5)]
    labels = ['preferred grating alone', 'orthogonal grating alone', 'both together (plaid)',
              'relay prediction: the sum of the two alone']
    f.legend(handles, labels, loc='upper left', bbox_to_anchor=(.05, .80), ncol=4, frameon=False, fontsize=13,
             handletextpad=.4, columnspacing=1.6, labelcolor=INK)
    panels = (('intact', 'Intact', [.075, .19, .40, .50], (-0.75, 4.25)),
              ('lesion', 'Plain lesion (intracortical connections off)', [.565, .19, .40, .50], (-0.9, 11.9)))
    offsets = {'own': -0.26, 'other': 0.0, 'plaid': 0.26}
    colour = {'own': OWN, 'other': OTHER, 'plaid': PLAID}
    for run, title, rect, ylim in panels:
        ax = f.add_axes(rect, facecolor=PAPER)
        cells = group_data(r, run)
        for x, cell in enumerate(cells):
            for k, dx in offsets.items():
                ys = cell['steps'][k]
                jit = [(j - (len(ys) - 1) / 2) * 0.022 for j in range(len(ys))]
                ax.plot([x + dx + j for j in jit], ys, 'o', ms=4, color=colour[k], alpha=.45, mew=0, zorder=2)
                ax.plot(x + dx, cell['mean'][k], 'o', ms=11, color=colour[k], mec=PAPER, mew=2, zorder=4)
            relay = cell['mean']['own'] + cell['mean']['other']
            xp = x + offsets['plaid']
            ax.plot([xp - .11, xp + .11], [relay, relay], color=RELAY, lw=2.5, solid_capstyle='butt', zorder=5)
            ax.plot([xp, xp], [cell['mean']['plaid'], relay], color=RELAY, lw=1, zorder=3)
            if run == 'intact':
                ax.text(x, ylim[1] - 0.04 * (ylim[1] - ylim[0]), f'XOS {cell["xos"]:.2f}', ha='center', va='top',
                        fontsize=12, color=INK)
        if run == 'lesion':
            cross = r['connection_readings']['group_measures']['cross_drive']['estimate']
            lo, hi = min(c['cross'] for c in cells), max(c['cross'] for c in cells)
            ax.text(-0.45, ylim[1] - 0.04 * (ylim[1] - ylim[0]), f'The orthogonal grating alone drives these cells at '
                    f'{lo:.2f} to {hi:.2f}\nof their preferred response (mean {cross:.2f}): no suppression index here.',
                    ha='left', va='top', fontsize=12, color=INK, linespacing=1.3)
        ax.axhline(0, color=MUTED, lw=1, zorder=1)
        ax.set_xlim(-0.55, 3.55)
        ax.set_ylim(*ylim)
        ax.set_xticks(range(4))
        ax.set_xticklabels([f'{c["g"]}° cells\n{c["c"]} % gratings' for c in cells], fontsize=12.5, color=INK)
        ax.tick_params(axis='x', length=0, pad=8)
        ax.set_ylabel('spikes per cell above the blank')
        ax.grid(axis='y', color=LINE, lw=0.6)
        ax.set_axisbelow(True)
        quiet(ax, keep=())
        ax.text(-0.55, ylim[1] + 0.05 * (ylim[1] - ylim[0]), title, fontsize=16, color=INK, va='bottom')
    f.text(.055, .085, 'Each column: 8 single steps (small dots) and their mean (large dot). Each XOS above is the drop '
           'from the relay prediction to the plaid, divided by', fontsize=12, color=MUTED)
    f.text(.055, .06, 'the response to the preferred grating alone. The panels have different scales: during the '
           'gratings, layer 4 fires 6 to 9 times as often without its connections.', fontsize=12, color=MUTED)
    save(f, out, 'plaids-suppression')


# ---------------------------------------------------------------- figure 3: the readout (plaids-readout)
def s_of(z):
    """017's decoded signal: theta = arg(z) / 2 modulo 180 degrees, s = (theta - 90 degrees) / 45 degrees."""
    theta = (cmath.phase(z) / 2) % math.pi
    return (theta - math.pi / 2) / (math.pi / 4)


def readout_data(r, run):
    m = r['measures'][run]
    rm, zs, conds = m['readout_means']['D'], m['per_step']['z_D'], r['sequence']['conditions']
    blank = complex(*rm['blank'])
    z = {k: complex(*v) - blank for k, v in rm.items()}
    rows = []
    for p, strong, weak in (('135@30+90@15', '135@30', '90@15'), ('135@15+90@30', '90@30', '135@15')):
        zl = z[strong] + z[weak]
        rotation = cmath.phase(z[p] * zl.conjugate()) / 2 / (math.pi / 4)
        sigma = math.copysign(1.0, cmath.phase(z[strong] * zl.conjugate()))
        part = m['S1']['ds']['parts'][p]
        check(f'{run} rotation {p}', rotation, part['rotation']['estimate'])
        check(f'{run} ds {p}', sigma * rotation, part['ds']['estimate'])
        steps = [s_of(complex(*v) - blank) for v, cond in zip(zs, conds) if cond == p]
        rows.append({'plaid': p, 'strong': strong, 'weak': weak, 's_strong': s_of(z[strong]),
                     's_weak': s_of(z[weak]), 's_lin': s_of(zl), 's_plaid': s_of(z[p]), 'ds': sigma * rotation,
                     'steps': steps})
    return rows


def name_of(label):
    deg, c = label.split('@')
    return f'{deg}° at {c} %'


def readout(r, out):
    f = plt.figure(figsize=(12.8, 8), dpi=150, facecolor=PAPER)
    header(f, 'The readout leaned further toward the stronger grating than a relay',
           'Plaids of two gratings 45° apart. Decoded direction in the controller\'s signal units: 90° reads as 0 '
           'and 135° as 1.')
    handles = [Line2D([], [], ls='', marker='D', ms=8, color=MUTED, mec=PAPER, mew=1.5),
               Line2D([], [], ls='', marker='o', ms=11, mfc=PAPER, mec=RELAY, mew=2),
               Line2D([], [], ls='', marker='o', ms=11, color=PLAID, mec=PAPER, mew=2),
               Line2D([], [], ls='', marker='o', ms=5, color=PLAID, alpha=.5, mew=0)]
    labels = ['each grating alone', 'relay prediction: sum of the two readouts alone', 'the plaid',
              'single plaid steps']
    f.legend(handles, labels, loc='upper left', bbox_to_anchor=(.05, .80), ncol=4, frameon=False, fontsize=13,
             handletextpad=.4, columnspacing=1.6, labelcolor=INK)
    readings = r['connection_readings']['readout']
    panels = (('intact', 'Intact', [.075, .29, .40, .40], readings['ds_intact']),
              ('lesion', 'Plain lesion (intracortical connections off)', [.565, .29, .40, .40],
               readings['ds_lesion']))
    for run, title, rect, mean in panels:
        ax = f.add_axes(rect, facecolor=PAPER)
        rows = readout_data(r, run)
        for i, row in enumerate(rows):
            y = 1 - i
            ax.plot([-0.25, 1.25], [y, y], color=LINE, lw=1, zorder=1)
            for key, s in (('strong', row['s_strong']), ('weak', row['s_weak'])):
                ax.plot(s, y, 'D', ms=9, color=MUTED, mec=PAPER, mew=1.5, zorder=3)
                ax.text(s, y - 0.13, name_of(row[key]) + (' (stronger)' if key == 'strong' else ''), ha='center',
                        va='top', fontsize=11.5, color=INK if key == 'strong' else MUTED)
            ax.plot(row['steps'], [y + 0.11] * len(row['steps']), 'o', ms=5, color=PLAID, alpha=.5, mew=0,
                    zorder=3)
            ax.add_patch(FancyArrowPatch((row['s_lin'], y), (row['s_plaid'], y), arrowstyle='-|>',
                                         mutation_scale=16, color=PLAID, lw=2, shrinkA=8, shrinkB=8, zorder=4))
            ax.plot(row['s_lin'], y, 'o', ms=12, mfc=PAPER, mec=RELAY, mew=2, zorder=5)
            ax.plot(row['s_plaid'], y, 'o', ms=12, color=PLAID, mec=PAPER, mew=2, zorder=5)
            toward = '135°' if row['strong'].startswith('135') else '90°'
            ax.text((row['s_lin'] + row['s_plaid']) / 2, y + 0.2, f'{signed(row["ds"])} toward {toward}',
                    ha='center', va='bottom', fontsize=12.5, color=INK)
        ax.set_xlim(-0.3, 1.3)
        ax.set_ylim(-0.55, 1.5)
        ax.set_yticks([])
        ax.set_xticks([0, 0.25, 0.5, 0.75, 1])
        ax.set_xticklabels(['0\n(90°)', '0.25', '0.5', '0.75', '1\n(135°)'], fontsize=12)
        ax.set_xlabel('decoded direction (signal units)')
        ax.grid(axis='x', color=LINE, lw=0.6)
        ax.set_axisbelow(True)
        quiet(ax)
        ax.text(-0.3, 1.62, title, fontsize=16, color=INK, va='bottom')
        left = rect[0]
        f.text(left, .15, f'Mean shift toward the stronger grating, ds: {signed(mean["estimate"])}', fontsize=13,
               color=INK)
        f.text(left, .123, f'95 % interval {interval(mean)}', fontsize=12, color=INK)
    dds = readings['dds']
    f.text(.055, .075, f'Intact minus lesion: {signed(dds["estimate"])} (95 %: {interval(dds)}). Declared reading: '
           'same direction without the connections. In the declared words:', fontsize=12, color=MUTED)
    f.text(.055, .051, '“The lesion\'s own saturation can produce this, so it does not show that the intact readout\'s '
           'departure comes from the feedforward path.”', fontsize=12, color=MUTED)
    save(f, out, 'plaids-readout')


def main():
    a = argparse.ArgumentParser(description=__doc__.splitlines()[0])
    a.add_argument('results')
    a.add_argument('out')
    a.add_argument('--font', action='append', default=[])
    args = a.parse_args()
    family = fonts(args.font)
    r = json.loads(Path(args.results).read_text())
    if r.get('outcome') != 'GO':
        sys.exit(f'refused: these figures were written for the declared outcome GO, not {r.get("outcome")!r}')
    out = Path(args.out)
    out.mkdir(parents=True, exist_ok=True)
    primaries(r, out)
    suppression(r, out)
    readout(r, out)
    print('font:', family, '| wrote', ', '.join(f'{n}.png/.svg' for n in
                                               ('plaids-primaries', 'plaids-suppression', 'plaids-readout')))


if __name__ == '__main__':
    main()
