storage.py 4.5 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3
from __future__ import absolute_import

from visualdl import core
S
superjom 已提交
4 5 6 7

dtypes = ("float", "double", "int32", "int64")


8
class LogReader(object):
Q
Qiao Longfei 已提交
9 10 11 12 13
    """LogReader is a Python wrapper to read and analysis the data that
    saved with data format defined in storage.proto. user can get
    Scalar Reader/Image Reader/Histogram Reader from this module and use
    them to reade the data you need.
    """
S
superjom 已提交
14

S
superjom 已提交
15
    def __init__(self, dir, reader=None):
Q
Qiao Longfei 已提交
16 17 18 19 20
        """
        create a LogReader
        :param dir: the dir where log file is.
        :param reader: create a new LogReader with a formal one
        """
S
superjom 已提交
21
        self.dir = dir
22
        self.reader = reader if reader else core.LogReader(dir)
S
superjom 已提交
23

S
superjom 已提交
24
    def mode(self, mode):
Q
Qiao Longfei 已提交
25 26 27 28 29 30 31 32 33
        """
        Set the current mode of reader.

        :param mode: the mode is something like a scope, it's used to
        put some related data together. for example: train or test.
        data generated during training can be marked mode train, and data
        generated during testing can be marked test.
        :return: the reader itself
        """
S
superjom 已提交
34 35
        self.reader.set_mode(mode)
        return self
S
superjom 已提交
36

S
superjom 已提交
37
    def as_mode(self, mode):
Q
Qiao Longfei 已提交
38 39 40
        """
        create a new LogReader with mode and return it to user.
        """
41
        tmp = LogReader(dir, self.reader.as_mode(mode))
S
superjom 已提交
42 43 44
        return tmp

    def modes(self):
Q
Qiao Longfei 已提交
45 46 47 48
        """
        Get all modes of the log file
        :return:
        """
S
superjom 已提交
49 50
        return self.reader.modes()

Q
Qiao Longfei 已提交
51 52 53 54 55 56 57
    def tags(self, component):
        """
        Get all tags from the current log file for one kind of component
        :param component:  Scalar|Histogram|Images
        :return: all the tags
        """
        return self.reader.tags(component)
S
superjom 已提交
58 59

    def scalar(self, tag, type='float'):
Q
Qiao Longfei 已提交
60 61 62
        """
        Get a scalar reader with tag and data type
        """
S
superjom 已提交
63 64 65 66 67 68 69
        type2scalar = {
            'float': self.reader.get_scalar_float,
            'double': self.reader.get_scalar_double,
            'int': self.reader.get_scalar_int,
        }
        return type2scalar[type](tag)

S
superjom 已提交
70
    def image(self, tag):
Q
Qiao Longfei 已提交
71 72 73
        """
        Get a image reader with tag
        """
S
superjom 已提交
74 75
        return self.reader.get_image(tag)

76
    def histogram(self, tag, type='float'):
Q
Qiao Longfei 已提交
77 78 79
        """
        Get a histogram reader with tag and data type
        """
80 81 82 83 84 85 86
        type2scalar = {
            'float': self.reader.get_histogram_float,
            'double': self.reader.get_histogram_double,
            'int': self.reader.get_histogram_int,
        }
        return type2scalar[type](tag)

S
superjom 已提交
87
    def __enter__(self):
S
superjom 已提交
88
        return self
S
superjom 已提交
89 90

    def __exit__(self, type, value, traceback):
S
superjom 已提交
91
        self.reader.set_mode("default")
S
superjom 已提交
92

S
superjom 已提交
93

94
class LogWriter(object):
Q
Qiao Longfei 已提交
95 96 97 98
    """LogWriter is a Python wrapper to write data to log file with the data
    format defined in storage.proto. user can get Scalar Reader/Image Reader/
    Histogram Reader from this module and use them to write the data to log file.
    """
S
superjom 已提交
99

S
superjom 已提交
100 101
    cur_mode = None

S
superjom 已提交
102 103 104
    def __init__(self, dir, sync_cycle, writer=None):
        self.dir = dir
        self.sync_cycle = sync_cycle
105
        self.writer = writer if writer else core.LogWriter(dir, sync_cycle)
S
superjom 已提交
106

S
superjom 已提交
107
    def mode(self, mode):
S
superjom 已提交
108 109
        self.writer.set_mode(mode)
        return self
S
superjom 已提交
110

S
superjom 已提交
111
    def as_mode(self, mode):
Q
Qiao Longfei 已提交
112 113 114
        """
        create a new LogWriter with mode and return it.
        """
115 116
        LogWriter.cur_mode = LogWriter(self.dir, self.sync_cycle, self.writer.as_mode(mode))
        return LogWriter.cur_mode
S
superjom 已提交
117 118

    def scalar(self, tag, type='float'):
Q
Qiao Longfei 已提交
119 120 121
        """
        Create a scalar writer with tag and type to write scalar data.
        """
S
superjom 已提交
122 123 124 125 126 127
        type2scalar = {
            'float': self.writer.new_scalar_float,
            'double': self.writer.new_scalar_double,
            'int': self.writer.new_scalar_int,
        }
        return type2scalar[type](tag)
S
superjom 已提交
128

S
superjom 已提交
129
    def image(self, tag, num_samples, step_cycle):
Q
Qiao Longfei 已提交
130 131 132
        """
        Create an image writer that used to write image data.
        """
S
superjom 已提交
133
        return self.writer.new_image(tag, num_samples, step_cycle)
S
superjom 已提交
134

135
    def histogram(self, tag, num_buckets, type='float'):
Q
Qiao Longfei 已提交
136 137 138 139
        """
        Create a histogram writer that used to write
        histogram related data.
        """
140 141 142 143 144 145 146
        types = {
            'float': self.writer.new_histogram_float,
            'double': self.writer.new_histogram_double,
            'int': self.writer.new_histogram_int,
        }
        return types[type](tag, num_buckets)

S
superjom 已提交
147
    def __enter__(self):
S
superjom 已提交
148
        return self
S
superjom 已提交
149 150

    def __exit__(self, type, value, traceback):
S
superjom 已提交
151
        self.writer.set_mode("default")