Call to env.render(<mode>)
will return environment visualisation as numpy array [ whith
x height
x 3 ]. This rgb array can be viewed or saved to image file using PILLOW or Matpltlib methods.
human
: human-readable one-step state visualisation of price lines and text information;agent
: if featurized state observation is computed, chances are it is better visualized as 2D image, enabling to look at what agent algorithm is feeded;episode
: after single episode is finished, btgym can visualise baktest run results. This feature actually uses backtrader Cerebro.plot()
method.
In [1]:
import itertools
import random
import os
import numpy as np
import sys
sys.path.insert(0,'..')
import IPython.display as Display
import PIL.Image as Image
from gym import spaces
from btgym import BTgymEnv
In [2]:
def show_rendered_image(rgb_array):
"""
Convert numpy array to RGB image using PILLOW and
show it inline using IPykernel.
"""
Display.display(Image.fromarray(rgb_array))
def render_all_modes(env):
"""
Retrieve and show environment renderings
for all supported modes.
"""
for mode in env.metadata['render.modes']:
print('[{}] mode:'.format(mode))
show_rendered_image(env.render(mode))
def take_some_steps(env, some_steps):
"""Just does it. Acting randomly."""
for step in range(some_steps):
rnd_action = env.action_space.sample()
o, r, d, i = env.step(rnd_action)
if d:
print('Episode finished,')
break
print(step+1, 'steps made.\n')
In [3]:
env = BTgymEnv(
filename='../examples/data/DAT_ASCII_EURUSD_M1_2016.csv',
state_shape={'raw': spaces.Box(low=-100, high=100,shape=(30,4), dtype=np.float32)},
skip_frame=5,
start_cash=100,
render_ylabel='Price Lines',
render_size_episode=(12,8),
render_size_human=(8, 3.5),
render_size_state=(10, 3.5),
render_dpi=75,
verbose=0,
)
In [4]:
render_all_modes(env)
In [5]:
o = env.reset()
render_all_modes(env)
In [6]:
take_some_steps(env, 100)
render_all_modes(env)
In [7]:
take_some_steps(env, 10000)
render_all_modes(env)
In [8]:
env.reset()
take_some_steps(env, 50)
render_all_modes(env)
In [9]:
env._force_control_mode()
render_all_modes(env)
Render given mode if possible, else just pass last rendered image:
If `episode` is finished/not started [server Control mode]:
if mode = `human`:
return stored image from last render `human` call
if mode = `agent`:
return stored image from last render `agent` call
if mode = `episode`:
render last finished episode,
update and return `episode` image.
If `episode` is running [server Episode mode]:
if mode = `human`:
render current state observation in conventional format,
update and return `human` image;
if mode = `agent`:
render observation 'as seen by agent',
update and return stored `agent` image.
if mode = `episode`:
return stored image of last finished `episode`.
If server process has been stopped:
rise exception.
...with these environment kwargs (default values are given):
render_enabled = True
render_state_as_image = True
render_size_human = (6, 3.5)
render_size_state = (7, 3.5)
render_size_episode = (12,8)
render_dpi=75
render_plotstyle = 'seaborn'
render_cmap = 'PRGn'
render_xlabel = 'Relative timesteps'
render_ylabel = 'Value'
render_title = 'step: {}, state observation min: {:.4f}, max: {:.4f}'
render_boxtext = dict(fontsize=12,
fontweight='bold',
color='w',
bbox={'facecolor': 'k', 'alpha': 0.3, 'pad': 3},
)
In [10]:
env.close()
env = BTgymEnv(
filename='../examples/data/DAT_ASCII_EURUSD_M1_2016.csv',
state_shape={'raw': spaces.Box(low=-100, high=100, shape=(30,4), dtype=np.float32)},
skip_frame=5,
start_cash=100,
render_modes=['human', 'episode', 'raw'],
render_ylabel='SOMETHING IN COLOR',
render_xlabel = 'BACKWARD TIME',
render_size_episode=(14, 10),
render_size_human=(11, 5),
render_size_state=(14, 5),
render_cmap = 'inferno',
render_plotstyle = 'ggplot',
render_dpi=75,
verbose=0,
render_boxtext = dict(
fontsize=14,
fontweight='regular',
color='k',
bbox={'facecolor': 'w', 'alpha': 0.4, 'pad': 3},
)
)
env.reset()
take_some_steps(env, 1000)
render_all_modes(env)
In [11]:
import matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
...though one have to fiddle around with figsize
parameter to get decent picture quality:
In [12]:
# Pyplot setup for rgb_array image:
plt.figure(figsize=(14,10))
plt.style.use('ggplot')
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.tight_layout(pad=0)
# Here:
plt.imshow(env.render('raw'))
plt.show()
dpi=300
to get print quality renderings;PIL: Image.save()
or Matplotlib: pyplot.savefig()
methods.render_enabled
>:render_enabled
kwarg is set to False
, rendering will be disabled for all modes; reset()
method just returns null-image
.
In [13]:
# Clean up:
env.close()
In [ ]: