"""Protocol 024: the two-signal decision rule, applied to recorded runs of the plaid stimulus set.

  python3 -B decide_024.py OUT.json NAME=EXPORT.json.gz [NAME=EXPORT.json.gz ...] [--design]

Two signals arrive together as two superimposed gratings: signal A at 45 degrees, signal B at 135 degrees, each
signal's strength as its grating's contrast. The rule reads one step of the circuit's response and names one of
three actions: A, B or hold.

  z(k)   protocol 017's frozen readout of step k: all layer 4 excitatory cells, balanced weights, counts in window D
         = [40, 300) ms after onset (pipeline.decoder_weights, unchanged). 45 degrees lies along +i, 135 along -i.
  z0     the mean z over the instance's 8 blank steps.
  S      [Im mean z(45@30) - Im mean z(135@30)] / 2, the instance's response scale. Refused below MIN_SCALE.
  v(k)   -Im(z(k) - z0) / S: positive toward B, negative toward A; about +-1 for a single 30 % grating.
  action B if v > T, A if v < -T, else hold. T = 0.32, fixed from the pilot (instance A) before any other instance
         was read by this code.

Calibration steps (blank, 45@30, 135@30) set z0 and S and are not test steps. Test steps, 56 per instance, with the
action intended for each: the equal plaids (hold), the unequal plaids (the stronger signal's action), and the single
gratings at 15 and 60 % (their own signal's action). Block B (gratings at 90 degrees) is described, not tested.

Outcome (non-design runs: exactly the six instances of protocol 023 revision 2, their exports at the published hashes):
  PASS               every instance takes the intended action in at least 51 of its 56 test steps, and all six
                     together in at least 320 of 336;
  FAIL               otherwise;
  NOT INTERPRETABLE  a refusal: a wrong export, an export that cannot be read, a sequence that is not the declared
                     set, S below MIN_SCALE, changed frozen code, or a readout that differs from the z_D that
                     protocol 023's analysis published for that step (confirm-023r2/results/results.json).
Run from work/controller-024 of the workspace (the frozen code is found from there).
The relay rule (the same threshold on the sum of the two single gratings' mean v) is reported beside it, without a
decision role. Simulation only; an engineering rule, not a claim about cat cortex.
"""
import gzip, hashlib, json, sys
import numpy as np

sys.dont_write_bytecode = True
T = 0.32
MIN_SCALE = 1500.0
NEED_INSTANCE, OF_INSTANCE = 51, 56
NEED_POOLED, OF_POOLED = 320, 336
# the six analysed exports of protocol 023 revision 2 (confirm-023r2/receipts/data-sha256.txt)
DECLARED = {'s8': '999852f1736596123882fda2df1ba46d04e46db647732fbb1c207077d4fc8981', 's10': 'd27f0737b4cef70f1a71defdb44281630e0db23053eee1b571b30c8d37aaca29',
            's11': '864409407ba9676f3a8083cd1a435c1b7699d947d0126329ff2e51f34cd505be',
            's12': 'fd92c61739ff305772353af92df7aa024f42c9c335168fa62a16de627dee37ed',
            's13': 'd4b01c1342e08b7e5ff5f6bfc91e2baff47096cd4044204a977702352ad0a108', 's14': '22261efc90ae0cbeb18c0b5db8d09e1b88a7f3caaff5f0004b39d010b8fdca74'}
FROZEN = {'protocol-022/plaid_stats.py': '7baf22fb8db4fec41579e4899e89319f486c3357d9f89317bfe3a844f2e7cc4c',
          'protocol-017/pipeline.py': '9801606903df981ab15966d2b0182a4ebb4bdea4e9ad24e427b3165f3abc51d4'}
PUBLISHED = 'outputs/felisyn-research/confirm-023r2/results/results.json'      # holds measures/<seed>/per_step/z_D
CAL = ('blank', '45@30', '135@30')
INTENDED = {'45@15+135@15': 'hold', '45@30+135@30': 'hold', '45@30+135@15': 'A', '45@15+135@30': 'B',
            '45@15': 'A', '45@60': 'A', '135@15': 'B', '135@60': 'B'}
BLOCK_B = ('90@15', '90@30', '135@30+90@15', '135@15+90@30')
RELAY = {'45@15+135@15': ('45@15', '135@15'), '45@30+135@30': ('45@30', '135@30'),
         '45@30+135@15': ('45@30', '135@15'), '45@15+135@30': ('45@15', '135@30')}


