btgym.rendering package

btgym.rendering.renderer module

class btgym.rendering.renderer.BTgymRendering(render_modes, **kwargs)[source]

Handles BTgym Environment rendering.

Note

Call initialize_pyplot() method before first render() call!

Plotting controls, can be passed as kwargs.

Parameters:
  • render_state_as_image=True,
  • render_state_channel=0,
  • 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',
  • timesteps', (render_xlabel='Relative) –
  • render_ylabel='Value',
  • step (render_title='local) – {}, state observation min: {:.4f}, max: {:.4f}’,
  • render_boxtext=dict(fontsize=12, – fontweight=’bold’, color=’w’, bbox={‘facecolor’: ‘k’, ‘alpha’: 0.3, ‘pad’: 3}, )
initialize_pyplot()[source]

Call me before use! [Supposed to be done inside already running server process]

to_string(dictionary, excluded=[])[source]

Converts given dictionary to more-or-less good looking text block string.

rgb_empty()[source]

Returns empty ‘plug’ image.

parse_response(state, mode, reward, info, done)[source]

Converts environment response to plotting attributes: state, title, text.

render(mode_list, cerebro=None, step_to_render=None, send_img=True)[source]

Renders given mode if possible, else just passes last already rendered image. Returns rgb image as numpy array.

Logic:
  • If cerebro arg is received:
    render entire episode, using built-in backtrader plotting feature, update stored episode image.
  • If `step_to_render’ arg is received:
    • if mode = ‘raw_state’:
      render current state observation in conventional ‘price’ format, update stored raw_state image;
    • if mode = something_else’:
      visualise observation as ‘seen’ by agent, update stored ‘agent’ image.
Returns:mode image.

Note

It can actually return several modes in a single dict. It prevented by Gym modes convention, but done internally at the end of the episode.

draw_plot(data, figsize=(10, 6), title='', box_text='', xlabel='X', ylabel='Y', line_labels=None)[source]

Visualises environment state as 2d line plot. Retrurns image as rgb_array.

Parameters:
  • data – np.array of shape [num_values, num_lines]
  • figsize – figure size (in.)
  • title
  • box_text
  • xlabel
  • ylabel
  • line_labels – iterable holding line legends as str
Returns:

rgb image as np.array of size [with, height, 3]

draw_image(data, figsize=(12, 6), title='', box_text='', xlabel='X', ylabel='Y', line_labels=None)[source]

Visualises environment state as image. Returns rgb_array.

draw_episode(cerebro)[source]

Hacky way to render episode. Due to backtrader/matplotlib memory leaks have to encapsulate it in separate process. Strange but reliable. PID’s are driving crazy.

Parameters:instance (cerebro) –
Returns:rgb array.
class btgym.rendering.renderer.BTgymNullRendering(*args, **kwargs)[source]

Empty renderer to use when resources are concern.

btgym.rendering.plotter module

class btgym.rendering.plotter.BTgymPlotter(**kwargs)[source]

Hacky way to get cerebro.plot() renderings. Overrides default backtrader plotter behaviour.

pass

savefig(fig, filename, width=16, height=9, dpi=300, tight=True)[source]

We neither need picture to appear in <stdout> nor file to be written to disk (slow). Just set params and return fig to be converted to rgb array.

class btgym.rendering.plotter.DrawCerebro(cerebro, width, height, dpi, result_pipe, use=None, rowsmajor=1)[source]

That’s the way we plot it…

run()[source]
Returns:rgb_array.