"""Tests on Lab 10's recorded results (eval labs, The Judge as a Measuring Instrument). Everything is simulated:
20,000 summaries with a known human verdict (85.06% pass) and three judges built with known mistakes. The samples
below are the exact ones the lab's scripts draw with their seeds. 1 = pass, 0 = fail.
Run: pytest test_judge_practice.py"""
from judge_practice import (catch_rates, corrected_pass_rate, expected_raw, flagged_design, holds, labels_needed,
                            wilson_ci)

TRUTH = 0.8506  # the real pass rate of the 20,000 simulated summaries

# 01_calibrate.py, Exercise 1 (seed 0): a person labels 100 random summaries; each judge grades the same 100
HUMAN = "1111111111010111101111111110011111111111110101111111101111111011111111111011101111111110111111111011"
SMALL = "1111111111111111101111111111011111111110110101111111001111111111111111111010100011111110111111111011"
LARGE = "1111111111010111111111011110011111111101110101111111101111111010111111101011101111111110111111011001"


def bits(s):
    return [int(ch) for ch in s]


def close(a, b, tol=1e-4):
    return abs(a - b) <= tol


def test_catch_rates_small_judge():
    r = catch_rates(bits(SMALL), bits(HUMAN))
    assert (r["n_pass"], r["n_fail"]) == (87, 13)  # most summaries pass: only 13 failures
    assert (r["tp"], r["tn"]) == (82, 9)
    assert close(r["tpr"], 82 / 87) and close(r["tnr"], 9 / 13)


def test_catch_rates_large_judge():
    r = catch_rates(bits(LARGE), bits(HUMAN))
    assert (r["tp"], r["tn"]) == (81, 12)


def test_tnr_interval_is_wide_and_overlaps():
    small, large = catch_rates(bits(SMALL), bits(HUMAN)), catch_rates(bits(LARGE), bits(HUMAN))
    lo, hi = wilson_ci(small["tn"], small["n_fail"])  # small judge's TNR: 9 of 13
    assert (round(lo * 100), round(hi * 100)) == (42, 87)
    llo, lhi = wilson_ci(large["tn"], large["n_fail"])  # large judge's TNR: 12 of 13
    assert (round(llo * 100), round(lhi * 100)) == (67, 99)
    assert llo < hi  # the bars overlap: 100 labels can't tell the judges apart
    tlo, thi = wilson_ci(small["tp"], small["n_pass"])  # small judge's TPR, 82 of 87: narrow
    assert thi - tlo < 0.12 < hi - lo


def test_labels_needed():
    # 01_calibrate.py, Exercise 2: TPR ~0.95, TNR ~0.85, 15% of summaries failing
    assert labels_needed(0.05, 0.95, 0.85, 0.15) == {"n_pass": 73, "n_fail": 196, "total_random": 1307}
    assert labels_needed(0.03, 0.95, 0.85, 0.15)["total_random"] == 3634
    assert labels_needed(0.10, 0.95, 0.85, 0.15)["total_random"] == 327


def test_flagged_design():
    # Exercise 3: 70.4% of user-flagged summaries fail; 196 failures + 73 random passes
    assert flagged_design(196, 0.704009, 73) == 352


def test_corrected_small_judge():
    # 02_corrected_score.py, Exercise 1: raw 88.7%; 300 labels: 236 of 242 passes, 41 of 58 failures
    got = corrected_pass_rate(0.887, 236 / 242, 41 / 58)
    assert round(got * 100, 1) == 87.1
    assert close(corrected_pass_rate(0.887, 0.975, 0.707), 0.594 / 0.682)  # the worked example, 3 decimals
    assert round(corrected_pass_rate(0.887, 0.98, 0.71) * 100, 1) == 86.5  # the 2-decimal printout gives this


def test_corrected_large_and_small_plus():
    assert round(corrected_pass_rate(0.830, 240 / 253, 41 / 47) * 100, 1) == 85.6
    assert round(corrected_pass_rate(0.851, 237 / 253, 35 / 47) * 100, 1) == 87.4  # moved away from the truth


def test_coin_judge_is_refused():
    try:
        corrected_pass_rate(0.8, 0.6, 0.43)  # TPR + TNR - 1 = 0.03
    except ValueError:
        return
    raise AssertionError("a judge this close to a coin must raise ValueError")


def test_corrected_is_clipped():
    assert corrected_pass_rate(0.99, 0.9, 0.9) == 1.0


def test_expected_raw():
    # real rates on all 20,000 summaries (biases make TNR lower than the README's base rates)
    assert round(expected_raw(TRUTH, 0.95, 0.7981) * 100, 1) == 83.8  # small+: usually reads ~1 point low
    assert expected_raw(TRUTH, 0.97, 0.6005) > TRUTH  # the lenient small judge reads high


