updated readme file due to moving CMake scripts to the root folder
[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     def update_node(self):
25         pass
26     __getattr__ = dict.get
27
28
29 class BaseExtractorsTestingClass(unittest.TestCase):
30     expected = None
31     res = None
32     call_args = None
33     expected_call_args = None
34
35     def setUp(self):
36         if hasattr(self, 'patcher') and self.patcher:  # pylint: disable=no-member
37             patcher = patch(self.patcher)  # pylint: disable=no-member
38             self.addCleanup(patcher.stop)
39             self.infer_mock = patcher.start()
40
41     def compare(self):
42         if hasattr(self, 'infer_mock'):
43             self.assertTrue(self.infer_mock.called)
44         for key, val in self.expected.items():
45             if key == "infer":
46                 self.assertEqual(self.call_args, self.expected_call_args)
47             if type(val) is np.ndarray:
48                 np.testing.assert_equal(val, self.res[key])
49             elif type(val) is list:
50                 self.assertTrue(np.all([val == self.res[key]]))
51             else:
52                 self.assertAlmostEqual(val, self.res[key], 7,
53                                        "{} attribute comparison failed! Expected {} but {} given.".format(key, val,
54                                                                                                     self.res[key]))
55
56
57 class FakeParam:
58     def __init__(self, param_key, param_val):
59         setattr(self, param_key, param_val)
60
61
62 class FakeMultiParam:
63     def __init__(self, dict_values):
64         self.dict_values = dict_values
65         for (key, value) in dict_values.items():
66             # if type(value) != dict:
67             setattr(self, key, value)
68             # else:
69             #     setattr(self, key, FakeMultiParam(value))
70
71
72 class FakeBlob:
73     def __init__(self, param_key, param_val):
74         setattr(self, param_key, param_val)
75
76
77 class FakeModelLayer:
78     def __init__(self, blobs_val):
79         self.blobs = [FakeBlob('data', val) for val in blobs_val]
80
81
82 class FakeValue:
83     def __init__(self, val):
84         self.shape = val