|
| 1 | +import os |
| 2 | +from pathlib import Path |
1 | 3 | from typing import List, Optional, Tuple |
2 | 4 |
|
3 | 5 | from autoarray.plot.wrap.base.ticks import YTicks |
@@ -104,29 +106,6 @@ def plot_via_func(self, plotter, figure_name: str, func_name: str, kwargs): |
104 | 106 | else: |
105 | 107 | func(**{**{figure_name: True}, **kwargs}) |
106 | 108 |
|
107 | | - def output_subplot(self, filename_suffix: str = ""): |
108 | | - """ |
109 | | - Outplot the subplot to a file after all figures have been plotted on the subplot. |
110 | | -
|
111 | | - The multi-plotter requires its own output function to ensure that the subplot is output to a file, which |
112 | | - this provides. |
113 | | -
|
114 | | - Parameters |
115 | | - ---------- |
116 | | - filename_suffix |
117 | | - The suffix of the filename that the subplot is output to. |
118 | | - """ |
119 | | - |
120 | | - if self.plotter_list[0].mat_plot_1d is not None: |
121 | | - self.plotter_list[0].mat_plot_1d.output.subplot_to_figure( |
122 | | - auto_filename=f"subplot_{filename_suffix}" |
123 | | - ) |
124 | | - if self.plotter_list[0].mat_plot_2d is not None: |
125 | | - self.plotter_list[0].mat_plot_2d.output.subplot_to_figure( |
126 | | - auto_filename=f"subplot_{filename_suffix}" |
127 | | - ) |
128 | | - self.plotter_list[0].close_subplot_figure() |
129 | | - |
130 | 109 | def subplot_of_figure( |
131 | 110 | self, func_name: str, figure_name: str, filename_suffix: str = "", **kwargs |
132 | 111 | ): |
@@ -266,6 +245,99 @@ def subplot_of_multi_yx_1d(self, filename_suffix="", **kwargs): |
266 | 245 | ) |
267 | 246 | self.plotter_list[0].plotter_list[0].close_subplot_figure() |
268 | 247 |
|
| 248 | + def output_subplot(self, filename_suffix: str = ""): |
| 249 | + """ |
| 250 | + Outplot the subplot to a file after all figures have been plotted on the subplot. |
| 251 | +
|
| 252 | + The multi-plotter requires its own output function to ensure that the subplot is output to a file, which |
| 253 | + this provides. |
| 254 | +
|
| 255 | + Parameters |
| 256 | + ---------- |
| 257 | + filename_suffix |
| 258 | + The suffix of the filename that the subplot is output to. |
| 259 | + """ |
| 260 | + |
| 261 | + plotter = self.plotter_list[0] |
| 262 | + |
| 263 | + if plotter.mat_plot_1d is not None: |
| 264 | + plotter.mat_plot_1d.output.subplot_to_figure( |
| 265 | + auto_filename=f"subplot_{filename_suffix}" |
| 266 | + ) |
| 267 | + if plotter.mat_plot_2d is not None: |
| 268 | + plotter.mat_plot_2d.output.subplot_to_figure( |
| 269 | + auto_filename=f"subplot_{filename_suffix}" |
| 270 | + ) |
| 271 | + plotter.close_subplot_figure() |
| 272 | + |
| 273 | + def output_to_fits( |
| 274 | + self, |
| 275 | + func_name_list: List[str], |
| 276 | + figure_name_list: List[str], |
| 277 | + filename: str, |
| 278 | + tag_list: Optional[List[str]] = None, |
| 279 | + remove_fits_first: bool = False, |
| 280 | + **kwargs, |
| 281 | + ): |
| 282 | + """ |
| 283 | + Outputs a list of figures of the plotter objects in the `plotter_list` to a single .fits file. |
| 284 | +
|
| 285 | + This function takes as input lists of function names and figure names and then calls them via |
| 286 | + the `plotter_list` with an interface that outputs each to a .fits file. |
| 287 | +
|
| 288 | + For example, if you have multiple `ImagingPlotter` objects and want to output the `data` and `noise_map` of |
| 289 | + each to a single .fits files, you would input: |
| 290 | +
|
| 291 | + - `func_name_list=['figures_2d', 'figures_2d']` and |
| 292 | + - `figure_name_list=['data', 'noise_map']`. |
| 293 | +
|
| 294 | + The implementation of this code is hacky, with it using a specific interface in the `Output` object |
| 295 | + which sets the format to `fits_multi` to call a function which outputs the .fits files. A major visualuzation |
| 296 | + refactor is required to make this more elegant. |
| 297 | +
|
| 298 | + Parameters |
| 299 | + ---------- |
| 300 | + func_name_list |
| 301 | + The list of function names that are called to plot the figures on the subplot. |
| 302 | + figure_name_list |
| 303 | + The list of figure names that are plotted on the subplot. |
| 304 | + filenane |
| 305 | + The filename that the .fits file is output to. |
| 306 | + tag_list |
| 307 | + The list of tags that are used to set the `EXTNAME` of each hdu of the .fits file. |
| 308 | + remove_fits_first |
| 309 | + If the .fits file already exists, it is removed before the new .fits file is output, else it is updated |
| 310 | + with the figure going into the next hdu. |
| 311 | + kwargs |
| 312 | + Any additional keyword arguments that are passed to the function that plots the figure on the subplot. |
| 313 | + """ |
| 314 | + |
| 315 | + output_path = self.plotter_list[0].mat_plot_2d.output.output_path_from( |
| 316 | + format="fits_multi" |
| 317 | + ) |
| 318 | + output_fits_file = Path(output_path)/ f"{filename}.fits" |
| 319 | + |
| 320 | + if remove_fits_first: |
| 321 | + output_fits_file.unlink(missing_ok=True) |
| 322 | + |
| 323 | + for i, plotter in enumerate(self.plotter_list): |
| 324 | + plotter.mat_plot_2d.output._format = "fits_multi" |
| 325 | + |
| 326 | + plotter.set_filename(filename=f"{filename}") |
| 327 | + |
| 328 | + for j, (func_name, figure_name) in enumerate( |
| 329 | + zip(func_name_list, figure_name_list) |
| 330 | + ): |
| 331 | + if tag_list is not None: |
| 332 | + plotter.mat_plot_2d.output._tag_fits_multi = tag_list[j] |
| 333 | + |
| 334 | + self.plot_via_func( |
| 335 | + plotter=plotter, |
| 336 | + figure_name=figure_name, |
| 337 | + func_name=func_name, |
| 338 | + kwargs=kwargs, |
| 339 | + ) |
| 340 | + |
269 | 341 |
|
270 | 342 | class MultiYX1DPlotter: |
271 | 343 | def __init__( |
|
0 commit comments