"""Which averaging rule gives the conductance values stated in the paper's text?

The paper's text gives 1.15 nS (excitatory) and 3.56 nS (inhibitory) as averages over the model.
The Figure 4 code logs population-weighted means, which give about 4.09 nS for the inhibitory
conductance (benchmark-004). This program combines the four population means of run 011 (our full
run) with several averaging rules and checks which rule rounds to both values in the text.
Descriptive only: no simulation and no new analysis of recordings.

Inputs, each checked against a recorded SHA-256 before use:
  RESEARCH/run-011/analysis/analysis-parity.json   our run's population means (our code and the
                                                    authors' Figure 4 code agree) and neuron counts
  RESEARCH/benchmark-004/reference-means.csv        the values printed in Figure 4
  RESEARCH/protocol-007/model/visualization_functions.py   the pinned Figure 4 code
  sources/antolik-2024.xml (this package)           the article text (Europe PMC full-text XML)

  python average_conductances.py --research RESEARCH --output OUTPUT_DIR
"""
import argparse, csv, hashlib, json, math, re
from decimal import Decimal
from pathlib import Path

HERE = Path(__file__).resolve().parent
EXPECTED = {  # recorded hashes: run-011 and benchmark-004 manifests, protocol-007 manifest, audit-001 retrieval record
    'parity': ('run-011/analysis/analysis-parity.json', 'affb604cae6deed990890f2990df4c77d0ade5167edf46146480024bb95bdd9d'),
    'printed': ('benchmark-004/reference-means.csv', 'fb460c31da5296601d91d4b2cb198c77f96517fb05dba74d2e115347bd9e11bf'),
    'code': ('protocol-007/model/visualization_functions.py', '1f1bc47d44629c1a9ff81a7a59771217799305fe2cd33ff044c75db4c74eaabd'),
    'article': (None, '0ffddca89f9a2ad227ec048149b4c4f6b8aa16d69a4f083584d67bb0cedc8236'),
}
# Population sizes as built (run-011 run-summary.json network.sheets; recipe-check-009 construction log).
POPS = [('V1_Exc_L4', 'L4 excitatory', 37500), ('V1_Inh_L4', 'L4 inhibitory', 9375),
        ('V1_Exc_L2/3', 'L2/3 excitatory', 37500), ('V1_Inh_L2/3', 'L2/3 inhibitory', 9375)]
METRICS = {'exc': 'exc_conductance_nS', 'inh': 'inh_conductance_nS'}
CODE_LINES = {'exc': 538, 'inh': 549}


def checked(path, expected):
    data = Path(path).read_bytes()
    digest = hashlib.sha256(data).hexdigest()
    if digest != expected:
        raise SystemExit(f'SHA-256 mismatch for {path}: {digest} != {expected}')
    return data, digest


def sentence_with(text, needle):
    """The sentence of the article's plain text that contains needle, verbatim."""
    i = text.index(needle)
    start = text.rfind('. ', 0, i) + 2
    end = text.index('. ', i) + 1
    return text[start:end].strip()


def weighted(values, weights):
    return sum(v * w for v, w in zip(values, weights)) / sum(weights)


def require(condition, message):
    if not condition:
        raise SystemExit(message)


