Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yzhao062 committed Apr 19, 2019
1 parent 3c59e3f commit c9570e9
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 79 deletions.
18 changes: 4 additions & 14 deletions pyod/test/test_abod.py
Expand Up @@ -30,22 +30,17 @@

class TestFastABOD(unittest.TestCase):
def setUp(self):
self.n_train = 100
self.n_test = 50
self.n_train = 200
self.n_test = 100
self.contamination = 0.1
self.roc_floor = 0.6
self.roc_floor = 0.8
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, random_state=42)

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

def test_sklearn_estimator(self):
# TODO: sklearn check does not support Numba optimization
# check_estimator(self.clf)
pass

def test_parameters(self):
assert_true(hasattr(self.clf, 'decision_scores_') and
self.clf.decision_scores_ is not None)
Expand Down Expand Up @@ -118,19 +113,14 @@ def setUp(self):
self.n_train = 50
self.n_test = 50
self.contamination = 0.2
self.roc_floor = 0.6
self.roc_floor = 0.8
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, random_state=42)

self.clf = ABOD(contamination=self.contamination, method='default')
self.clf.fit(self.X_train)

def test_sklearn_estimator(self):
# TODO: sklearn check does not support Numba optimization
# check_estimator(self.clf)
pass

def test_parameters(self):
if not hasattr(self.clf,
'decision_scores_') or self.clf.decision_scores_ is None:
Expand Down
11 changes: 3 additions & 8 deletions pyod/test/test_cblof.py
Expand Up @@ -30,22 +30,17 @@

class TestLOF(unittest.TestCase):
def setUp(self):
self.n_train = 100
self.n_test = 50
self.n_train = 200
self.n_test = 100
self.contamination = 0.1
self.roc_floor = 0.6
self.roc_floor = 0.8
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, random_state=42)

self.clf = CBLOF(contamination=self.contamination, random_state=42)
self.clf.fit(self.X_train)

def test_sklearn_estimator(self):
# TODO: sklearn examples are too small to form valid
# check_estimator(self.clf)
pass

def test_parameters(self):
assert_true(hasattr(self.clf, 'decision_scores_') and
self.clf.decision_scores_ is not None)
Expand Down
10 changes: 3 additions & 7 deletions pyod/test/test_feature_bagging.py
Expand Up @@ -31,21 +31,17 @@

class TestFeatureBagging(unittest.TestCase):
def setUp(self):
self.n_train = 100
self.n_test = 50
self.n_train = 200
self.n_test = 100
self.contamination = 0.1
self.roc_floor = 0.6
self.roc_floor = 0.8
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, random_state=42)

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

# TODO: failed due to sklearn uses 2 feature examples.
# def test_sklearn_estimator(self):
# check_estimator(self.clf)

def test_parameters(self):
assert_true(hasattr(self.clf, 'decision_scores_') and
self.clf.decision_scores_ is not None)
Expand Down
6 changes: 3 additions & 3 deletions pyod/test/test_hbos.py
Expand Up @@ -30,10 +30,10 @@

class TestHBOS(unittest.TestCase):
def setUp(self):
self.n_train = 100
self.n_test = 50
self.n_train = 200
self.n_test = 100
self.contamination = 0.1
self.roc_floor = 0.6
self.roc_floor = 0.8
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, random_state=42)
Expand Down
6 changes: 3 additions & 3 deletions pyod/test/test_iforest.py
Expand Up @@ -31,10 +31,10 @@

class TestIForest(unittest.TestCase):
def setUp(self):
self.n_train = 100
self.n_test = 50
self.n_train = 200
self.n_test = 100
self.contamination = 0.1
self.roc_floor = 0.6
self.roc_floor = 0.8
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, random_state=42)
Expand Down
28 changes: 14 additions & 14 deletions pyod/test/test_knn.py
Expand Up @@ -32,10 +32,10 @@

class TestKnn(unittest.TestCase):
def setUp(self):
self.n_train = 100
self.n_test = 50
self.n_train = 200
self.n_test = 100
self.contamination = 0.1
self.roc_floor = 0.6
self.roc_floor = 0.8
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, random_state=42)
Expand Down Expand Up @@ -132,10 +132,10 @@ def tearDown(self):
class TestKnnMean(unittest.TestCase):

