storage.py 2.7 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):
S
superjom 已提交
9

S
superjom 已提交
10 11
    cur_mode = None

S
superjom 已提交
12 13
    def __init__(self, dir, reader=None):
        self.dir = dir
14
        self.reader = reader if reader else core.LogReader(dir)
S
superjom 已提交
15

S
superjom 已提交
16
    def mode(self, mode):
S
superjom 已提交
17 18
        self.reader.set_mode(mode)
        return self
S
superjom 已提交
19

S
superjom 已提交
20
    def as_mode(self, mode):
21
        tmp = LogReader(dir, self.reader.as_mode(mode))
S
superjom 已提交
22 23 24 25 26 27 28
        return tmp

    def modes(self):
        return self.reader.modes()

    def tags(self, kind):
        return self.reader.tags(kind)
S
superjom 已提交
29 30 31 32 33 34 35 36 37

    def scalar(self, tag, type='float'):
        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 已提交
38 39 40
    def image(self, tag):
        return self.reader.get_image(tag)

41 42 43 44 45 46 47 48
    def histogram(self, tag, type='float'):
        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 已提交
49
    def __enter__(self):
S
superjom 已提交
50
        return self
S
superjom 已提交
51 52

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

S
superjom 已提交
55

56
class LogWriter(object):
S
superjom 已提交
57

S
superjom 已提交
58 59
    cur_mode = None

S
superjom 已提交
60 61 62
    def __init__(self, dir, sync_cycle, writer=None):
        self.dir = dir
        self.sync_cycle = sync_cycle
63
        self.writer = writer if writer else core.LogWriter(dir, sync_cycle)
S
superjom 已提交
64

S
superjom 已提交
65
    def mode(self, mode):
S
superjom 已提交
66 67
        self.writer.set_mode(mode)
        return self
S
superjom 已提交
68

S
superjom 已提交
69
    def as_mode(self, mode):
70 71
        LogWriter.cur_mode = LogWriter(self.dir, self.sync_cycle, self.writer.as_mode(mode))
        return LogWriter.cur_mode
S
superjom 已提交
72 73

    def scalar(self, tag, type='float'):
74 75 76
        '''
        Create a scalar component.
        '''
S
superjom 已提交
77 78 79 80 81 82
        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 已提交
83

S
superjom 已提交
84
    def image(self, tag, num_samples, step_cycle):
85 86 87
        '''
        Create an image component.
        '''
S
superjom 已提交
88
        return self.writer.new_image(tag, num_samples, step_cycle)
S
superjom 已提交
89

90 91 92 93 94 95 96 97 98 99 100
    def histogram(self, tag, num_buckets, type='float'):
        '''
        Create a histogram component.
        '''
        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 已提交
101
    def __enter__(self):
S
superjom 已提交
102
        return self
S
superjom 已提交
103 104

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