plotagain.saveplotcontext.SavePlotContext¶
- class plotagain.saveplotcontext.SavePlotContext(save_dir: Path | str, locals_dict: Dict[str, Any], overwrite: bool = False, globals_dict: Dict[str, Any] | None = None, script_name: str = 'make_plot.py', script_template: Path | str | None = None, skip_show_plots: bool = False)¶
Context manager which wraps matplotlib.pyplot. Usage:
``` with SavePlotContext(“/…/save-dir/”, locals(), …) as pla:
pla.plot( … ) … pla.title( … ) pla.show()
Any calls to pla are passed to matplotlib.pyplot and all args and kwargs are stored. On exiting the ‘with’ block, all args/kwargs are saved to save-dir/ as pickle files and a script is autogenerated to recreate the produced plots. Using the locals() dict, the variable names of the objects passed in as arguments are inferred and re-used in the autogenerated script. Any objects which don’t appear in the locals() dict are given the name ‘unnamed_arg’ post-fixed with an integer if multiple unnamed args are present.
Outside the ‘with’ block, all calls continue to be passed onto matplotlib.pyplot but nothing is stored or recorded. This allows one to adobt a usage pattern of:
``` import matplotlib.pyplot as plt from plotagain import SavePlotContext
…
- with SavePlotContext(“/…/save-dir/”, locals(), …) as plt:
…
Before the first ‘with’ block, the plt variable points to the actual matplotib.pyplot. Within the with variable plt is re-assigned to a SavePlotContext instance and the behaviour is as decribed above. After the with block, plt is still a SavePlotContext instance but the object will pass all calls directly to the actual matplotlib.pyplot without any recording/storage or anything. This allows one to easily wrap existing matplotlib plotting code by through one additional line of code
Attributes¶
- save_dirPath
The path to a directory into which the argument pickles and autogenerated script are saved. If the directory does not exist it is created. If the directory is not empty and self.overwrite is False, an exception is raised.
- outer_locals_dict
The locals() dict containing the variables in the local scope of the code which created the SavePlotContext
- outer_globals_dict
The globals() dict containing the variables in the global scope of the code which created the SavePlotContext
- overwrite
If False, the directory self.save_dir must be empty, otherwise an exception is raised
- regenerate_script_name
The name of the autogenerated script
- script_template_path
A path to the template used to autogenerate the script which recreates the plots created using this SavePlotContext. See script_template.txt for an example
- has_exited
Boolean tracking whether the contextmanager has exited. If the context manager has exited, all calls continue to be passed onto matplotlib.pyplot but nothing is stored or recorded.
- used_variables
A mapping from the variable name to the objects passed into any matplotlib.pyplot call
- calls
A list of PyplotCall objects, one for each call made to matplotlib.pyplot. E.g. ‘plot’, ‘show’, ‘hist’
Parameters¶
- save_dir
The path to a directory into which the argument pickles and autogenerated script are saved. If the directory does not exist it is created. If the directory is not empty and self.overwrite is False, an exception is raised
- locals_dict
The dict returned by ‘locals()’ called in the scope of the plotting code
- globals_dict
The dict returned by ‘globals()’ called in the scope of the plotting code
- overwrite
If False, the directory self.save_dir must be empty, otherwise an exception is raised
- script_name
The name of the autogenerated script
- script_template
A path to the template used to autogenerate the script which recreates the plots created using this SavePlotContext. See script_template.txt for an example
- skip_show_plots
If set to True, calls for plt.show are NOT passed on to matplotlib.pyplot but are still stored for the reconstruction script. Instead of plt.show, plt.close is called instead. Allows for execution without pausing for plots. As an example, code developed locally is likely to involve lots of plots to check the output at various stages. Code executed remotely is likely to be unsupervised and therefore the plots ought not to render. Setting skip_show_plots to True allows for the disabling of manual checks while the plots that would be displayed are still saved and accessible later if required. See set_skip_all_show_plots() for a global flag of this nature
- __init__(save_dir: Path | str, locals_dict: Dict[str, Any], overwrite: bool = False, globals_dict: Dict[str, Any] | None = None, script_name: str = 'make_plot.py', script_template: Path | str | None = None, skip_show_plots: bool = False)¶
Parameters¶
- save_dir
The path to a directory into which the argument pickles and autogenerated script are saved. If the directory does not exist it is created. If the directory is not empty and self.overwrite is False, an exception is raised
- locals_dict
The dict returned by ‘locals()’ called in the scope of the plotting code
- globals_dict
The dict returned by ‘globals()’ called in the scope of the plotting code
- overwrite
If False, the directory self.save_dir must be empty, otherwise an exception is raised
- script_name
The name of the autogenerated script
- script_template
A path to the template used to autogenerate the script which recreates the plots created using this SavePlotContext. See script_template.txt for an example
- skip_show_plots
If set to True, calls for plt.show are NOT passed on to matplotlib.pyplot but are still stored for the reconstruction script. Instead of plt.show, plt.close is called instead. Allows for execution without pausing for plots. As an example, code developed locally is likely to involve lots of plots to check the output at various stages. Code executed remotely is likely to be unsupervised and therefore the plots ought not to render. Setting skip_show_plots to True allows for the disabling of manual checks while the plots that would be displayed are still saved and accessible later if required. See set_skip_all_show_plots() for a global flag of this nature
Methods
__init__(save_dir, locals_dict[, overwrite, ...])Parameters save_dir The path to a directory into which the argument pickles and autogenerated script are saved. If the directory does not exist it is created. If the directory is not empty and self.overwrite is False, an exception is raised locals_dict The dict returned by 'locals()' called in the scope of the plotting code globals_dict The dict returned by 'globals()' called in the scope of the plotting code overwrite If False, the directory self.save_dir must be empty, otherwise an exception is raised script_name The name of the autogenerated script script_template A path to the template used to autogenerate the script which recreates the plots created using this SavePlotContext. See script_template.txt for an example skip_show_plots If set to True, calls for plt.show are NOT passed on to matplotlib.pyplot but are still stored for the reconstruction script. Instead of plt.show, plt.close is called instead. Allows for execution without pausing for plots. As an example, code developed locally is likely to involve lots of plots to check the output at various stages. Code executed remotely is likely to be unsupervised and therefore the plots ought not to render. Setting skip_show_plots to True allows for the disabling of manual checks while the plots that would be displayed are still saved and accessible later if required. See set_skip_all_show_plots() for a global flag of this nature
save()Saves all matplotlib.pyplot arguments and creates the script needed to reproduce the plots produced using this SavePlotContext instance
save_regenerate_script()Creates and saves a python script which reproduces all the plots produced using this SavePlotContext instance
save_used_variables()Saves a pickle file for each object passed in as an argument to a matplotlib.pyplot call
Attributes
save_dirouter_locals_dictouter_globals_dictoverwriteregenerate_script_nameskip_show_plotshas_exitedused_variablescalls