feat(explorer): extend diagnostic inspector to surface mp samples/counts

chore(explorer): add get_debug_trajectories_enabled helper

feat(explorer): instrument trajectories with debug diagnostics and un-silence helper exceptions
This commit is contained in:
2026-03-31 00:28:14 +02:00
parent 0f2db0a9be
commit baee50f3a5
4 changed files with 1256 additions and 106 deletions
@@ -0,0 +1,49 @@
import os
import types
import explorer
def test_load_positions_empty_sets_diagnostics(monkeypatch):
# Monkeypatch load_positions to return empty positions
monkeypatch.setattr(
explorer, "load_positions", lambda db_path, window_size: ({}, {})
)
monkeypatch.setenv("EXPLORER_DEBUG_TRAJECTORIES", "1")
# Call build_trajectories_tab; it should set diagnostics and return without exception
explorer.build_trajectories_tab(db_path="unused", window_size="annual")
assert (
explorer._last_trajectories_diagnostics.get("stage") == "load_positions_empty"
)
def test_select_helper_exception_is_captured(monkeypatch):
# Provide a minimal non-empty positions_by_window
positions = {"W1": {"mp1": (0.1, 0.2)}}
def fake_load_positions(db_path, window_size):
return positions, {}
monkeypatch.setattr(explorer, "load_positions", fake_load_positions)
# Ensure party_map maps the mp so centroids/path that invoke select_trajectory_plot_data
monkeypatch.setattr(explorer, "load_party_map", lambda db_path: {"mp1": "P1"})
# Patch select_trajectory_plot_data to raise
def bad_helper(*args, **kwargs):
raise ValueError("boom")
monkeypatch.setattr(explorer, "select_trajectory_plot_data", bad_helper)
monkeypatch.setenv("EXPLORER_DEBUG_TRAJECTORIES", "1")
explorer.build_trajectories_tab(db_path="unused", window_size="annual")
# Ensure the helper function has diagnostics attached and module diagnostics updated
assert getattr(explorer.select_trajectory_plot_data, "_last_diagnostics", None)
assert "exception" in explorer.select_trajectory_plot_data._last_diagnostics
assert (
explorer._last_trajectories_diagnostics.get("stage")
== "select_helper_exception"
)
assert "ValueError" in explorer._last_trajectories_diagnostics.get("exception", "")
@@ -0,0 +1,22 @@
import numpy as np
from explorer_helpers import inspect_positions_for_issues
def test_inspect_positions_for_issues_basic():
positions_by_window = {
"w1": {"mp1": (1.0, 2.0), "mp2": (float("nan"), float("nan"))},
"w2": {},
}
party_map = {"mp1": "P1"}
d = inspect_positions_for_issues(positions_by_window, party_map)
# basic keys still present
assert d["windows_count"] == 2
assert isinstance(d["mp_id_set"], set)
# new diagnostics
assert "mp_positions_count" in d
assert d["mp_positions_count"] >= 1
assert "mp_positions_sample" in d
assert isinstance(d["mp_positions_sample"], list)
assert "windows_with_no_positions" in d
assert isinstance(d["windows_with_no_positions"], list)