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 = [{'type': 'mae'}]
31 expected = EvaluationResult(
32 pytest.approx([0.0, 0.0]),
37 {'postfix': ' ', 'scale': 1, 'names': ['mean', 'std'], 'calculate_mean': False}
39 dispatcher = MetricsExecutor(config, None)
41 dispatcher.update_metrics_on_batch(annotations, predictions)
43 for _, evaluation_result in dispatcher.iterate_metrics(annotations, predictions):
44 assert evaluation_result == expected
46 def test_mae_with_negative_diff_between_annotation_and_prediction(self):
47 annotations = [RegressionAnnotation('identifier', 3), RegressionAnnotation('identifier2', 1)]
48 predictions = [RegressionPrediction('identifier', 5), RegressionPrediction('identifier2', 5)]
49 config = [{'type': 'mae'}]
50 expected = EvaluationResult(
51 pytest.approx([3.0, 1.0]),
56 {'postfix': ' ', 'scale': 1, 'names': ['mean', 'std'], 'calculate_mean': False}
58 dispatcher = MetricsExecutor(config, None)
60 dispatcher.update_metrics_on_batch(annotations, predictions)
62 for _, evaluation_result in dispatcher.iterate_metrics(annotations, predictions):
63 assert evaluation_result == expected
65 def test_mae_with_positive_diff_between_annotation_and_prediction(self):
66 annotations = [RegressionAnnotation('identifier', 3), RegressionAnnotation('identifier2', 1)]
67 predictions = [RegressionPrediction('identifier', 1), RegressionPrediction('identifier2', -3)]
68 config = [{'type': 'mae'}]
69 expected = EvaluationResult(
70 pytest.approx([3.0, 1.0]),
75 {'postfix': ' ', 'scale': 1, 'names': ['mean', 'std'], 'calculate_mean': False}
77 dispatcher = MetricsExecutor(config, None)
79 dispatcher.update_metrics_on_batch(annotations, predictions)
81 for _, evaluation_result in dispatcher.iterate_metrics(annotations, predictions):
82 assert evaluation_result == expected
84 def test_mse_with_zero_diff_between_annotation_and_prediction(self):
85 annotations = [RegressionAnnotation('identifier', 3)]
86 predictions = [RegressionPrediction('identifier', 3)]
87 config = [{'type': 'mse'}]
88 expected = EvaluationResult(
89 pytest.approx([0.0, 0.0]),
94 {'postfix': ' ', 'scale': 1, 'names': ['mean', 'std'], 'calculate_mean': False}
96 dispatcher = MetricsExecutor(config, None)
98 dispatcher.update_metrics_on_batch(annotations, predictions)
100 for _, evaluation_result in dispatcher.iterate_metrics(annotations, predictions):
101 assert evaluation_result == expected
103 def test_mse_with_negative_diff_between_annotation_and_prediction(self):
104 annotations = [RegressionAnnotation('identifier', 3), RegressionAnnotation('identifier2', 1)]
105 predictions = [RegressionPrediction('identifier', 5), RegressionPrediction('identifier2', 5)]
106 config = [{'type': 'mse'}]
107 expected = EvaluationResult(
108 pytest.approx([10.0, 6.0]),
113 {'postfix': ' ', 'scale': 1, 'names': ['mean', 'std'], 'calculate_mean': False}
115 dispatcher = MetricsExecutor(config, None)
117 dispatcher.update_metrics_on_batch(annotations, predictions)
119 for _, evaluation_result in dispatcher.iterate_metrics(annotations, predictions):
120 assert evaluation_result == expected
122 def test_mse_with_positive_diff_between_annotation_and_prediction(self):
123 annotations = [RegressionAnnotation('identifier', 3), RegressionAnnotation('identifier2', 1)]
124 predictions = [RegressionPrediction('identifier', 1), RegressionPrediction('identifier2', -3)]
125 config = [{'type': 'mse'}]
126 expected = EvaluationResult(
127 pytest.approx([10.0, 6.0]),
132 {'postfix': ' ', 'scale': 1, 'names': ['mean', 'std'], 'calculate_mean': False}
134 dispatcher = MetricsExecutor(config, None)
136 dispatcher.update_metrics_on_batch(annotations, predictions)
138 for _, evaluation_result in dispatcher.iterate_metrics(annotations, predictions):
139 assert evaluation_result == expected
141 def test_missed_interval(self):
142 config = [{'type': 'mae_on_interval'}]
143 with pytest.raises(ValueError):
144 MetricsExecutor(config, None)
146 def test_mae_on_interval_default_all_missed(self):
147 annotations = [RegressionAnnotation('identifier', -2)]
148 predictions = [RegressionPrediction('identifier', 1)]
149 config = [{'type': 'mae_on_interval', 'end': 1}]
150 expected = EvaluationResult(
151 pytest.approx([0.0]),
156 {'postfix': ' ', 'scale': 1, 'names': [], 'calculate_mean': False}
158 dispatcher = MetricsExecutor(config, None)
160 dispatcher.update_metrics_on_batch(annotations, predictions)
162 with pytest.warns(UserWarning) as warnings:
163 for _, evaluation_result in dispatcher.iterate_metrics(annotations, predictions):
164 assert len(warnings) == 1
165 assert evaluation_result == expected
167 def test_mae_on_interval_default_all_not_in_range_not_ignore_out_of_range(self):
168 annotations = [RegressionAnnotation('identifier', -1), RegressionAnnotation('identifier', 2)]
169 predictions = [RegressionPrediction('identifier', 1), RegressionPrediction('identifier', 2)]
170 expected = EvaluationResult(
171 pytest.approx([2.0, 0.0, 0.0, 0.0]),
179 'names': ['mean: < 0.0', 'std: < 0.0', 'mean: > 1.0', 'std: > 1.0'],
180 'calculate_mean': False
183 config = [{'type': 'mae_on_interval', 'end': 1, 'ignore_values_not_in_interval': False}]
184 dispatcher = MetricsExecutor(config, None)
186 dispatcher.update_metrics_on_batch(annotations, predictions)
188 for _, evaluation_result in dispatcher.iterate_metrics(annotations, predictions):
189 assert evaluation_result == expected
191 def test_mae_on_interval_values_in_range(self):
192 annotations = [RegressionAnnotation('identifier', 0.5), RegressionAnnotation('identifier', 0.5)]
193 predictions = [RegressionPrediction('identifier', 1), RegressionPrediction('identifier', 0.25)]
194 config = [{'type': 'mae_on_interval', 'end': 1}]
195 expected = EvaluationResult(
196 pytest.approx([0.375, 0.125]),
201 {'postfix': ' ', 'scale': 1, 'names': ['mean: <= 0.0 < 1.0', 'std: <= 0.0 < 1.0'], 'calculate_mean': False}
203 dispatcher = MetricsExecutor(config, None)
205 dispatcher.update_metrics_on_batch(annotations, predictions)
207 for _, evaluation_result in dispatcher.iterate_metrics(annotations, predictions):
208 assert evaluation_result == expected
210 def test_mae_on_interval_default_not_ignore_out_of_range(self):
212 RegressionAnnotation('identifier', -1),
213 RegressionAnnotation('identifier', 2),
214 RegressionAnnotation('identifier', 0.5)
217 RegressionPrediction('identifier', 1),
218 RegressionPrediction('identifier', 2),
219 RegressionPrediction('identifier', 1)
221 config = [{'type': 'mae_on_interval', 'end': 1, 'ignore_values_not_in_interval': False}]
222 expected = EvaluationResult(
223 pytest.approx([2.0, 0.0, 0.5, 0.0, 0.0, 0.0]),
234 'mean: <= 0.0 < 1.0',
239 'calculate_mean': False
242 dispatcher = MetricsExecutor(config, None)
244 dispatcher.update_metrics_on_batch(annotations, predictions)
246 for _, evaluation_result in dispatcher.iterate_metrics(annotations, predictions):
247 assert evaluation_result == expected
249 def test_mae_on_interval_with_given_interval(self):
251 RegressionAnnotation('identifier', -1),
252 RegressionAnnotation('identifier', 2),
253 RegressionAnnotation('identifier', 1)
256 RegressionPrediction('identifier', 1),
257 RegressionPrediction('identifier', 3),
258 RegressionPrediction('identifier', 1)
260 config = [{'type': 'mae_on_interval', 'intervals': [0.0, 2.0, 4.0]}]
261 expected = EvaluationResult(
262 pytest.approx([0.0, 0.0, 1.0, 0.0]),
270 'names': ['mean: <= 0.0 < 2.0', 'std: <= 0.0 < 2.0', 'mean: <= 2.0 < 4.0', 'std: <= 2.0 < 4.0'],
271 'calculate_mean': False
274 dispatcher = MetricsExecutor(config, None)
276 dispatcher.update_metrics_on_batch(annotations, predictions)
278 for _, evaluation_result in dispatcher.iterate_metrics(annotations, predictions):
279 assert evaluation_result == expected
281 def test_mae_on_interval_with_repeated_values(self):
283 RegressionAnnotation('identifier', -1),
284 RegressionAnnotation('identifier', 2),
285 RegressionAnnotation('identifier', 1)
288 RegressionPrediction('identifier', 1),
289 RegressionPrediction('identifier', 3),
290 RegressionPrediction('identifier', 1)
292 config = [{'type': 'mae_on_interval', 'intervals': [0.0, 2.0, 2.0, 4.0]}]
293 expected = EvaluationResult(
294 pytest.approx([0.0, 0.0, 1.0, 0.0]),
302 'names': ['mean: <= 0.0 < 2.0', 'std: <= 0.0 < 2.0', 'mean: <= 2.0 < 4.0', 'std: <= 2.0 < 4.0'],
303 'calculate_mean': False
306 dispatcher = MetricsExecutor(config, None)
308 dispatcher.update_metrics_on_batch(annotations, predictions)
310 for _, evaluation_result in dispatcher.iterate_metrics(annotations, predictions):
311 assert evaluation_result == expected
313 def test_mae_on_interval_with_unsorted_values(self):
315 RegressionAnnotation('identifier', -1),
316 RegressionAnnotation('identifier', 2),
317 RegressionAnnotation('identifier', 1)
320 RegressionPrediction('identifier', 1),
321 RegressionPrediction('identifier', 3),
322 RegressionPrediction('identifier', 1)
324 config = [{'type': 'mae_on_interval', 'intervals': [2.0, 0.0, 4.0]}]
325 expected = EvaluationResult(
326 pytest.approx([0.0, 0.0, 1.0, 0.0]),
332 'postfix': ' ', 'scale': 1,
333 'names': ['mean: <= 0.0 < 2.0', 'std: <= 0.0 < 2.0', 'mean: <= 2.0 < 4.0', 'std: <= 2.0 < 4.0'],
334 'calculate_mean': False
337 dispatcher = MetricsExecutor(config, None)
339 dispatcher.update_metrics_on_batch(annotations, predictions)
341 for _, evaluation_result in dispatcher.iterate_metrics(annotations, predictions):
342 assert evaluation_result == expected