Skip to content

Fix dictionary key type mismatch causing KeyError during metrics eval#1337

Closed
abhayrajjais01 wants to merge 1 commit intoweecology:mainfrom
abhayrajjais01:fix-metrics-evaluation
Closed

Fix dictionary key type mismatch causing KeyError during metrics eval#1337
abhayrajjais01 wants to merge 1 commit intoweecology:mainfrom
abhayrajjais01:fix-metrics-evaluation

Conversation

@abhayrajjais01
Copy link
Copy Markdown
Contributor

@abhayrajjais01 abhayrajjais01 commented Mar 6, 2026

Description

This PR fixes a bug in src/deepforest/metrics.py where evaluating predictions resulted in a KeyError due to dictionary type mismatches.

Changes:

  • In the RecallPrecision.compute() method, the numeric_to_label_dict generation now casts the keys (the numeric value from label_dict) to int.
  • Previously, if the original label_dict contained string-based integer values (e.g. {"Tree": "0"}), the model outputs (which are proper integers) failed to map backward to their corresponding labels during the evaluation metrics calculation.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Related Issue

closes #1344

AI-Assisted Development

  • I used AI tools (e.g., GitHub Copilot, ChatGPT, etc.) in developing this PR
  • I understand all the code I'm submitting
  • I have reviewed and validated all AI-generated code

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.34%. Comparing base (ecf3a1c) to head (d45bfb4).
⚠️ Report is 11 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1337   +/-   ##
=======================================
  Coverage   87.34%   87.34%           
=======================================
  Files          24       24           
  Lines        2978     2978           
=======================================
  Hits         2601     2601           
  Misses        377      377           
Flag Coverage Δ
unittests 87.34% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@abhayrajjais01 abhayrajjais01 force-pushed the fix-metrics-evaluation branch from 0390dd8 to d45bfb4 Compare March 6, 2026 19:59
@henrykironde
Copy link
Copy Markdown
Contributor

I’m trying to understand this bug quickly. Could you explain how you discovered it? If you used AI, that’s totally fine. It would help if you could share the script you were running or a small example that reproduces the issue.

Typically, we ask contributors to first open an issue so we can reproduce the bug and then give a formal go-ahead for a PR. This helps us better understand and track the problem.

Copy link
Copy Markdown
Contributor

@henrykironde henrykironde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @abhayrajjais01 for the bug report. Please provide script to reproduce the bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KeyError in RecallPrecision.compute() when label_dict values are strings

2 participants