summary.py 2.8 KB
Newer Older
S
superjom 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
__all__ = [
    'set_storage',
    'scalar',
]
import core

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


def set_storage(dir):
    '''
    :param dir: str
        directory of summary to write log.
    :return: None
    '''
S
superjom 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28
    core.im().storage().set_dir(dir)


def set_readable_storage(dir):
    core.start_read_service(dir)


def set_writable_storage(dir):
    core.start_write_service(dir)


def stop_service():
    core.stop_threads()
S
superjom 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107


class _Scalar(object):
    '''
    Python syntax wrapper for the core.ScalarHelper object.
    '''

    def __init__(self, core_object):
        self._core_object = core_object

    def add(self, id, vs):
        '''
        add a scalar record
        :param id: int
        id in the x-corrdinate
        :param vs: list
        values
        :return: None
        '''
        self._core_object.add_record(id, vs)

    def set_captions(self, cs):
        '''
        set the captions, one caption for one line.
        :param cs: list of str
        :return: None
        '''
        self._core_object.set_captions(cs)

    @property
    def captions(self):
        return self._core_object.get_captions()

    @property
    def records(self):
        '''
        get all the records, format like
        [
        [0.1, 0.2], # first record
        [0.2, 0.3], # second record
        # ...
        ]
        :return: list of list
        '''
        return self._core_object.get_records()

    @property
    def ids(self):
        '''
        get all the ids for the records
        :return: list of int
        '''
        return self._core_object.get_ids()

    @property
    def timestamps(self):
        '''
        get all the timestamps for the records
        :return: list of int
        '''
        return self._core_object.get_timestamps()

    @property
    def size(self):
        return self._core_object.get_record_size()


def scalar(tag, dtype='float'):
    '''
    create a scalar component.

    :param tag: str
        name of this component.
    :param dtype: string
        the data type that will be used in underlying storage.
    :return: object of core.Tablet
    '''
    assert dtype in dtypes, "invalid dtype(%s), should be one of %s" % (
        dtype, str(dtypes))
S
superjom 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120
    tablet = core.im().add_tablet(tag, -1)
    dtype2obj = {
        'float': tablet.as_float_scalar,
        'double': tablet.as_double_scalar,
        'int32': tablet.as_int32_scalar,
        'int64': tablet.as_int64_scalar,
    }
    obj = dtype2obj[dtype]()
    return _Scalar(obj)


def read_scalar(tag, dtype='float'):
    tablet = core.im().tablet(tag)
S
superjom 已提交
121 122 123 124 125 126 127 128
    dtype2obj = {
        'float': tablet.as_float_scalar,
        'double': tablet.as_double_scalar,
        'int32': tablet.as_int32_scalar,
        'int64': tablet.as_int64_scalar,
    }
    obj = dtype2obj[dtype]()
    return _Scalar(obj)