reader.py 7.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2020 VisualDL Authors. All Rights Reserve.
#
# 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.
# =======================================================================
15
import os
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
from visualdl.io import bfile
from visualdl.component import components
from visualdl.reader.record_reader import RecordReader
from visualdl.server.data_manager import default_data_manager
from visualdl.proto import record_pb2
from visualdl.utils.string_util import decode_tag


def is_VDLRecord_file(path):
    """Determine whether it is a VDL log file according to the file name.

    File name of a VDL log file must contain `vdlrecords`.

    Args:
        path: File name to determine.

    Returns:
        True if the file is a VDL log file, otherwise false.
    """
    return "vdlrecords" in path


class LogReader(object):
    """Log reader to read vdl log, support for frontend api in lib.py.

    """

    def __init__(self, logdir):
        """Instance of LogReader

        Args:
            logdir: The dir include vdl log files, multiple subfolders allowed.
        """
49 50 51 52 53
        if isinstance(logdir, str):
            self.dir = [logdir]
        else:
            self.dir = logdir

54 55 56 57 58 59 60 61 62 63 64
        self.reader = None
        self.readers = {}
        self.walks = None
        self._tags = {}

        self.file_readers = {}
        self._environments = components
        self.data_manager = default_data_manager
        self.load_new_data(update=True)
        self._a_tags = {}

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        self._model = ""

    @property
    def model(self):
        return self._model

    @model.setter
    def model(self, model_path):
        if not os.path.isfile(model_path):
            print("Model path %s should be file path, please check this path." % model_path)
        else:
            if os.path.exists(model_path):
                self._model = model_path
            else:
                print("Model path %s is invalid, please check this path." % model_path)

81 82 83 84
    @property
    def logdir(self):
        return self.dir

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    def parse_from_bin(self, record_bin):
        """Register to self._tags by component type.

        Args:
            record_bin: Binary data from vdl log file.
        """
        record = record_pb2.Record()
        record.ParseFromString(record_bin)
        value = record.values[0]
        tag = decode_tag(value.tag)
        path = bfile.join(self.reader.dir, tag)

        if path not in self._tags.keys():
            value_type = value.WhichOneof("one_value")
            if "value" == value_type:
                component = "scalar"
            elif "image" == value_type:
                component = "image"
            elif "embeddings" == value_type:
                component = "embeddings"
            elif "audio" == value_type:
                component = "audio"
107 108
            elif "histogram" == value_type:
                component = "histogram"
走神的阿圆's avatar
走神的阿圆 已提交
109 110
            elif "pr_curve" == value_type:
                component = "pr_curve"
111 112 113 114 115 116 117 118
            else:
                raise TypeError("Invalid value type `%s`." % value_type)
            self._tags[path] = component

        return self._tags[path], self.reader.dir, tag, value

    def get_all_walk(self):
        self.walks = {}
119 120 121
        for dir in self.dir:
            for root, dirs, files in bfile.walk(dir):
                self.walks.update({root: files})
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

    def components_listing(self):
        """Get available component types.

        Indicates what components are included.

        Returns:
            self._environments: A dict like `{"image": False, "scalar":
                True}`
        """
        keys_enable = self.data_manager.get_keys()
        for key in self._environments.keys():
            if key in keys_enable:
                self._environments[key].update({"enable": True})
        return self._environments

    def logs(self, update=False):
        """Get logs.

        Every dir(means `run` in vdl) has only one log(meads `actual log file`).

        Returns:
            walks: A dict like {"exp1": "vdlrecords.1587375595.log",
                                "exp2": "vdlrecords.1587375685.log"}
        """
        if self.walks is None or update is True:
            self.get_all_walk()

            walks_temp = {}
            for run, tags in self.walks.items():
                tags_temp = [tag for tag in tags if is_VDLRecord_file(tag)]
                tags_temp.sort(reverse=True)
                if len(tags_temp) > 0:
                    walks_temp.update({run: tags_temp[0]})
            self.walks = walks_temp
        return self.walks

    def get_log_reader(self, dir, log):
        """Get log reader for every vdl log file.

        Get instance of class RecordReader base on BFile. Note that each
        `log` may contain multi `tag`.

        Args:
            dir: Dir name of log.
            log: Vdl log file name.
        """
        if self.walks is None:
            self.logs()
        if self.walks.get(dir, None) != log:
            raise FileNotFoundError("Can't find file %s.", (dir + "/" + log))

        filepath = bfile.join(dir, log)
        if filepath not in self.readers.keys():
            self._register_reader(filepath, dir)
        self.reader = self.readers[filepath]
        return self.reader

    def _register_reader(self, path, dir):
        if path not in list(self.readers.keys()):
            reader = RecordReader(filepath=path, dir=dir)
            self.readers[path] = reader

    def register_readers(self, update=False):
        """Register all readers for all vdl log files.

        Args:
            update: Need update if `update` is True.
        """
        self.logs(update)
        for dir, path in self.walks.items():
            filepath = bfile.join(dir, path)
            self._register_reader(filepath, dir)

    def add_remain(self):
        """Add remain data to data_manager.

        Add remain data to data manager according its component type and tag
        one by one.
        """
        for reader in self.readers.values():
            self.reader = reader

            remain = self.reader.get_remain()
            for item in remain:
                component, dir, tag, record = self.parse_from_bin(item)
                self.data_manager.add_item(component, self.reader.dir, tag,
                                           record)

    def get_remain(self):
        """Get all remain data by self.reader.
        """
        if self.reader is None:
            raise RuntimeError("Please specify log path!")
        return self.reader.get_remain()

    def runs(self, update=True):
        self.logs(update=update)
        return list(self.walks.keys())

    def tags(self):
        if self._tags is None:
            self.add_remain()
        return self._tags

227
    def components(self, update=False):
228 229
        """Get components type used by vdl.
        """
230 231
        if self.logdir is None:
            return set()
232 233
        if update is True:
            self.load_new_data(update=update)
234
        components_set = set(self._tags.values())
235 236
        components_set.add('scalar')

237
        return components_set
238 239 240 241 242 243 244

    def load_new_data(self, update=True):
        """Load remain data.

        Make sure all readers for every vdl log file are registered, load all
        remain data.
        """
245 246 247
        if self.logdir is not None:
            self.register_readers(update=update)
            self.add_remain()