From 62daad321ef58009148590ca5fde5bddb2e6db7e Mon Sep 17 00:00:00 2001 From: Sven Geboers Date: Sun, 29 Mar 2026 19:18:33 +0200 Subject: [PATCH] fix: add outer exception handling to motion helpers in axis_classifier --- analysis/axis_classifier.py | 42 +++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/analysis/axis_classifier.py b/analysis/axis_classifier.py index 4fd11c6..325faca 100644 --- a/analysis/axis_classifier.py +++ b/analysis/axis_classifier.py @@ -185,16 +185,20 @@ def _project_motions( Returns {motion_id: (x_score, y_score)}. """ - projections: Dict[int, Tuple[float, float]] = {} - for mid, vec in motion_vecs.items(): - try: - centered = vec - global_mean - x_score = float(np.dot(centered, x_axis)) - y_score = float(np.dot(centered, y_axis)) - projections[mid] = (x_score, y_score) - except Exception: - continue - return projections + try: + projections: Dict[int, Tuple[float, float]] = {} + for mid, vec in motion_vecs.items(): + try: + centered = vec - global_mean + x_score = float(np.dot(centered, x_axis)) + y_score = float(np.dot(centered, y_axis)) + projections[mid] = (x_score, y_score) + except Exception: + continue + return projections + except Exception as exc: + _logger.debug("Failed to project motions: %s", exc) + return {} def _top_motion_ids( @@ -208,11 +212,17 @@ def _top_motion_ids( Returns {'+': [motion_ids], '-': [motion_ids]} (highest positive first, most negative first in the '-' list). """ - idx = 0 if axis == "x" else 1 - sorted_ids = sorted(projections, key=lambda mid: projections[mid][idx]) - neg_ids = sorted_ids[:n] # most negative - pos_ids = sorted_ids[-n:][::-1] # most positive - return {"+": pos_ids, "-": neg_ids} + try: + if axis not in ("x", "y"): + raise ValueError("axis must be 'x' or 'y'") + idx = 0 if axis == "x" else 1 + sorted_ids = sorted(projections, key=lambda mid: projections[mid][idx]) + neg_ids = sorted_ids[:n] + pos_ids = sorted_ids[-n:][::-1] + return {"+": pos_ids, "-": neg_ids} + except Exception as exc: + _logger.debug("Failed to compute top_motion_ids: %s", exc) + return {"+": [], "-": []} def _fetch_motion_titles( @@ -229,7 +239,7 @@ def _fetch_motion_titles( try: import duckdb - placeholders = ", ".join("?" * len(motion_ids)) + placeholders = ", ".join("?" for _ in motion_ids) conn = duckdb.connect(db_path, read_only=True) rows = conn.execute( f"SELECT id, title, date FROM motions WHERE id IN ({placeholders})",