Skip to content

Commit

Permalink
added new tests and improved existing tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
yuezhao@cs.toronto.edu authored and yuezhao@cs.toronto.edu committed May 25, 2018
1 parent 98aeec9 commit 7d50dee
Show file tree
Hide file tree
Showing 6 changed files with 341 additions and 8 deletions.
32 changes: 30 additions & 2 deletions pyod/test/test_abod.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import unittest
from sklearn.utils.testing import assert_equal
from sklearn.utils.testing import assert_greater
from sklearn.utils.testing import assert_greater_equal
from sklearn.utils.testing import assert_less_equal
from sklearn.metrics import roc_auc_score

from pyod.models.abod import ABOD
Expand All @@ -15,7 +17,7 @@

class TestABOD(unittest.TestCase):
def setUp(self):
self.n_train = 100
self.n_train = 50
self.n_test = 50
self.contamination = 0.1
self.roc_floor = 0.6
Expand All @@ -26,11 +28,20 @@ def setUp(self):
self.clf = clf = ABOD(contamination=self.contamination)
self.clf.fit(self.X_train)

def test_parameters(self):
if not hasattr(self.clf,
'decision_scores') or self.clf.decision_scores is None:
self.assertRaises(AttributeError, 'decision_scores is not set')
if not hasattr(self.clf, 'y_pred') or self.clf.y_pred is None:
self.assertRaises(AttributeError, 'y_pred is not set')
if not hasattr(self.clf, 'threshold_') or self.clf.threshold_ is None:
self.assertRaises(AttributeError, 'threshold_ is not set')

def test_train_scores(self):
assert_equal(len(self.clf.decision_scores), self.X_train.shape[0])

def test_prediction_scores(self):
pred_scores = self.clf.decision_function(self.X_test) * -1
pred_scores = self.clf.decision_function(self.X_test)

# check score shapes
assert_equal(pred_scores.shape[0], self.X_test.shape[0])
Expand All @@ -42,6 +53,23 @@ def test_prediction_labels(self):
pred_labels = self.clf.predict(self.X_test)
assert_equal(pred_labels.shape, self.y_test.shape)

def test_prediction_proba(self):
pred_proba = self.clf.predict_proba(self.X_test)
assert_greater_equal(pred_proba.min(), 0)
assert_less_equal(pred_proba.max(), 1)

def test_prediction_proba(self):
pred_proba = self.clf.predict_proba(self.X_test)
assert_greater_equal(pred_proba.min(), 0)
assert_less_equal(pred_proba.max(), 1)

def test_fit_predict(self):
pred_labels = self.clf.fit_predict(self.X_train)
assert_equal(pred_labels.shape, self.y_train.shape)

def test_evaluate(self):
self.clf.evaluate(self.X_test, self.y_test)

def tearDown(self):
pass

Expand Down
32 changes: 32 additions & 0 deletions pyod/test/test_hbos.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import unittest
from sklearn.utils.testing import assert_equal
from sklearn.utils.testing import assert_greater
from sklearn.utils.testing import assert_greater_equal
from sklearn.utils.testing import assert_less_equal
from sklearn.metrics import roc_auc_score

from pyod.models.hbos import HBOS
Expand All @@ -26,6 +28,19 @@ def setUp(self):
self.clf = clf = HBOS(contamination=self.contamination)
self.clf.fit(self.X_train)

def test_parameters(self):
if not hasattr(self.clf,
'decision_scores') or self.clf.decision_scores is None:
self.assertRaises(AttributeError, 'decision_scores is not set')
if not hasattr(self.clf, 'y_pred') or self.clf.y_pred is None:
self.assertRaises(AttributeError, 'y_pred is not set')
if not hasattr(self.clf, 'threshold_') or self.clf.threshold_ is None:
self.assertRaises(AttributeError, 'threshold_ is not set')
if not hasattr(self.clf, 'mu') or self.clf.mu is None:
self.assertRaises(AttributeError, 'mu is not set')
if not hasattr(self.clf, 'sigma') or self.clf.sigma is None:
self.assertRaises(AttributeError, 'sigma is not set')

def test_train_scores(self):
assert_equal(len(self.clf.decision_scores), self.X_train.shape[0])

Expand All @@ -42,6 +57,23 @@ def test_prediction_labels(self):
pred_labels = self.clf.predict(self.X_test)
assert_equal(pred_labels.shape, self.y_test.shape)

