Files
geointel/tests/test_build_regional_yolo_dataset.py
T
Jens 3e66c78694
GeoIntel release gates / Compile, test, contracts and builds (push) Canceled after 0s
GeoIntel release gates / Python and npm vulnerability policy (push) Canceled after 0s
GeoIntel release gates / GIS image, SBOM and container scan (push) Canceled after 0s
Harden regional dataset split validation
2026-07-30 01:27:05 +02:00

60 lines
2.6 KiB
Python

from __future__ import annotations
import importlib.util
from pathlib import Path
import pytest
SCRIPT = Path(__file__).resolve().parents[1] / "scripts" / "build_regional_yolo_dataset.py"
SPEC = importlib.util.spec_from_file_location("build_regional_yolo_dataset", SCRIPT)
assert SPEC and SPEC.loader
module = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(module)
def test_regional_dataset_balances_contexts_and_keeps_validation_closed() -> None:
manifest = {"samples": [
{"sample_slug": "f-train", "region": "flanders", "split": "train", "context": "ribbon"},
{"sample_slug": "f-val", "region": "flanders", "split": "val", "context": "mixed"},
{"sample_slug": "w-train", "region": "wallonia", "split": "train", "context": "ribbon"},
]}
summary = {"tiles": [
{"sample_slug": "f-train", "split": "train", "image_path": "/f-pos.png", "kept": True},
{"sample_slug": "f-train", "split": "train", "image_path": "/f-neg.png", "is_negative": True},
{"sample_slug": "f-val", "split": "val", "image_path": "/f-val.png"},
{"sample_slug": "w-train", "split": "train", "image_path": "/w.png"},
]}
train, val, evidence = module.build(
summary=summary, manifest=manifest, region="flanders",
priority_contexts={"ribbon"}, priority_repeat=3, negative_repeat=2,
)
assert train == ["/f-pos.png"] * 3 + ["/f-neg.png"] * 2
assert val == ["/f-val.png"]
assert evidence["negative_train_entry_count"] == 2
assert evidence["protected_samples_in_training"] == []
def test_regional_dataset_rejects_protected_tiles() -> None:
manifest = {"samples": [
{"sample_slug": "f-cal", "region": "flanders", "split": "calibration", "context": "ribbon"},
]}
summary = {"tiles": [{"sample_slug": "f-cal", "split": "calibration", "image_path": "/cal.png"}]}
with pytest.raises(ValueError, match="protected"):
module.build(
summary=summary, manifest=manifest, region="flanders",
priority_contexts=set(), priority_repeat=1, negative_repeat=1,
)
def test_regional_dataset_rejects_manifest_protection_hidden_by_tile_split() -> None:
manifest = {"samples": [
{"sample_slug": "f-cal", "region": "flanders", "split": "calibration", "context": "ribbon"},
]}
summary = {"tiles": [{"sample_slug": "f-cal", "split": "train", "image_path": "/cal.png"}]}
with pytest.raises(ValueError, match="protected"):
module.build(
summary=summary, manifest=manifest, region="flanders",
priority_contexts=set(), priority_repeat=1, negative_repeat=1,
)