def setUp(self):
self.n_train = 100
self.n_test = 50
self.n_train = 200
self.n_test = 100
self.contamination = 0.1
self.roc_floor = 0.75
self.roc_floor = 0.8
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, random_state=42)
Expand All @@ -160,10 +160,10 @@ def tearDown(self):
class TestKnnMedian(unittest.TestCase):

def setUp(self):
self.n_train = 100
self.n_test = 50
self.n_train = 200
self.n_test = 100
self.contamination = 0.1
self.roc_floor = 0.75
self.roc_floor = 0.8
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, random_state=42)
Expand All @@ -187,10 +187,10 @@ def tearDown(self):

class TestKnnMahalanobis(unittest.TestCase):
def setUp(self):
self.n_train = 100
self.n_test = 50
self.n_train = 200
self.n_test = 100
self.contamination = 0.1
self.roc_floor = 0.6
self.roc_floor = 0.8
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, random_state=42)
Expand Down Expand Up @@ -268,7 +268,7 @@ def test_predict_rank(self):
pred_ranks = self.clf._predict_rank(self.X_test)

# assert the order is reserved
assert_allclose(rankdata(pred_ranks), rankdata(pred_socres), atol=2)
assert_allclose(rankdata(pred_ranks), rankdata(pred_socres), atol=3)
assert_array_less(pred_ranks, self.X_train.shape[0] + 1)
assert_array_less(-0.1, pred_ranks)

Expand All @@ -277,7 +277,7 @@ def test_predict_rank_normalized(self):
pred_ranks = self.clf._predict_rank(self.X_test, normalized=True)

# assert the order is reserved
assert_allclose(rankdata(pred_ranks), rankdata(pred_socres), atol=2)
assert_allclose(rankdata(pred_ranks), rankdata(pred_socres), atol=3)
assert_array_less(pred_ranks, 1.01)
assert_array_less(-0.1, pred_ranks)

Expand Down
6 changes: 3 additions & 3 deletions pyod/test/test_loci.py
Expand Up @@ -31,10 +31,10 @@

class TestLOCI(unittest.TestCase):
def setUp(self):
self.n_train = 100
self.n_test = 50
self.n_train = 200
self.n_test = 100
self.contamination = 0.1
self.roc_floor = 0.6
self.roc_floor = 0.8
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, random_state=42)
Expand Down
8 changes: 3 additions & 5 deletions pyod/test/test_lof.py
Expand Up @@ -30,10 +30,10 @@

class TestLOF(unittest.TestCase):
def setUp(self):
self.n_train = 100
self.n_test = 50
self.n_train = 200
self.n_test = 100
self.contamination = 0.1
self.roc_floor = 0.6
self.roc_floor = 0.8
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, random_state=42)
Expand Down Expand Up @@ -110,8 +110,6 @@ def test_fit_predict_score(self):
def test_predict_rank(self):
pred_socres = self.clf.decision_function(self.X_test)
pred_ranks = self.clf._predict_rank(self.X_test)
print(pred_ranks)
print(pred_ranks)

# assert the order is reserved
assert_allclose(rankdata(pred_ranks), rankdata(pred_socres), atol=2)
Expand Down
6 changes: 3 additions & 3 deletions pyod/test/test_lscp.py
Expand Up @@ -32,10 +32,10 @@

class TestLSCP(unittest.TestCase):
def setUp(self):
self.n_train = 100
self.n_test = 50
self.n_train = 200
self.n_test = 100
self.contamination = 0.1
self.roc_floor = 0.6
self.roc_floor = 0.8
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, random_state=42)
Expand Down
10 changes: 5 additions & 5 deletions pyod/test/test_mcd.py
Expand Up @@ -30,10 +30,10 @@

