# Path: drain/metrics.py
# def precision(y_true, y_score, k=None, return_bounds=False):
#     """
#     If return_bounds is False then returns precision on the
#         labeled examples in the top k.
#     If return_bounds is True the returns a tuple containing:
#         - precision on the labeled examples in the top k
#         - number of labeled examples in the top k
#         - lower bound of precision in the top k, assuming all
#             unlabaled examples are False
#         - upper bound of precision in the top k, assuming all
#             unlabaled examples are True
#     """
#     y_true, y_score = to_float(y_true, y_score)
#     top = _argtop(y_score, k)
# 
#     n = np.nan_to_num(y_true[top]).sum()    # fill missing labels with 0
#     d = (~np.isnan(y_true[top])).sum()      # count number of labels
#     p = n/d
# 
#     if return_bounds:
#         k = len(y_true) if k is None else k
#         bounds = (n/k, (n+k-d)/k) if k != 0 else (np.nan, np.nan)
#         return p, d, bounds[0], bounds[1]
#     else:
#         return p


import numpy as np
import pytest
from drain.metrics import precision
from numpy.testing import assert_almost_equal


def test_precision():
    y_true = np.array([True, True, False])
    y_score = np.array([1, 0, .5])

    assert precision(y_true, y_score,k=2) == .5

def test_precision_nan():
    y_true = np.array([True, False, np.nan])
    y_score = np.array([1, 0, .5])


# Path: drain/metrics.py
# def precision(y_true, y_score, k=None, return_bounds=False):
#     """
#     If return_bounds is False then returns precision on the
#         labeled examples in the top k.
#     If return_bounds is True the returns a tuple containing:
#         - precision on the labeled examples in the top k
#         - number of labeled examples in the top k
#         - lower bound of precision in the top k, assuming all
#             unlabaled examples are False
#         - upper bound of precision in the top k, assuming all
#             unlabaled examples are True
#     """
#     y_true, y_score = to_float(y_true, y_score)
#     top = _argtop(y_score, k)
# 
#     n = np.nan_to_num(y_true[top]).sum()    # fill missing labels with 0
#     d = (~np.isnan(y_true[top])).sum()      # count number of labels
#     p = n/d
# 
#     if return_bounds:
#         k = len(y_true) if k is None else k
#         bounds = (n/k, (n+k-d)/k) if k != 0 else (np.nan, np.nan)
#         return p, d, bounds[0], bounds[1]
#     else:
#         return p


# Path: tests/test_metrics.py
import numpy as np
import pytest
from drain.metrics import precision
from numpy.testing import assert_almost_equal



def test_precision():
    y_true = np.array([True, True, False])
    y_score = np.array([1, 0, .5])

    assert precision(y_true, y_score,k=2) == .5

def test_precision_nan():
    y_true = np.array([True, False, np.nan])
    y_score = np.array([1, 0, .5])


    p = precision(y_true, y_score,None, return_bounds=True)