Skip to content

Commit 7cfd644

Browse files
committed
update viz_utils and code cleanup , black
1 parent 70a98ff commit 7cfd644

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

element_moseq/moseq_train.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,6 @@ def make_compute(
342342
kpms_project_output_dir = find_full_path(
343343
get_kpms_processed_data_dir(), kpms_project_output_dir
344344
)
345-
346345
except FileNotFoundError:
347346
kpms_project_output_dir = (
348347
Path(get_kpms_processed_data_dir()) / kpms_project_output_dir

element_moseq/plotting/viz_utils.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,26 @@ def plot_medoid_distance_outliers(
6565
"""
6666
from keypoint_moseq.util import get_distance_to_medoid, plot_keypoint_traces
6767

68-
plot_path = os.path.join(
69-
project_dir,
70-
"quality_assurance",
71-
"plots",
72-
"keypoint_distance_outliers",
73-
f"{recording_name}.png",
74-
)
75-
os.makedirs(os.path.dirname(plot_path), exist_ok=True)
68+
qa_dirs = ["QA", "quality_assurance"]
69+
plot_path = None
70+
71+
for qa_dir in qa_dirs:
72+
potential_path = os.path.join(
73+
project_dir,
74+
qa_dir,
75+
"plots",
76+
"keypoint_distance_outliers",
77+
f"{recording_name}.png",
78+
)
79+
# Create directory if it doesn't exist
80+
os.makedirs(os.path.dirname(potential_path), exist_ok=True)
81+
plot_path = potential_path
82+
break # Use first available directory
83+
84+
if plot_path is None:
85+
raise FileNotFoundError(
86+
f"Could not determine plot directory for {recording_name}"
87+
)
7688

7789
original_distances = get_distance_to_medoid(
7890
original_coordinates
@@ -252,9 +264,8 @@ def plot_pcs(
252264
plt.tight_layout()
253265

254266
if savefig:
255-
assert project_dir is not None, fill(
256-
"The `savefig` option requires a `project_dir`"
257-
)
267+
if project_dir is None:
268+
raise ValueError(fill("The `savefig` option requires a `project_dir`"))
258269
plt.savefig(os.path.join(project_dir, f"pcs-{name}.pdf"))
259270
plt.show()
260271

@@ -275,14 +286,13 @@ def copy_pdf_to_png(project_dir, model_name):
275286
"""
276287
Convert PDF progress plot to PNG format using pdf2image.
277288
The fit_model function generates a single fitting_progress.pdf file.
278-
This function will gracefully handle missing poppler dependency.
279289
280290
Args:
281291
project_dir (Path): Project directory path
282292
model_name (str): Model name directory
283293
284294
Returns:
285-
bool: True if conversion was successful, False if poppler is not available or conversion fails
295+
None: The function raises errors instead of returning boolean values
286296
"""
287297
from pdf2image import convert_from_path
288298

@@ -292,17 +302,11 @@ def copy_pdf_to_png(project_dir, model_name):
292302
png_path = model_dir / "fitting_progress.png"
293303

294304
if not pdf_path.exists():
295-
logger.error(f"PDF progress plot not found at {pdf_path}")
296-
return False
297-
298-
try:
299-
images = convert_from_path(str(pdf_path), dpi=300)
300-
if not images:
301-
logger.error(f"Could not convert PDF at {pdf_path} (no images returned).")
302-
return False
303-
images[0].save(png_path, "PNG")
304-
logger.info(f"Generated PNG progress plot at {png_path}")
305-
return True
306-
except Exception as ex:
307-
logger.error(f"Failed to convert PDF to PNG: {ex}")
308-
return False
305+
raise FileNotFoundError(f"PDF progress plot not found at {pdf_path}")
306+
307+
images = convert_from_path(str(pdf_path), dpi=300)
308+
if not images:
309+
raise ValueError(f"Could not convert PDF at {pdf_path} (no images returned)")
310+
311+
images[0].save(png_path, "PNG")
312+
logger.info(f"Generated PNG progress plot at {png_path}")

0 commit comments

Comments
 (0)