2 Copyright (c) 2019 Intel Corporation
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
18 from accuracy_checker.metrics import MetricsExecutor
19 from accuracy_checker.representation import RegressionPrediction, RegressionAnnotation
20 from accuracy_checker.presenters import EvaluationResult
23 class TestRegressionMetric:
24 def setup_method(self):
25 self.module = 'accuracy_checker.metrics.metric_evaluator'
27 def test_mae_with_zero_diff_between_annotation_and_prediction(self):
28 annotations = [RegressionAnnotation('identifier', 3)]
29 predictions = [RegressionPrediction('identifier', 3)]
30 config = {'annotation': 'mocked', 'metrics': [{'type': 'mae'}]}
31 expected = EvaluationResult(
32 pytest.approx([0.0, 0.0]),
36 {'postfix': ' ', 'scale': 1, 'names': ['mean', 'std'], 'calculate_mean': False}
38 dispatcher = MetricsExecutor(config, None)
40 dispatcher.update_metrics_on_batch(annotations, predictions)
42 for _, evaluation_result in dispatcher.iterate_metrics(annotations, predictions):
43 assert evaluation_result == expected
45 def test_mae_with_negative_diff_between_annotation_and_prediction(self):
46 annotations = [RegressionAnnotation('identifier', 3), RegressionAnnotation('identifier2', 1)]
47 predictions = [RegressionPrediction('identifier', 5), RegressionPrediction('identifier2', 5)]
48 config = {'annotation': 'mocked', 'metrics': [{'type': 'mae'}]}
49 expected = EvaluationResult(
50 pytest.approx([3.0, 1.0]),
54 {'postfix': ' ', 'scale': 1, 'names': ['mean', 'std'], 'calculate_mean': False}
56 dispatcher = MetricsExecutor(config, None)
58 dispatcher.update_metrics_on_batch(annotations, predictions)
60 for _, evaluation_result in dispatcher.iterate_metrics(annotations, predictions):
61 assert evaluation_result == expected
63 def test_mae_with_positive_diff_between_annotation_and_prediction(self):
64 annotations = [RegressionAnnotation('identifier', 3), RegressionAnnotation('identifier2', 1)]
65 predictions = [RegressionPrediction('identifier', 1), RegressionPrediction('identifier2', -3)]
66 config = {'annotation': 'mocked', 'metrics': [{'type': 'mae'}]}
67 expected = EvaluationResult(
68 pytest.approx([3.0, 1.0]),
72 {'postfix': ' ', 'scale': 1, 'names': ['mean', 'std'], 'calculate_mean': False}
74 dispatcher = MetricsExecutor(config, None)
76 dispatcher.update_metrics_on_batch(annotations, predictions)
78 for _, evaluation_result in dispatcher.iterate_metrics(annotations, predictions):
79 assert evaluation_result == expected
81 def test_mse_with_zero_diff_between_annotation_and_prediction(self):
82 annotations = [RegressionAnnotation('identifier', 3)]
83 predictions = [RegressionPrediction('identifier', 3)]
84 config = {'annotation': 'mocked', 'metrics': [{'type': 'mse'}]}
85 expected = EvaluationResult(
86 pytest.approx([0.0, 0.0]),
90 {'postfix': ' ', 'scale': 1, 'names': ['mean', 'std'], 'calculate_mean': False}
92 dispatcher = MetricsExecutor(config, None)
94 dispatcher.update_metrics_on_batch(annotations, predictions)
96 for _, evaluation_result in dispatcher.iterate_metrics(annotations, predictions):
97 assert evaluation_result == expected
99 def test_mse_with_negative_diff_between_annotation_and_prediction(self):
100 annotations = [RegressionAnnotation('identifier', 3), RegressionAnnotation('identifier2', 1)]
101 predictions = [RegressionPrediction('identifier', 5), RegressionPrediction('identifier2', 5)]
102 config = {'annotation': 'mocked', 'metrics': [{'type': 'mse'}]}
103 expected = EvaluationResult(
104 pytest.approx([10.0, 6.0]),
108 {'postfix': ' ', 'scale': 1, 'names': ['mean', 'std'], 'calculate_mean': False}
110 dispatcher = MetricsExecutor(config, None)
112 dispatcher.update_metrics_on_batch(annotations, predictions)
114 for _, evaluation_result in dispatcher.iterate_metrics(annotations, predictions):
115 assert evaluation_result == expected
117 def test_mse_with_positive_diff_between_annotation_and_prediction(self):
118 annotations = [RegressionAnnotation('identifier', 3), RegressionAnnotation('identifier2', 1)]
119 predictions = [RegressionPrediction('identifier', 1), RegressionPrediction('identifier2', -3)]
120 config = {'annotation': 'mocked', 'metrics': [{'type': 'mse'}]}
121 expected = EvaluationResult(
122 pytest.approx([10.0, 6.0]),
126 {'postfix': ' ', 'scale': 1, 'names': ['mean', 'std'], 'calculate_mean': False}
128 dispatcher = MetricsExecutor(config, None)
130 dispatcher.update_metrics_on_batch(annotations, predictions)
132 for _, evaluation_result in dispatcher.iterate_metrics(annotations, predictions):
133 assert evaluation_result == expected
135 def test_missed_interval(self):
136 config = {'annotation': 'mocked', 'metrics': [{'type': 'mae_on_interval'}]}
137 with pytest.raises(ValueError):
138 MetricsExecutor(config, None)
140 def test_mae_on_interval_default_all_missed(self):
141 annotations = [RegressionAnnotation('identifier', -2)]
142 predictions = [RegressionPrediction('identifier', 1)]
143 config = {'annotation': 'mocked', 'metrics': [{'type': 'mae_on_interval', 'end': 1}]}
144 expected = EvaluationResult(
145 pytest.approx([0.0]),
149 {'postfix': ' ', 'scale': 1, 'names': [], 'calculate_mean': False}
151 dispatcher = MetricsExecutor(config, None)
153 dispatcher.update_metrics_on_batch(annotations, predictions)
155 with pytest.warns(UserWarning) as warnings:
156 for _, evaluation_result in dispatcher.iterate_metrics(annotations, predictions):
157 assert len(warnings) == 1
158 assert evaluation_result == expected
160 def test_mae_on_interval_default_all_not_in_range_not_ignore_out_of_range(self):
161 annotations = [RegressionAnnotation('identifier', -1), RegressionAnnotation('identifier', 2)]
162 predictions = [RegressionPrediction('identifier', 1), RegressionPrediction('identifier', 2)]
163 expected = EvaluationResult(
164 pytest.approx([2.0, 0.0, 0.0, 0.0]),
171 'names': ['mean: < 0.0', 'std: < 0.0', 'mean: > 1.0', 'std: > 1.0'],
172 'calculate_mean': False
176 'annotation': 'mocked',
177 'metrics': [{'type': 'mae_on_interval', 'end': 1, 'ignore_values_not_in_interval': False}]
179 dispatcher = MetricsExecutor(config, None)
181 dispatcher.update_metrics_on_batch(annotations, predictions)
183 for _, evaluation_result in dispatcher.iterate_metrics(annotations, predictions):
184 assert evaluation_result == expected
186 def test_mae_on_interval_values_in_range(self):
187 annotations = [RegressionAnnotation('identifier', 0.5), RegressionAnnotation('identifier', 0.5)]
188 predictions = [RegressionPrediction('identifier', 1), RegressionPrediction('identifier', 0.25)]
189 config = {'annotation': 'mocked', 'metrics': [{'type': 'mae_on_interval', 'end': 1}]}
190 expected = EvaluationResult(
191 pytest.approx([0.375, 0.125]),
195 {'postfix': ' ', 'scale': 1, 'names': ['mean: <= 0.0 < 1.0', 'std: <= 0.0 < 1.0'], 'calculate_mean': False}
197 dispatcher = MetricsExecutor(config, None)
199 dispatcher.update_metrics_on_batch(annotations, predictions)
201 for _, evaluation_result in dispatcher.iterate_metrics(annotations, predictions):
202 assert evaluation_result == expected
204 def test_mae_on_interval_default_not_ignore_out_of_range(self):
206 RegressionAnnotation('identifier', -1),
207 RegressionAnnotation('identifier', 2),
208 RegressionAnnotation('identifier', 0.5)
211 RegressionPrediction('identifier', 1),
212 RegressionPrediction('identifier', 2),
213 RegressionPrediction('identifier', 1)
216 'annotation': 'mocked',
217 'metrics': [{'type': 'mae_on_interval', 'end': 1, 'ignore_values_not_in_interval': False}]
219 expected = EvaluationResult(
220 pytest.approx([2.0, 0.0, 0.5, 0.0, 0.0, 0.0]),
230 'mean: <= 0.0 < 1.0',
235 'calculate_mean': False
238 dispatcher = MetricsExecutor(config, None)
240 dispatcher.update_metrics_on_batch(annotations, predictions)
242 for _, evaluation_result in dispatcher.iterate_metrics(annotations, predictions):
243 assert evaluation_result == expected
245 def test_mae_on_interval_with_given_interval(self):
247 RegressionAnnotation('identifier', -1),
248 RegressionAnnotation('identifier', 2),
249 RegressionAnnotation('identifier', 1)
252 RegressionPrediction('identifier', 1),
253 RegressionPrediction('identifier', 3),
254 RegressionPrediction('identifier', 1)
257 'annotation': 'mocked',
258 'metrics': [{'type': 'mae_on_interval', 'intervals': [0.0, 2.0, 4.0]}]
260 expected = EvaluationResult(
261 pytest.approx([0.0, 0.0, 1.0, 0.0]),
268 'names': ['mean: <= 0.0 < 2.0', 'std: <= 0.0 < 2.0', 'mean: <= 2.0 < 4.0', 'std: <= 2.0 < 4.0'],
269 'calculate_mean': False
272 dispatcher = MetricsExecutor(config, None)
274 dispatcher.update_metrics_on_batch(annotations, predictions)
276 for _, evaluation_result in dispatcher.iterate_metrics(annotations, predictions):
277 assert evaluation_result == expected
279 def test_mae_on_interval_with_repeated_values(self):
281 RegressionAnnotation('identifier', -1),
282 RegressionAnnotation('identifier', 2),
283 RegressionAnnotation('identifier', 1)
286 RegressionPrediction('identifier', 1),
287 RegressionPrediction('identifier', 3),
288 RegressionPrediction('identifier', 1)
290 config = {'annotation': 'mocked', 'metrics': [{'type': 'mae_on_interval', 'intervals': [0.0, 2.0, 2.0, 4.0]}]}
291 expected = EvaluationResult(
292 pytest.approx([0.0, 0.0, 1.0, 0.0]),
299 'names': ['mean: <= 0.0 < 2.0', 'std: <= 0.0 < 2.0', 'mean: <= 2.0 < 4.0', 'std: <= 2.0 < 4.0'],
300 'calculate_mean': False
303 dispatcher = MetricsExecutor(config, None)
305 dispatcher.update_metrics_on_batch(annotations, predictions)
307 for _, evaluation_result in dispatcher.iterate_metrics(annotations, predictions):
308 assert evaluation_result == expected
310 def test_mae_on_interval_with_unsorted_values(self):
312 RegressionAnnotation('identifier', -1),
313 RegressionAnnotation('identifier', 2),
314 RegressionAnnotation('identifier', 1)
317 RegressionPrediction('identifier', 1),
318 RegressionPrediction('identifier', 3),
319 RegressionPrediction('identifier', 1)
321 config = {'annotation': 'mocked', 'metrics': [{'type': 'mae_on_interval', 'intervals': [2.0, 0.0, 4.0]}]}
322 expected = EvaluationResult(
323 pytest.approx([0.0, 0.0, 1.0, 0.0]),
328 'postfix': ' ', 'scale': 1,
329 'names': ['mean: <= 0.0 < 2.0', 'std: <= 0.0 < 2.0', 'mean: <= 2.0 < 4.0', 'std: <= 2.0 < 4.0'],
330 'calculate_mean': False
333 dispatcher = MetricsExecutor(config, None)
335 dispatcher.update_metrics_on_batch(annotations, predictions)
337 for _, evaluation_result in dispatcher.iterate_metrics(annotations, predictions):
338 assert evaluation_result == expected