stable-baselines3/tests/test_logger.py

84 lines
2 KiB
Python
Raw Normal View History

2020-01-20 10:58:00 +00:00
import pytest
import numpy as np
2020-05-05 13:02:35 +00:00
from stable_baselines3.common.logger import (make_output_format, read_csv, read_json, DEBUG, ScopedConfigure,
info, debug, set_level, configure, record, record_dict,
dump, record_mean, warn, error, reset)
2020-01-20 10:58:00 +00:00
KEY_VALUES = {
"test": 1,
"b": -3.14,
"8": 9.9,
"l": [1, 2],
"a": np.array([1, 2, 3]),
"f": np.array(1),
"g": np.array([[[1]]]),
}
KEY_EXCLUDED = {}
for key in KEY_VALUES.keys():
KEY_EXCLUDED[key] = None
2020-01-20 10:58:00 +00:00
def test_main(tmp_path):
2020-01-20 10:58:00 +00:00
"""
tests for the logger module
"""
info("hi")
debug("shouldn't appear")
set_level(DEBUG)
debug("should appear")
configure(folder=str(tmp_path))
record("a", 3)
record("b", 2.5)
dump()
record("b", -2.5)
record("a", 5.5)
dump()
2020-01-20 10:58:00 +00:00
info("^^^ should see a = 5.5")
record_mean("b", -22.5)
record_mean("b", -44.4)
record("a", 5.5)
dump()
2020-01-20 10:58:00 +00:00
with ScopedConfigure(None, None):
info("^^^ should see b = 33.3")
with ScopedConfigure(str(tmp_path / "test-logger"), ["json"]):
record("b", -2.5)
dump()
2020-01-20 10:58:00 +00:00
reset()
record("a", "longasslongasslongasslongasslongasslongassvalue")
dump()
2020-01-20 10:58:00 +00:00
warn("hey")
error("oh")
record_dict({"test": 1})
2020-01-20 10:58:00 +00:00
@pytest.mark.parametrize('_format', ['stdout', 'log', 'json', 'csv', 'tensorboard'])
def test_make_output(tmp_path, _format):
2020-01-20 10:58:00 +00:00
"""
test make output
:param _format: (str) output format
"""
if _format == 'tensorboard':
# Skip if no tensorboard installed
pytest.importorskip("tensorboard")
writer = make_output_format(_format, tmp_path)
writer.write(KEY_VALUES, KEY_EXCLUDED)
2020-01-20 10:58:00 +00:00
if _format == "csv":
read_csv(tmp_path / 'progress.csv')
2020-01-20 10:58:00 +00:00
elif _format == 'json':
read_json(tmp_path / 'progress.json')
2020-01-20 10:58:00 +00:00
writer.close()
def test_make_output_fail(tmp_path):
2020-01-20 10:58:00 +00:00
"""
test value error on logger
"""
with pytest.raises(ValueError):
make_output_format('dummy_format', tmp_path)