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

Code before:
    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
    )


# 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])

Review comment (suggestion):
The `_check_auc` function is still also defined above the test function but not used anymore. I think you can remove it

Code after:
    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(
        ValueError, match="`y_pred` and `y_score` cannot be both specified"
    ):
        RocCurveDisplay.from_predictions(y_true, y_score=y_score, y_pred=y_pred)

    with pytest.warns(FutureWarning, match="y_pred was deprecated in 1.7"):
        display_y_pred = RocCurveDisplay.from_predictions(y_true, y_pred=y_score)
    desired_fpr, desired_fnr, _ = roc_curve(y_true, y_score)
    assert_allclose(display_y_pred.fpr, desired_fpr)
    assert_allclose(display_y_pred.tpr, desired_fnr)