test_query_model.py 6.9 KB
Newer Older
G
gaocongli 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Test the query_model module."""
from unittest import TestCase

李鸿章 已提交
18 19
from mindinsight.lineagemgr.common.exceptions.exceptions import (LineageEventFieldNotExistException,
                                                                 LineageEventNotExistException)
G
gaocongli 已提交
20
from mindinsight.lineagemgr.querier.query_model import LineageObj
李鸿章 已提交
21

G
gaocongli 已提交
22
from . import event_data
李鸿章 已提交
23
from .test_querier import create_filtration_result, create_lineage_info
24
from ....utils.tools import deal_float_for_dict
G
gaocongli 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53


class TestLineageObj(TestCase):
    """Test the class of `LineageObj`."""

    def setUp(self):
        """Initialization before test case execution."""
        lineage_info = create_lineage_info(
            event_data.EVENT_TRAIN_DICT_0,
            event_data.EVENT_EVAL_DICT_0,
            event_data.EVENT_DATASET_DICT_0
        )
        self.summary_dir = '/path/to/summary0'
        self.lineage_obj = LineageObj(
            self.summary_dir,
            train_lineage=lineage_info.train_lineage,
            evaluation_lineage=lineage_info.eval_lineage,
            dataset_graph=lineage_info.dataset_graph,
        )

        lineage_info = create_lineage_info(
            event_data.EVENT_TRAIN_DICT_0,
            None, None)
        self.lineage_obj_no_eval = LineageObj(
            self.summary_dir,
            train_lineage=lineage_info.train_lineage,
            evaluation_lineage=lineage_info.eval_lineage
        )

54 55 56 57
    def _assert_dict_equal(self, dict1, dict2):
        deal_float_for_dict(dict1, dict2)
        self.assertDictEqual(dict1, dict2)

G
gaocongli 已提交
58 59 60
    def test_property(self):
        """Test the function of getting property."""
        self.assertEqual(self.summary_dir, self.lineage_obj.summary_dir)
61
        self._assert_dict_equal(
G
gaocongli 已提交
62 63 64
            event_data.EVENT_TRAIN_DICT_0['train_lineage']['algorithm'],
            self.lineage_obj.algorithm
        )
65
        self._assert_dict_equal(
G
gaocongli 已提交
66 67 68
            event_data.EVENT_TRAIN_DICT_0['train_lineage']['model'],
            self.lineage_obj.model
        )
69
        self._assert_dict_equal(
G
gaocongli 已提交
70 71 72
            event_data.EVENT_TRAIN_DICT_0['train_lineage']['train_dataset'],
            self.lineage_obj.train_dataset
        )
73
        self._assert_dict_equal(
G
gaocongli 已提交
74 75 76
            event_data.EVENT_TRAIN_DICT_0['train_lineage']['hyper_parameters'],
            self.lineage_obj.hyper_parameters
        )
77 78
        self._assert_dict_equal(event_data.METRIC_0, self.lineage_obj.metric)
        self._assert_dict_equal(
G
gaocongli 已提交
79 80 81 82 83 84 85
            event_data.EVENT_EVAL_DICT_0['evaluation_lineage']['valid_dataset'],
            self.lineage_obj.valid_dataset
        )

    def test_property_eval_not_exist(self):
        """Test the function of getting property with no evaluation event."""
        self.assertEqual(self.summary_dir, self.lineage_obj.summary_dir)
86
        self._assert_dict_equal(
G
gaocongli 已提交
87 88 89
            event_data.EVENT_TRAIN_DICT_0['train_lineage']['algorithm'],
            self.lineage_obj_no_eval.algorithm
        )
90
        self._assert_dict_equal(
G
gaocongli 已提交
91 92 93
            event_data.EVENT_TRAIN_DICT_0['train_lineage']['model'],
            self.lineage_obj_no_eval.model
        )
94
        self._assert_dict_equal(
G
gaocongli 已提交
95 96 97
            event_data.EVENT_TRAIN_DICT_0['train_lineage']['train_dataset'],
            self.lineage_obj_no_eval.train_dataset
        )
98
        self._assert_dict_equal(
G
gaocongli 已提交
99 100 101
            event_data.EVENT_TRAIN_DICT_0['train_lineage']['hyper_parameters'],
            self.lineage_obj_no_eval.hyper_parameters
        )
102 103
        self._assert_dict_equal({}, self.lineage_obj_no_eval.metric)
        self._assert_dict_equal({}, self.lineage_obj_no_eval.valid_dataset)
G
gaocongli 已提交
104 105 106 107 108 109 110 111 112 113

    def test_get_summary_info(self):
        """Test the function of get_summary_info."""
        filter_keys = ['algorithm', 'model']
        expected_result = {
            'summary_dir': self.summary_dir,
            'algorithm': event_data.EVENT_TRAIN_DICT_0['train_lineage']['algorithm'],
            'model': event_data.EVENT_TRAIN_DICT_0['train_lineage']['model']
        }
        result = self.lineage_obj.get_summary_info(filter_keys)
114
        self._assert_dict_equal(expected_result, result)
G
gaocongli 已提交
115

116 117
    def test_to_model_lineage_dict(self):
        """Test the function of to_model_lineage_dict."""
G
gaocongli 已提交
118 119 120 121 122 123 124
        expected_result = create_filtration_result(
            self.summary_dir,
            event_data.EVENT_TRAIN_DICT_0,
            event_data.EVENT_EVAL_DICT_0,
            event_data.METRIC_0,
            event_data.DATASET_DICT_0
        )
125 126 127
        expected_result['model_lineage']['dataset_mark'] = None
        expected_result.pop('dataset_graph')
        result = self.lineage_obj.to_model_lineage_dict()
128
        self._assert_dict_equal(expected_result, result)
129 130 131 132 133 134 135 136

    def test_to_dataset_lineage_dict(self):
        """Test the function of to_dataset_lineage_dict."""
        expected_result = {
            "summary_dir": self.summary_dir,
            "dataset_graph": event_data.DATASET_DICT_0
        }
        result = self.lineage_obj.to_dataset_lineage_dict()
G
gaocongli 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
        self.assertDictEqual(expected_result, result)

    def test_get_value_by_key(self):
        """Test the function of get_value_by_key."""
        result = self.lineage_obj.get_value_by_key('model_size')
        self.assertEqual(
            event_data.EVENT_TRAIN_DICT_0['train_lineage']['model']['size'],
            result
        )

    def test_init_fail(self):
        """Test the function of init with exception."""
        with self.assertRaises(LineageEventNotExistException):
            LineageObj(self.summary_dir)

        lineage_info = create_lineage_info(
            event_data.EVENT_TRAIN_DICT_EXCEPTION, None, None
        )
        with self.assertRaises(LineageEventFieldNotExistException):
            self.lineage_obj = LineageObj(
                self.summary_dir,
                train_lineage=lineage_info.train_lineage,
                evaluation_lineage=lineage_info.eval_lineage
            )

        lineage_info = create_lineage_info(
            event_data.EVENT_TRAIN_DICT_0,
            event_data.EVENT_EVAL_DICT_EXCEPTION,
            event_data.EVENT_DATASET_DICT_0
        )
        with self.assertRaises(LineageEventFieldNotExistException):
            self.lineage_obj = LineageObj(
                self.summary_dir,
                train_lineage=lineage_info.train_lineage,
                evaluation_lineage=lineage_info.eval_lineage
            )