def setup(log_dir): logger.configure(log_dir, prefix='main_test_script') logger.remove('') logger.log_line('hey') logger.log_data(dict(test=True), "test-data/dict.pkl") print(f"logging to {pathJoin(logger.log_directory, logger.prefix)}")
def render(env_name): from many_world import IS_PATCHED assert IS_PATCHED, "need the patched gym" import gym, numpy as np from tqdm import tqdm from ml_logger import logger logger.prefix = env_name env = gym.make(env_name) env.reset() frames = [] y = 0 args = [ dict(x=x, y=y, filename=f"images/{x:0.3f},{y:0.3f}.png") for x in np.linspace(-0.25, 0.25, 128) ] logger.log_data(args, 'index.pkl') for p in tqdm(args): x, y, filename = p['x'], p['y'], p['filename'] env.set_state(np.array([x, y, 0, 0]), np.array([0, 0, 0, 0])) # env.do_simulation([0, 0], 1) # PointMass does not need this. image = env.render('grey', width=20, height=20) frames.append(image) logger.log_image(image, filename) print('saving video') logger.log_video(frames, f"{env_name}.mp4") print('done')
def test_log_data(setup): import numpy d1 = numpy.random.randn(20, 10) logger.log_data(d1, 'test_file.pkl') sleep(1.0) d2 = numpy.random.randn(20, 10) logger.log_data(d2, 'test_file.pkl', overwrite=True) sleep(1.0) data = logger.load_pkl('test_file.pkl') assert len(data) == 1, "data should contain only one array because we overwrote it." assert numpy.array_equal(data[0], d2), "first should be the same as d2"
def test_load_pkl(setup): import numpy d1 = numpy.random.randn(20, 10) logger.log_data(d1, 'test_file.pkl') sleep(1.0) d2 = numpy.random.randn(20, 10) logger.log_data(d2, 'test_file.pkl') sleep(1.0) data = logger.load_pkl('test_file.pkl') assert len(data) == 2, "data should contain two arrays" assert numpy.array_equal(data[0], d1), "first should be the same as d1" assert numpy.array_equal(data[1], d2), "first should be the same as d2"