Skip to content

Commit

Permalink
add testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
yzhao062 committed Jul 18, 2019
1 parent 5ea1524 commit 31bceab
Showing 1 changed file with 125 additions and 2 deletions.
127 changes: 125 additions & 2 deletions combo/test/test_classifier_comb.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,128 @@
# if combo is installed, no need to use the following line
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from combo.models.classifier_comb import BaseClassifierAggregator
from combo.models.classifier_comb import SimpleClassifierAggregator


# Check sklearn\tests\test_base
# A few test classes
# noinspection PyMissingConstructor,PyPep8Naming
class MyEstimator(BaseClassifierAggregator):

def __init__(self, l1=0, empty=None):
self.l1 = l1
self.empty = empty

def fit(self, X, y=None):
pass

def predict(self, X):
pass

def predict_proba(self, X):
pass


# noinspection PyMissingConstructor
class K(BaseClassifierAggregator):
def __init__(self, c=None, d=None):
self.c = c
self.d = d

def fit(self, X, y=None):
pass

def predict(self, X):
pass

def predict_proba(self, X):
pass


# noinspection PyMissingConstructor
class T(BaseClassifierAggregator):
def __init__(self, a=None, b=None):
self.a = a
self.b = b

def fit(self, X, y=None):
pass

def predict(self, X):
pass

def predict_proba(self, X):
pass


# noinspection PyMissingConstructor
class ModifyInitParams(BaseClassifierAggregator):
"""Deprecated behavior.
Equal parameters but with a type cast.
Doesn't fulfill a is a
"""

def __init__(self, a=np.array([0])):
self.a = a.copy()

def fit(self, X, y=None):
pass

def predict(self, X):
pass

def predict_proba(self, X):
pass


# noinspection PyMissingConstructor
class VargEstimator(BaseClassifierAggregator):
"""scikit-learn estimators shouldn't have vargs."""

def __init__(self, *vargs):
pass

def fit(self, X, y=None):
pass

def predict(self, X):
pass

def predict_proba(self, X):
pass


class TestBase(unittest.TestCase):
def test_repr(self):
# Smoke test the repr of the base estimator.
my_estimator = MyEstimator()
repr(my_estimator)
test = T(K(), K())
assert_equal(
repr(test),
"T(a=K(c=None, d=None), b=K(c=None, d=None))"
)

some_est = T(a=["long_params"] * 1000)
assert_equal(len(repr(some_est)), 415)

def test_str(self):
# Smoke test the str of the base estimator
my_estimator = MyEstimator()
str(my_estimator)

def test_get_params(self):
test = T(K(), K())

assert_true('a__d' in test.get_params(deep=True))
assert_true('a__d' not in test.get_params(deep=False))

test.set_params(a__d=2)
assert_true(test.a.d == 2)
assert_raises(ValueError, test.set_params, a__a=2)


class TestAverage(unittest.TestCase):
def setUp(self):
self.roc_floor = 0.9
Expand Down Expand Up @@ -127,6 +246,12 @@ def test_parameters(self):
assert_true(hasattr(self.clf, 'classifiers') and
self.clf.classifiers is not None)

# print clf details
self.clf

# set parameters
self.clf.set_params()

def test_train_scores(self):
y_train_predicted = self.clf.predict(self.X_train)
assert_equal(len(y_train_predicted), self.X_train.shape[0])
Expand Down Expand Up @@ -156,13 +281,11 @@ def test_prediction_proba(self):
n_classes = len(np.unique(self.y_train))
assert_equal(y_test_predicted.shape, (self.X_test.shape[0], n_classes))


# check probability sum is 1
y_test_predicted_sum = np.sum(y_test_predicted, axis=1)
assert_allclose(np.ones([self.X_test.shape[0], ]),
y_test_predicted_sum)


def tearDown(self):
pass

Expand Down

0 comments on commit 31bceab

Please sign in to comment.