Repository: scikit-learn/scikit-learn
File: sklearn/metrics/_plot/tests/test_roc_curve_display.py (Python)

Code before:
            ["fold1 (AUC = 0.80)", "fold2 (AUC = 0.70)"],
        ),
        (None, ["fold1", "fold2"], [{"c": "blue"}, {"c": "red"}], ["fold1", "fold2"]),
    ],
)
def test_roc_curve_display_default_labels(
    pyplot, roc_auc, name, curve_kwargs, expected_labels
):
    """Check the default labels used in the display."""
    fpr = [np.array([0, 0.5, 1]), np.array([0, 0.3, 1])]
    tpr = [np.array([0, 0.5, 1]), np.array([0, 0.3, 1])]
    disp = RocCurveDisplay(fpr=fpr, tpr=tpr, roc_auc=roc_auc, name=name).plot(
        curve_kwargs=curve_kwargs
    )
    for idx, expected_label in enumerate(expected_labels):
        assert disp.line_[idx].get_label() == expected_label


def _check_auc(display, constructor_name):
    roc_auc_limit = 0.95679
    roc_auc_limit_multi = [0.97007, 0.985915, 0.980952]

    if constructor_name == "from_cv_results":
        for idx, roc_auc in enumerate(display.roc_auc):
            assert roc_auc == pytest.approx(roc_auc_limit_multi[idx])
    else:
        assert display.roc_auc == pytest.approx(roc_auc_limit)
        assert trapezoid(display.tpr, display.fpr) == pytest.approx(roc_auc_limit)


@pytest.mark.parametrize("response_method", ["predict_proba", "decision_function"])
@pytest.mark.parametrize(
    "constructor_name", ["from_estimator", "from_predictions", "from_cv_results"]
)
def test_plot_roc_curve_pos_label(pyplot, response_method, constructor_name):
    """Check switching `pos_label` give correct statistics, using imbalanced data."""

    def _check_auc(display, constructor_name, pos_label):
        roc_auc_limit = 0.95679
        roc_auc_limit_multi = [0.97007, 0.985915, 0.980952]

        if constructor_name == "from_cv_results":
            for idx, roc_auc in enumerate(display.roc_auc):
                assert roc_auc == pytest.approx(roc_auc_limit_multi[idx])
        else:
            assert display.roc_auc == pytest.approx(roc_auc_limit)
            assert trapezoid(display.tpr, display.fpr) == pytest.approx(roc_auc_limit)

    _check_pos_label_statistics(
        RocCurveDisplay, response_method, constructor_name, _check_auc
    )

Review comment (refactor):
Note that `pos_label` is not needed for roc_auc because it is the same regardless of `pos_label`, but it is needed for `average_precsion_score` (which will also use the `_check_pos_label_statistics` helper)

Code after:
            ["fold1 (AUC = 0.80)", "fold2 (AUC = 0.70)"],
        ),
        (None, ["fold1", "fold2"], [{"c": "blue"}, {"c": "red"}], ["fold1", "fold2"]),
    ],
)
def test_roc_curve_display_default_labels(
    pyplot, roc_auc, name, curve_kwargs, expected_labels
):
    """Check the default labels used in the display."""
    fpr = [np.array([0, 0.5, 1]), np.array([0, 0.3, 1])]
    tpr = [np.array([0, 0.5, 1]), np.array([0, 0.3, 1])]
    disp = RocCurveDisplay(fpr=fpr, tpr=tpr, roc_auc=roc_auc, name=name).plot(
        curve_kwargs=curve_kwargs
    )
    for idx, expected_label in enumerate(expected_labels):
        assert disp.line_[idx].get_label() == expected_label


@pytest.mark.parametrize("response_method", ["predict_proba", "decision_function"])
@pytest.mark.parametrize(
    "constructor_name", ["from_estimator", "from_predictions", "from_cv_results"]
)
def test_plot_roc_curve_pos_label(pyplot, response_method, constructor_name):
    """Test switching `pos_label` gives correct statistics, using imbalanced data."""

    def _check_auc(display, constructor_name, pos_label):
        roc_auc_limit = 0.95679
        roc_auc_limit_multi = [0.97007, 0.985915, 0.980952]

        if constructor_name == "from_cv_results":
            for idx, roc_auc in enumerate(display.roc_auc):
                assert roc_auc == pytest.approx(roc_auc_limit_multi[idx])
        else:
            assert display.roc_auc == pytest.approx(roc_auc_limit)
            assert trapezoid(display.tpr, display.fpr) == pytest.approx(roc_auc_limit)

    _check_pos_label_statistics(
        RocCurveDisplay, response_method, constructor_name, _check_auc
    )


# TODO(1.9): remove
def test_y_score_and_y_pred_specified_error(pyplot):
    """1. Check that an error is raised when both y_score and y_pred are specified.
    2. Check that a warning is raised when y_pred is specified.
    """
    y_true = np.array([0, 1, 1, 0])
    y_score = np.array([0.1, 0.4, 0.35, 0.8])
    y_pred = np.array([0.2, 0.3, 0.5, 0.1])

    with pytest.raises(