"""Exact recorder counts on the pinned LSV1M realization, without building any connection.

Builds the unchanged model's sheets with the released seeds and 16 MPI ranks, stops at the
first connector call (all sheets exist by then; no projection does), and applies the released
recorder definitions of both recipes (param/ and param_spont/) with Mozaik's own
Sheet.setup_to_record_list(). No simulation is run.

Run from anywhere, as the research user, with the pinned runtime:
  FELISYN_MODEL=/opt/felisyn/protocol-006/model FELISYN_OUT=/path/out.json \
  mpirun --oversubscribe -np 16 python count_recorders.py nest 1 param_spont/defaults \
      results_dir '/path/results/' trial 0 felisyn_recorder_probe
Mozaik reads the command line itself, so configuration comes from the two variables above.
"""
import hashlib, json, os, sys
from pathlib import Path

MODEL = Path(os.environ['FELISYN_MODEL']).resolve()
OUT = Path(os.environ['FELISYN_OUT']).resolve()
sys.path.insert(0, str(MODEL))
os.chdir(MODEL)

from mpi4py import MPI
comm = MPI.COMM_WORLD
assert comm.Get_size() == 16, 'Use the 16 ranks of the declared run'
import nest
nest.Install('stepcurrentmodule')
import numpy
import model as M
import mozaik
from mozaik.controller import prepare_workflow
from mozaik.tools.distribution_parametrization import load_parameters

# S5 Table, panel G (official PLOS supplement journal.pcbi.1012342.s007), transcribed in this package.
S5 = {
    'spontaneous': {'V1_Exc_L4': (12128, 400), 'V1_Inh_L4': (3467, 399), 'V1_Exc_L2/3': (12176, 400), 'V1_Inh_L2/3': (3487, 398)},
    'evoked': {'V1_Exc_L4': (838, 62), 'V1_Inh_L4': (152, 22), 'V1_Exc_L2/3': (862, 80), 'V1_Inh_L2/3': (173, 26)},
}
CORTICAL = ['l4_cortex_exc', 'l4_cortex_inh', 'l23_cortex_exc', 'l23_cortex_inh']

captured = {}


class StopBeforeConnections(Exception):
    pass


def stop(model, *args, **kwargs):
    captured['model'] = model
    raise StopBeforeConnections


M.GaborConnector = stop  # the first connector called after all sheets are created
sim, num_threads, parameters = prepare_workflow('SelfSustainedPushPull', M.SelfSustainedPushPull)
try:
    M.SelfSustainedPushPull(sim, num_threads, parameters)
    raise RuntimeError('Model construction finished without reaching the first connector')
except StopBeforeConnections:
    pass
model = captured['model']

recipes = {'param': load_parameters('param/defaults', {}), 'param_spont': load_parameters('param_spont/defaults', {})}
result = {'sheets': {}, 'mpi_ranks': comm.Get_size(), 'num_threads': num_threads}
for key in CORTICAL:
    name = parameters.sheets[key].params.name
    sheet = model.sheets[name]
    positions = numpy.asarray(sheet.pop.positions)
    entry = {'parameter_key': key, 'neurons': len(sheet.pop),
             'positions_sha256': hashlib.sha256(numpy.ascontiguousarray(positions).tobytes()).hexdigest(), 'recipes': {}}
    for recipe, tree in recipes.items():
        sheet.parameters.recorders = tree.sheets[key].params.recorders
        sheet.setup_to_record_list()
        selected = {var: sorted(int(i) for i in idx) for var, idx in sheet.to_record.items()}
        entry['recipes'][recipe] = {
            'counts': {var: len(idx) for var, idx in selected.items()},
            'selection_sha256': hashlib.sha256(json.dumps(selected, sort_keys=True).encode()).hexdigest(),
        }
        entry['recipes'][recipe]['_spikes'] = selected.get('spikes', [])
    a, b = set(entry['recipes']['param']['_spikes']), set(entry['recipes']['param_spont']['_spikes'])
    entry['spike_sets'] = {'param_also_in_param_spont': len(a & b), 'param_total': len(a)}
    for recipe in recipes:
        del entry['recipes'][recipe]['_spikes']
    result['sheets'][name] = entry

# Every rank must have computed the same realization and the same selections.
digest = hashlib.sha256(json.dumps(result['sheets'], sort_keys=True).encode()).hexdigest()
digests = comm.gather(digest, root=0)
if comm.rank == 0:
    result['ranks_agree'] = len(set(digests)) == 1
    comparison = {}
    for label, recipe in [('spontaneous', 'param_spont'), ('evoked', 'param')]:
        for name, (spikes, analog) in S5[label].items():
            c = result['sheets'][name]['recipes'][recipe]['counts']
            analog_counts = {c.get(v) for v in ('v', 'gsyn_exc', 'gsyn_inh')}
            comparison[f'{label}|{name}'] = {
                'S5_spikes': spikes, 'computed_spikes': c.get('spikes'), 'spikes_equal': c.get('spikes') == spikes,
                'S5_analog': analog, 'computed_analog': sorted(x for x in analog_counts if x is not None),
                'analog_equal': analog_counts == {analog},
            }
    result['S5_comparison'] = comparison
    result['all_16_counts_equal'] = all(v['spikes_equal'] and v['analog_equal'] for v in comparison.values())
    result['seeds'] = {'pynn_seed': parameters['pynn_seed'] if 'pynn_seed' in parameters else None,
                       'mozaik_seed_in_parameters': 'mozaik_seed' in parameters,
                       'mozaik_seed_effective': parameters['mozaik_seed'] if 'mozaik_seed' in parameters else 513}
    result['scope'] = 'Sheets only; stopped before the first connector. No connection, stimulus or simulation.'
    OUT.write_text(json.dumps(result, indent=2) + '\n')
    print(json.dumps({'all_16_counts_equal': result['all_16_counts_equal'], 'ranks_agree': result['ranks_agree']}))
comm.Barrier()
