|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from __future__ import division, print_function |
| 3 | + |
| 4 | +import os |
| 5 | +import sys |
| 6 | +import unittest |
| 7 | + |
| 8 | +# noinspection PyProtectedMember |
| 9 | +from numpy.testing import (assert_allclose, assert_array_less, assert_equal, |
| 10 | + assert_raises) |
| 11 | +from scipy.stats import rankdata |
| 12 | +from sklearn.base import clone |
| 13 | +from sklearn.metrics import roc_auc_score |
| 14 | + |
| 15 | +# temporary solution for relative imports in case pyod is not installed |
| 16 | +# if pyod is installed, no need to use the following line |
| 17 | +sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) |
| 18 | + |
| 19 | +from pyod.models.sampling import Sampling |
| 20 | +from pyod.utils.data import generate_data |
| 21 | + |
| 22 | + |
| 23 | +class TestSampling(unittest.TestCase): |
| 24 | + def setUp(self): |
| 25 | + self.n_train = 200 |
| 26 | + self.n_test = 100 |
| 27 | + self.contamination = 0.1 |
| 28 | + self.roc_floor = 0.8 |
| 29 | + self.X_train, self.y_train, self.X_test, self.y_test = generate_data( |
| 30 | + n_train=self.n_train, |
| 31 | + n_test=self.n_test, |
| 32 | + contamination=self.contamination, |
| 33 | + random_state=42, |
| 34 | + ) |
| 35 | + |
| 36 | + self.clf = Sampling(contamination=self.contamination, random_state=42) |
| 37 | + self.clf.fit(self.X_train) |
| 38 | + |
| 39 | + def test_parameters(self): |
| 40 | + assert ( |
| 41 | + hasattr(self.clf, "decision_scores_") |
| 42 | + and self.clf.decision_scores_ is not None |
| 43 | + ) |
| 44 | + assert hasattr(self.clf, "labels_") and self.clf.labels_ is not None |
| 45 | + assert hasattr(self.clf, "threshold_") and self.clf.threshold_ is not None |
| 46 | + |
| 47 | + def test_train_scores(self): |
| 48 | + assert_equal(len(self.clf.decision_scores_), self.X_train.shape[0]) |
| 49 | + |
| 50 | + def test_prediction_scores(self): |
| 51 | + pred_scores = self.clf.decision_function(self.X_test) |
| 52 | + |
| 53 | + # check score shapes |
| 54 | + assert_equal(pred_scores.shape[0], self.X_test.shape[0]) |
| 55 | + |
| 56 | + # check performance |
| 57 | + assert roc_auc_score(self.y_test, pred_scores) >= self.roc_floor |
| 58 | + |
| 59 | + def test_prediction_labels(self): |
| 60 | + pred_labels = self.clf.predict(self.X_test) |
| 61 | + assert_equal(pred_labels.shape, self.y_test.shape) |
| 62 | + |
| 63 | + def test_prediction_proba(self): |
| 64 | + pred_proba = self.clf.predict_proba(self.X_test) |
| 65 | + assert pred_proba.min() >= 0 |
| 66 | + assert pred_proba.max() <= 1 |
| 67 | + |
| 68 | + def test_prediction_proba_linear(self): |
| 69 | + pred_proba = self.clf.predict_proba(self.X_test, method="linear") |
| 70 | + assert pred_proba.min() >= 0 |
| 71 | + assert pred_proba.max() <= 1 |
| 72 | + |
| 73 | + def test_prediction_proba_unify(self): |
| 74 | + pred_proba = self.clf.predict_proba(self.X_test, method="unify") |
| 75 | + assert pred_proba.min() >= 0 |
| 76 | + assert pred_proba.max() <= 1 |
| 77 | + |
| 78 | + def test_prediction_proba_parameter(self): |
| 79 | + with assert_raises(ValueError): |
| 80 | + self.clf.predict_proba(self.X_test, method="something") |
| 81 | + |
| 82 | + def test_prediction_labels_confidence(self): |
| 83 | + pred_labels, confidence = self.clf.predict(self.X_test, return_confidence=True) |
| 84 | + assert_equal(pred_labels.shape, self.y_test.shape) |
| 85 | + assert_equal(confidence.shape, self.y_test.shape) |
| 86 | + assert confidence.min() >= 0 |
| 87 | + assert confidence.max() <= 1 |
| 88 | + |
| 89 | + def test_prediction_proba_linear_confidence(self): |
| 90 | + pred_proba, confidence = self.clf.predict_proba( |
| 91 | + self.X_test, method="linear", return_confidence=True |
| 92 | + ) |
| 93 | + assert pred_proba.min() >= 0 |
| 94 | + assert pred_proba.max() <= 1 |
| 95 | + |
| 96 | + assert_equal(confidence.shape, self.y_test.shape) |
| 97 | + assert confidence.min() >= 0 |
| 98 | + assert confidence.max() <= 1 |
| 99 | + |
| 100 | + def test_fit_predict(self): |
| 101 | + pred_labels = self.clf.fit_predict(self.X_train) |
| 102 | + assert_equal(pred_labels.shape, self.y_train.shape) |
| 103 | + |
| 104 | + def test_fit_predict_score(self): |
| 105 | + self.clf.fit_predict_score(self.X_test, self.y_test) |
| 106 | + self.clf.fit_predict_score(self.X_test, self.y_test, scoring="roc_auc_score") |
| 107 | + self.clf.fit_predict_score(self.X_test, self.y_test, scoring="prc_n_score") |
| 108 | + with assert_raises(NotImplementedError): |
| 109 | + self.clf.fit_predict_score(self.X_test, self.y_test, scoring="something") |
| 110 | + |
| 111 | + def test_predict_rank(self): |
| 112 | + pred_socres = self.clf.decision_function(self.X_test) |
| 113 | + pred_ranks = self.clf._predict_rank(self.X_test) |
| 114 | + |
| 115 | + # assert the order is reserved |
| 116 | + assert_allclose(rankdata(pred_ranks), rankdata(pred_socres), atol=2) |
| 117 | + assert_array_less(pred_ranks, self.X_train.shape[0] + 1) |
| 118 | + assert_array_less(-0.1, pred_ranks) |
| 119 | + |
| 120 | + def test_predict_rank_normalized(self): |
| 121 | + pred_socres = self.clf.decision_function(self.X_test) |
| 122 | + pred_ranks = self.clf._predict_rank(self.X_test, normalized=True) |
| 123 | + |
| 124 | + # assert the order is reserved |
| 125 | + assert_allclose(rankdata(pred_ranks), rankdata(pred_socres), atol=2) |
| 126 | + assert_array_less(pred_ranks, 1.01) |
| 127 | + assert_array_less(-0.1, pred_ranks) |
| 128 | + |
| 129 | + def test_model_clone(self): |
| 130 | + clone_clf = clone(self.clf) |
| 131 | + |
| 132 | + def tearDown(self): |
| 133 | + pass |
| 134 | + |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + unittest.main() |
0 commit comments