class Refusal(Exception):
    pass


def action(v, t=T):
    return 'B' if v > t else ('A' if v < -t else 'hold')


def calibrate(cond, z):
    """z0 and S from the calibration steps. cond: condition label per step; z: complex readout per step."""
    cond, z = np.asarray(cond), np.asarray(z, complex)
    for c in CAL:
        if (cond == c).sum() != 8:
            raise Refusal(f'{c}: {int((cond == c).sum())} calibration steps, not 8')
    z0 = z[cond == 'blank'].mean()
    s = (z[cond == '45@30'].mean().imag - z[cond == '135@30'].mean().imag) / 2
    if not np.isfinite(s) or s < MIN_SCALE:
        raise Refusal(f'the response scale S is {s:.1f}, below {MIN_SCALE:.0f}')
    return z0, float(s)


def decide(cond, z):
    """Per-instance record: calibration, v and action per step, the test tally, the relay rule, descriptions."""
    cond, z = list(cond), np.asarray(z, complex)
    z0, s = calibrate(cond, z)
    v = -(z - z0).imag / s
    off = (z - z0).real / s
    acts = [action(x) for x in v]
    carr = np.asarray(cond)
    test = [(c, a) for c, a in zip(cond, acts) if c in INTENDED]
    if len(test) != OF_INSTANCE:
        raise Refusal(f'{len(test)} test steps, not {OF_INSTANCE}')
    by = {}
    for c in list(INTENDED) + list(CAL) + list(BLOCK_B):
        m = carr == c
        if not m.any():
            raise Refusal(f'no step of condition {c}')
        a = [acts[i] for i in np.flatnonzero(m)]
        by[c] = {'n': int(m.sum()), 'v_mean': float(v[m].mean()), 'v_sd': float(v[m].std(ddof=1)),
                 'v_min': float(v[m].min()), 'v_max': float(v[m].max()), 'off_axis_mean': float(off[m].mean()),
                 'actions': {k: a.count(k) for k in ('A', 'hold', 'B')},
                 'role': 'test' if c in INTENDED else ('calibration' if c in CAL else 'block B, described only')}
        if c in INTENDED:
            by[c]['intended'] = INTENDED[c]
            by[c]['as_intended'] = a.count(INTENDED[c])
    relay = {}
    for p, (x, y) in RELAY.items():
        lin = by[x]['v_mean'] + by[y]['v_mean']
        relay[p] = {'v_relay': lin, 'relay_action': action(lin), 'v_circuit_mean': by[p]['v_mean'],
                    'intended': INTENDED[p], 'relay_as_intended': action(lin) == INTENDED[p]}
    hits = sum(a == INTENDED[c] for c, a in test)
    return {'z0': [float(z0.real), float(z0.imag)], 'S': s, 'threshold': T, 'as_intended': hits, 'of': len(test),
            'conditions': by, 'relay_rule': relay,
            'steps': [{'condition': c, 'v': float(x), 'action': a} for c, x, a in zip(cond, v, acts)]}


def outcome(per_instance):
    """The declared outcome from the six instance tallies."""
    hits = {k: r['as_intended'] for k, r in per_instance.items()}
    pooled = sum(hits.values())
    ok = all(h >= NEED_INSTANCE for h in hits.values()) and pooled >= NEED_POOLED
    return {'outcome': 'PASS' if ok else 'FAIL', 'as_intended': hits, 'pooled': pooled, 'of_pooled': OF_POOLED,
            'rule': f'every instance at least {NEED_INSTANCE} of {OF_INSTANCE}, and all six at least {NEED_POOLED} of {OF_POOLED}'}


def sha256(path):
    h = hashlib.sha256()
    with open(path, 'rb') as f:
        for b in iter(lambda: f.read(1 << 20), b''):
            h.update(b)
    return h.hexdigest()


