Publishing 2019 R1 content
[platform/upstream/dldt.git] / tools / utils / configuration_filter.py
1 """
2 Copyright (C) 2018-2019 Intel Corporation
3
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
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 """
16
17 import copy
18 import os
19
20
21 class ConfigurationFilter:
22     @staticmethod
23     def filter(configuration, filter_metric_name: str, filter_metric_type: str, logger = None):
24         updated_configuration = copy.deepcopy(configuration)
25         if 'models' not in updated_configuration or len(updated_configuration['models']) == 0:
26             raise ValueError("'models' key is absent in configuration")
27
28         updated_configuration['models'] = [model for model in updated_configuration['models'] if 'launchers' in model and model['launchers']]
29         if len(updated_configuration['models']) > 1:
30             raise ValueError("too many models")
31
32         if not updated_configuration['models']:
33             raise ValueError("there are no models")
34
35         model = updated_configuration['models'][0]
36         if 'datasets' not in model or len(model['datasets']) == 0:
37             raise ValueError("'datasets' key is absent in models")
38
39         if len(model['datasets']) > 1:
40             raise ValueError("too many datasets in model")
41
42         dataset = model['datasets'][0]
43         if filter_metric_name:
44             dataset['metrics'] = [i for i in dataset['metrics'] if i['name'] == filter_metric_name]
45
46         if filter_metric_type:
47             dataset['metrics'] = [i for i in dataset['metrics'] if i['type'] == filter_metric_type]
48
49         if 'metrics' not in dataset or len(dataset['metrics']) == 0:
50             raise ValueError("can not find appropriate metric in dataset{}{}".format(
51                 ", filter_metric_name='{}'".format(filter_metric_name) if filter_metric_name else "",
52                 ", filter_metric_type='{}'".format(filter_metric_type) if filter_metric_type else ""))
53
54         if filter_metric_name is None and filter_metric_type is None and len(dataset['metrics']) > 1:
55             dataset['metrics'] = [dataset['metrics'][0]]
56             if logger:
57                 logger.warn("too many metrics without filters, first metric '{}' is used".format(str(dataset['metrics'][0])))
58
59         if len(dataset['metrics']) > 1:
60             raise ValueError("too many metrics in datasets")
61
62         metric = dataset['metrics'][0]
63         if 'presenter' in metric and metric['presenter'] != 'return_value':
64             original_presenter = metric['presenter']
65             metric['presenter'] = 'return_value'
66             if logger:
67                 logger.warn("presenter was changed from '{}' to '{}'".format(original_presenter, metric['presenter']))
68         else:
69             metric['presenter'] = 'return_value'
70             if logger:
71                 logger.warn("presenter was set to '{}'".format(metric['presenter']))
72
73         return updated_configuration
74