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

@ -204,6 +204,7 @@ def _swap_axes(
("x_interpretation", "y_interpretation"),
("x_top_motions", "y_top_motions"),
("x_label_confidence", "y_label_confidence"),
("x_axis", "y_axis"),
]:
new_ax[x_key] = axis_def.get(y_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:
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 ---

Loading…
Cancel
Save