"""Tests of protocol 026's tools. Light tests read no export; the end-to-end test builds a small synthetic export."""
import gzip, hashlib, json, os, sys, tempfile, unittest
import numpy as np
sys.dont_write_bytecode = True
import decide_026 as d, compare_026 as c, make_sequence_026 as m

V1 = {0: 0.0, 7.5: 0.3, 15: 0.6, 22.5: 0.85, 30: 1.0}


def wta(a, b):
    """Winner-take-all with a hold on ties; a lead of 7.5 gives 0.42."""
    if a == b:
        return 0.0
    if a and b and abs(a - b) == 7.5:
        return -0.42 if a > b else 0.42
    return (-V1[a] if a > b else V1[b]) * (0.8 if a and b else 1.0)


def instance(v_of=wta, scale=5000.0, z0=50 + 200j, k=18, by_position=(1.0, 1.0, 1.0)):
    doc = m.build(k)
    st = doc['steps'][1:]
    cells = [(s['a'], s['b']) for s in st]
    labels = [s['condition'] for s in st]
    blocks = [s['block'] for s in st]
    z = [z0 - 1j * scale * v_of(s['a'], s['b']) * (by_position[s['position'] - 1] if s['block'] else 1.0) for s in st]
    return cells, labels, z, blocks


class Sequence(unittest.TestCase):
    def test_counts_and_rule(self):
        for k in d.SEEDS:
            st = m.build(k)['steps']
            self.assertEqual(len(st), 109)
            self.assertEqual(st[0]['condition'], 'warm-up')
            an = st[1:]
            singles = [s['condition'] for s in an if s['block'] == 0]
            self.assertEqual({l: singles.count(l) for l in set(singles)}, {'blank': 8, 'A30': 8, 'B30': 8})
            blocks = {}
            for i, s in enumerate(an):
                if s['block']:
                    blocks.setdefault(s['block'], []).append((i, s['condition'], s['position']))
            self.assertEqual(len(blocks), 28)
            for b in blocks.values():
                self.assertEqual([p for _, _, p in b], [1, 2, 3])
                self.assertEqual(b[2][0] - b[0][0], 2)
                self.assertEqual(len({l for _, l, _ in b}), 1)
            per_cell = {}
            for b in blocks.values():
                per_cell[b[0][1]] = per_cell.get(b[0][1], 0) + 1
            self.assertEqual(sorted(per_cell.values()), [1] * 8 + [2] * 10)
            labels = [s['condition'] for s in an]
            self.assertFalse(any(len(set(labels[i:i + 4])) == 1 for i in range(len(labels) - 3)))
            for i, s in enumerate(an):               # no single directly repeats its neighbour's cell, the warm-up included
                if not s['block']:
                    before = st[i]                   # st[i] is the step before an[i]
                    self.assertNotEqual((before['a'], before['b']), (s['a'], s['b']))
                    if i + 1 < len(an):
                        self.assertNotEqual((an[i + 1]['a'], an[i + 1]['b']), (s['a'], s['b']))
            self.assertTrue(all(s['contrast'] + s.get('contrast2', 0) <= 60 for s in st))
            for h in (1, 2):                     # each half: one block of every two-block cell, 14 blocks, 12 singles
                half = [s for s in an if s['half'] == h]
                self.assertEqual(len({s['block'] for s in half if s['block']}), 14)
                self.assertEqual(sum(1 for s in half if not s['block']), 12)

    def test_declared_files(self):
        here = os.path.dirname(os.path.abspath(__file__))
        for k, h in d.SEQUENCES.items():
            p = f'{here}/sequences/sequence-026-s{k}.json'
            self.assertEqual(hashlib.sha256(open(p, 'rb').read()).hexdigest(), h)
            self.assertEqual(json.load(open(p)), json.loads(json.dumps(m.build(k))))


