"""Check transcribed postural-sway statistics. No force-plate file is read."""
import json
import math
from pathlib import Path

ROOT = Path(__file__).resolve().parent if "__file__" in globals() else Path.cwd()
ABSTRACT_FAMILIES = {
    "music": "supported",
    "interaction": "unsupported",
    "nonlinear": "null",
    "spectral": "null",
    "surrogate": "failed",
    "classifier": "chance",
}


def reproduce():
    source = json.loads((ROOT / "source.json").read_text(encoding="utf-8"))
    low, high = source["interval"]
    ratio = source["ratio"]
    return {
        "ratio": ratio,
        "low": low,
        "high": high,
        "ratio_inside_interval": low <= ratio <= high,
        "interval_excludes_one": low > 1 or high < 1,
        "families_match_abstract": source["families"] == ABSTRACT_FAMILIES,
    }


if __name__ == "__main__":
    result = reproduce()
    expected = json.loads((ROOT / "expected.json").read_text(encoding="utf-8"))
    for key, value in expected.items():
        if isinstance(value, float):
            assert math.isclose(result[key], value, rel_tol=0, abs_tol=1e-12), key
        else:
            assert result[key] is value or result[key] == value, key
    assert result["ratio_inside_interval"] and result["interval_excludes_one"]
    assert result["families_match_abstract"]
    print(json.dumps(result, indent=2))
    print("PASS: transcribed sway statistics checked; no trial reanalyzed.")
