wikalid / tests /test_package.py
jpaulpoliquit's picture
WikaLID 1.0.1: rename from OurLID, public model card, ourlid alias kept
8f78c5a verified
Raw History Blame Contribute Delete
4.25 kB
"""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