def read_steps(path, frozen):
    """Condition label and complex readout z of every analysed step of one export (the warm-up step is dropped)."""
    for d in frozen:
        if d not in sys.path:
            sys.path.insert(0, d)
    import plaid_stats as ps, pipeline as p
    ex = json.load(gzip.open(path, 'rt'))
    seg = [s for s in ex['segments'] if s['sheet'] == 'V1_Exc_L4' and 'sequence' in s['stimulus']]
    if len(seg) != 1:
        raise Refusal(f'{len(seg)} sequence presentations of layer 4, not 1')
    st = seg[0]['stimulus']
    frame = float(st['frame_duration'])
    period = (float(st['step_frames']) + float(st['gap_frames'])) * frame
    lead = float(st['lead_frames']) * frame
    steps = json.loads(st['sequence']) if isinstance(st['sequence'], str) else st['sequence']
    if ps.components_of(steps[0]) != tuple((float(d), float(c)) for d, c in ps.WARMUP):
        raise Refusal('the first step is not the declared warm-up')
    cond = [ps.condition_of(x) for x in steps[1:]]
    if None in cond or np.bincount(cond, minlength=ps.N_CONDITIONS).tolist() != [c[2] for c in ps.CONDITIONS]:
        raise Refusal('the shown steps are not the declared stimulus set')
    ids = ex['neurons']['V1_Exc_L4']['ids']
    w = p.decoder_weights(np.asarray(ex['neurons']['V1_Exc_L4']['orientation_pref'], float), 'balanced')
    sp = seg[0]['spikes']
    trains = [np.sort(np.asarray(sp.get(str(i), []), float)) for i in ids]
    a, b = ps.WINDOWS_MS['D']
    z = []
    for k in range(1, len(steps)):
        lo, hi = lead + period * k + a, lead + period * k + b
        c = np.array([np.searchsorted(t, hi, 'left') - np.searchsorted(t, lo, 'left') for t in trains])
        z.append(complex(np.sum(c * w)))
    return [ps.CONDITIONS[c][0] for c in cond], z


def main(argv):
    design = '--design' in argv
    args = [a for a in argv if a != '--design']
    out, runs = args[0], dict(a.split('=', 1) for a in args[1:])
    root = __file__.rsplit('/work/', 1)[0]
    frozen = [root + '/outputs/felisyn-research/protocol-022/controller',
              root + '/outputs/felisyn-research/protocol-017/controller']
    res = {'scope': 'protocol 024, the two-signal decision rule; simulation only; an engineering rule',
           'design_run': design, 'threshold': T, 'min_scale': MIN_SCALE, 'intended': INTENDED,
           'frozen_sha256': {d.rsplit('/', 2)[-2] + '/' + n: sha256(f'{d}/{n}') for d, n in
                             ((frozen[0], 'plaid_stats.py'), (frozen[1], 'pipeline.py'))},
           'published_z_checked': {},
           'tool_sha256': sha256(__file__), 'exports': {}, 'instances': {}, 'refusals': {}}
    try:
        if res['frozen_sha256'] != FROZEN:
            raise Refusal('the frozen code is not at its recorded hashes')
        published = None if design else json.load(open(f'{root}/{PUBLISHED}'))['measures']
        if not design and set(runs) != set(DECLARED):
            raise Refusal(f'the runs given are {sorted(runs)}, not the six declared instances {sorted(DECLARED)}')
        for name, path in runs.items():
            h = sha256(path)
            res['exports'][name] = h
            if not design and h != DECLARED[name]:
                raise Refusal(f'{name}: the export is not the declared one')
            cond, z = read_steps(path, frozen)
            if not design:
                pub = [complex(a, b) for a, b in published[name[1:]]['per_step']['z_D']]
                if len(pub) != len(z) or any(x != y for x, y in zip(pub, z)):
                    raise Refusal(f'{name}: the readout differs from the published z_D')
                res['published_z_checked'][name] = len(pub)
            res['instances'][name] = decide(cond, z)
    except Refusal as exc:
        res['refusals']['refusal'] = str(exc)
    except (KeyError, OSError, ValueError, EOFError, TypeError, IndexError) as exc:     # unreadable or malformed input
        res['refusals']['refusal'] = f'{type(exc).__name__}: {exc}'
    if design:
        res['outcome'] = 'none: a design run on the pilot'
    elif res['refusals']:
        res['outcome'] = 'NOT INTERPRETABLE'
    else:
        res.update(outcome(res['instances']))
    with open(out, 'w') as f:
        json.dump(res, f, indent=1, sort_keys=True)
        f.write('\n')
    print(res['outcome'], {k: f"{r['as_intended']}/{r['of']}" for k, r in res['instances'].items()}, res['refusals'])


if __name__ == '__main__':
    main(sys.argv[1:])
