Skip to content

Commit

Permalink
created more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
yzhao062 authored and yuezhao@cs.toronto.edu committed May 26, 2018
1 parent e899841 commit 421698e
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 3 deletions.
108 changes: 108 additions & 0 deletions pyod/test/test_combination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
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 import shuffle
from sklearn.utils.testing import assert_equal
from sklearn.utils.testing import assert_array_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

import numpy as np
from pyod.models.combination import aom
from pyod.models.combination import moa


class TestAOM(unittest.TestCase):
def setUp(self):
self.scores = np.asarray([[0.5, 0.8, 0.6, 0.9, 0.7, 0.6],
[0.8, 0.75, 0.25, 0.6, 0.45, 0.8],
[0.8, 0.3, 0.28, 0.99, 0.28, 0.3],
[0.74, 0.85, 0.38, 0.47, 0.27, 0.69]])

def test_aom_static_norepeat(self):
score = aom(self.scores, 3, method='static', replace=False,
random_state=42)

assert_equal(score.shape, (4,))

shuffled_list = shuffle(list(range(0, 6, 1)), random_state=42)
manual_scores = np.zeros([4, 3])
manual_scores[:, 0] = np.max(self.scores[:, shuffled_list[0:2]],
axis=1)
manual_scores[:, 1] = np.max(self.scores[:, shuffled_list[2:4]],
axis=1)
manual_scores[:, 2] = np.max(self.scores[:, shuffled_list[4:6]],
axis=1)

manual_score = np.mean(manual_scores, axis=1)
assert_array_equal(score, manual_score)

def test_aom_static_repeat(self):
score = aom(self.scores, 3, method='static', replace=True,
random_state=42)
assert_equal(score.shape, (4,))

# TODO: add more complicated testcases

def test_aom_dynamic_repeat(self):
score = aom(self.scores, 3, method='dynamic', replace=True,
random_state=42)
assert_equal(score.shape, (4,))

# TODO: add more complicated testcases

def tearDown(self):
pass


class TestMOA(unittest.TestCase):
def setUp(self):
self.scores = np.asarray([[0.5, 0.8, 0.6, 0.9, 0.7, 0.6],
[0.8, 0.75, 0.25, 0.6, 0.45, 0.8],
[0.8, 0.3, 0.28, 0.99, 0.28, 0.3],
[0.74, 0.85, 0.38, 0.47, 0.27, 0.69]])

def test_moa_static_norepeat(self):
score = moa(self.scores, 3, method='static', replace=False,
random_state=42)

assert_equal(score.shape, (4,))

shuffled_list = shuffle(list(range(0, 6, 1)), random_state=42)
manual_scores = np.zeros([4, 3])
manual_scores[:, 0] = np.mean(self.scores[:, shuffled_list[0:2]],
axis=1)
manual_scores[:, 1] = np.mean(self.scores[:, shuffled_list[2:4]],
axis=1)
manual_scores[:, 2] = np.mean(self.scores[:, shuffled_list[4:6]],
axis=1)

manual_score = np.max(manual_scores, axis=1)
assert_array_equal(score, manual_score)

def test_moa_static_repeat(self):
score = moa(self.scores, 3, method='static', replace=True,
random_state=42)
assert_equal(score.shape, (4,))

# TODO: add more complicated testcases

def test_moa_dynamic_repeat(self):
score = moa(self.scores, 3, method='dynamic', replace=True,
random_state=42)
assert_equal(score.shape, (4,))

# TODO: add more complicated testcases

def tearDown(self):
pass


if __name__ == '__main__':
unittest.main()
Binary file added pyod/test/test_data/cardio.mat
Binary file not shown.
4 changes: 2 additions & 2 deletions pyod/test/test_knn.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from sklearn.utils.testing import assert_less_equal
from sklearn.metrics import roc_auc_score

from pyod.models.knn import Knn
from pyod.models.knn import KNN
from pyod.utils.load_data import generate_data


Expand All @@ -25,7 +25,7 @@ def setUp(self):
n_train=self.n_train, n_test=self.n_test,
contamination=self.contamination)

self.clf = clf = Knn(contamination=self.contamination)
self.clf = clf = KNN(contamination=self.contamination)
self.clf.fit(self.X_train)

def test_parameters(self):
Expand Down
72 changes: 71 additions & 1 deletion pyod/test/test_utility.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
'''Testing utilities.'''
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 numpy as np

import unittest
from sklearn.utils.testing import assert_equal
from sklearn.utils.testing import assert_allclose
from sklearn.utils.testing import assert_less_equal
import numpy as np
from sklearn.utils.testing import assert_raises
from sklearn.metrics import precision_score

from pyod.utils.load_data import generate_data
from pyod.utils.utility import check_parameter_range
from pyod.utils.utility import standardizer
from pyod.utils.utility import get_label_n
from pyod.utils.utility import precision_n_scores


class TestUtils(unittest.TestCase):
Expand Down Expand Up @@ -43,5 +52,66 @@ def tearDown(self):
pass


class TestParameters(unittest.TestCase):
def setUp(self):
pass

def test_check_para_range(self):
with assert_raises(ValueError):
check_parameter_range(2)
with assert_raises(ValueError):
check_parameter_range(2, 100, 99)
with assert_raises(ValueError):
check_parameter_range(2, 100, 100)
with assert_raises(TypeError):
check_parameter_range('f', 3, 10)

def tearDown(self):
pass


class TestScaler(unittest.TestCase):

def setUp(self):
self.X_train = np.random.rand(500, 5)
self.X_test = np.random.rand(50, 5)

def test_normalization(self):
norm_X_train, norm_X_test = standardizer(self.X_train, self.X_train)
assert_allclose(norm_X_train.mean(), 0, atol=0.05)
assert_allclose(norm_X_train.std(), 1, atol=0.05)

assert_allclose(norm_X_test.mean(), 0, atol=0.05)
assert_allclose(norm_X_test.std(), 1, atol=0.05)

def tearDown(self):
pass


class TestMetrics(unittest.TestCase):

def setUp(self):
self.y = [0, 0, 1, 1, 1, 0, 0, 0, 1, 0]
self.y_pred = [0.1, 0.2, 0.2, 0.8, 0.2, 0.5, 0.7, 0.9, 1, 0.3]
self.manual_y_pred = [0, 0, 0, 1, 0, 0, 1, 1, 1, 0]
self.outlier_perc = 0.3

def test_precision_n_scores(self):
assert_equal(precision_score(self.y, self.manual_y_pred),
precision_n_scores(self.y, self.y_pred))

def test_get_label_n(self):
assert_allclose(self.manual_y_pred,
get_label_n(self.y, self.y_pred))

def test_get_label_n_equal_3(self):
manual_y_pred = [0, 0, 0, 1, 0, 0, 0, 1, 1, 0]
assert_allclose(manual_y_pred,
get_label_n(self.y, self.y_pred, n=3))

def tearDown(self):
pass


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

0 comments on commit 421698e

Please sign in to comment.