-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathtest_HierarchicalClassifier.py
More file actions
237 lines (177 loc) · 7.12 KB
/
Copy pathtest_HierarchicalClassifier.py
File metadata and controls
237 lines (177 loc) · 7.12 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import logging
import tempfile
import networkx as nx
import numpy as np
import pytest
from numpy.testing import assert_array_equal
from sklearn.linear_model import LogisticRegression
from hiclass.HierarchicalClassifier import HierarchicalClassifier, make_leveled
@pytest.fixture
def ambiguous_node_str():
classifier = HierarchicalClassifier()
classifier.y_ = np.array([["a", "b"], ["b", "c"]])
return classifier
def test_disambiguate_str(ambiguous_node_str):
ground_truth = np.array(
[["a", "a::HiClass::Separator::b"], ["b", "b::HiClass::Separator::c"]]
)
ambiguous_node_str._disambiguate()
ground_truth = np.array(
[ambiguous_node_str.label_encoder_.transform(row) for row in ground_truth]
)
assert_array_equal(ground_truth, ambiguous_node_str.y_)
@pytest.fixture
def ambiguous_node_int():
classifier = HierarchicalClassifier()
classifier.y_ = np.array([[1, 2], [2, 3]])
return classifier
def test_disambiguate_int(ambiguous_node_int):
ground_truth = np.array(
[["1", "1::HiClass::Separator::2"], ["2", "2::HiClass::Separator::3"]]
)
ambiguous_node_int._disambiguate()
ground_truth = np.array(
[ambiguous_node_int.label_encoder_.transform(row) for row in ground_truth]
)
assert_array_equal(ground_truth, ambiguous_node_int.y_)
@pytest.fixture
def graph_1d():
classifier = HierarchicalClassifier()
classifier.y_ = np.array(["a", "b", "c", "d"])
classifier.logger_ = logging.getLogger("HC")
return classifier
def test_create_digraph_1d(graph_1d):
ground_truth = nx.DiGraph()
ground_truth.add_nodes_from(np.array(["a", "b", "c", "d"]))
graph_1d._create_digraph()
assert nx.is_isomorphic(ground_truth, graph_1d.hierarchy_)
assert list(ground_truth.nodes) == list(graph_1d.hierarchy_.nodes)
assert list(ground_truth.edges) == list(graph_1d.hierarchy_.edges)
@pytest.fixture
def graph_1d_disguised_as_2d():
classifier = HierarchicalClassifier()
classifier.y_ = np.array([["a"], ["b"], ["c"], ["d"]])
classifier.logger_ = logging.getLogger("HC")
return classifier
def test_create_digraph_1d_disguised_as_2d(graph_1d_disguised_as_2d):
ground_truth = nx.DiGraph()
ground_truth.add_nodes_from(np.array(["a", "b", "c", "d"]))
graph_1d_disguised_as_2d._create_digraph()
assert nx.is_isomorphic(ground_truth, graph_1d_disguised_as_2d.hierarchy_)
assert list(ground_truth.nodes) == list(graph_1d_disguised_as_2d.hierarchy_.nodes)
assert list(ground_truth.edges) == list(graph_1d_disguised_as_2d.hierarchy_.edges)
@pytest.fixture
def digraph_2d():
classifier = HierarchicalClassifier()
classifier.y_ = np.array([["a", "b", "c"], ["d", "e", "f"]])
classifier.hierarchy_ = nx.DiGraph([("a", "b"), ("b", "c"), ("d", "e"), ("e", "f")])
classifier.logger_ = logging.getLogger("HC")
classifier.edge_list = tempfile.TemporaryFile()
classifier.separator_ = "::HiClass::Separator::"
return classifier
def test_create_digraph_2d(digraph_2d):
ground_truth = nx.DiGraph([("a", "b"), ("b", "c"), ("d", "e"), ("e", "f")])
digraph_2d._create_digraph()
assert nx.is_isomorphic(ground_truth, digraph_2d.hierarchy_)
assert list(ground_truth.nodes) == list(digraph_2d.hierarchy_.nodes)
assert list(ground_truth.edges) == list(digraph_2d.hierarchy_.edges)
@pytest.fixture
def digraph_3d():
classifier = HierarchicalClassifier()
classifier.y_ = np.arange(27).reshape((3, 3, 3))
classifier.logger_ = logging.getLogger("HC")
return classifier
def test_create_digraph_3d(digraph_3d):
with pytest.raises(ValueError):
digraph_3d._create_digraph()
def test_export_digraph(digraph_2d):
ground_truth = b'"a","b",{}\n"b","c",{}\n"d","e",{}\n"e","f",{}\n'
digraph_2d._export_digraph()
digraph_2d.edge_list.seek(0)
assert digraph_2d.edge_list.read() == ground_truth
@pytest.fixture
def cyclic_graph():
classifier = HierarchicalClassifier()
classifier.hierarchy_ = nx.DiGraph([("a", "b"), ("b", "c"), ("c", "a")])
classifier.logger_ = logging.getLogger("HC")
return classifier
def test_assert_digraph_is_dag(cyclic_graph):
with pytest.raises(ValueError):
cyclic_graph._assert_digraph_is_dag()
def test_convert_1d_y_to_2d(graph_1d):
ground_truth = np.array([["a"], ["b"], ["c"], ["d"]])
graph_1d._convert_1d_y_to_2d()
assert_array_equal(ground_truth, graph_1d.y_)
@pytest.fixture
def digraph_one_root():
classifier = HierarchicalClassifier()
classifier.logger_ = logging.getLogger("HC")
classifier.hierarchy_ = nx.DiGraph([("a", "b"), ("b", "c"), ("c", "d")])
return classifier
def test_add_artificial_root(digraph_one_root):
digraph_one_root._add_artificial_root()
successors = list(digraph_one_root.hierarchy_.successors("hiclass::root"))
assert ["a"] == successors
assert "hiclass::root" == digraph_one_root.root_
@pytest.fixture
def digraph_multiple_roots():
classifier = HierarchicalClassifier()
classifier.logger_ = logging.getLogger("HC")
classifier.hierarchy_ = nx.DiGraph([("a", "b"), ("c", "d"), ("e", "f")])
classifier.X_ = np.array([[1, 2], [3, 4], [5, 6]])
classifier.y_ = np.array([["a", "b"], ["c", "d"], ["e", "f"]])
classifier.sample_weight_ = None
return classifier
def test_add_artificial_root_multiple_roots(digraph_multiple_roots):
digraph_multiple_roots._add_artificial_root()
successors = list(digraph_multiple_roots.hierarchy_.successors("hiclass::root"))
assert ["a", "c", "e"] == successors
assert "hiclass::root" == digraph_multiple_roots.root_
def test_initialize_local_classifiers_2(digraph_multiple_roots):
digraph_multiple_roots.local_classifier = None
digraph_multiple_roots._initialize_local_classifiers()
assert isinstance(digraph_multiple_roots.local_classifier_, LogisticRegression)
def test_clean_up(digraph_multiple_roots):
digraph_multiple_roots._clean_up()
with pytest.raises(AttributeError):
assert digraph_multiple_roots.X_ is None
with pytest.raises(AttributeError):
assert digraph_multiple_roots.y_ is None
@pytest.fixture
def empty_levels():
y = [
["a"],
["b", "c"],
["d", "e", "f"],
]
return y
def test_make_leveled(empty_levels):
ground_truth = np.array(
[
["a", "", ""],
["b", "c", ""],
["d", "e", "f"],
]
)
result = make_leveled(empty_levels)
assert_array_equal(ground_truth, result)
@pytest.fixture
def noniterable_y():
y = [1, 2, 3]
return y
def test_make_leveled_non_iterable_y(noniterable_y):
assert noniterable_y == make_leveled(noniterable_y)
def test_fit_classifier():
with pytest.raises(NotImplementedError):
HierarchicalClassifier._fit_classifier(None, None)
def test_fit_digraph():
with pytest.raises(NotImplementedError):
HierarchicalClassifier._fit_digraph(None, None)
def test_pre_fit_bert():
classifier = HierarchicalClassifier()
classifier.logger_ = logging.getLogger("HC")
classifier.bert = True
x = [[0, 1], [2, 3]]
y = [["a", "b"], ["c", "d"]]
sample_weight = None
classifier._pre_fit(x, y, sample_weight)