mfitzp / pathomx

Workflow based scientific analysis built on Python
https://www.mfitzp.com/tools/pathomx/
GNU General Public License v3.0
91 stars 30 forks source link

View: A difference graph for custom data #14

Open mfitzp opened 10 years ago

mfitzp commented 10 years ago

A matplotlib driven difference graph is available in the views, but not currently supported as an independent tool. Add a new tool to the basic_graphs plugin to support this.

The matplotlib code is mostly concentrated in the views.py module. Each of these views handle taking in data, manipulating it and then outputting it via a matplotlib graph. The simplest way to add a new basic graph is through the basic_graphs plugin ( plugins/basic_graphs/basic_graphs.py )

If you open basic_graphs.py you'll see two basic tools defined. Tools are what you use to manipulate/visualise data - and the things you can drag around in the Pathomx interface. The init sets up the toolbars, inputs and ouput ports (appear as circles on the editor), defines the type of data the tool accepts and then adds a view component. The view is the thing that you'll be creating (a new type of graph).

Below this there are two functions generate and prerender. In this case 'generate' does nothing except take in data from the input port and puts it out the output port (labelled 'View'). In more complex tools this is where the most of the processing occurs and it's multi-threaded. The 'prerender' function takes the output of the generate function and adapts it read for the view. This allows multiple tools to use the same basic view code, regardless of their outputs. Of course all this could happen in the 'generate' function but since it doesn't affect down-stream tools, we move it out of the way. This allows the next tool in line to start processing before we've finished rendering the graph.

The 'prerender' function is also multi-threaded, but the actual rendering is not (code in views.py currently occurs in the GUI thread) so the goal is to do as much as possible before passing the data on. In BarTool for example we generate a mean set of data, and add stddev to the statistics for the dataset. This allows the plot to plot the mean + error bars.

The output of this function is passed to the view. In this case it's the one defined by: self.views.addView(MplCategoryBarView(self), 'View') i.e. it's MplCategoryBarView (Mpl is a convention to identify it as a matplotlib graph view). If you open views.py on line 1016 you'll find the definition for MplCategoryBarView. As you can see it takes the one input output by the 'prerender' then turns it into a graph. The code for this is quite complicated for axis grouping, etc. so we'll skip that for now.

If you work your way back up views.py to MplDifferenceView you'll see a much simpler view. This takes two dso (data set objects) holding data, then plots them highlighting the difference between them. Simple as that.

So your first challenge (should you choose to accept it) is to create a new Tool in the basic_graphs.py plugin. To do this I recommend you copy-paste the SpectraTool and amend it to support two inputs (input1, input2) and pass them to a MplDifferenceView.