mirror of
https://github.com/azaion/gps-denied-onboard.git
synced 2026-04-22 22:36:37 +00:00
dd9835c0cd
- ruff --fix: removed trailing whitespace (W293), sorted imports (I001) - Manual: broke long lines (E501) in eskf, rotation, vo, gpr, metric, pipeline, rotation tests - Removed unused imports (F401) in models.py, schemas/__init__.py - pyproject.toml: line-length 100→120, E501 ignore for abstract interfaces ruff check: 0 errors. pytest: 195 passed / 8 skipped. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
113 lines
4.0 KiB
Python
113 lines
4.0 KiB
Python
"""Tests for Global Place Recognition (F08)."""
|
|
|
|
import numpy as np
|
|
import pytest
|
|
|
|
from gps_denied.core.gpr import GlobalPlaceRecognition
|
|
from gps_denied.core.models import ModelManager
|
|
from gps_denied.schemas.gpr import TileCandidate
|
|
|
|
|
|
@pytest.fixture
|
|
def gpr():
|
|
manager = ModelManager()
|
|
gpr = GlobalPlaceRecognition(manager)
|
|
gpr.load_index("flight_123", "dummy_path.faiss")
|
|
return gpr
|
|
|
|
def test_compute_location_descriptor(gpr):
|
|
img = np.zeros((200, 200, 3), dtype=np.uint8)
|
|
desc = gpr.compute_location_descriptor(img)
|
|
|
|
assert desc.shape == (4096,)
|
|
# Should be L2 normalized
|
|
assert np.isclose(np.linalg.norm(desc), 1.0)
|
|
|
|
def test_retrieve_candidate_tiles(gpr):
|
|
img = np.zeros((200, 200, 3), dtype=np.uint8)
|
|
candidates = gpr.retrieve_candidate_tiles(img, top_k=5)
|
|
|
|
assert len(candidates) == 5
|
|
for c in candidates:
|
|
assert isinstance(c, TileCandidate)
|
|
assert c.similarity_score >= 0.0
|
|
|
|
def test_retrieve_candidate_tiles_for_chunk(gpr):
|
|
imgs = [np.zeros((200, 200, 3), dtype=np.uint8) for _ in range(5)]
|
|
candidates = gpr.retrieve_candidate_tiles_for_chunk(imgs, top_k=3)
|
|
|
|
assert len(candidates) == 3
|
|
# Ensure they are sorted descending (GPR-03)
|
|
assert candidates[0].similarity_score >= candidates[1].similarity_score
|
|
|
|
|
|
# ---------------------------------------------------------------
|
|
# GPR-01: Real Faiss index with file path
|
|
# ---------------------------------------------------------------
|
|
|
|
def test_load_index_missing_file_falls_back(tmp_path):
|
|
"""GPR-01: non-existent index path → numpy fallback, still usable."""
|
|
from gps_denied.core.gpr import GlobalPlaceRecognition
|
|
from gps_denied.core.models import ModelManager
|
|
|
|
g = GlobalPlaceRecognition(ModelManager())
|
|
ok = g.load_index("f1", str(tmp_path / "nonexistent.index"))
|
|
assert ok is True
|
|
assert g._is_loaded is True
|
|
# Should still answer queries
|
|
img = np.zeros((200, 200, 3), dtype=np.uint8)
|
|
cands = g.retrieve_candidate_tiles(img, top_k=3)
|
|
assert len(cands) == 3
|
|
|
|
|
|
def test_load_index_not_loaded_returns_empty():
|
|
"""query_database before load_index → empty list (no crash)."""
|
|
from gps_denied.core.gpr import GlobalPlaceRecognition
|
|
from gps_denied.core.models import ModelManager
|
|
|
|
g = GlobalPlaceRecognition(ModelManager())
|
|
desc = np.random.rand(4096).astype(np.float32)
|
|
matches = g.query_database(desc, top_k=5)
|
|
assert matches == []
|
|
|
|
|
|
# ---------------------------------------------------------------
|
|
# GPR-03: Ranking is deterministic (sorted by similarity)
|
|
# ---------------------------------------------------------------
|
|
|
|
def test_rank_candidates_sorted(gpr):
|
|
"""rank_candidates must return descending similarity order."""
|
|
from gps_denied.schemas import GPSPoint
|
|
from gps_denied.schemas.gpr import TileCandidate
|
|
from gps_denied.schemas.satellite import TileBounds
|
|
|
|
dummy_bounds = TileBounds(
|
|
nw=GPSPoint(lat=49.1, lon=32.0), ne=GPSPoint(lat=49.1, lon=32.1),
|
|
sw=GPSPoint(lat=49.0, lon=32.0), se=GPSPoint(lat=49.0, lon=32.1),
|
|
center=GPSPoint(lat=49.05, lon=32.05), gsd=0.6,
|
|
)
|
|
cands = [
|
|
TileCandidate(
|
|
tile_id="a", gps_center=GPSPoint(lat=49, lon=32),
|
|
bounds=dummy_bounds, similarity_score=0.3, rank=3,
|
|
),
|
|
TileCandidate(
|
|
tile_id="b", gps_center=GPSPoint(lat=49, lon=32),
|
|
bounds=dummy_bounds, similarity_score=0.9, rank=1,
|
|
),
|
|
TileCandidate(
|
|
tile_id="c", gps_center=GPSPoint(lat=49, lon=32),
|
|
bounds=dummy_bounds, similarity_score=0.6, rank=2,
|
|
),
|
|
]
|
|
ranked = gpr.rank_candidates(cands)
|
|
scores = [c.similarity_score for c in ranked]
|
|
assert scores == sorted(scores, reverse=True)
|
|
|
|
|
|
def test_descriptor_is_l2_normalised(gpr):
|
|
"""DINOv2 descriptor returned by compute_location_descriptor is unit-norm."""
|
|
img = np.random.randint(0, 255, (200, 200, 3), dtype=np.uint8)
|
|
desc = gpr.compute_location_descriptor(img)
|
|
assert np.isclose(np.linalg.norm(desc), 1.0, atol=1e-5)
|