def test_prediction_proba(self):
pred_proba = self.clf.predict_proba(self.X_test)
assert_greater_equal(pred_proba.min(), 0)
assert_less_equal(pred_proba.max(), 1)

def test_prediction_proba(self):
pred_proba = self.clf.predict_proba(self.X_test)
assert_greater_equal(pred_proba.min(), 0)
assert_less_equal(pred_proba.max(), 1)

def test_fit_predict(self):
pred_labels = self.clf.fit_predict(self.X_train)
assert_equal(pred_labels.shape, self.y_train.shape)

def test_evaluate(self):
self.clf.evaluate(self.X_test, self.y_test)

def tearDown(self):
pass

Expand Down
87 changes: 87 additions & 0 deletions pyod/test/test_iforest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import os, sys

# temporary solution for relative imports in case pyod is not installed
# if pyod is installed, no need to use the following line
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

import unittest
from sklearn.utils.testing import assert_equal
from sklearn.utils.testing import assert_greater
from sklearn.utils.testing import assert_greater_equal
from sklearn.utils.testing import assert_less_equal
from sklearn.metrics import roc_auc_score

from pyod.models.iforest import IForest
from pyod.utils.load_data import generate_data


class TestIForest(unittest.TestCase):
def setUp(self):
self.n_train = 100
self.n_test = 50
self.contamination = 0.1
self.roc_floor = 0.6
self.X_train, self.y_train, _, self.X_test, self.y_test, _ = generate_data(
n_train=self.n_train, n_test=self.n_test,
contamination=self.contamination)

self.clf = clf = IForest(contamination=self.contamination)
self.clf.fit(self.X_train)

def test_parameters(self):
if not hasattr(self.clf,
'decision_scores') or self.clf.decision_scores is None:
self.assertRaises(AttributeError, 'decision_scores is not set')
if not hasattr(self.clf, 'y_pred') or self.clf.y_pred is None:
self.assertRaises(AttributeError, 'y_pred is not set')
if not hasattr(self.clf, 'threshold_') or self.clf.threshold_ is None:
self.assertRaises(AttributeError, 'threshold_ is not set')
if not hasattr(self.clf,
'estimators_') or self.clf.estimators_ is None:
self.assertRaises(AttributeError, 'estimators_ is not set')
if not hasattr(self.clf,
'estimators_samples_') or self.clf.estimators_samples_ is None:
self.assertRaises(AttributeError, 'estimators_samples_ is not set')
if not hasattr(self.clf,
'max_samples_') or self.clf.max_samples_ is None:
self.assertRaises(AttributeError, 'max_samples_ is not set')

def test_train_scores(self):
assert_equal(len(self.clf.decision_scores), self.X_train.shape[0])

def test_prediction_scores(self):
pred_scores = self.clf.decision_function(self.X_test)

# check score shapes
assert_equal(pred_scores.shape[0], self.X_test.shape[0])

# check performance
assert_greater(roc_auc_score(self.y_test, pred_scores), self.roc_floor)

def test_prediction_labels(self):
pred_labels = self.clf.predict(self.X_test)
assert_equal(pred_labels.shape, self.y_test.shape)

def test_prediction_proba(self):
pred_proba = self.clf.predict_proba(self.X_test)
assert_greater_equal(pred_proba.min(), 0)
assert_less_equal(pred_proba.max(), 1)

def test_prediction_proba(self):
pred_proba = self.clf.predict_proba(self.X_test)
assert_greater_equal(pred_proba.min(), 0)
assert_less_equal(pred_proba.max(), 1)

def test_fit_predict(self):
pred_labels = self.clf.fit_predict(self.X_train)
assert_equal(pred_labels.shape, self.y_train.shape)

def test_evaluate(self):
self.clf.evaluate(self.X_test, self.y_test)

def tearDown(self):
pass


if __name__ == '__main__':
unittest.main()
20 changes: 14 additions & 6 deletions pyod/test/test_knn.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ def setUp(self):
self.clf.fit(self.X_train)