class Rule(unittest.TestCase):
    def test_roles(self):
        self.assertEqual(d.role(7.5, 7.5), ('equal', 'hold'))
        self.assertEqual(d.role(22.5, 15), ('close', 'A'))
        self.assertEqual(d.role(22.5, 30), ('close', 'B'))
        self.assertEqual(d.role(0, 15), ('clear', 'B'))
        self.assertEqual(d.role(22.5, 7.5), ('clear', 'A'))
        self.assertEqual(d.role(7.5, 0), ('weak', None))
        with self.assertRaises(d.Refusal):
            d.role(30, 0)
        with self.assertRaises(d.Refusal):
            d.role(30, 7.5)

    def test_clean_instance(self):
        r = d.decide(*instance())
        self.assertEqual((r['as_intended'], r['of']), (26, 26))
        self.assertAlmostEqual(r['S'], 5000.0)
        self.assertEqual(r['by_kind']['close']['as_intended'], 12)
        self.assertEqual(r['by_kind']['close']['first_step'], 12)
        self.assertEqual(len(r['blocks']), 28)
        self.assertEqual(len(r['singles']), 24)
        self.assertEqual(r['by_kind']['equal']['actions'], {'A': 0, 'hold': 8, 'B': 0})
        self.assertEqual(r['by_kind']['close']['actions'], {'A': 6, 'hold': 0, 'B': 6})
        self.assertIsNone(r['by_kind']['weak']['as_intended'])
        self.assertEqual(r['by_kind']['weak']['actions']['hold'], 2)        # 0.3 alone holds
        self.assertAlmostEqual(r['calibration']['A30']['v_mean'], -1.0)
        self.assertAlmostEqual(r['calibration']['B30']['v_mean'], 1.0)

    def test_the_mean_of_three_decides(self):
        # the response to a repeated stimulus shrinks: 0.42, 0.315, 0.21 has mean 0.315: a close block holds
        r = d.decide(*instance(by_position=(1.0, 0.75, 0.5)))
        k = r['by_kind']['close']
        self.assertEqual((k['as_intended'], k['first_step'], k['second_step'], k['third_step']), (0, 12, 0, 0))
        self.assertEqual(k['first_two'], 12)                       # 0.3675
        self.assertEqual(r['by_kind']['clear']['as_intended'], 6)
        self.assertEqual(r['as_intended'], 14)
        self.assertAlmostEqual(k['mean_v_toward_by_position'][2], 0.21)

    def test_one_noisy_step_is_outvoted(self):
        cells, labels, z, blocks = instance()
        i = next(i for i, (cl, b) in enumerate(zip(cells, blocks)) if cl == (15.0, 7.5) and b)
        z[i] = 50 + 200j - 1j * 5000.0 * -0.25                     # one step of the block falls into the hold band
        r = d.decide(cells, labels, z, blocks)
        blk = next(b for b in r['blocks'] if b['block'] == blocks[i])
        self.assertEqual(blk['step_actions'][0], 'hold')
        self.assertEqual(blk['action'], 'A')                       # mean -0.363
        self.assertEqual(r['as_intended'], 26)

    def test_refusals(self):
        cells, labels, z, blocks = instance()
        with self.assertRaises(d.Refusal):
            d.decide(*instance(scale=1499.0))
        with self.assertRaises(d.Refusal):
            d.decide(*instance(scale=-5000.0))
        bad = list(blocks)
        i = next(i for i, b in enumerate(bad) if b == 3)
        bad[i] = 0                                                  # a block step passed off as a single
        with self.assertRaises(d.Refusal):
            d.decide(cells, labels, z, bad)
        swapped = list(blocks)
        i, j = swapped.index(1), swapped.index(2)
        swapped[i], swapped[j] = 2, 1                               # blocks that are not consecutive steps of one cell
        with self.assertRaises(d.Refusal):
            d.decide(cells, labels, z, swapped)

    def test_thresholds(self):
        self.assertEqual([d.action(x) for x in (0.33, 0.32, -0.32, -0.33)], ['B', 'hold', 'hold', 'A'])

    def test_outcome(self):
        mk = lambda hits: {f's{18 + i}': {'as_intended': h} for i, h in enumerate(hits)}
        self.assertEqual(d.outcome(mk([26, 26, 26]))['outcome'], 'PASS')
        self.assertEqual(d.outcome(mk([26, 26, 23]))['outcome'], 'FAIL')
        self.assertEqual(d.outcome(mk([26, 25, 24]))['outcome'], 'PASS')      # 75
        self.assertEqual(d.outcome(mk([25, 25, 24]))['outcome'], 'FAIL')      # 74
        self.assertEqual(d.outcome(mk([26, 26]))['outcome'], 'FAIL')


class Compare(unittest.TestCase):
    def test_agree_and_disagree(self):
        def export(tmp, name, run, t=1.0, spike=10.0):
            ex = {'run_seconds': t, 'argv': ['nest', '4', 'pynn_seed', '18', run], 'resolved': {'pynn_seed': 18},
                  'segments': [{'sheet': 'X', 'stimulus': {}, 'spikes': {'1': [1.0, spike], '2': []}}]}
            p = os.path.join(tmp, name)
            with gzip.open(p, 'wt') as f:
                json.dump(ex, f)
            return p
        with tempfile.TemporaryDirectory() as tmp:
            a, b = export(tmp, 'a.json.gz', 'm26-s18-a1'), export(tmp, 'b.json.gz', 'm26-s18-a2', 2.0)
            x = export(tmp, 'x.json.gz', 'm26-s18-a3', 2.0, spike=10.01)
            self.assertTrue(c.compare(a, b)['agree'])
            self.assertFalse(c.compare(a, x)['agree'])
            self.assertFalse(c.compare(a, export(tmp, 's.json.gz', 'm26-s18-a1', 3.0))['agree'])


