import numpy as np import pandas as pd import pytest from volve_dia import audit def test_allocate_minimum_then_sqrt_share(): a = audit.allocate({"big": 1600, "mid": 100, "tiny": 1}, 20, 2) assert sum(a.values()) == 20 and a["tiny"] == 1 and a["mid"] >= 2 and a["big"] > a["mid"] assert audit.allocate({"x": 3, "y": 3}, 10, 2) == {"x": 3, "y": 3} # never more than a stratum holds with pytest.raises(ValueError): audit.allocate({s: 10 for s in "abcdef"}, 5, 1) def test_draw_weights_and_spread_over_wells(): f = pd.DataFrame({"act_id": range(400), "well": np.repeat(list("ABCD"), 100), "zone": ["a"] * 100 + ["none"] * 300, "stratum": ["a/x"] * 100 + ["none/y"] * 300}) s = audit.draw(f, {"a": 8, "none": 8}, 2, seed=1) assert len(s) == 16 and s["act_id"].is_unique assert set(s.loc[s["zone"] == "none", "well"]) == {"B", "C", "D"} # spread over the wells of the stratum assert s.groupby("stratum")["weight"].first().to_dict() == {"a/x": 12.5, "none/y": 37.5} assert audit.draw(f, {"a": 8, "none": 8}, 2, seed=1)["act_id"].tolist() == s["act_id"].tolist() def test_cohen_kappa(): a = pd.Series(list("yynnyn")) assert audit.cohen_kappa(a, a) == pytest.approx(1.0) b = pd.Series(list("ynynyn")) po, pe = 4 / 6, 0.5 * 0.5 + 0.5 * 0.5 assert audit.cohen_kappa(a, b) == pytest.approx((po - pe) / (1 - pe)) w = pd.Series([1, 1, 1, 1, 1, 1.0]) assert audit.cohen_kappa(a, b, w) == pytest.approx(audit.cohen_kappa(a, b)) assert audit.cohen_kappa(pd.Series(["y", "y"]), pd.Series(["y", "y"])) is None # pe = 1 def test_zone(): z = audit.zone(pd.Series([True, False, False]), pd.Series([True, True, False])) assert z.tolist() == ["a", "b_only", "none"]