jacobgil / confidenceinterval

The long missing library for python confidence intervals
MIT License
131 stars 14 forks source link

Bugfix: Update negation syntax in compute_ground_truth_statistics #10

Closed CallumWSimprints closed 3 months ago

CallumWSimprints commented 4 months ago

Hi,

First of all, amazing library - great work! ✨

I've spotted a potential bug surrounding negation in numpy. The ('-') syntax does not work for reversing a boolean array, which seems to get passed to the compute_ground_truth_statistics in certain scenarios (e.g., sometimes, but not always, when asking for the fast delong solver).

I've updated the code to use the tilde sign ('~'). I hope this small change helps as it works for both integer and boolean arrays.

Error

image

Minimal reproducible example & test case:

from confidenceinterval.delong import compute_ground_truth_statistics
import numpy as np
import pytest

@pytest.mark.parametrize(
    'ground_truth',
    [
        (np.array([0,0,1,1,1])), # Values are integers
        (np.array([False, False, True, True, True])) # values are bools
        ]
)
def test_compute_ground_truth_statistics_bool(ground_truth):

    sample_weight = np.array([1,1,1,1,1])

    expected_order = np.array([2,3,4,0,1])
    expected_label_1_count = 3
    expected_ordered_sample_weight = np.array([1,1,1,1,1])

    order, label_1_count, ordered_sample_weight = compute_ground_truth_statistics(ground_truth=ground_truth, sample_weight=sample_weight)

    assert np.array_equal(expected_order, order)
    assert expected_label_1_count == label_1_count
    assert np.array_equal(expected_ordered_sample_weight, ordered_sample_weight)

I can add this test to the library if you can let me know where it should go 😄 e.g., a new file called test_utils.py, or similar?

Keep up the good work, Callum

jacobgil commented 4 months ago

Thanks so much! For the PR, and for the kind words.

I think test_delong.py would be great!

CallumWSimprints commented 3 months ago

Thank you for the lightning-fast reply!

Test added in a new file called test_delong.py, ready for your review @jacobgil.