class EndToEnd(unittest.TestCase):
    """main() on synthetic exports with the queue's records: the reading, the lead-in check and the pairing rule."""

    def export(self, folder, k, root, wrong_seed=False):
        ref = json.load(open(f'{root}/{d.LEADIN[0]}'))
        doc = m.build(k)
        shown = []
        for s in doc['steps']:
            e = [s['orientation_rad'], s['contrast']]
            if 'contrast2' in s:
                e += [s['orientation2_rad'], s['contrast2']]
            shown.append(e)
        n = 180
        pref = [np.pi * i / n for i in range(n)]
        ids = list(range(1000, 1000 + n))
        spikes = {str(i): [] for i in ids}
        for step, s in enumerate(doc['steps']):
            v = 0.0 if step == 0 else wta(s['a'], s['b'])               # v < 0: cells near 45 degrees fire
            t0 = 504.0 + 357.0 * step + 41.0
            for i, ph in zip(ids, pref):
                drive = max(0.0, -v * np.sin(2 * ph))                   # sin(2 * 45 deg) = 1
                spikes[str(i)] += [t0 + 2.0 * j for j in range(int(round(5 + 60 * drive)))]
        stim = {'sequence': json.dumps(shown), 'frame_duration': 7.0, 'step_frames': 30, 'gap_frames': 21, 'lead_frames': 72,
                'duration_ms': 504.0 + 357.0 * len(shown)}
        seg = [{'sheet': 'V1_Exc_L4', 'stimulus': stim, 't_start_ms': 0.0, 'spikes': spikes}]
        for sh in ('X_ON', 'X_OFF'):
            seg.append({'sheet': sh, 'stimulus': stim, 't_start_ms': 0.0, 'spikes': ref['trains'][sh]})
        seed = k + 1 if wrong_seed else k
        ex = {'run_seconds': 1.0, 'argv': ['nest', '4', 'pynn_seed', str(seed), f'm26-s{k}-a1'], 'resolved': {'pynn_seed': seed},
              'neurons': {'V1_Exc_L4': {'ids': ids, 'orientation_pref': pref}}, 'segments': seg}
        p = f'{folder}/s{k}-a1.json.gz'
        with gzip.open(p, 'wt') as f:
            json.dump(ex, f)
        h = hashlib.sha256(open(p, 'rb').read()).hexdigest()
        open(f'{folder}/s{k}-status.txt', 'w').write('agreed a1 a2\n')
        json.dump({'agree': True, 'sha256': [h, 'x']}, open(f'{folder}/s{k}-a1-a2-compare.json', 'w'))
        return p

    def test_main(self):
        root = os.path.abspath(__file__).rsplit('/work/', 1)[0]
        with tempfile.TemporaryDirectory() as tmp:
            runs = [f's{k}={self.export(tmp, k, root)}' for k in (18, 19, 20)]
            d.main([f'{tmp}/out.json'] + runs)
            r = json.load(open(f'{tmp}/out.json'))
            self.assertEqual(r['refusals'], {})
            self.assertEqual((r['outcome'], r['pooled']), ('PASS', 78))
            self.assertAlmostEqual(r['instances']['s18']['calibration']['A30']['v_mean'], -1.0, places=1)
            d.main([f'{tmp}/again.json'] + runs)                              # computed twice: identical bytes
            self.assertEqual(open(f'{tmp}/out.json', 'rb').read(), open(f'{tmp}/again.json', 'rb').read())
            reserve = [runs[0], runs[1], f's21={self.export(tmp, 21, root)}']   # a trio with the reserve
            d.main([f'{tmp}/res.json'] + reserve)
            self.assertEqual(json.load(open(f'{tmp}/res.json'))['outcome'], 'PASS')
            open(f'{tmp}/s19-status.txt', 'w').write('agreed a2 a3\n')       # not the earlier run of the pair
            d.main([f'{tmp}/out2.json'] + runs)
            self.assertEqual(json.load(open(f'{tmp}/out2.json'))['outcome'], 'NOT INTERPRETABLE')
            d.main([f'{tmp}/out3.json'] + runs[:2])                          # two instances
            self.assertEqual(json.load(open(f'{tmp}/out3.json'))['outcome'], 'NOT INTERPRETABLE')
        with tempfile.TemporaryDirectory() as tmp:
            runs = [f's{k}={self.export(tmp, k, root, wrong_seed=(k == 20))}' for k in (18, 19, 20)]
            d.main([f'{tmp}/out.json'] + runs)
            self.assertIn('not a run of pynn_seed 20', json.load(open(f'{tmp}/out.json'))['refusals']['refusal'])


if __name__ == '__main__':
    unittest.main()