class TestMCD(unittest.TestCase):
def setUp(self):
self.n_train = 100
self.n_test = 50
self.n_train = 200
self.n_test = 100
self.contamination = 0.1
self.roc_floor = 0.6
self.roc_floor = 0.8
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, random_state=42)
Expand Down Expand Up @@ -124,7 +124,7 @@ def test_predict_rank(self):
pred_ranks = self.clf._predict_rank(self.X_test)

# assert the order is reserved
assert_allclose(rankdata(pred_ranks), rankdata(pred_socres), atol=2)
assert_allclose(rankdata(pred_ranks), rankdata(pred_socres), atol=2.5)
assert_array_less(pred_ranks, self.X_train.shape[0] + 1)
assert_array_less(-0.1, pred_ranks)

Expand All @@ -133,7 +133,7 @@ def test_predict_rank_normalized(self):
pred_ranks = self.clf._predict_rank(self.X_test, normalized=True)

# assert the order is reserved
assert_allclose(rankdata(pred_ranks), rankdata(pred_socres), atol=2)
assert_allclose(rankdata(pred_ranks), rankdata(pred_socres), atol=2.5)
assert_array_less(pred_ranks, 1.01)
assert_array_less(-0.1, pred_ranks)

Expand Down
10 changes: 5 additions & 5 deletions pyod/test/test_ocsvm.py
Expand Up @@ -31,10 +31,10 @@

class TestOCSVM(unittest.TestCase):
def setUp(self):
self.n_train = 100
self.n_test = 50
self.n_train = 200
self.n_test = 100
self.contamination = 0.1
self.roc_floor = 0.6
self.roc_floor = 0.8
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, random_state=42)
Expand Down Expand Up @@ -118,7 +118,7 @@ def test_predict_rank(self):
pred_ranks = self.clf._predict_rank(self.X_test)

# assert the order is reserved
assert_allclose(rankdata(pred_ranks), rankdata(pred_socres), atol=2)
assert_allclose(rankdata(pred_ranks), rankdata(pred_socres), atol=3.5)
assert_array_less(pred_ranks, self.X_train.shape[0] + 1)
assert_array_less(-0.1, pred_ranks)

Expand All @@ -127,7 +127,7 @@ def test_predict_rank_normalized(self):
pred_ranks = self.clf._predict_rank(self.X_test, normalized=True)

# assert the order is reserved
assert_allclose(rankdata(pred_ranks), rankdata(pred_socres), atol=2)
assert_allclose(rankdata(pred_ranks), rankdata(pred_socres), atol=3.5)
assert_array_less(pred_ranks, 1.01)
assert_array_less(-0.1, pred_ranks)

Expand Down
11 changes: 6 additions & 5 deletions pyod/test/test_pca.py
Expand Up @@ -30,15 +30,16 @@

class TestPCA(unittest.TestCase):
def setUp(self):
self.n_train = 100
self.n_test = 50
self.n_train = 200
self.n_test = 100
self.contamination = 0.1
self.roc_floor = 0.5
self.roc_floor = 0.8
self.X_train, self.y_train, self.X_test, self.y_test = generate_data(
n_train=self.n_train, n_test=self.n_test,
n_train=self.n_train, n_test=self.n_test, n_features=10,
contamination=self.contamination, random_state=42)

self.clf = PCA(contamination=self.contamination, random_state=42)
self.clf = PCA(contamination=self.contamination,
random_state=42)
self.clf.fit(self.X_train)

def test_sklearn_estimator(self):
Expand Down
8 changes: 4 additions & 4 deletions pyod/test/test_sos.py
Expand Up @@ -32,12 +32,12 @@

class TestSOS(unittest.TestCase):
def setUp(self):
self.n_train = 100
self.n_test = 50
self.n_train = 200
self.n_test = 100
self.contamination = 0.1
self.roc_floor = 0.6
# self.roc_floor = 0.8
self.X_train, self.y_train, self.X_test, self.y_test = generate_data(
n_train=self.n_train, n_test=self.n_test,
n_train=self.n_train, n_test=self.n_test, n_features=5,
contamination=self.contamination, random_state=42)

self.clf = SOS(contamination=self.contamination)
Expand Down

0 comments on commit c9570e9

Please sign in to comment.