"""Reproducible MaleCNS-derived reservoir and supervised golf readout.

CPU only. Input files are the official v1.0 Feather downloads. Synapse counts
are anatomical measurements; signs, scaling, dynamics and sensory encoding
are modeling assumptions. Neither these assumptions nor the golf examples
claim biological validation. No graph weights are learned.
"""
import argparse
import gzip
import hashlib
import json
from pathlib import Path

import numpy as np
import pyarrow.feather as feather
from scipy.sparse import csr_matrix

ROOT = Path(__file__).resolve().parent
BASE = 'https://storage.googleapis.com/flyem-male-cns/v1.0/connectome-data/flat-connectome/'
FILES = {
    'weights.feather': 'connectome-weights-male-cns-v1.0-minconf-0.5.feather',
    'neurotransmitters.feather': 'body-neurotransmitters-male-cns-v1.0.feather',
}


def samples(count, seed):
    rng = np.random.default_rng(seed)
    distance = np.exp(rng.uniform(np.log(.15), np.log(40), count))
    delta = rng.uniform(-np.pi, np.pi, count)
    lie = rng.integers(0, 5, count)  # tee, green, sand, rough, fairway
    # Include the actual joint domain: putting greens are small, tee shots long.
    distance[lie == 1] = rng.uniform(.15, 9, (lie == 1).sum())
    distance[lie == 0] = rng.uniform(8, 40, (lie == 0).sum())
    x = np.column_stack([np.ones(count), distance / 40,
                         np.sin(delta), np.cos(delta),
                         lie == 0, lie == 1, lie == 2, lie == 3]).astype('float32')
    # A documented teacher, not reinforcement learning or spontaneous discovery.
    # Aim along fairway for approaches, toward flag for putts/short shots.
    direct = (lie == 1) | (distance < 4.4)
    direction = np.where(direct, delta, 0)
    cover = np.where(lie == 1, distance, np.minimum(10, distance * .34 + 2.2))
    cover = np.minimum(distance + 1, cover)
    club = np.where(lie == 0, 0, np.where((lie == 1) | ((distance < 2.5) & (lie != 2)), 2, 1))
    y = np.column_stack([np.sin(direction), np.cos(direction), cover / 11,
                         club == 0, club == 1, club == 2]).astype('float32')
    return x, y


def features(matrix, inputs, encoding, outputs, x):
    result = []
    for start in range(0, len(x), 256):
        batch = x[start:start + 256]
        drive = np.zeros((matrix.shape[0], len(batch)), dtype='float32')
        drive[inputs] = np.tanh(encoding @ batch.T)
        state = drive.copy()
        for _ in range(4):
            state = np.tanh(drive + matrix @ state)
        result.append(state[outputs].T)
    return np.concatenate(result)


def metrics(prediction, expected):
    error = np.arctan2(prediction[:, 0], prediction[:, 1]) - np.arctan2(expected[:, 0], expected[:, 1])
    error = np.abs(np.arctan2(np.sin(error), np.cos(error))) * 180 / np.pi
    return {'aim_mae_degrees': float(error.mean()),
            'range_mae_metres': float(np.abs(prediction[:, 2] - expected[:, 2]).mean() * 11),
            'club_accuracy': float((prediction[:, 3:].argmax(1) == expected[:, 3:].argmax(1)).mean())}


