validation.py 5.5 KB
Newer Older
X
test  
xjqbest 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# 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.

from paddlerec.core.utils import envs


class ValueFormat:
19
    def __init__(self, value_type, value, value_handler, required=False):
X
xjqbest 已提交
20
        self.value_type = value_type
X
test  
xjqbest 已提交
21
        self.value_handler = value_handler
22 23
        self.value = value
        self.required = required
X
test  
xjqbest 已提交
24 25

    def is_valid(self, name, value):
26 27 28 29 30 31

        if not self.value_type:
            ret = True
        else:
            ret = self.is_type_valid(name, value)

X
test  
xjqbest 已提交
32 33 34
        if not ret:
            return ret

35 36 37
        if not self.value or not self.value_handler:
            return True

X
test  
xjqbest 已提交
38 39 40 41
        ret = self.is_value_valid(name, value)
        return ret

    def is_type_valid(self, name, value):
X
xjqbest 已提交
42
        if self.value_type == "int":
X
test  
xjqbest 已提交
43 44
            if not isinstance(value, int):
                print("\nattr {} should be int, but {} now\n".format(
45
                    name, type(value)))
X
test  
xjqbest 已提交
46 47 48
                return False
            return True

X
xjqbest 已提交
49
        elif self.value_type == "str":
X
test  
xjqbest 已提交
50 51
            if not isinstance(value, str):
                print("\nattr {} should be str, but {} now\n".format(
52
                    name, type(value)))
X
test  
xjqbest 已提交
53 54 55
                return False
            return True

X
xjqbest 已提交
56
        elif self.value_type == "strs":
X
test  
xjqbest 已提交
57 58
            if not isinstance(value, list):
                print("\nattr {} should be list(str), but {} now\n".format(
59
                    name, type(value)))
X
test  
xjqbest 已提交
60 61 62 63 64 65 66 67
                return False
            for v in value:
                if not isinstance(v, str):
                    print("\nattr {} should be list(str), but list({}) now\n".
                          format(name, type(v)))
                    return False
            return True

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        elif self.value_type == "dict":
            if not isinstance(value, dict):
                print("\nattr {} should be str, but {} now\n".format(
                    name, type(value)))
                return False
            return True

        elif self.value_type == "dicts":
            if not isinstance(value, list):
                print("\nattr {} should be list(dist), but {} now\n".format(
                    name, type(value)))
                return False
            for v in value:
                if not isinstance(v, dict):
                    print("\nattr {} should be list(dist), but list({}) now\n".
                          format(name, type(v)))
                    return False
            return True

X
xjqbest 已提交
87
        elif self.value_type == "ints":
X
test  
xjqbest 已提交
88 89
            if not isinstance(value, list):
                print("\nattr {} should be list(int), but {} now\n".format(
90
                    name, type(value)))
X
test  
xjqbest 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104
                return False
            for v in value:
                if not isinstance(v, int):
                    print("\nattr {} should be list(int), but list({}) now\n".
                          format(name, type(v)))
                    return False
            return True

        else:
            print("\nattr {}'s type is {}, can not be supported now\n".format(
                name, type(value)))
            return False

    def is_value_valid(self, name, value):
105
        ret = self.value_handler(name, value, self.value)
X
test  
xjqbest 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
        return ret


def in_value_handler(name, value, values):
    if value not in values:
        print("\nattr {}'s value is {}, but {} is expected\n".format(
            name, value, values))
        return False
    return True


def eq_value_handler(name, value, values):
    if value != values:
        print("\nattr {}'s value is {}, but == {} is expected\n".format(
            name, value, values))
        return False
    return True


def ge_value_handler(name, value, values):
    if value < values:
        print("\nattr {}'s value is {}, but >= {} is expected\n".format(
            name, value, values))
        return False
    return True


def le_value_handler(name, value, values):
    if value > values:
        print("\nattr {}'s value is {}, but <= {} is expected\n".format(
            name, value, values))
        return False
    return True


def register():
    validations = {}
143 144 145 146 147 148
    validations["workspace"] = ValueFormat("str", None, None, True)
    validations["mode"] = ValueFormat(None, None, None, True)
    validations["runner"] = ValueFormat("dicts", None, None, True)
    validations["phase"] = ValueFormat("dicts", None, None, True)
    validations["hyper_parameters"] = ValueFormat("dict", None, None, False)
    return validations
X
test  
xjqbest 已提交
149 150 151


def yaml_validation(config):
152 153 154 155 156 157
    all_checkers = register()

    require_checkers = []
    for name, checker in all_checkers.items():
        if checker.required:
            require_checkers.append(name)
X
test  
xjqbest 已提交
158 159 160 161

    _config = envs.load_yaml(config)

    for required in require_checkers:
162
        if required not in _config.keys():
X
test  
xjqbest 已提交
163 164 165 166
            print("\ncan not find {} in yaml, which is required\n".format(
                required))
            return False

167
    for name, value in _config.items():
X
test  
xjqbest 已提交
168
        checker = all_checkers.get(name, None)
169 170 171 172
        if checker:
            ret = checker.is_valid(name, value)
            if not ret:
                return False
X
test  
xjqbest 已提交
173 174

    return True