"""Rerun every AC64 tutorial number and compare it with expected-outputs.json.

Usage:  python3 reproduce.py            # compare against expected-outputs.json
        python3 reproduce.py --write    # overwrite expected-outputs.json
Requires Python >= 3.9, numpy and scipy. Runtime is under a minute on a 2020s laptop.
Inputs: chime-frb20180725a-intensity.json and r3-catalog2-rows.csv in this folder.
"""
import json
import math
import platform
import sys
from pathlib import Path

import numpy as np
import scipy

import propagation_fit
import repeat_statistics

HERE = Path(__file__).resolve().parent
# Nelder-Mead stops at a tolerance, so fitted values may differ in the last digits between platforms.
RELATIVE_TOLERANCE, ABSOLUTE_TOLERANCE = 5e-3, 0.05


def compare(expected, actual, path='outputs'):
    problems = []
    if isinstance(expected, dict):
        for key in expected:
            if key not in actual:
                problems.append(f'{path}.{key} missing')
            else:
                problems += compare(expected[key], actual[key], f'{path}.{key}')
    elif isinstance(expected, list):
        if len(expected) != len(actual):
            return [f'{path} has {len(actual)} items, expected {len(expected)}']
        for index, (left, right) in enumerate(zip(expected, actual)):
            problems += compare(left, right, f'{path}[{index}]')
    elif isinstance(expected, float) or isinstance(actual, float):
        if not math.isclose(float(expected), float(actual), rel_tol=RELATIVE_TOLERANCE, abs_tol=ABSOLUTE_TOLERANCE):
            problems.append(f'{path}: expected {expected}, got {actual}')
    elif expected != actual:
        problems.append(f'{path}: expected {expected!r}, got {actual!r}')
    return problems


def main():
    outputs = {
        'environment': {'python': platform.python_version(), 'numpy': np.__version__, 'scipy': scipy.__version__},
        'propagation_fit': propagation_fit.run(),
        'repeat_statistics': repeat_statistics.run(),
    }
    target = HERE / 'expected-outputs.json'
    if '--write' in sys.argv:
        target.write_text(json.dumps(outputs, indent=1) + '\n')
        print(f'wrote {target.name}')
        return
    expected = json.loads(target.read_text())
    expected.pop('environment', None)
    problems = compare(expected, outputs)
    print(json.dumps(outputs['environment']))
    if problems:
        print('\n'.join(problems))
        raise SystemExit(f'{len(problems)} values differ from expected-outputs.json')
    print('all tutorial values match expected-outputs.json')


if __name__ == '__main__':
    main()
