fix: close duckdb connections safely, swap x/y_axis vectors, fix EVR caption after axis swap

main
Sven Geboers 1 month ago
parent ea3c68ece9
commit ab9b91e4a8
  1. 26
      analysis/axis_classifier.py
  2. 3
      explorer.py

@ -162,12 +162,14 @@ def _load_motion_vectors(db_path: str, window_id: str) -> Dict[int, np.ndarray]:
import duckdb import duckdb
conn = duckdb.connect(db_path, read_only=True) conn = duckdb.connect(db_path, read_only=True)
rows = conn.execute( try:
"SELECT entity_id, vector FROM svd_vectors " rows = conn.execute(
"WHERE entity_type = 'motion' AND window_id = ?", "SELECT entity_id, vector FROM svd_vectors "
[window_id], "WHERE entity_type = 'motion' AND window_id = ?",
).fetchall() [window_id],
conn.close() ).fetchall()
finally:
conn.close()
result: Dict[int, np.ndarray] = {} result: Dict[int, np.ndarray] = {}
for entity_id, vector_raw in rows: for entity_id, vector_raw in rows:
try: try:
@ -248,11 +250,13 @@ def _fetch_motion_titles(
placeholders = ", ".join("?" for _ in motion_ids) placeholders = ", ".join("?" for _ in motion_ids)
conn = duckdb.connect(db_path, read_only=True) conn = duckdb.connect(db_path, read_only=True)
rows = conn.execute( try:
f"SELECT id, title, date FROM motions WHERE id IN ({placeholders})", rows = conn.execute(
motion_ids, f"SELECT id, title, date FROM motions WHERE id IN ({placeholders})",
).fetchall() motion_ids,
conn.close() ).fetchall()
finally:
conn.close()
return {int(row[0]): (str(row[1]), str(row[2])) for row in rows} return {int(row[0]): (str(row[1]), str(row[2])) for row in rows}
except Exception as exc: except Exception as exc:
_logger.debug("Failed to fetch motion titles: %s", exc) _logger.debug("Failed to fetch motion titles: %s", exc)

@ -204,6 +204,7 @@ def _swap_axes(
("x_interpretation", "y_interpretation"), ("x_interpretation", "y_interpretation"),
("x_top_motions", "y_top_motions"), ("x_top_motions", "y_top_motions"),
("x_label_confidence", "y_label_confidence"), ("x_label_confidence", "y_label_confidence"),
("x_axis", "y_axis"),
]: ]:
new_ax[x_key] = axis_def.get(y_key) new_ax[x_key] = axis_def.get(y_key)
new_ax[y_key] = axis_def.get(x_key) new_ax[y_key] = axis_def.get(x_key)
@ -1065,7 +1066,7 @@ def build_compass_tab(db_path: str, window_size: str) -> None:
if evr0 is not None: if evr0 is not None:
st.caption( st.caption(
f"As 1 verklaart {evr0:.1%} van de variantie in stemgedrag." f"De sterkste component verklaart {evr0:.1%} van de variantie in stemgedrag."
) )
# --- Voting discipline section --- # --- Voting discipline section ---

Loading…
Cancel
Save