Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / utils / unittest / extractors.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 unittest
18 from unittest.mock import patch
19
20 import numpy as np
21
22
23 class PB(dict):
24     __getattr__ = dict.get
25
26
27 class BaseExtractorsTestingClass(unittest.TestCase):
28     expected = None
29     res = None
30     call_args = None
31     expected_call_args = None
32
33     def setUp(self):
34         if hasattr(self, 'patcher') and self.patcher:  # pylint: disable=no-member
35             patcher = patch(self.patcher)  # pylint: disable=no-member
36             self.addCleanup(patcher.stop)
37             self.infer_mock = patcher.start()
38
39     def compare(self):
40         if hasattr(self, 'infer_mock'):
41             self.assertTrue(self.infer_mock.called)
42         for key, val in self.expected.items():
43             if key == "infer":
44                 self.assertEqual(self.call_args, self.expected_call_args)
45             if type(val) is np.ndarray:
46                 np.testing.assert_equal(val, self.res[key])
47             elif type(val) is list:
48                 self.assertTrue(np.all([val == self.res[key]]))
49             else:
50                 self.assertEqual(val, self.res[key],
51                                  "{} attribute comparison failed! Expected {} but {} given.".format(key, val,
52                                                                                                     self.res[key]))
53
54
55 class FakeParam:
56     def __init__(self, param_key, param_val):
57         setattr(self, param_key, param_val)
58
59
60 class FakeMultiParam:
61     def __init__(self, dict_values):
62         self.dict_values = dict_values
63         for (key, value) in dict_values.items():
64             # if type(value) != dict:
65             setattr(self, key, value)
66             # else:
67             #     setattr(self, key, FakeMultiParam(value))
68
69
70 class FakeBlob:
71     def __init__(self, param_key, param_val):
72         setattr(self, param_key, param_val)
73
74
75 class FakeModelLayer:
76     def __init__(self, blobs_val):
77         self.blobs = [FakeBlob('data', val) for val in blobs_val]
78
79
80 class FakeValue:
81     def __init__(self, val):
82         self.shape = val