Skip to content

Commit

Permalink
Lift Curve: Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
janezd committed Nov 6, 2020
1 parent 5e1af6f commit 9212d09
Showing 1 changed file with 65 additions and 1 deletion.
66 changes: 65 additions & 1 deletion Orange/widgets/evaluate/tests/test_owliftcurve.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import copy
import unittest
from unittest.mock import Mock

import numpy as np

Expand All @@ -9,7 +11,8 @@
from Orange.widgets.evaluate.tests.base import EvaluateTest
from Orange.widgets.tests.base import WidgetTest
from Orange.widgets.tests.utils import simulate
from Orange.widgets.evaluate.owliftcurve import OWLiftCurve
from Orange.widgets.evaluate.owliftcurve import OWLiftCurve, cumulative_gains, \
cumulative_gains_from_results
from Orange.tests import test_filename


Expand Down Expand Up @@ -53,3 +56,64 @@ def test_nan_input(self):
self.assertTrue(self.widget.Error.invalid_results.is_shown())
self.send_signal(self.widget.Inputs.evaluation_results, None)
self.assertFalse(self.widget.Error.invalid_results.is_shown())


class UtilsTest(unittest.TestCase):
def test_cumulative_gains(self):
shuffle = [1, 2, 0, 3, 5, 4]
y_true = np.array([1, 1, 0, 0, 1, 0])[shuffle]
y_scores = np.array([0.9, 0.6, 0.5, 0.4, 0.4, 0.2])[shuffle]

assert_almost_equal = np.testing.assert_almost_equal

contacted, respondents, thresholds = cumulative_gains(y_true, y_scores)
assert_almost_equal(contacted, np.array([1, 2, 3, 5, 6]) / 6)
assert_almost_equal(thresholds, [0.9, 0.6, 0.5, 0.4, 0.2])
assert_almost_equal(respondents, np.array([1, 2, 2, 3, 3]) / 3)

contacted, respondents, thresholds = cumulative_gains(y_true, 1 - y_scores, target=0)
assert_almost_equal(contacted, np.array([1, 3, 4, 5, 6]) / 6)
assert_almost_equal(thresholds, [0.8, 0.6, 0.5, 0.4, 0.1])
assert_almost_equal(respondents, np.array([1, 2, 3, 3, 3]) / 3)

contacted, respondents, thresholds = \
cumulative_gains(np.array([], dtype=int), np.array([]))
assert_almost_equal(contacted, [])
assert_almost_equal(respondents, [])
assert_almost_equal(thresholds, [])

def test_cumulative_gains_from_results(self):
shuffle = [1, 2, 0, 3, 5, 4]
y_true = np.array([1, 1, 0, 0, 1, 0])[shuffle]
y_scores = np.array([0.9, 0.6, 0.5, 0.4, 0.4, 0.2])[shuffle]

results = Mock()
results.actual = y_true
results.probabilities = \
[Mock(), Mock(), np.vstack((1 - y_scores, y_scores)).T]

assert_almost_equal = np.testing.assert_almost_equal

contacted, respondents, thresholds = \
cumulative_gains_from_results(results, 1, 2)
assert_almost_equal(thresholds, [0.9, 0.6, 0.5, 0.4, 0.2])
assert_almost_equal(contacted, np.array([1, 2, 3, 5, 6]) / 6)
assert_almost_equal(respondents, np.array([1, 2, 2, 3, 3]) / 3)

contacted, respondents, thresholds = \
cumulative_gains_from_results(results, 0, 2)
assert_almost_equal(contacted, np.array([1, 3, 4, 5, 6]) / 6)
assert_almost_equal(thresholds, [0.8, 0.6, 0.5, 0.4, 0.1])
assert_almost_equal(respondents, np.array([1, 2, 3, 3, 3]) / 3)

results.actual = np.array([], dtype=int)
results.probabilities = np.empty((3, 0, 2))
contacted, respondents, thresholds = \
cumulative_gains(np.array([], dtype=int), np.array([]))
assert_almost_equal(contacted, [])
assert_almost_equal(respondents, [])
assert_almost_equal(thresholds, [])


if __name__ == "__main__":
unittest.main()

0 comments on commit 9212d09

Please sign in to comment.