-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_class_unittest.py
86 lines (67 loc) · 2.96 KB
/
test_class_unittest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import unittest
import pandas as pd
import numpy as np
import sys
import re
import logging
import statsmodels.api as sm
sys.path.append("refactored_pd/")
import data
from class_modelperf import ModelPerfomance
diagnostics_logger = logging.getLogger("class_logistic_unittest")
diagnostics_logger.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
console_handler.setFormatter(
logging.Formatter(fmt="{levelname}:{name}:{message}", style="{")
)
diagnostics_logger.addHandler(console_handler)
diagnostics_logger.info("LOGISTIC REGRESSION UNIT TESTS")
class TestDataframe(unittest.TestCase):
def __str__(self):
"""This executed when calling print on this object"""
pattern = re.compile(r"^_")
method_names = []
for name, func in TestDataframe.__dict__.items():
if not pattern.match(name) and callable(func):
method_names.append(name)
return f"This is Class {self.__class__.__name__} with methods {method_names}"
def test_no_missing_values(self):
"""Here we are testing if our Imputation methods worked form the Imputation class"""
dataframe = data.imputer_cat
self.assertFalse(
dataframe.isnull().values.any(), "Dataframe contains missing values"
)
def test_zeros_and_ones_values(self):
"""Here we are testing if Onehot Encoding worked from OneHotEncoding class"""
dataframe = data.instance_stats
self.assertTrue(
(dataframe.onehot_encoding().applymap(lambda x: x in [0, 1])).all().all(),
"Dataframe contains values other than zero or one",
)
class TestProbability(unittest.TestCase, object):
def __str__(self):
"""This executed when calling print on this object"""
pattern = re.compile(r"^_")
method_names = []
for name, func in TestProbability.__dict__.items():
if not pattern.match(name) and callable(func):
method_names.append(name)
return f"This is Class {self.__class__.__name__} with methods {method_names}"
def test_no_values_less_than_one(self):
"""Here we are testing if our Logistic regression does not return nonsensical values,
e.g. values less than zero"""
x_test = sm.add_constant(data.m.x_test_glm.values, has_constant="add")
values = np.array(data.m.glm_probability_prediction(x_test))
self.assertFalse(
(values < 0).any(), "Prediction contains missing values less than 0"
)
def test_no_values_greater_than_one(self):
"""Here we are testing if our Logistic regression does not return nonsensical values,
e.g. values greater than one"""
x_test = sm.add_constant(data.m.x_test_glm.values, has_constant="add")
values = np.array(data.m.glm_probability_prediction(x_test))
self.assertFalse(
(values > 1).any(), "Prediction contains missing values less than 0"
)
if __name__ == "__main__":
unittest.main()