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

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

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

T
Thuan Nguyen 已提交
7

Y
Yan Chunwei 已提交
8 9 10
def check_tag_name_valid(tag):
    assert '%' not in tag, "character % is a reserved word, it is not allowed in tag."

T
Thuan Nguyen 已提交
11

Y
Yan Chunwei 已提交
12 13 14
def check_mode_name_valid(tag):
    for char in ['%', '/']:
        assert char not in tag, "character %s is a reserved word, it is not allowed in mode." % char
S
superjom 已提交
15

T
Thuan Nguyen 已提交
16

17
class LogReader(object):
Q
Qiao Longfei 已提交
18
    """LogReader is a Python wrapper to read and analysis the data that
19
    saved with data format defined in storage.proto. A User can get
Q
Qiao Longfei 已提交
20 21 22
    Scalar Reader/Image Reader/Histogram Reader from this module and use
    them to reade the data you need.
    """
S
superjom 已提交
23

S
superjom 已提交
24
    def __init__(self, dir, reader=None):
Q
Qiao Longfei 已提交
25 26 27 28 29
        """
        create a LogReader
        :param dir: the dir where log file is.
        :param reader: create a new LogReader with a formal one
        """
S
superjom 已提交
30
        self.dir = dir
31
        self.reader = reader if reader else core.LogReader(dir)
S
superjom 已提交
32

S
superjom 已提交
33
    def mode(self, mode):
Q
Qiao Longfei 已提交
34 35 36
        """
        Set the current mode of reader.

37 38 39 40
        :param mode: The log reader will read the data grouped by mode.
        :type mode: basestring
        :return: the log reader itself
        :rtype: LogReader
Q
Qiao Longfei 已提交
41
        """
Y
Yan Chunwei 已提交
42
        check_mode_name_valid(mode)
S
superjom 已提交
43 44
        self.reader.set_mode(mode)
        return self
S
superjom 已提交
45

S
superjom 已提交
46
    def as_mode(self, mode):
Q
Qiao Longfei 已提交
47 48
        """
        create a new LogReader with mode and return it to user.
49 50 51 52 53

        :param mode: The log reader will read the data grouped by mode.
        :type mode: basestring
        :return: a new log reader instance
        :rtype: LogReader
Q
Qiao Longfei 已提交
54
        """
Y
Yan Chunwei 已提交
55
        check_mode_name_valid(mode)
56
        tmp = LogReader(dir, self.reader.as_mode(mode))
S
superjom 已提交
57 58 59
        return tmp

    def modes(self):
Q
Qiao Longfei 已提交
60 61
        """
        Get all modes of the log file
62 63 64

        :return: a list of all modes
        :rtype: list
Q
Qiao Longfei 已提交
65
        """
S
superjom 已提交
66 67
        return self.reader.modes()

Q
Qiao Longfei 已提交
68 69 70
    def tags(self, component):
        """
        Get all tags from the current log file for one kind of component
71 72

        :param component:  scalar|histogram|image
Q
Qiao Longfei 已提交
73
        :return: all the tags
74
        :type: list
Q
Qiao Longfei 已提交
75 76
        """
        return self.reader.tags(component)
S
superjom 已提交
77 78

    def scalar(self, tag, type='float'):
Q
Qiao Longfei 已提交
79 80
        """
        Get a scalar reader with tag and data type
81 82 83

        :param tag:  The reader will read the scalar data marked with tag
        :type tag: basestring
Q
Qiao Longfei 已提交
84
        """
Y
Yan Chunwei 已提交
85
        check_tag_name_valid(tag)
S
superjom 已提交
86 87 88 89 90 91 92
        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 已提交
93
    def image(self, tag):
Q
Qiao Longfei 已提交
94 95
        """
        Get a image reader with tag
96 97 98

        :param tag:  The reader will read the image data marked with tag
        :type tag: basestring
Q
Qiao Longfei 已提交
99
        """
Y
Yan Chunwei 已提交
100
        check_tag_name_valid(tag)
S
superjom 已提交
101 102
        return self.reader.get_image(tag)

103
    def histogram(self, tag, type='float'):
Q
Qiao Longfei 已提交
104 105
        """
        Get a histogram reader with tag and data type
106 107 108

        :param tag:  The reader will read the histogram data marked with tag
        :type tag: basestring
Q
Qiao Longfei 已提交
109
        """
110 111 112 113 114
        type2scalar = {
            'float': self.reader.get_histogram_float,
            'double': self.reader.get_histogram_double,
            'int': self.reader.get_histogram_int,
        }
Y
Yan Chunwei 已提交
115
        check_tag_name_valid(tag)
116 117
        return type2scalar[type](tag)

J
Jeff Wang 已提交
118
    def text(self, tag):
J
Jeff Wang 已提交
119 120 121 122 123 124
        """
        Get a text reader with tag

        :param tag:  The reader will read the text data marked with tag
        :type tag: basestring
        """
J
Jeff Wang 已提交
125 126 127
        check_tag_name_valid(tag)
        return self.reader.get_text(tag)

J
Jeff Wang 已提交
128 129 130 131
    def embedding(self, tag):
        check_tag_name_valid(tag)
        return self.reader.get_embedding(tag)

N
Nicky Chan 已提交
132 133 134 135 136 137 138 139 140 141
    def audio(self, tag):
        """
        Get an audio reader with tag

        :param tag:  The reader will read the audio data marked with tag
        :type tag: basestring
        """
        check_tag_name_valid(tag)
        return self.reader.get_audio(tag)

S
superjom 已提交
142
    def __enter__(self):
S
superjom 已提交
143
        return self
S
superjom 已提交
144 145

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