def main():
    ap = argparse.ArgumentParser(description=__doc__.split('\n')[0])
    ap.add_argument('--research', required=True, help='directory holding run-011, benchmark-004 and protocol-007')
    ap.add_argument('--article', default=str(HERE / 'sources/antolik-2024.xml'))
    ap.add_argument('--output', required=True)
    args = ap.parse_args()
    research = Path(args.research)
    inputs = {}
    raw = {}
    for key, (rel, sha) in EXPECTED.items():
        path = Path(args.article) if key == 'article' else research / rel
        raw[key], digest = checked(path, sha)
        inputs[key] = {'path': rel or 'sources/antolik-2024.xml', 'sha256': digest}

    parity = json.loads(raw['parity'])
    means = {(c['population'], c['metric']): (c['independent'], c['upstream'])
             for c in parity['comparisons'] if c['statistic'] == 'mean'}
    sds = {(c['population'], c['metric']): c['independent'] for c in parity['comparisons'] if c['statistic'] == 'sd'}
    recorded = {p['population']: {m: p['analog'][m]['neurons'] for m in METRICS.values()} for p in parity['populations']}
    printed = {r['population']: r for r in csv.DictReader(raw['printed'].decode().splitlines())}

    plain = re.sub(r'\s+', ' ', re.sub(r'<[^>]+>', '', raw['article'].decode('utf-8')))
    prose = {'exc': {'value_nS': '1.15', 'sentence': sentence_with(plain, '(1.15 nS, averaged across all layers)')},
             'inh': {'value_nS': '3.56', 'sentence': sentence_with(plain, 'was 3.56 nS when averaged across all model neurons')}}
    code_lines = raw['code'].decode('utf-8').splitlines()
    code = {k: {'line': n, 'text': code_lines[n - 1].strip()} for k, n in CODE_LINES.items()}
    for k in code:  # the logged mean must be the (1, 0.25, 1, 0.25) / 2.5 formula this program reproduces
        require('+0.25*' in code[k]['text'] and code[k]['text'].endswith(')/2.5))'), f'unexpected code line: {code[k]}')

    populations, run_means, label_means, run_ses = [], {k: [] for k in METRICS}, {k: [] for k in METRICS}, {k: [] for k in METRICS}
    for pid, label, size in POPS:
        row = {'population': pid, 'label': label, 'model_neurons': size}
        for k, metric in METRICS.items():
            ours, upstream = means[(pid, metric)]
            require(abs(ours - upstream) <= 1e-10 + 1e-12 * abs(upstream), f'parity broken for {pid} {metric}')
            row[f'{k}_mean_nS'] = ours
            row[f'{k}_sd_across_neurons_nS'] = sds[(pid, metric)]
            row[f'{k}_recorded_neurons'] = recorded[pid][metric]
            run_ses[k].append(sds[(pid, metric)] / math.sqrt(recorded[pid][metric]))
            row[f'{k}_printed_nS'] = printed[label][metric]
            run_means[k].append(ours)
            label_means[k].append(float(printed[label][metric]))
        populations.append(row)

    rules = [
        ('size_weighted', 'Weighted by population size (37,500 : 9,375 : 37,500 : 9,375); identical to the Figure 4 code\'s logged mean (L4E + 0.25 L4I + L23E + 0.25 L23I) / 2.5',
         lambda k: [p[2] for p in POPS]),
        ('recorded_pooled', 'Pooled over the recorded neurons (each population weighted by its recorded neuron count)',
         lambda k: [recorded[p[0]][METRICS[k]] for p in POPS]),
        ('unweighted', 'Unweighted mean of the four population means', lambda k: [1, 1, 1, 1]),
    ]
    results = []
    for name, description, weights_for in rules:
        entry = {'rule': name, 'description': description}
        for k in METRICS:
            w = weights_for(k)
            target = Decimal(prose[k]['value_nS'])
            half_unit = float(Decimal(1).scaleb(target.as_tuple().exponent)) / 2
            value = weighted(run_means[k], w)
            from_labels = weighted(label_means[k], w)
            se = math.sqrt(sum((wi / sum(w) * s) ** 2 for wi, s in zip(w, run_ses[k])))
            entry[k] = {'weights': w, 'run_011_nS': value, 'sampling_se_nS': se,
                        'run_011_rounds_to_text_value': abs(value - float(target)) <= half_unit,
                        'printed_labels_nS': from_labels,
                        'printed_labels_interval_nS': [from_labels - 0.05, from_labels + 0.05],
                        'text_value_inside_printed_labels_interval': from_labels - 0.05 <= float(target) <= from_labels + 0.05}
        results.append(entry)
    code_formula = {k: (run_means[k][0] + 0.25 * run_means[k][1] + run_means[k][2] + 0.25 * run_means[k][3]) / 2.5 for k in METRICS}
    for k in METRICS:  # the code's formula and size weighting are the same number
        require(abs(code_formula[k] - results[0][k]['run_011_nS']) < 1e-12, 'code formula differs from size weighting')

    out = {
        'scope': 'Descriptive check of how the paper\'s text values could have been averaged. Uses run 011\'s population means; '
                 'no simulation. The code that produced the text values is not available, so any match is an inference.',
        'inputs': inputs,
        'text_values': prose,
        'figure_4_code_logged_means': code,
        'populations': populations,
        'rules': results,
        'matches_both_text_values': [r['rule'] for r in results if r['exc']['run_011_rounds_to_text_value'] and r['inh']['run_011_rounds_to_text_value']],
        'rounding_rule': 'a value matches when it lies within half a unit of the last digit given in the text (0.005 nS)',
        'sampling_se_note': 'sampling_se_nS: rough standard error of each average if a different set of recorded neurons of the same size had been '
                            'drawn, from the across-neuron SDs and recorded counts (independent populations). It does not cover differences '
                            'between network realizations or simulator versions.',
    }
    Path(args.output).mkdir(parents=True, exist_ok=True)
    (Path(args.output) / 'conductance-averages.json').write_text(json.dumps(out, indent=1, ensure_ascii=False) + '\n')
    for r in results:
        print(f"{r['rule']:16s} exc {r['exc']['run_011_nS']:.4f} ({r['exc']['run_011_rounds_to_text_value']})  "
              f"inh {r['inh']['run_011_nS']:.4f} ({r['inh']['run_011_rounds_to_text_value']})")
    print('matches both text values:', out['matches_both_text_values'])


if __name__ == '__main__':
    main()
