File size: 1,478 Bytes
079c32c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
49
50
51
52
53
54
55
56
import random
import pytest
from easydict import EasyDict
from ditk import logging

from ding.utils.log_helper import build_logger, pretty_print
from ding.utils.file_helper import remove_file

cfg = EasyDict(
    {
        'env': {},
        'env_num': 4,
        'common': {
            'save_path': "./summary_log",
            'load_path': '',
            'name': 'fakeLog',
            'only_evaluate': False,
        },
        'logger': {
            'print_freq': 10,
            'save_freq': 200,
            'eval_freq': 200,
        },
        'data': {
            'train': {},
            'eval': {},
        },
        'learner': {
            'log_freq': 100,
        },
    }
)


@pytest.mark.unittest
class TestLogger:

    def test_pretty_print(self):
        pretty_print(cfg)

    def test_logger(self):
        logger, tb_logger = build_logger(cfg.common.save_path, name="fake_test", need_tb=True, text_level=logging.DEBUG)
        variables = {'aa': 3.0, 'bb': 4, 'cc': 3e4}
        # text logger
        logger.info("I'm an info")
        logger.debug("I'm a bug")
        logger.error("I'm an error")
        logger.info(logger.get_tabulate_vars(variables))
        # tensorboard logger
        for i in range(10):
            new_vars = {k: v * (i + random.random()) for k, v in variables.items()}
            for k, v in new_vars.items():
                tb_logger.add_scalar(k, v, i)
        remove_file(cfg.common.save_path)
        tb_logger.close()