File: error_test.py

package info (click to toggle)
python-pycm 4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,048 kB
  • sloc: python: 5,178; sh: 8; makefile: 6
file content (317 lines) | stat: -rw-r--r-- 14,892 bytes parent folder | download | duplicates (2)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# -*- coding: utf-8 -*-
"""
>>> from pycm import *
>>> import numpy as np
>>> import os
>>> import json
>>> y_actu = [2, 0, 2, 2, 0, 1, 1, 2, 2, 0, 1, 2]
>>> cm_2 = ConfusionMatrix(y_actu, 2)
Traceback (most recent call last):
        ...
pycm.errors.pycmVectorError: Input vectors must be provided as a list or a NumPy array.
>>> cm_3 = ConfusionMatrix(y_actu, [1, 2])
Traceback (most recent call last):
        ...
pycm.errors.pycmVectorError: Input vectors must have the same length.
>>> cm_4 = ConfusionMatrix([], [])
Traceback (most recent call last):
        ...
pycm.errors.pycmVectorError: Input vectors must not be empty.
>>> cm_5 = ConfusionMatrix([1, 1, 1, ], [1, 1, 1, 1])
Traceback (most recent call last):
        ...
pycm.errors.pycmVectorError: Input vectors must have the same length.
>>> cm_6 = ConfusionMatrix(matrix={0: {0: 2, 1: 50, 2: 6}, 1: {0: 5, 1: 50, 2: 3}, 2: {0: 1, 1: 7, 2: 50}})
>>> cm_6.position()
Traceback (most recent call last):
        ...
pycm.errors.pycmVectorError: This option is only available in vector mode.
>>> cm = ConfusionMatrix([1, 2, 3, 4], [1, 2, 3, 4], classes=[1, 2, 2, 2])
Traceback (most recent call last):
        ...
pycm.errors.pycmVectorError: `classes` must contain unique labels with no duplicates.
>>> cm3=ConfusionMatrix(matrix={})
Traceback (most recent call last):
        ...
pycm.errors.pycmMatrixError: Invalid input confusion matrix format.
>>> cm_4=ConfusionMatrix(matrix={1: {1: 2, "1": 2}, "1": {1: 2, "1": 3}})
Traceback (most recent call last):
        ...
pycm.errors.pycmMatrixError: All input matrix classes must be of the same type.
>>> cm_5=ConfusionMatrix(matrix={1: {1: 2}})
Traceback (most recent call last):
        ...
pycm.errors.pycmMatrixError: The number of classes must be at least 2.
>>> cm = ConfusionMatrix([1, 2, 3, 4], [1, 2, 3, 4], classes=[1])
Traceback (most recent call last):
        ...
pycm.errors.pycmMatrixError: The number of classes must be at least 2.
>>> cm = ConfusionMatrix([1, 1, 1, 1], [1, 2, 1, 1], classes=[])
Traceback (most recent call last):
        ...
pycm.errors.pycmMatrixError: The number of classes must be at least 2.
>>> y_actu = [2, 0, 2, 2, 0, 1, 1, 2, 2, 0, 1, 2]
>>> y_pred = [0, 0, 2, 1, 0, 2, 1, 0, 2, 0, 2, 2]
>>> cm = ConfusionMatrix(y_actu, y_pred)
>>> cm.distance(metric = 2)
Traceback (most recent call last):
        ...
pycm.errors.pycmMatrixError: `metric` type must be DistanceType.
>>> cm.relabel([1, 2, 3])
Traceback (most recent call last):
        ...
pycm.errors.pycmMatrixError: Invalid mapping format.
>>> cm.relabel({1: "L1", 2: "L2"})
Traceback (most recent call last):
        ...
pycm.errors.pycmMatrixError: Invalid mapping class names.
>>> cm.relabel({0: "L1", 1: "L2", 2: "L2"})
Traceback (most recent call last):
        ...
pycm.errors.pycmMatrixError: Invalid mapping class names.
>>> cp = Compare([cm, cm])
Traceback (most recent call last):
        ...
pycm.errors.pycmCompareError: Input must be provided as a dictionary.
>>> cp = Compare({"cm1": cm})
Traceback (most recent call last):
        ...
pycm.errors.pycmCompareError: At least 2 confusion matrices are required for comparison.
>>> cp = Compare({"cm1": cm, "cm2": []})
Traceback (most recent call last):
        ...
pycm.errors.pycmCompareError: Input must be a dictionary containing pycm.ConfusionMatrix objects.
>>> cm2 = ConfusionMatrix(matrix={"Class1": {"Class1": 9, "Class2": 3, "Class3": 0}, "Class2": {"Class1": 3, "Class2": 5, "Class3": 1}, "Class3": {"Class1": 1, "Class2": 1, "Class3": 4}})
>>> cp = Compare({"cm1": cm, "cm2": cm2})
Traceback (most recent call last):
        ...
pycm.errors.pycmCompareError: All ConfusionMatrix objects must have the same domain (same sample size and number of classes).
>>> cm = ConfusionMatrix(matrix={1: {1: 9, 2: 3}, 2: {1: 3, 2: 5}}, classes=[1, 2, 3])
Traceback (most recent call last):
        ...
pycm.errors.pycmMatrixError: The specified classes are not a subset of the matrix's classes.
>>> y_pred = [0, 0, 2, 1, 0, 2, 1, 0, 2, 0, 2, 1]
>>> cm3 = ConfusionMatrix(y_actu, y_pred)
>>> cp = Compare({"cm1": cm, "cm2": cm3}, by_class=True, class_weight={1: 1, 2: 1})
Traceback (most recent call last):
        ...
pycm.errors.pycmCompareError: `class_weight` must be a dictionary and specified for all classes.
>>> cp = Compare({"cm1": cm, "cm2": cm3}, by_class=True, class_weight=[])
Traceback (most recent call last):
        ...
pycm.errors.pycmCompareError: `class_weight` must be a dictionary and specified for all classes.
>>> cp = Compare({"cm1": cm, "cm2": cm3}, by_class=True, class_benchmark_weight=[])
Traceback (most recent call last):
        ...
pycm.errors.pycmCompareError: `class_benchmark_weight` must be a dictionary and specified for all class benchmarks.
>>> cp = Compare({"cm1": cm, "cm2": cm3}, by_class=True, overall_benchmark_weight=[])
Traceback (most recent call last):
        ...
pycm.errors.pycmCompareError: `overall_benchmark_weight` must be a dictionary and specified for all overall benchmarks.
>>> cm1 = ConfusionMatrix([1, 1, 1, 0], [1, 0, 1, 1], metrics_off=True)
>>> cm2 = ConfusionMatrix([1, 1, 1, 0], [1, 0, 1, 1], metrics_off=False)
>>> cp = Compare({"cm1":cm1, "cm2":cm2})
Traceback (most recent call last):
        ...
pycm.errors.pycmCompareError: Comparison cannot be performed when `metrics_off=True` in any matrix.
>>> cm.CI("MCC")
Traceback (most recent call last):
        ...
pycm.errors.pycmCIError: Confidence interval calculation for this parameter is not supported in this version of pycm.
     Supported parameters are: TPR, TNR, PPV, NPV, ACC, PLR, NLR, FPR, FNR, AUC, PRE, Kappa, Overall ACC
>>> cm.CI(2)
Traceback (most recent call last):
        ...
pycm.errors.pycmCIError: Input must be provided as a string.
>>> cm.average("AUCC")
Traceback (most recent call last):
        ...
pycm.errors.pycmAverageError: Invalid parameter!
>>> cm.weighted_average("AUCC")
Traceback (most recent call last):
        ...
pycm.errors.pycmAverageError: Invalid parameter!
>>> cm.weighted_average("AUC", weight=2)
Traceback (most recent call last):
        ...
pycm.errors.pycmAverageError: `weight` must be a dictionary and specified for all classes.
>>> cm.weighted_average("AUC", weight={1: 23})
Traceback (most recent call last):
        ...
pycm.errors.pycmAverageError: `weight` must be a dictionary and specified for all classes.
>>> cm.combine(1)
Traceback (most recent call last):
        ...
pycm.errors.pycmMatrixError: Input must be an instance of pycm.ConfusionMatrix.
>>> cm = ConfusionMatrix([1, 0, 2, 0], [1, 1, 2, 1])
>>> cm.brier_score()
Traceback (most recent call last):
        ...
pycm.errors.pycmVectorError: This option is only available in binary probability mode.
>>> cm.log_loss()
Traceback (most recent call last):
        ...
pycm.errors.pycmVectorError: This option is only available in binary probability mode.
>>> cm = ConfusionMatrix(["ham", "spam", "ham", "ham"], [0.1, 0.4, 0.25, 1], threshold=lambda x : "ham")
>>> cm.brier_score()
Traceback (most recent call last):
        ...
pycm.errors.pycmVectorError: Actual vector contains strings; `pos_class` must be explicitly specified.
>>> cm.log_loss()
Traceback (most recent call last):
        ...
pycm.errors.pycmVectorError: Actual vector contains strings; `pos_class` must be explicitly specified.
>>> matrix = [[1, 2, 3], [4, 6, 1], [1, 2, 3]]
>>> cm = ConfusionMatrix(matrix=matrix, classes=["L1", "L1", "L3", "L2"])
Traceback (most recent call last):
        ...
pycm.errors.pycmMatrixError: `classes` must contain unique labels with no duplicates.
>>> cm = ConfusionMatrix(matrix=matrix, classes=[1, 2])
Traceback (most recent call last):
        ...
pycm.errors.pycmMatrixError: The length of the classes does not match the length of the array.
>>> crv = Curve([1, 2, 2, 1], {1, 2, 2, 1}, classes=[1, 2])
Traceback (most recent call last):
        ...
pycm.errors.pycmCurveError: Input vectors must be provided as a list or a NumPy array.
>>> crv = Curve({1, 2, 2, 1}, [1, 2, 2, 1], classes=[1, 2])
Traceback (most recent call last):
        ...
pycm.errors.pycmCurveError: Input vectors must be provided as a list or a NumPy array.
>>> crv = Curve([1, 2, 2, 1], [[0.1, 0.9]], classes=[1, 2])
Traceback (most recent call last):
        ...
pycm.errors.pycmCurveError: Input vectors must have the same length.
>>> crv = Curve([1, 2, 2, 1], [[0.1, 0.9], [0.1, 0.9], [0.1, 0.9], [0.2, 0.9]], classes=[1, 2])
Traceback (most recent call last):
        ...
pycm.errors.pycmCurveError: The sum of the probability values must equal 1.
>>> crv = Curve([1, 2, 2, 1], [[0.1, 0.9], [0.1, 0.9], [0.1, 0.9], [0.1, 0.9]], classes={1, 2})
Traceback (most recent call last):
        ...
pycm.errors.pycmCurveError: `classes` must be provided as a list.
>>> crv = Curve([1, 2, 2, 1], [[0.1, 0.9], [0.1, 0.9], [0.1, 0.9], [0.1, 0.9]], classes=[1, 2, 3])
Traceback (most recent call last):
        ...
pycm.errors.pycmCurveError: `classes` does not match the actual vector.
>>> crv = Curve([1, 1, 1, 1], [[0.1, 0.9], [0.1, 0.9], [0.1, 0.9], [0.1, 0.9]], classes=[1])
Traceback (most recent call last):
        ...
pycm.errors.pycmCurveError: The number of classes must be at least 2.
>>> crv = Curve([1, 2, 2, 1], [[0.1, 0.9], [0.1, 0.9], [0.1, 0.9], [0.2, "salam"]], classes=[1, 2])
Traceback (most recent call last):
        ...
pycm.errors.pycmCurveError: Probability vector elements must be numeric.
>>> crv = Curve([1, 2, 2, 1], [[0.1, 0.9], [0.1, 0.9], [0.1, 0.9], [0.2, 0.8]], classes=[1, 2], thresholds={1, 2})
Traceback (most recent call last):
        ...
pycm.errors.pycmCurveError: `thresholds` must be provided as a list or a NumPy array.
>>> crv = Curve([1, 2, 2, 1], [[0.1, 0.9], [0.1, 0.9], [0.1, 0.9], [0.2, 0.8]], classes=[1, 2], thresholds=[0.1])
Traceback (most recent call last):
        ...
pycm.errors.pycmCurveError: The number of thresholds must be at least 2.
>>> crv = Curve([1, 2, 2, 1], [[0.1, 0.9], [0.1, 0.9], [0.1, 0.9], [0.2, 0.8]], classes=[1, 2], thresholds=[0.1, "q"])
Traceback (most recent call last):
        ...
pycm.errors.pycmCurveError: `thresholds` must contain only numeric values.
>>> crv = Curve([1, 2, 2, 1], [[0.1, 0.9], [0.1, 0.9], [0.1, 0.9], [0.2, 0.8]], classes=[1, 1, 2])
Traceback (most recent call last):
        ...
pycm.errors.pycmCurveError: `classes` must contain unique labels with no duplicates.
>>> crv = Curve([1, 2, 2, 1], [[0.1, 0.9], [0.1, 0.9], [0.1, 0.8, 0.1], [0.2, 0.8]], classes=[1, 2])
Traceback (most recent call last):
        ...
pycm.errors.pycmCurveError: All elements of the probability vector must have the same length and match the number of classes.
>>> crv = Curve([1, 2, 2, 1], [[1], [1], [1], [1]], classes=[1, 2])
Traceback (most recent call last):
        ...
pycm.errors.pycmCurveError: All elements of the probability vector must have the same length and match the number of classes.
>>> crv = Curve(actual_vector = np.array([1, 1, 2, 2]), probs = np.array([[0.1, 0.9], [0.4, 0.6], [0.35, 0.65], [0.8, 0.2]]), classes=[2, 1])
>>> crv.area(method="trpz")
Traceback (most recent call last):
        ...
pycm.errors.pycmCurveError: The integral method must be either 'trapezoidal' or 'midpoint'.
>>> cm = ConfusionMatrix(y_actu, y_pred, metrics_off=True)
>>> cm.stat()
Traceback (most recent call last):
        ...
pycm.errors.pycmMatrixError: This method cannot be executed when `metrics_off=True`.
>>> cm.sensitivity_index()
Traceback (most recent call last):
        ...
pycm.errors.pycmMatrixError: This method cannot be executed when `metrics_off=True`.
>>> cm.IBA_alpha(0.2)
Traceback (most recent call last):
        ...
pycm.errors.pycmMatrixError: This method cannot be executed when `metrics_off=True`.
>>> cm.NB()
Traceback (most recent call last):
        ...
pycm.errors.pycmMatrixError: This method cannot be executed when `metrics_off=True`.
>>> cm.CI("Kappa")
Traceback (most recent call last):
        ...
pycm.errors.pycmMatrixError: This method cannot be executed when `metrics_off=True`.
>>> cm.average("ACC")
Traceback (most recent call last):
        ...
pycm.errors.pycmMatrixError: This method cannot be executed when `metrics_off=True`.
>>> cm.weighted_average("ACC")
Traceback (most recent call last):
        ...
pycm.errors.pycmMatrixError: This method cannot be executed when `metrics_off=True`.
>>> cm.weighted_kappa()
Traceback (most recent call last):
        ...
pycm.errors.pycmMatrixError: This method cannot be executed when `metrics_off=True`.
>>> cm.weighted_alpha()
Traceback (most recent call last):
        ...
pycm.errors.pycmMatrixError: This method cannot be executed when `metrics_off=True`.
>>> cm.aickin_alpha()
Traceback (most recent call last):
        ...
pycm.errors.pycmMatrixError: This method cannot be executed when `metrics_off=True`.
>>> mlcm = MultiLabelCM([[0, 1], [1, 1]], [[1, 0], [1, 0]])
Traceback (most recent call last):
        ...
pycm.errors.pycmVectorError: Failed to extract classes from input. Input vectors should be a list of sets with unified types.
>>> mlcm = MultiLabelCM([{'dog'}, {'cat', 'dog'}], ['cat', {'cat'}])
Traceback (most recent call last):
        ...
pycm.errors.pycmVectorError: Failed to extract classes from input. Input vectors should be a list of sets with unified types.
>>> mlcm = MultiLabelCM(['dog', {'cat', 'dog'}], [{'cat'}, {'cat'}])
Traceback (most recent call last):
        ...
pycm.errors.pycmVectorError: Failed to extract classes from input. Input vectors should be a list of sets with unified types.
>>> mlcm = MultiLabelCM([{'dog'}, {'cat', 'dog'}], [{'cat'}, {'cat'}])
>>> mlcm.get_cm_by_class(1)
Traceback (most recent call last):
        ...
pycm.errors.pycmMultiLabelError: The specified class name is not among the confusion matrix's classes.
>>> mlcm.get_cm_by_sample(2)
Traceback (most recent call last):
        ...
pycm.errors.pycmMultiLabelError: Index is out of range for the given vector.
>>> mlcm = MultiLabelCM([{'dog'}, {'cat', 'dog'}], [{'cat'}, {'cat', 'bird'}], classes=['dog', 'cat'])
>>> mlcm.get_cm_by_class('bird')
Traceback (most recent call last):
        ...
pycm.errors.pycmMultiLabelError: The specified class name is not among the confusion matrix's classes.
>>> mlcm = MultiLabelCM(2, [[1, 0], [1, 0]])
Traceback (most recent call last):
        ...
pycm.errors.pycmVectorError: Input vectors must be provided as a list or a NumPy array.
>>> mlcm = MultiLabelCM([{1, 0}, {1, 0}, {1,1}], [{1, 0}, {1, 0}])
Traceback (most recent call last):
        ...
pycm.errors.pycmVectorError: Input vectors must have the same length.
>>> mlcm = MultiLabelCM([], [])
Traceback (most recent call last):
        ...
pycm.errors.pycmVectorError: Input vectors must not be empty.
>>> mlcm = MultiLabelCM([{1, 0}, {1, 0}], [{1, 0}, {1, 0}], classes=[1,0,1])
Traceback (most recent call last):
        ...
pycm.errors.pycmVectorError: `classes` must contain unique labels with no duplicates.
"""