psyplot icon indicating copy to clipboard operation
psyplot copied to clipboard

NEW FEATURE: Export projects as python scripts

Open Chilipp opened this issue 8 years ago • 0 comments

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

  1. Only datasets that have been stored to disc can be opened in the script
  2. The matplotlib axes setup has to be reproduced
  3. Some formatoptions do require more complex data structures, such as colormaps or numpy arrays

Proposed methodology to generate the script

  1. 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 disc
    

    The number ds<number> should be determined by the psyplot.data.DatasetAccessor.num attribute

  2. We should not use the psyplot.project.plot API 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

    1. reproduce the DataArray (or InteractiveList) from the datasets ds1, ds2, ...
    2. Create the matplotlib axes that is visualized (if there is a plotter)
    3. open the plotter class from psyplot (if there is a plotter)
  3. Concatenate all the extracted data arrays/lists into one project

Proposed API additions:

  1. 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 the to_script method of the contents

  2. 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 the to_script method of the plotter

  3. A new abstract method psyplot.plotter.Plotter.to_script() method, that

    1. Creates the matplotlib axes
    2. Creates the plotter by first checking the prepare_string methods of the formatoptions and then the as_string method
  4. A new psyplot.plotter.Formatoption.prepare_string method, 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

  5. A new psyplot.plotter.Formatoption.as_string method, that is called after the prepare_string method and sets the value for the formatoption in the Plotter(...) initialization. By default, this should just be repr(self.value)

Examples

Paraview for example has this functionality with Tools->Start Trace

Chilipp avatar Apr 11 '18 15:04 Chilipp