# How to see what your agent did ?¶

Be aware that this notebook, as well as the plotting capabilities in grid2op are a work in progress. This notebook will change, and some function might change name in future major grid2op release.

## I General plotting utilities¶

With the module "grid2op.Plot" (more information on the official documentation here) we offer some possibility to inspect visually (aka plot) some informations about the state of the powergrid.

This module counts today 3 "base" classes:

• PlotPyGame that uses the "pygame" lirabrie to plot information about the powergrid. This library is particularly suited for making videos, looking at the temporal dynamics of the grid etc. It is not recommended to use it for studying a particular step.
• PlotMatplotlib which uses the well-known matplotlib python library to render the plot. Matplotlib being the most used plotting library in python, we decided to add its support in grid2op.
• PlotPlotly that uses the plotply librairy. As opposed to pygame, plotly is particularly suited for making in depth study of some particular time step.

It is not recommended to use any of these. Rather, we developped two higher level classes:

• Plotting that will allow easier manipulation of the "base" classes above mentionned. This should be the main class using for studying the behaviour of your agent as of version 0.7.0.
• EpisodeReplay which uses the "PlotPyGame" class and is used to render a movie "as a gif or a mp4 file mainly for communication about the results of your agent
• env.render which is the familiar method for all people used to the open ai gym framework.

We want to emphasize that for now, the results of the plottings are not always aesthetically pleasing. We are working on improving this for the next releases of grid2op.

Last but not least, a package called grid2viz has been developped to help you diagnose in depth the behaviour of your agent. This package is much more advance than all the methods presented above and we highly recommend its usage to get the best of your agents!

In [1]:
import matplotlib.pyplot as plt  # pip install matplotlib
import seaborn as sns # pip install seaborn
import pygame  # pip install pygame
import plotly.graph_objects as go  # pip install plotly
import imageio  # pip install imageio
import imageio_ffmpeg  # pip install imageio-ffmpeg

pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html


This notebook will not work if one of the 6 packages above cannot be imported. We then highly recommend you to install them on your machine. (we put the pip command to install them if you have any trouble)

In [2]:
import grid2op
env = grid2op.make_new(test=True)

/home/benjamin/Documents/grid2op_test/getting_started/grid2op/MakeEnv.py:686: UserWarning:

Your are using only 2 chronics for this environment. More can be download by running, from a command line:



## II Plot.Plotting utility¶

As we already said, the "Plot.Plotting" module can help render a powergrid using 3 different methods: pygame, matplotlib or plotly. The display method is defined when you create a "plotting" object as shown bellow.

All functions exposed here are available for pygame, plotly and matplotlib. Fill free to switch from one to the other in order to see the differences.

### II A) Plot Static informations¶

The next cell will plot the names of each object on the powergrid, as well as their id.

In [3]:
from grid2op.Plot import Plotting
plot_helper = Plotting(env.observation_space,
display_mod="matplotlib"  # all available values are : "matplotlib", "pygame" or "plotly"
)
plot_helper.plot_layout()

/home/benjamin/Documents/grid2op_test/getting_started/grid2op/Plot/Plotting.py:47: UserWarning:

Matplotlib is currently using module://ipykernel.pylab.backend_inline, which is a non-GUI backend, so cannot show the figure.


Out[3]:
(<Figure size 1080x1080 with 1 Axes>,
<matplotlib.axes._subplots.AxesSubplot at 0x7f3182b9a5f8>)

It is also possible to display some "external" information on this layout, for example, you can plot the thermal limit of each powerlines:

In [4]:
plot_helper.plot_info(line_info=env._thermal_limit_a, colormap="line")

Out[4]:
(<Figure size 1080x1080 with 1 Axes>,
<matplotlib.axes._subplots.AxesSubplot at 0x7f3183253908>)

The argument "line_info" shows that you want to plot information about powerlines (here the thermal limit). The argument "colormap=line" is present to indicate you want to color differently the different values on the powerlines. As you can notice, the higher thermal limit are displayed in a darker color.

It is also possible to display information abouts loads, generators or substation in the same manner.

### II B) Plot a few observations¶

For this part, we highly recommend to use "plotly" method, as it allows more user interactions than a bare matplotlib figure, for example it is possible to zoom in or out.

In [5]:
plot_helper = Plotting(env.observation_space,
display_mod="plotly"  # all available values are : "matplotlib", "pygame" or "plotly"
)
obs = env.reset()
_ = plot_helper.plot_obs(obs)