"""Post-hoc: were G2 and G3 informative, or did the real effect carry them?

    python research/rain-before-seven-replication/gate-diagnostic.py

NOT part of the verdict. C1 and C2 are already scored and nothing here can
change them; rule.py's "no third estimator" is untouched because this computes
no new statistic on the real labels.

The suspicion: rule.py says G2 and G3 should "take the real rows" and inject.
The real rows already carry a large effect, so "the interval excludes 0" is
satisfied by the effect that was there before the injection. If so, G2 and G3
passed without testing anything, and the original study's construction --
inject onto SHUFFLED labels, where the baseline is zero by construction -- was
the right one and the seal weakened it.

This re-runs the same injections on shuffled labels and prints both, so the
difference is on the record.
"""
import sys

import numpy as np

import load
import rule
from run import inject, shuffled_R, excludes_zero

REPS = 2000
RUNS = 200


def floor_on(d, R, F, tag):
    base = load.d_mh(d["block"], d["n_blocks"], R, F)
    lo, hi = load.bootstrap(d, R, F, "ym", reps=REPS, seed=rule.SEED)
    print(f"\n{tag}: baseline D_MH = {base:+.3f}, 95 % [{lo:+.3f}, {hi:+.3f}], "
          f"excludes 0: {excludes_zero(lo, hi)}")
    for pts in (0.0, 1.0, 2.0, 5.0, 10.0):
        rng = np.random.default_rng(rule.SEED + 90)
        excl, shifts = 0, []
        for i in range(RUNS):
            Fi = F if pts == 0.0 else inject(d, R, F, pts, rng)
            shifts.append(load.d_mh(d["block"], d["n_blocks"], R, Fi) - base)
            lo, hi = load.bootstrap(d, R, Fi, "ym", reps=REPS,
                                    seed=rule.SEED + 3000 + i)
            excl += excludes_zero(lo, hi)
        print(f"  inject {pts:>4.1f} pts  mean shift {np.mean(shifts):+.3f}  "
              f"interval excluded 0 on {excl / RUNS:.1%}")


def main():
    d = load.days(rule.SITES)
    floor_on(d, d["R"], d["F"], "AS THE SEAL WRITES IT (real labels)")
    rng = np.random.default_rng(rule.SEED + 1)
    Rn = shuffled_R(d, rng)
    floor_on(d, Rn, d["F"], "AS THE ORIGINAL WROTE IT (labels shuffled first)")
    return 0


if __name__ == "__main__":
    sys.exit(main())
