Renders output using Matplotlib. Matplotlib requires objects to be kept around over the full lifetime of the plots; this is done through ``self.plots``. An interactive session is needed so that we can continue processing and just update the plots.
| 108 | |
| 109 | |
| 110 | class MatplotlibAnalyzer(PipelineAnalyzer): |
| 111 | # language=rst |
| 112 | """ |
| 113 | Renders output using Matplotlib. |
| 114 | |
| 115 | Matplotlib requires objects to be kept around over the full lifetime |
| 116 | of the plots; this is done through ``self.plots``. An interactive session |
| 117 | is needed so that we can continue processing and just update the |
| 118 | plots. |
| 119 | """ |
| 120 | |
| 121 | def __init__(self, **kwargs) -> None: |
| 122 | # language=rst |
| 123 | """ |
| 124 | Initializes the analyzer. |
| 125 | |
| 126 | Keyword arguments: |
| 127 | |
| 128 | :param str volts_type: Type of plotting for voltages (``"color"`` or ``"line"``). |
| 129 | """ |
| 130 | self.volts_type = kwargs.get("volts_type", "color") |
| 131 | plt.ion() |
| 132 | self.plots = {} |
| 133 | |
| 134 | def plot_obs(self, obs: torch.Tensor, tag: str = "obs", step: int = None) -> None: |
| 135 | # language=rst |
| 136 | """ |
| 137 | Pulls the observation off of torch and sets up for Matplotlib |
| 138 | plotting. |
| 139 | |
| 140 | :param obs: A 2D array of floats depicting an input image. |
| 141 | :param tag: A unique tag to associate the data with. |
| 142 | :param step: The step of the pipeline. |
| 143 | """ |
| 144 | obs = obs.detach().cpu().numpy() |
| 145 | obs = np.transpose(obs, (1, 2, 0)).squeeze() |
| 146 | |
| 147 | if tag in self.plots: |
| 148 | obs_ax, obs_im = self.plots[tag] |
| 149 | else: |
| 150 | obs_ax, obs_im = None, None |
| 151 | |
| 152 | if obs_im is None and obs_ax is None: |
| 153 | fig, obs_ax = plt.subplots() |
| 154 | obs_ax.set_title("Observation") |
| 155 | obs_ax.set_xticks(()) |
| 156 | obs_ax.set_yticks(()) |
| 157 | obs_im = obs_ax.imshow(obs, cmap="gray") |
| 158 | |
| 159 | self.plots[tag] = obs_ax, obs_im |
| 160 | else: |
| 161 | obs_im.set_data(obs) |
| 162 | |
| 163 | def plot_reward( |
| 164 | self, |
| 165 | reward_list: list, |
| 166 | reward_window: int = None, |
| 167 | tag: str = "reward", |
no outgoing calls