Skip to content

Fix visualization reshape error with descriptive error messages and validation#3047

Merged
MMathisLab merged 2 commits intomwm/humanbodyfrom
copilot/fix-f747ffce-9a53-4fd6-b2fb-8b369a6fa98c
Jul 5, 2025
Merged

Fix visualization reshape error with descriptive error messages and validation#3047
MMathisLab merged 2 commits intomwm/humanbodyfrom
copilot/fix-f747ffce-9a53-4fd6-b2fb-8b369a6fa98c

Conversation

Copy link
Contributor

Copilot AI commented Jul 5, 2025

Problem

The plot_evaluation_results function in deeplabcut/utils/visualization.py was failing with cryptic reshape errors when the input data didn't match expected dimensions:

ValueError: cannot reshape array of size 8 into shape (2,4,2)

This error provided no context about what data was being processed or why the reshape failed, making it difficult for users to debug data structure issues.

Root Cause

The function performed direct .reshape() operations without validating that the array size matched the target dimensions:

# Lines 517-518 (before fix)
ground_truth = df_gt.to_numpy().reshape((individuals, bodyparts, 2))
predictions = df_predictions.to_numpy().reshape((individuals, bodyparts, 3))

When the actual data didn't contain the expected number of individuals, bodyparts, or coordinates, these reshapes would fail with uninformative error messages.

Solution

Added a safe_reshape function that:

  1. Validates array sizes before attempting reshape operations
  2. Provides descriptive error messages with context about what data is being processed
  3. Suggests actionable debugging steps for users
  4. Maintains full backward compatibility for valid data

Example Error Message Improvement

Before:

ValueError: cannot reshape array of size 8 into shape (2,4,2)

After:

ValueError: Cannot reshape array of size 8 into shape (2, 4, 2) for ground truth data with 2 individuals and 4 bodyparts. Expected 16 elements but got 8. This may indicate missing data or inconsistent data structure. Please verify that all expected individuals, bodyparts, and coordinates are present in the data.

Changes Made

  1. Added safe_reshape function with comprehensive validation and error reporting
  2. Updated all reshape calls in plot_evaluation_results to use safe_reshape with meaningful context
  3. Added comprehensive unit tests covering valid operations, various error scenarios, and edge cases

Validation

  • ✅ All existing valid data scenarios continue to work unchanged
  • ✅ Invalid data now provides clear, actionable error messages
  • ✅ Added 15+ test cases covering various data mismatch scenarios
  • ✅ Maintains the exact same API - no breaking changes
  • ✅ Minimal code footprint - only 49 lines added, 10 lines modified

This fix transforms frustrating debugging sessions into clear guidance for users when their data doesn't match expected structure, while ensuring robust validation prevents silent failures.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@MMathisLab MMathisLab changed the base branch from main to mwm/humanbody July 5, 2025 20:02
…hensive tests

Co-authored-by: MMathisLab <28102185+MMathisLab@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix visualization error in plot_evaluation_results function Fix visualization reshape error with descriptive error messages and validation Jul 5, 2025
Copilot AI requested a review from MMathisLab July 5, 2025 20:12
@MMathisLab MMathisLab marked this pull request as ready for review July 5, 2025 20:27
@MMathisLab MMathisLab merged commit 2dbeea6 into mwm/humanbody Jul 5, 2025
1 check passed
@MMathisLab MMathisLab deleted the copilot/fix-f747ffce-9a53-4fd6-b2fb-8b369a6fa98c branch July 6, 2025 20:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants