feat(analysis): add 2D political compass and 2D trajectories
- compute_2d_axes (PCA + anchor) - compute_2d_trajectories - plot_political_compass, plot_2d_trajectories - unit test: tests/test_political_compass.py
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import numpy as np
|
||||
import types
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def test_compute_2d_axes_pca_synthetic(monkeypatch):
|
||||
"""Synthetic test for compute_2d_axes using patched alignment helper."""
|
||||
|
||||
# Create a fake trajectory module with required helpers
|
||||
fake_traj = types.SimpleNamespace()
|
||||
|
||||
# _load_window_ids should return ordered windows
|
||||
fake_traj._load_window_ids = lambda db: ["w1", "w2"]
|
||||
|
||||
# _load_mp_vectors_for_window is not used because we patch _procrustes_align_windows
|
||||
fake_traj._load_mp_vectors_for_window = lambda db, w: {}
|
||||
|
||||
# Provide aligned vectors directly
|
||||
aligned = {
|
||||
"w1": {"Alice": np.array([1.0, 0.0, 0.0]), "Bob": np.array([0.0, 1.0, 0.0])},
|
||||
"w2": {"Alice": np.array([0.8, 0.2, 0.0]), "Bob": np.array([0.1, 0.9, 0.0])},
|
||||
}
|
||||
|
||||
fake_traj._procrustes_align_windows = lambda x: aligned
|
||||
|
||||
# Insert fake module into sys.modules for import by analysis.political_axis
|
||||
monkeypatch.setitem(sys.modules, "analysis.trajectory", fake_traj)
|
||||
|
||||
# Now import the function under test
|
||||
from analysis.political_axis import compute_2d_axes
|
||||
|
||||
positions_by_window, axis_def = compute_2d_axes(
|
||||
db_path="dummy", window_ids=["w1", "w2"], method="pca"
|
||||
)
|
||||
|
||||
assert "w1" in positions_by_window and "w2" in positions_by_window
|
||||
for wid in ("w1", "w2"):
|
||||
for name, coord in positions_by_window[wid].items():
|
||||
assert len(coord) == 2
|
||||
assert np.isfinite(coord[0]) and np.isfinite(coord[1])
|
||||
|
||||
assert axis_def.get("method") == "pca"
|
||||
Reference in New Issue
Block a user