S
superjom 已提交
148

149
class LogWriter(object):
Q
Qiao Longfei 已提交
150
    """LogWriter is a Python wrapper to write data to log file with the data
151
    format defined in storage.proto. A User can get Scalar Reader/Image Reader/
Q
Qiao Longfei 已提交
152
    Histogram Reader from this module and use them to write the data to log file.
J
Jeff Wang 已提交
153 154 155 156 157 158 159 160 161

    :param dir: The directory path to the saved log files.
    :type dir: basestring
    :param sync_cycle: Specify how often should the system store data into the file system.
        Typically adding a record requires 6 operations.
        System will save the data into the file system once operations count reaches sync_cycle.
    :type sync_cycle: integer
    :return: a new LogWriter instance
    :rtype: LogWriter
Q
Qiao Longfei 已提交
162
    """
S
superjom 已提交
163

S
superjom 已提交
164 165
    cur_mode = None

S
superjom 已提交
166 167 168
    def __init__(self, dir, sync_cycle, writer=None):
        self.dir = dir
        self.sync_cycle = sync_cycle
169
        self.writer = writer if writer else core.LogWriter(dir, sync_cycle)
S
superjom 已提交
170

S
superjom 已提交
171
    def mode(self, mode):
172 173 174 175 176 177 178 179
        """
        Set the current mode of writer.

        :param mode: The logger will group data under mode.
        :type mode: basestring
        :return: a new LogWriter instance with mode
        :rtype: LogWriter
        """
Y
Yan Chunwei 已提交
180
        check_mode_name_valid(mode)
S
superjom 已提交
181 182
        self.writer.set_mode(mode)
        return self
S
superjom 已提交
183

S
superjom 已提交
184
    def as_mode(self, mode):
Q
Qiao Longfei 已提交
185 186
        """
        create a new LogWriter with mode and return it.
187 188 189 190 191

        :param mode: The logger will group data under mode.
        :type mode: basestring
        :return: the logWriter itself
        :rtype: LogWriter
Q
Qiao Longfei 已提交
192
        """
Y
Yan Chunwei 已提交
193
        check_mode_name_valid(mode)
T
Thuan Nguyen 已提交
194 195
        LogWriter.cur_mode = LogWriter(self.dir, self.sync_cycle,
                                       self.writer.as_mode(mode))
196
        return LogWriter.cur_mode
S
superjom 已提交
197 198

    def scalar(self, tag, type='float'):
Q
Qiao Longfei 已提交
199 200
        """
        Create a scalar writer with tag and type to write scalar data.
201 202 203

        :param tag: The scalar writer will label the data with tag
        :type tag: basestring
204 205
        :return: A scalar writer to handle step and value records
        :rtype: ScalarWriter
Q
Qiao Longfei 已提交
206
        """
Y
Yan Chunwei 已提交
207
        check_tag_name_valid(tag)
S
superjom 已提交
208 209 210 211 212 213
        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 已提交
214

Y
Yan Chunwei 已提交
215
    def image(self, tag, num_samples, step_cycle=1):
Q
Qiao Longfei 已提交
216 217
        """
        Create an image writer that used to write image data.
218 219 220

        :param tag: The image writer will label the image with tag
        :type tag: basestring
221 222 223 224 225 226
        :param num_samples: how many samples to take in a step.
        :type num_samples: integer
        :param step_cycle: store every `step_cycle` as a record.
        :type step_cycle: integer
        :return: A image writer to sample images
        :rtype: ImageWriter
Q
Qiao Longfei 已提交
227
        """
Y
Yan Chunwei 已提交
228
        check_tag_name_valid(tag)
S
superjom 已提交
229
        return self.writer.new_image(tag, num_samples, step_cycle)
S
superjom 已提交
230

231
    def histogram(self, tag, num_buckets, type='float'):
Q
Qiao Longfei 已提交
232 233 234
        """
        Create a histogram writer that used to write
        histogram related data.
235 236 237

        :param tag: The histogram writer will label the data with tag
        :type tag: basestring
238 239
        :return: A histogram writer to record distribution
        :rtype: HistogramWriter
Q
Qiao Longfei 已提交
240
        """
Y
Yan Chunwei 已提交
241
        check_tag_name_valid(tag)
242 243 244 245 246 247 248
        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)

N
Nicky Chan 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    def audio(self, tag, num_samples, step_cycle=1):
        """
        Create an audio writer that used to write audio data.

        :param tag: The audio writer will label the audio with tag
        :type tag: basestring
        :param num_samples: how many samples to take in a step.
        :type num_samples: integer
        :param step_cycle: store every `step_cycle` as a record.
        :type step_cycle: integer
        :return: A audio writer to sample audio
        :rtype: AudioWriter
        """
        check_tag_name_valid(tag)
        return self.writer.new_audio(tag, num_samples, step_cycle)

J
Jeff Wang 已提交
265
    def text(self, tag):
J
Jeff Wang 已提交
266 267 268 269 270 271 272 273 274
        """
        Create a text writer that used to write
        text related data.

        :param tag: The text writer will label the data with tag
        :type tag: basestring
        :return: A text writer to record distribution
        :rtype: TextWriter
        """
J
Jeff Wang 已提交
275 276 277
        check_tag_name_valid(tag)
        return self.writer.new_text(tag)

J
Jeff Wang 已提交
278 279 280 281
    def embedding(self, tag):
        check_tag_name_valid(tag)
        return self.writer.new_embedding(tag)

J
Jeff Wang 已提交
282 283 284
    def save(self):
        self.writer.save()

S
superjom 已提交
285
    def __enter__(self):
S
superjom 已提交
286
        return self
S
superjom 已提交
287 288

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