def build(data):
    somas = json.load(gzip.open(ROOT / 'assets/somas-lite.json.gz'))
    ids = np.array(somas['id'], dtype='int64')
    raw = feather.read_table(data / 'weights.feather').to_pandas()
    edges = raw[raw.body_pre.isin(ids) & raw.body_post.isin(ids)].copy()
    induced_count, induced_synapses = len(edges), int(edges.weight.sum())
    # Bound CPU cost: >=5 synapses, strongest 8 incoming per postsynaptic cell.
    edges = edges[edges.weight >= 5].sort_values(['body_post', 'weight', 'body_pre'], ascending=[True, False, True])
    edges = edges.groupby('body_post', sort=False).head(8)
    lookup = {int(body): i for i, body in enumerate(ids)}
    pre = np.array([lookup[int(body)] for body in edges.body_pre], dtype='int32')
    post = np.array([lookup[int(body)] for body in edges.body_post], dtype='int32')
    counts = edges.weight.to_numpy(dtype='int32')
    nt = feather.read_table(data / 'neurotransmitters.feather').to_pandas().set_index('body').consensus_nt
    transmitters = [str(nt.get(int(body), 'unclear')) for body in ids]
    # GABA and glutamate negative; all others positive. Deliberately simplified.
    signs = np.array([-1 if t in ('gaba', 'glutamate') else 1 for t in transmitters], dtype='float32')
    weights = np.sqrt(counts).astype('float32')
    denominator = np.bincount(post, weights=weights, minlength=len(ids))
    weights *= signs[pre] * 1.25 / denominator[post]
    matrix = csr_matrix((weights, (post, pre)), shape=(len(ids), len(ids)))
    inputs = np.array([i for i, sc in enumerate(somas['sc']) if sc.startswith(('ol', 'visual'))], dtype='int32')
    # Read from descendants of sensory cells, never from directly driven cells.
    reach = np.zeros(len(ids)); reach[inputs] = 1
    for _ in range(4):
        reach += abs(matrix) @ reach
    input_set = set(inputs.tolist())
    candidates = np.array([i for i in range(len(ids)) if i not in input_set and reach[i] > .01])
    rng = np.random.default_rng(1977)
    outputs = rng.choice(candidates, min(768, len(candidates)), replace=False)
    encoding = rng.normal(0, 1.4, (len(inputs), 8)).astype('float32')
    x, y = samples(12000, 101)
    activity = features(matrix, inputs, encoding, outputs, x)
    mean = activity.mean(0); scale = np.maximum(activity.std(0), .005)
    z = np.column_stack([(activity - mean) / scale, np.ones(len(x))]).astype('float64')
    penalty = np.eye(z.shape[1]) * .2; penalty[-1, -1] = 0
    readout = np.linalg.solve(z.T @ z + penalty, z.T @ y)
    # Validation chooses no parameters. Test seed fixed separately before running.
    test_x, test_y = samples(3000, 90914)
    def predict(m):
        f = features(m, inputs, encoding, outputs, test_x)
        return np.column_stack([(f - mean) / scale, np.ones(len(f))]) @ readout
    shuffled = matrix.copy(); shuffled.indices = rng.permutation(shuffled.indices)
    intact_prediction = predict(matrix)
    constant_prediction = np.tile(np.r_[np.zeros(len(outputs)), 1] @ readout, (len(test_x), 1))
    report = {'task': 'Supervised imitation of the published golf teacher on synthetic sensor states',
              'training_examples': len(x), 'test_examples': len(test_x), 'training_seed': 101, 'test_seed': 90914,
              'normal': metrics(intact_prediction, test_y),
              'zero_edges': metrics(predict(matrix * 0), test_y),
              'shuffled_presynaptic_ids': metrics(predict(shuffled), test_y),
              'constant_activity': metrics(constant_prediction, test_y),
              'range_metric': 'Raw decoder output before the runtime 0.15 to 11 metre safety clamp.',
              'limits': 'Held-out teacher imitation, not golf-score evaluation, biological validation, reinforcement learning, or discovery of golf rules.'}
    provenance = {'dataset': 'male-cns:v1.0', 'license': 'CC BY 4.0',
                  'source_page': 'https://male-cns.janelia.org/download/',
                  'sources': [{'url': BASE + remote, 'sha256': hashlib.file_digest(open(data / local, 'rb'), 'sha256').hexdigest()} for local, remote in FILES.items()],
                  'displayed_neurons': len(ids), 'induced_edges': induced_count, 'induced_synapses': induced_synapses,
                  'retained_edges': len(pre), 'retained_synapses': int(counts.sum()),
                  'active_neurons': int(len(np.unique(np.concatenate([pre, post])))),
                  'selection': 'Existing 11067 soma IDs; >=5 synapses; top 8 incoming edges per cell, count descending, bodyId ascending.',
                  'model': 'Four tanh recurrent propagation steps; sqrt(count), incoming-normalized gain 1.25. Fixed graph and random visual-cell encoder. Ridge-trained readout; no direct input-to-readout connection.',
                  'sign_assumption': 'GABA/glutamate negative; all other or unclear transmitter predictions positive. Not receptor-specific physiological signs.',
                  'readout_neurons': len(outputs), 'input_neurons': len(inputs),
                  'encoder_seed': 1977, 'ridge_penalty': .2,
                  'soma_file_sha256': hashlib.file_digest(open(ROOT / 'assets/somas-lite.json.gz', 'rb'), 'sha256').hexdigest()}
    model = {'version': 1, 'count': len(ids), 'ids': ids.tolist(), 'pre': pre.tolist(), 'post': post.tolist(),
             'synapses': counts.tolist(), 'weights': weights.tolist(), 'inputs': inputs.tolist(), 'encoding': encoding.tolist(),
             'outputs': outputs.tolist(), 'mean': mean.tolist(), 'scale': scale.tolist(), 'readout': readout.tolist(),
             'provenance': provenance}
    target = ROOT / 'assets/connectome-policy.json.gz'
    with open(target, 'wb') as f:
        with gzip.GzipFile(fileobj=f, mode='wb', mtime=0) as gz:
            gz.write(json.dumps(model, separators=(',', ':')).encode())
    (ROOT / 'connectome-evaluation.json').write_text(json.dumps(report, indent=2) + '\n')
    (ROOT / 'connectome-provenance.json').write_text(json.dumps(provenance, indent=2) + '\n')
    fixtures=[]
    for xrow, prediction in zip(test_x[:24], intact_prediction[:24]):
        fixtures.append({'sensors': {'distance': float(xrow[1]*40), 'fairwayYaw': 0,
                         'flagYaw': float(np.arctan2(xrow[2], xrow[3])), 'onTee': bool(xrow[4]),
                         'lie': 'green' if xrow[5] else 'sand' if xrow[6] else 'rough' if xrow[7] else 'fairway'},
                         'raw': prediction.tolist()})
    (ROOT / 'connectome-fixtures.json').write_text(json.dumps(fixtures, indent=2)+'\n')
    print(json.dumps({'provenance': provenance, 'evaluation': report}, indent=2), flush=True)


if __name__ == '__main__':
    parser = argparse.ArgumentParser(); parser.add_argument('--data', type=Path, required=True)
    build(parser.parse_args().data)
