NEW FEATURE: Export projects as python scripts
Summary
psyplot projects should be exportable directly to python scripts, not only as pickle files.
Reason
When trying to reproduce a plot created through the GUI, it would be helpful to have a python script to generate it, such that it can be copy-pasted to another python script.
Detailed explanation
One strength of psyplot is the short and easy syntax for formatoptions and
the structured data handling. And since anyway most of the formatoption
come in standard python objects like lists, strings, etc. it would be
straight-forward with python repr function to reproduce them in a script.
Challenges
- Only datasets that have been stored to disc can be opened in the script
- The matplotlib axes setup has to be reproduced
- Some formatoptions do require more complex data structures, such as colormaps or numpy arrays
Proposed methodology to generate the script
-
open the netCDF dataset from disk (if it is stored on the disk, otherwise leaf that empty such that the user can fill this in
ds1 = xr.open_dataset('nc-file1.nc') ds2 = xr.open_dataset('nc-file2.nc') ds3 = # dataset has not been stored on the discThe number
ds<number>should be determined by thepsyplot.data.DatasetAccessor.numattribute -
We should not use the
psyplot.project.plotAPI directly, since the plot methods can have diverse calling signatures. So we extract the data and create the plot for each array in the project from scratch. Therefore, for each data object in the project, export a script to- reproduce the DataArray (or InteractiveList) from the datasets
ds1, ds2, ... - Create the matplotlib axes that is visualized (if there is a plotter)
- open the plotter class from psyplot (if there is a plotter)
- reproduce the DataArray (or InteractiveList) from the datasets
-
Concatenate all the extracted data arrays/lists into one project
Proposed API additions:
-
A new method
psyplot.data.ArrayList.to_script()to top-level API method that generates the python script to reproduce the list/project. It should create the part to open the datasets and call theto_scriptmethod of the contents -
A new abstract method
psyplot.script.InteractiveBase.to_script()that generates the string to generate this data structure. If the object has a plotter, it should call theto_scriptmethod of the plotter -
A new abstract method
psyplot.plotter.Plotter.to_script()method, that- Creates the matplotlib axes
- Creates the plotter by first checking the
prepare_stringmethods of the formatoptions and then theas_stringmethod
-
A new
psyplot.plotter.Formatoption.prepare_stringmethod, that returns a string with necssary preparations for the formatoption. In case of a colormap instance, for example, this could be the example how the colormap is created. So by default, this should be an empty string -
A new
psyplot.plotter.Formatoption.as_stringmethod, that is called after theprepare_stringmethod and sets the value for the formatoption in thePlotter(...)initialization. By default, this should just berepr(self.value)
Examples
Paraview for example has this functionality with Tools->Start Trace