Skip to content

99 update plot precision recall curve with thresholds annotations to match sklearns plot chance level behavior#125

Merged
idanmoradarthas merged 7 commits into
masterfrom
99-update-plot_precision_recall_curve_with_thresholds_annotations-to-match-sklearns-plot_chance_level-behavior
Apr 27, 2026
Merged

99 update plot precision recall curve with thresholds annotations to match sklearns plot chance level behavior#125
idanmoradarthas merged 7 commits into
masterfrom
99-update-plot_precision_recall_curve_with_thresholds_annotations-to-match-sklearns-plot_chance_level-behavior

Conversation

@idanmoradarthas

@idanmoradarthas idanmoradarthas commented Apr 25, 2026

Copy link
Copy Markdown
Owner

Summary

This PR refactors and enhances the evaluation curve plotting functions in ds_utils.metrics.curves to align more closely with scikit-learn conventions and provide better visual diagnostics. The primary focus was on correcting the "chance level" representation in Precision-Recall curves and improving the overall usability and styling of both ROC and PR plots.

Key Changes

Precision-Recall Curves (plot_precision_recall_curve_with_thresholds_annotations)

  • Corrected Chance Level: Replaced the incorrect diagonal "random classifier" line with a proper horizontal line at the prevalence of the positive class, matching sklearn.metrics.PrecisionRecallDisplay.
  • AP in Legend: Integrated average_precision_score calculation. Classifier legends now display Classifier (AP = 0.XX), and the chance level legend displays Chance level (AP = prevalence).
  • New Parameters: Added plot_chance_level and chance_level_kw to control the display and styling of the prevalence line.
  • Support for Sample Weights: The prevalence calculation now correctly accounts for sample_weight.
  • Bug Fix: Fixed a side-effect bug where the positive_label parameter was being mutated internally.
  • Fail-Fast Validation: Implemented early binary-label validation when plotting chance levels without an explicit positive_label.

ROC Curves (plot_roc_curve_with_thresholds_annotations)

  • Styling Flexibility: Added random_classifier_line_kw to allow customization of the diagonal random classifier line.
  • Legend Standardization: Updated the default legend name to "Random Classifier (AUC = 0.50)" to match scikit-learn conventions.

General Improvements

  • Cleaner Tooltips: Set hoverinfo="text" for classifier curves and hoverinfo="name" for reference lines to reduce Plotly's default hover clutter.
  • Robust Error Handling: Separated try/except blocks for curve generation vs. metric calculation to provide more precise error messages.
  • Documentation: Updated README.md, skills/metrics/SKILL.md, and all function docstrings to reflect API changes and new parameters.

Testing & Verification

  • Refactored Test Suite: Migrated test_metrics/test_curves.py to use pytest.mark.parametrize for better coverage with less duplication.
  • Numerical Validation: Added tests that explicitly assert the y-values and legend names for the chance level line.
  • Mocking Consistency: Standardized the mocking of sklearn metrics (roc_auc_score, average_precision_score) across the test suite.
  • Regression Testing: Updated JSON test resources to include expected Average Precision values.
  • Visual Verification: Verified that pytest-mpl image comparisons pass with the new styling and legend updates.
  • Linting: All changes verified with ruff check.

Breaking Changes

  • Removed the add_random_classifier_line parameter from plot_precision_recall_curve_with_thresholds_annotations (as the diagonal line was incorrect for PR curves) and replaced it with plot_chance_level.

- Updated `plot_roc_curve_with_thresholds_annotations` to include customizable styling for the random classifier line via `random_classifier_line_kw`.
- Introduced `plot_precision_recall_curve_with_thresholds_annotations` to support plotting a chance level prevalence line with customizable styling through `chance_level_kw`.
- Added handling for average precision score calculation in the precision-recall curve function.
- Updated documentation to reflect new parameters and their usage.
- Modified tests to cover new functionalities and ensure robust validation.

These enhancements improve the flexibility and usability of the plotting functions, allowing for better customization in visualizations.
…Handling and Annotations

- Updated `plot_roc_curve_with_thresholds_annotations` and `plot_precision_recall_curve_with_thresholds_annotations` to include hover information for better interactivity in visualizations.
- Improved error handling for average precision score calculations, providing clearer error messages for users.
- Enhanced documentation to clarify parameter usage and updated descriptions for better understanding.
- Modified tests to ensure robust validation of new functionalities and error handling scenarios.

These enhancements improve the usability and clarity of the plotting functions, making them more informative and user-friendly.
- Updated `plot_precision_recall_curve_with_thresholds_annotations` to clarify the inference of the positive class when `positive_label` is None, aligning with scikit-learn's behavior.
- Refactored the handling of the `positive_label` parameter to use `effective_positive_label` for improved clarity and consistency.
- Modified tests for `plot_precision_recall_curve_with_thresholds_annotations` to ensure robust validation, including a new test for non-binary `y_true` inputs.

These changes improve the usability and accuracy of the precision-recall curve plotting function, enhancing its documentation and test coverage.
- Updated the `plot_precision_recall_curve_with_thresholds_annotations` function to clarify the resolution of the positive label when `plot_chance_level=True` and `positive_label=None`, ensuring compliance with scikit-learn conventions.
- Modified tests to validate the behavior of the function when handling non-binary `y_true` inputs, including assertions to confirm that the precision-recall curve function is not called in such cases.

These changes improve the robustness and clarity of the precision-recall curve plotting functionality.
- Increased the tolerance from 15 to 17 in the `test_plot_precision_recall_curve_with_thresholds_annotations_exists_figure` test to accommodate minor variations in generated plots.

This change ensures that the test remains robust against slight differences in rendering while maintaining the integrity of the precision-recall curve visualizations.
@idanmoradarthas
idanmoradarthas merged commit db7527f into master Apr 27, 2026
12 checks passed
@idanmoradarthas
idanmoradarthas deleted the 99-update-plot_precision_recall_curve_with_thresholds_annotations-to-match-sklearns-plot_chance_level-behavior branch April 27, 2026 18:20
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.

Update plot_precision_recall_curve_with_thresholds_annotations to Match sklearn's plot_chance_level Behavior

1 participant