def test_parameters(self):
if not hasattr(self.clf, 'decision_scores'):
if not hasattr(self.clf,
'decision_scores') or self.clf.decision_scores is None:
self.assertRaises(AttributeError, 'decision_scores is not set')
if not hasattr(self.clf, 'y_pred'):
if not hasattr(self.clf, 'y_pred') or self.clf.y_pred is None:
self.assertRaises(AttributeError, 'y_pred is not set')
if not hasattr(self.clf, 'threshold'):
self.assertRaises(AttributeError, 'threshold is not set')
if not hasattr(self.clf, 'mu'):
if not hasattr(self.clf, 'threshold_') or self.clf.threshold_ is None:
self.assertRaises(AttributeError, 'threshold_ is not set')
if not hasattr(self.clf, 'mu') or self.clf.mu is None:
self.assertRaises(AttributeError, 'mu is not set')
if not hasattr(self.clf, 'sigma'):
if not hasattr(self.clf, 'sigma') or self.clf.sigma is None:
self.assertRaises(AttributeError, 'sigma is not set')

def test_train_scores(self):
Expand Down Expand Up @@ -66,6 +67,13 @@ def test_prediction_proba(self):
assert_greater_equal(pred_proba.min(), 0)
assert_less_equal(pred_proba.max(), 1)

def test_fit_predict(self):
pred_labels = self.clf.fit_predict(self.X_train)
assert_equal(pred_labels.shape, self.y_train.shape)

def test_evaluate(self):
self.clf.evaluate(self.X_test, self.y_test)

def tearDown(self):
pass

Expand Down
86 changes: 86 additions & 0 deletions pyod/test/test_lof.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import os, sys

# temporary solution for relative imports in case pyod is not installed
# if pyod is installed, no need to use the following line
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

import unittest
from sklearn.utils.testing import assert_equal
from sklearn.utils.testing import assert_greater
from sklearn.utils.testing import assert_greater_equal
from sklearn.utils.testing import assert_less_equal
from sklearn.metrics import roc_auc_score

from pyod.models.lof import LOF
from pyod.utils.load_data import generate_data


class TestLOF(unittest.TestCase):
def setUp(self):
self.n_train = 100
self.n_test = 50
self.contamination = 0.1
self.roc_floor = 0.6
self.X_train, self.y_train, _, self.X_test, self.y_test, _ = generate_data(
n_train=self.n_train, n_test=self.n_test,
contamination=self.contamination)

self.clf = clf = LOF(contamination=self.contamination)
self.clf.fit(self.X_train)

def test_parameters(self):
if not hasattr(self.clf,
'decision_scores') or self.clf.decision_scores is None:
self.assertRaises(AttributeError, 'decision_scores is not set')
if not hasattr(self.clf, 'y_pred') or self.clf.y_pred is None:
self.assertRaises(AttributeError, 'y_pred is not set')
if not hasattr(self.clf, 'threshold_') or self.clf.threshold_ is None:
self.assertRaises(AttributeError, 'threshold_ is not set')
if not hasattr(self.clf,
'negative_outlier_factor_') or self.clf.negative_outlier_factor_ is None:
self.assertRaises(AttributeError,
'negative_outlier_factor_ is not set')

if not hasattr(self.clf,
'n_neighbors_') or self.clf.n_neighbors_ is None:
self.assertRaises(AttributeError, 'n_neighbors_ is not set')

def test_train_scores(self):
assert_equal(len(self.clf.decision_scores), self.X_train.shape[0])

def test_prediction_scores(self):
pred_scores = self.clf.decision_function(self.X_test)

# check score shapes
assert_equal(pred_scores.shape[0], self.X_test.shape[0])

# check performance
assert_greater(roc_auc_score(self.y_test, pred_scores), self.roc_floor)

def test_prediction_labels(self):
pred_labels = self.clf.predict(self.X_test)
assert_equal(pred_labels.shape, self.y_test.shape)

def test_prediction_proba(self):
pred_proba = self.clf.predict_proba(self.X_test)
assert_greater_equal(pred_proba.min(), 0)
assert_less_equal(pred_proba.max(), 1)

def test_prediction_proba(self):
pred_proba = self.clf.predict_proba(self.X_test)
assert_greater_equal(pred_proba.min(), 0)
assert_less_equal(pred_proba.max(), 1)

def test_fit_predict(self):
pred_labels = self.clf.fit_predict(self.X_train)
assert_equal(pred_labels.shape, self.y_train.shape)

def test_evaluate(self):
self.clf.evaluate(self.X_test, self.y_test)

def tearDown(self):
pass


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

0 comments on commit 7d50dee

Please sign in to comment.