# 02_corrected_score.py, Exercise 2 (seed 1): the first 60 of 400 studies with the small judge
RAW = [
    (0.8424, 0.8848), (0.8531, 0.8941), (0.8403, 0.8829), (0.8723, 0.9107),
    (0.8584, 0.8987), (0.8691, 0.9079), (0.8852, 0.9217), (0.8669, 0.9061),
    (0.8691, 0.9079), (0.8712, 0.9098), (0.8712, 0.9098), (0.8734, 0.9116),
    (0.8595, 0.8996), (0.8766, 0.9143), (0.8563, 0.8969), (0.8766, 0.9143),
    (0.8734, 0.9116), (0.8691, 0.9079), (0.8648, 0.9042), (0.8809, 0.9180),
    (0.8531, 0.8941), (0.8573, 0.8978), (0.8627, 0.9024), (0.8723, 0.9107),
    (0.8563, 0.8969), (0.8680, 0.9070), (0.8734, 0.9116), (0.8842, 0.9207),
    (0.8680, 0.9070), (0.8595, 0.8996), (0.8520, 0.8931), (0.8616, 0.9015),
    (0.8584, 0.8987), (0.8605, 0.9005), (0.8595, 0.8996), (0.8637, 0.9033),
    (0.8809, 0.9180), (0.8669, 0.9061), (0.8605, 0.9005), (0.8745, 0.9125),
    (0.8745, 0.9125), (0.8788, 0.9162), (0.8605, 0.9005), (0.8648, 0.9042),
    (0.8627, 0.9024), (0.8766, 0.9143), (0.8552, 0.8959), (0.8788, 0.9162),
    (0.8723, 0.9107), (0.8659, 0.9052), (0.8584, 0.8987), (0.8563, 0.8969),
    (0.8509, 0.8922), (0.8680, 0.9070), (0.8477, 0.8894), (0.8552, 0.8959),
    (0.8702, 0.9088), (0.8777, 0.9153), (0.8766, 0.9143), (0.8563, 0.8969),
]
EXACT = [
    (0.7806, 0.8522), (0.8229, 0.8907), (0.7933, 0.8649), (0.8318, 0.8983),
    (0.8082, 0.8715), (0.8240, 0.8888), (0.8344, 0.9046), (0.8516, 0.9204),
    (0.7538, 0.8315), (0.7708, 0.8494), (0.8221, 0.8846), (0.8410, 0.9070),
    (0.8189, 0.8936), (0.8233, 0.9016), (0.8369, 0.8963), (0.8287, 0.8873),
    (0.7917, 0.8698), (0.7906, 0.8625), (0.8217, 0.8849), (0.8704, 0.9302),
    (0.7735, 0.8440), (0.7394, 0.8171), (0.8112, 0.8803), (0.7879, 0.8601),
    (0.8054, 0.8776), (0.8313, 0.8957), (0.8429, 0.9161), (0.8926, 0.9432),
    (0.8622, 0.9157), (0.7543, 0.8381), (0.7778, 0.8578), (0.7632, 0.8405),
    (0.8239, 0.8937), (0.8593, 0.9163), (0.8096, 0.8778), (0.8046, 0.8754),
    (0.8459, 0.9109), (0.8193, 0.8836), (0.8171, 0.8997), (0.8595, 0.9165),
    (0.8397, 0.9062), (0.8379, 0.8978), (0.7976, 0.8770), (0.8682, 0.9412),
    (0.8118, 0.8879), (0.7570, 0.8376), (0.8152, 0.8777), (0.7999, 0.8853),
    (0.8220, 0.8890), (0.7512, 0.8329), (0.8319, 0.9071), (0.8015, 0.8742),
    (0.8067, 0.8808), (0.8293, 0.9026), (0.7587, 0.8382), (0.8055, 0.8725),
    (0.8021, 0.8662), (0.8325, 0.9049), (0.8468, 0.9011), (0.8014, 0.8736),
]
COMBINED = [
    (0.7313, 0.8736), (0.7862, 0.9128), (0.7608, 0.8854), (0.8001, 0.9228),
    (0.7785, 0.8869), (0.7925, 0.9061), (0.7928, 0.9182), (0.8201, 0.9454),
    (0.6905, 0.8567), (0.7061, 0.8757), (0.7951, 0.8953), (0.7990, 0.9307),
    (0.7758, 0.9203), (0.7741, 0.9361), (0.8125, 0.9146), (0.8005, 0.9038),
    (0.7202, 0.8951), (0.7490, 0.8816), (0.7834, 0.9029), (0.8376, 0.9493),
    (0.7392, 0.8579), (0.6797, 0.8405), (0.7715, 0.9060), (0.7351, 0.8822),
    (0.7695, 0.9003), (0.8002, 0.9195), (0.8070, 0.9397), (0.8714, 0.9628),
    (0.8462, 0.9310), (0.6631, 0.8681), (0.7362, 0.8837), (0.6896, 0.8652),
    (0.7933, 0.9192), (0.8305, 0.9357), (0.7701, 0.8960), (0.7643, 0.8921),
    (0.8044, 0.9292), (0.7828, 0.9023), (0.7767, 0.9293), (0.8357, 0.9393),
    (0.8121, 0.9261), (0.8096, 0.9172), (0.7500, 0.9010), (0.8311, 0.9677),
    (0.7716, 0.9101), (0.6955, 0.8674), (0.7849, 0.8965), (0.7484, 0.9121),
    (0.7803, 0.9116), (0.6944, 0.8586), (0.7893, 0.9423), (0.7601, 0.8944),
    (0.7507, 0.9092), (0.7917, 0.9270), (0.6906, 0.8688), (0.7737, 0.8871),
    (0.7612, 0.8820), (0.8045, 0.9248), (0.8251, 0.9173), (0.7657, 0.8905),
]


def test_honest_error_bar():
    assert 60 - holds(RAW, TRUTH) == 57  # raw: centred on a biased number
    assert 60 - holds(EXACT, TRUTH) == 16  # corrected, catch rates treated as exact: still too narrow
    assert 60 - holds(COMBINED, TRUTH) == 2  # labels' uncertainty included
    width = lambda ivs: sum(hi - lo for lo, hi in ivs) / len(ivs)
    assert width(RAW) < width(EXACT) < width(COMBINED)
