File size: 4,252 Bytes
8f78c5a
863c520
 
 
8f78c5a
 
863c520
 
 
 
 
 
 
 
 
 
8f78c5a
863c520
 
 
8f78c5a
 
863c520
 
 
8f78c5a
863c520
8f78c5a
863c520
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f78c5a
863c520
 
 
 
8f78c5a
863c520
8f78c5a
863c520
 
 
 
8f78c5a
863c520
 
 
 
 
 
 
 
 
8f78c5a
863c520
 
 
 
 
 
8f78c5a
863c520
 
 
 
 
8f78c5a
863c520
 
8f78c5a
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""WikaLID 1.0.1 package tests: version pin, weight manifest, labels on known texts, gate decisions, determinism, sidecar."""
import json, os, subprocess, sys
import numpy as np
import pytest
import wikalid
from wikalid.gate import Gate, TreeHead

EX = {"tgl": "Nag-submit na ako ng report pero hindi pa daw na-approve ng boss namin kahapon.",
      "hil": "Indi ko mahibal-an kon ngaa wala sila nagkadto sa balay ni Lola sang Domingo.",
      "ceb": "Wala ko kahibalo nganong wala sila miadto sa balay ni Lola niadtong Domingo.",
      "eng": "The city council approved the new budget for road repairs on Tuesday afternoon.",
      "ilo": "Saan ko nga ammo no apay a saan da immay idiay balay ni Lola idi Domingo."}


@pytest.fixture(scope="module")
def P():
    return wikalid.load()


def test_version_and_labels():
    assert wikalid.__version__ == "1.0.1" and wikalid.TOOL_ID == "wikalid-1.0.1+gate_v2"
    assert len(wikalid.LABELS) == 17 and "nol" in wikalid.LABELS


def test_manifest_matches_bundled_weights():
    r = subprocess.run([sys.executable, "-m", "wikalid", "verify", "--expect-version", "1.0.1"], capture_output=True, text=True)
    assert r.returncode == 0, r.stdout + r.stderr
    bad = subprocess.run([sys.executable, "-m", "wikalid", "verify", "--expect-version", "9.9.9"], capture_output=True, text=True)
    assert bad.returncode == 1


@pytest.mark.parametrize("lab", sorted(EX))
def test_known_texts(P, lab):
    a = P.analyze(EX[lab])
    assert a["label"] == lab and not a["abstain"] and a["label_set"] == [lab]


def test_taglish_is_a_statistic_not_a_class(P):
    a = P.analyze("Grabe yung traffic kanina sa EDSA, super late na ako sa meeting with the client kasi walang moving na cars.")
    assert a["label"] == "tgl" and a["cs_level"] in ("light", "heavy") and a["cs_share"] > 0.03


def test_deterministic(P):
    a, b = P.analyze(EX["tgl"]), wikalid.load().analyze(EX["tgl"])
    assert a == b


def test_classify_gate_fields():
    r = wikalid.classify(EX["tgl"])
    assert r["gate"] == "accept" and r["gate_reason"] == "accepted" and 0 < r["gate_p_correct"] <= 1 and r["label_final"] == "tgl"
    j = wikalid.classify("1. Juan Dela Cruz 2. Pedro Penduko 3. Maria Clara 4. Jose Rizal")
    assert j["gate"] in ("junk", "review")


def test_aklanon_rule_bucket():
    r = wikalid.classify("Ro Altavas, a opisyal nga Banwa it Altavas, hay sangka banwa sa probinsya it Aklan. Eon ro mayad-ayad nga baeay.")
    assert r["label_final"] == "oth" and r["oth_sub"] == "akl" and r["gate"] == "accept_ph_other"


def test_tree_head_matches_sklearn_when_available():
    sk = pytest.importorskip("sklearn")
    pkl = "/workspace/ourlid/eval/gate_v2_head.pkl"
    if not os.path.exists(pkl):
        pytest.skip("research gate pickle not on this machine")
    import pickle
    h = pickle.load(open(pkl, "rb")); T = TreeHead(os.path.join(wikalid.model_dir(), "gate_head.npz"))
    X = np.random.default_rng(0).random((2000, 20)) * 5; X[::40, 5] = np.nan
    assert (h.predict_proba(X)[:, 1] == T.p_correct(X)).all()


def test_sidecar_roundtrip(tmp_path):
    pa = pytest.importorskip("pyarrow"); import pyarrow.parquet as pq
    from wikalid.sidecar import sidecar
    ids = sorted(EX)
    pq.write_table(pa.table({"document_id": ids, "source_name": ["s"] * len(ids), "text": [EX[i] for i in ids]}), tmp_path / "d.parquet")
    rep = sidecar(str(tmp_path / "d.parquet"), str(tmp_path / "o.parquet"), workers=1, log=lambda *a: None)
    t = pq.read_table(tmp_path / "o.parquet")
    assert t.column("ourlid_label_final").to_pylist() == ids
    assert t.schema.metadata[b"ourlid_version"] == b"1.0.1" and sum(rep["gate"].values()) == len(ids)
    for c in ("gate_v2", "gate_v2_reason", "gate_v2_p_correct", "ourlid_oth_sub", "text_sha256"):
        assert c in t.column_names


def test_ourlid_alias_still_works():
    import ourlid
    from ourlid.gate import Gate as G2
    from ourlid.r3 import PredictorR3  # noqa: F401
    assert ourlid.__version__ == wikalid.__version__ and G2 is Gate and ourlid.classify(EX["tgl"])["label_final"] == "tgl"
    r = subprocess.run([sys.executable, "-m", "ourlid", "verify", "--expect-version", "1.0.1"], capture_output=True, text=True)
    assert r.returncode == 0, r.stdout + r.stderr