-
-
Notifications
You must be signed in to change notification settings - Fork 553
/
test_wrapper.py
125 lines (101 loc) · 3.72 KB
/
test_wrapper.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# tests.test_contrib.test_wrapper
# Tests third-party estimator wrapper
#
# Author: Benjamin Bengfort
# Created: Fri Oct 02 17:27:50 2020 -0400
#
# Copyright (C) 2020 The scikit-yb developers
# For license information, see LICENSE.txt
#
# ID: test_wrapper.py [] benjamin@bengfort.com $
"""
Tests third-party estimator wrapper
"""
##########################################################################
## Imports
##########################################################################
import pytest
from yellowbrick.contrib.wrapper import *
from yellowbrick.utils.helpers import get_model_name
from yellowbrick.exceptions import YellowbrickAttributeError
from yellowbrick.utils.types import is_estimator, is_probabilistic
from yellowbrick.utils.types import is_classifier, is_clusterer, is_regressor
from sklearn.base import is_regressor as sk_is_regressor
from sklearn.base import is_classifier as sk_is_classifier
from sklearn.base import is_outlier_detector as sk_is_outlier_detector
##########################################################################
## Mocks and Fixtures
##########################################################################
class ThirdPartyEstimator(object):
def __init__(self, **params):
for attr, param in params.items():
setattr(self, attr, param)
def fit(self, X, y=None):
return 42
def predict_proba(self, X, y=None):
return 24
##########################################################################
## Test Suite
##########################################################################
class TestContribWrapper(object):
"""
Third party ContribEstimator wrapper
"""
def test_wrapped_estimator(self):
"""
Check that the contrib wrapper passes through correctly
"""
tpe = ContribEstimator(ThirdPartyEstimator(foo="bar"), "foo")
assert tpe.fit([1,2,3]) == 42
assert tpe._estimator_type == "foo"
def test_attribute_error(self):
"""
Assert a correct exception is raised on failed access
"""
tpe = ContribEstimator(ThirdPartyEstimator())
with pytest.raises(YellowbrickAttributeError):
tpe.foo
def test_get_model_name(self):
"""
ContribWrapper should return the underlying model name
"""
tpe = ContribEstimator(ThirdPartyEstimator())
assert get_model_name(tpe) == "ThirdPartyEstimator"
def test_wraps_is_estimator(self):
"""
Assert a wrapped estimator passes is_estimator check
"""
tpe = wrap(ThirdPartyEstimator())
assert is_estimator(tpe)
def test_wraps_is_classifier(self):
"""
Assert a wrapped estimator passes is_classifier check
"""
tpe = classifier(ThirdPartyEstimator())
assert is_classifier(tpe)
assert sk_is_classifier(tpe)
def test_wraps_is_regressor(self):
"""
Assert a wrapped estimator passes is_regressor check
"""
tpe = regressor(ThirdPartyEstimator())
assert is_regressor(tpe)
assert sk_is_regressor(tpe)
def test_wraps_is_clusterer(self):
"""
Assert a wrapped estimator passes is_clusterer check
"""
tpe = clusterer(ThirdPartyEstimator())
assert is_clusterer(tpe)
def test_wraps_is_outlier_detector(self):
"""
Assert a wrapped estimator passes is_outlier_detector check
"""
tpe = wrap(ThirdPartyEstimator(), OUTLIER_DETECTOR)
assert sk_is_outlier_detector(tpe)
def test_wraps_is_probabilistic(self):
"""
Assert a wrapped estimator passes is_probabilistic check
"""
tpe = wrap(ThirdPartyEstimator())
assert is_probabilistic(tpe)