Add portable corpus, annotation provenance, admission, review and bounded agent-cycle contracts. Preserve dataset ownership and explicit research acceptance limits.
92 lines
3.1 KiB
Python
92 lines
3.1 KiB
Python
"""Coverage experiments must not turn unknown into background or fit controls."""
|
|
|
|
import importlib
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
np = pytest.importorskip("numpy")
|
|
for dependency in ["cv2", "scipy", "shapely"]:
|
|
pytest.importorskip(dependency)
|
|
from shapely.geometry import box, mapping # noqa: E402
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "simulation/semantic-learning"))
|
|
study = importlib.import_module("coverage_admission_study")
|
|
|
|
|
|
def test_background_is_explicit_unknown_stays_unknown_and_paint_wins():
|
|
review = dict(
|
|
objects=[dict(geometry=mapping(box(10, 10, 20, 20)))],
|
|
negative=[],
|
|
complete_background=[mapping(box(0, 0, 100, 100))],
|
|
)
|
|
candidates = [
|
|
dict(geometry=mapping(g))
|
|
for g in [
|
|
box(10, 10, 20, 20),
|
|
box(40, 40, 60, 60),
|
|
box(110, 110, 130, 130),
|
|
box(90, 90, 110, 110),
|
|
]
|
|
]
|
|
assert study.labels(candidates, review).tolist() == [1, -1, -1, -1]
|
|
assert study.expanded_labels(candidates, review).tolist() == [1, 0, -1, -1]
|
|
|
|
|
|
def test_both_arms_exclude_control_and_regression_records():
|
|
records = [
|
|
dict(id=rid, split=split)
|
|
for rid, split in [
|
|
("old", "development"),
|
|
("new-coverage-1", "development"),
|
|
("control-coverage-1", "heldout"),
|
|
("old-control", "regression"),
|
|
]
|
|
]
|
|
assert [r["id"] for r in study.training_records(records, "old104")] == ["old"]
|
|
assert [r["id"] for r in study.training_records(records, "expanded")] == [
|
|
"old",
|
|
"new-coverage-1",
|
|
]
|
|
|
|
|
|
def test_expanded_fit_only_passed_records_and_unknown_excluded(monkeypatch):
|
|
geoms = [box(i * 20, 0, i * 20 + 10, 10) for i in range(5)]
|
|
record = dict(id="dev", variants={"retained": [dict(geometry=mapping(g)) for g in geoms]})
|
|
review = {
|
|
"dev": dict(
|
|
objects=[dict(geometry=mapping(g)) for g in geoms[:2]],
|
|
negative=[],
|
|
complete_background=[mapping(box(40, 0, 70, 10))],
|
|
)
|
|
}
|
|
data = {
|
|
"dev": {"c4_512": np.arange(15).reshape(5, 3)},
|
|
"control": {"c4_512": np.full((5, 3), 999)},
|
|
}
|
|
captured = []
|
|
|
|
def fitted(x, y, l2, maxiter):
|
|
captured.append((x.copy(), y.copy()))
|
|
return {}
|
|
|
|
monkeypatch.setattr(study, "fit_classifier", fitted)
|
|
model = study.fit([record], review, data, 0.1, True)
|
|
assert model == dict(positive=2, negative=2)
|
|
assert captured[0][1].tolist() == [1, 1, 0, 0]
|
|
assert np.array_equal(captured[0][0], data["dev"]["c4_512"][:4])
|
|
data["control"]["c4_512"] *= 2
|
|
study.fit([record], review, data, 0.1, True)
|
|
assert np.array_equal(captured[0][0], captured[1][0])
|
|
|
|
|
|
def test_clipped_identity_loss_cannot_be_hidden_by_another_gain():
|
|
def result(ids):
|
|
return {
|
|
"rows": [{"clipped_components": {"objects": [{"id": i, "matched": True} for i in ids]}}]
|
|
}
|
|
|
|
assert study.partial_gate(result(["a", "b"]), result(["a", "c"])) == ["b"]
|
|
assert study.partial_gate(result(["a"]), result(["a", "b"])) == []
|