Classify operator background corpus
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib.util
|
||||
import json
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
|
||||
|
||||
def load_sample_preparer():
|
||||
script_path = ROOT / "scripts" / "prepare_operator_real_data_samples.py"
|
||||
spec = importlib.util.spec_from_file_location("operator_sample_preparer_s156", script_path)
|
||||
assert spec is not None
|
||||
assert spec.loader is not None
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
sys.modules[spec.name] = module
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
def test_background_samples_are_classified_by_actual_reference_density() -> None:
|
||||
module = load_sample_preparer()
|
||||
|
||||
background = module.OperatorSample(
|
||||
slug="background",
|
||||
display_name="Background",
|
||||
center_lon=5.0,
|
||||
center_lat=51.0,
|
||||
sample_role="background_candidate",
|
||||
allow_empty_reference=True,
|
||||
)
|
||||
reference = module.OperatorSample(
|
||||
slug="reference",
|
||||
display_name="Reference",
|
||||
center_lon=5.0,
|
||||
center_lat=51.0,
|
||||
)
|
||||
|
||||
assert module.background_category_for_sample(background, 0) == "pure_empty_negative"
|
||||
assert module.background_category_for_sample(background, 3) == "sparse_building_context"
|
||||
assert module.background_category_for_sample(reference, 30) == "reference_aoi"
|
||||
|
||||
|
||||
def test_prepare_sample_manifest_records_background_category_from_cached_reference(
|
||||
tmp_path: Path,
|
||||
monkeypatch,
|
||||
) -> None:
|
||||
module = load_sample_preparer()
|
||||
sample = module.OperatorSample(
|
||||
slug="background",
|
||||
display_name="Background",
|
||||
center_lon=5.0,
|
||||
center_lat=51.0,
|
||||
sample_role="background_candidate",
|
||||
allow_empty_reference=True,
|
||||
)
|
||||
raster_path, reference_path = module.sample_artifact_paths(sample, tmp_path)
|
||||
raster_path.write_bytes(b"placeholder raster")
|
||||
reference_path.write_text(
|
||||
json.dumps(
|
||||
{
|
||||
"type": "FeatureCollection",
|
||||
"features": [
|
||||
{
|
||||
"type": "Feature",
|
||||
"geometry": {"type": "Point", "coordinates": [5.0, 51.0]},
|
||||
"properties": {},
|
||||
}
|
||||
],
|
||||
}
|
||||
),
|
||||
encoding="utf-8",
|
||||
)
|
||||
monkeypatch.setattr(module, "raster_summary", lambda path: {"path": str(path)})
|
||||
monkeypatch.setattr(module, "sample_bounds", lambda current: ((0.0, 0.0, 1.0, 1.0), [4.9, 50.9, 5.1, 51.1]))
|
||||
|
||||
prepared = module.prepare_sample(sample, tmp_path, force=False)
|
||||
|
||||
assert prepared["background_category"] == "sparse_building_context"
|
||||
assert prepared["reference_feature_count"] == 1
|
||||
|
||||
|
||||
def test_hard_negative_matrix_can_filter_background_categories() -> None:
|
||||
script = (ROOT / "scripts" / "run_operator_hard_negative_detection_matrix.sh").read_text(encoding="utf-8")
|
||||
|
||||
assert "OPERATOR_BACKGROUND_CATEGORIES" in script
|
||||
assert "background_category" in script
|
||||
assert "pure_empty_negative" in script
|
||||
assert "sparse_building_context" in script
|
||||
assert "background_category_counts" in script
|
||||
|
||||
|
||||
def test_yolo_tile_export_preserves_background_category_provenance() -> None:
|
||||
script = (ROOT / "scripts" / "export_operator_yolo_tile_dataset.py").read_text(encoding="utf-8")
|
||||
|
||||
assert "background_category = str(sample.get(\"background_category\") or \"reference_aoi\")" in script
|
||||
assert "\"background_category\": background_category" in script
|
||||
Reference in New Issue
Block a user