fsleyes.views.plotpanel¶
This module provides the PlotPanel and OverlayPlotPanel
classes. The PlotPanel class is the base class for all FSLeyes views
which display some sort of data plot. The OverlayPlotPanel is a
PlotPanel which contains some extra logic for displaying plots related to
the currently selected overlay.
-
class
fsleyes.views.plotpanel.PlotPanel(parent, overlayList, displayCtx, frame)¶ Bases:
fsleyes.views.viewpanel.ViewPanelThe
PlotPanelclass is the base class for all FSLeyes views which display some sort of 2D data plot, such as theTimeSeriesPanel, and theHistogramPanel. See also theOverlayPlotPanel, which contains extra logic for displaying plots related to the currently selected overlay.PlotPanelusesmatplotlibfor its plotting. ThematplotlibFigure,Axis, andCanvasinstances can be accessed via thegetFigure(),getAxis(), andgetCanvas()methods, if they are needed. Various display settings can be configured throughPlotPanelproperties, includinglegend,smooth, etc.Sub-class requirements
Sub-class implementations of
PlotPanelmust do the following:- Call the
PlotPanelconstructor. - Define a
DataSeriessub-class. - Override the
draw()method, so it calls thedrawDataSeries()method. - If necessary, override the
prepareDataSeries()method to perform any preprocessing onextraSeriespassed to thedrawDataSeries()method (but not applied toDataSeriesthat have been added to thedataSerieslist). - If necessary, override the
destroy()method, but make sure that the base-class implementation is called.
Data series
A
PlotPanelinstance plots data contained in one or moreDataSeriesinstances; allDataSeriesclasses are defined in theplottingsub-package. Therefore,PlotPanelsub-classes also need to define a sub-class of theDataSeriesbase class.DataSeriesobjects can be plotted by passing them to thedrawDataSeries()method.Or, if you want one or more
DataSeriesto be held, i.e. plotted every time, you can add them to thedataSerieslist. TheDataSeriesin thedataSerieslist will be plotted on every call todrawDataSeries()(in addition to anyDataSeriespassed directly todrawDataSeries()) until they are removed from thedataSerieslist.The draw queue
The
PlotPaneluses aasync.TaskThreadto asynchronously extract and prepare data for plotting, This is because data preparation may take a long time for largeImageoverlays, and the main application thread should not be blocked while this is occurring. TheTaskThreadinstance is accessible through thegetDrawQueue()method, in case anything needs to be scheduled on it.Plot panel actions
A number of
actionsare also provided by thePlotPanelclass:screenshotPrompts the user to select a file name, then saves a screenshot of the current plot. importDataSeriesImports data series from a text file. exportDataSeriesExports displayed data series to a text file. -
dataSeries= <MagicMock name='mock.List()' id='139845843011176'>¶ This list contains
DataSeriesinstances which are plotted on every call todrawDataSeries().DataSeriesinstances can be added/removed directly to/from this list.
-
artists= <MagicMock name='mock.List()' id='139845843011176'>¶ This list contains any
matplotlib.Artistinstances which are plotted every call todrawArtists().
-
legend= <MagicMock name='mock.Boolean()' id='139845844115072'>¶ If
True, a legend is added to the plot, with an entry for everyDataSeriesinstance in thedataSerieslist.
-
xAutoScale= <MagicMock name='mock.Boolean()' id='139845844115072'>¶ If
True, the plotlimitsfor the X axis are automatically updated to fit all plotted data.
-
yAutoScale= <MagicMock name='mock.Boolean()' id='139845844115072'>¶ If
True, the plotlimitsfor the Y axis are automatically updated to fit all plotted data.
-
xLogScale= <MagicMock name='mock.Boolean()' id='139845844115072'>¶ Toggle a \(log_{10}\) x axis scale.
-
yLogScale= <MagicMock name='mock.Boolean()' id='139845844115072'>¶ Toggle a \(log_{10}\) y axis scale.
-
ticks= <MagicMock name='mock.Boolean()' id='139845844115072'>¶ Toggle axis ticks and tick labels on/off.
-
grid= <MagicMock name='mock.Boolean()' id='139845844115072'>¶ Toggle an axis grid on/off.
-
bgColour= <MagicMock name='mock.Colour()' id='139845842273280'>¶ Plot background colour.
-
smooth= <MagicMock name='mock.Boolean()' id='139845844115072'>¶ If
Trueall plotted data is up-sampled, and smoothed using spline interpolation.
-
xlabel= <MagicMock name='mock.String()' id='139845843558864'>¶ A label to show on the x axis.
-
ylabel= <MagicMock name='mock.String()' id='139845843558864'>¶ A label to show on the y axis.
-
limits= <MagicMock name='mock.Bounds()' id='139845843098144'>¶ The x/y axis limits. If
xAutoScaleandyAutoScaleareTrue, these limit values are automatically updated on every call todrawDataSeries().
-
__init__(parent, overlayList, displayCtx, frame)¶ Create a
PlotPanel.Parameters: - parent – The
wxparent object. - overlayList – An
OverlayListinstance. - displayCtx – A
DisplayContextinstance. - frame – The
FSLeyesFrameinstance.
- parent – The
-
getFigure()¶ Returns the
matplotlibFigureinstance.
-
getAxis()¶ Returns the
matplotlibAxisinstance.
-
getCanvas()¶ Returns the
matplotlibCanvasinstance.
-
getDrawQueue()¶ Returns the :class`.idle.TaskThread` instance used for data preparation.
-
draw(*a)¶ This method must be overridden by
PlotPanelsub-classes.It is called whenever a
DataSeriesis added to thedataSerieslist, or when any plot display properties change.Sub-class implementations should call the
drawDataSeries()and meth:drawArtists methods.
-
asyncDraw(*a)¶ Schedules
draw()to be run asynchronously. This method should be used in preference to callingdraw()directly in most cases, particularly where the call occurs within a property callback function.
-
destroy()¶ Removes some property listeners, and then calls
ViewPanel.destroy().
-
screenshot(*a)¶ Prompts the user to select a file name, then saves a screenshot of the current plot.
See the
ScreenshotAction.
-
importDataSeries(*a)¶ Imports data series from a text file.
See the
ImportDataSeriesAction.
-
exportDataSeries(*args, **kwargs)¶ Exports displayed data series to a text file.
See the
ExportDataSeriesAction.
-
message(msg, clear=True, border=False)¶ Displays the given message in the centre of the figure.
This is a convenience method provided for use by subclasses.
-
getArtist(ds)¶ Returns the
matplotlib.Artist(typically aLine2Dinstance) associated with the givenDataSeriesinstance. AKeyErroris raised if there is no such artist.
-
getDrawnDataSeries()¶ Returns a list of tuples, each tuple containing the
(DataSeries, x, y)data for oneDataSeriesinstance as it is shown on the plot.
-
prepareDataSeries(ds)¶ Prepares the data from the given
DataSeriesso it is ready to be plotted. Called by the__drawOneDataSeries()method for anyextraSeriespassed to thedrawDataSeries()method (but not applied toDataSeriesthat have been added to thedataSerieslist).This implementation just returns
DataSeries.getData- override it to perform any custom preprocessing.
-
drawArtists(refresh=True, immediate=False)¶ Draw all
matplotlib.Artistinstances in theartistslist, then refresh the canvas.Parameters: refresh – If True(default), the canvas is refreshed.
-
drawDataSeries(extraSeries=None, refresh=False, **plotArgs)¶ Queues a request to plot all of the
DataSeriesinstances in thedataSerieslist.This method does not do the actual plotting - it is performed asynchronously, to avoid locking up the GUI:
- The data for each
DataSeriesinstance is prepared on separate threads (usingidle.run()). - A call to
idle.wait()is enqueued on aTaskThread. - This
waitfunction waits until all of the data preparation threads have completed, and then passes all of the data to the__drawDataSeries()method.
Parameters: - extraSeries – A sequence of additional
DataSeriesto be plotted. These series are passed through theprepareDataSeries()method before being plotted. - refresh – If
True, the canvas is refreshed. Otherwise, you must callgetCanvas().draw()manually. Defaults toFalse- thedrawArtists()method will refresh the canvas, so if you calldrawArtists()immediately after calling this method (which you should), then you don’t need to manually refresh the canvas. - plotArgs – Passed through to the
__drawDataSeries()method.
Note
This method must only be called from the main application thread (the
wxevent loop).- The data for each
-
_PlotPanel__artistsChanged(*a)¶ Called when the
artistslist changes. CallsasyncDraw().
-
_PlotPanel__calcLimits(dataxlims, dataylims, axisxlims, axisylims, axWidth, axHeight)¶ Calculates and returns suitable axis limits for the current plot. Also updates the
limitsproperty. This method is called by thedrawDataSeries()method.If
xAutoScaleoryAutoScaleare enabled, the limits are calculated from the data range, using the canvas width and height to maintain consistent padding around the plotted data, irrespective of the canvas size.. Otherwise, the existing axis limits are retained.
Parameters: - dataxlims – A tuple containing the (min, max) x data range.
- dataylims – A tuple containing the (min, max) y data range.
- axisxlims – A tuple containing the current (min, max) x axis limits.
- axisylims – A tuple containing the current (min, max) y axis limits.
- axWidth – Canvas width in pixels
- axHeight – Canvas height in pixels
-
_PlotPanel__dataSeriesChanged(*a)¶ Called when the
dataSerieslist changes. Adds listeners to any newDataSeriesinstances, and then callsasyncDraw().
-
_PlotPanel__drawDataSeries(dataSeries, allXdata, allYdata, oldxlim, oldylim, refresh, xlabel=None, ylabel=None, **plotArgs)¶ Called by
__drawDataSeries(). Plots all of the data associated with the givendataSeries.Parameters: - dataSeries – The list of
DataSeriesinstances to plot. - allXdata – A list of arrays containing X axis data, one for each
DataSeries. - allYdata – A list of arrays containing Y axis data, one for each
DataSeries. - oldxlim – X plot limits from the previous draw. If
xAutoScaleis disabled, this limit is preserved. - oldylim – Y plot limits from the previous draw. If
yAutoScaleis disabled, this limit is preserved. - refresh – Refresh the canvas - see
drawDataSeries(). - xlabel – If provided, overrides the value of the
xlabelproperty. - ylabel – If provided, overrides the value of the
ylabelproperty. - plotArgs – Remaining arguments passed to the
__drawOneDataSeries()method.
- dataSeries – The list of
-
_PlotPanel__drawOneDataSeries(ds, xdata, ydata, **plotArgs)¶ Plots a single
DataSeriesinstance. This method is called by thedrawDataSeries()method.Parameters: - ds – The
DataSeriesinstance. - xdata – X axis data.
- ydata – Y axis data.
- plotArgs – May be used to customise the plot - these
arguments are all passed through to the
Axis.plotfunction.
- ds – The
-
__module__= 'fsleyes.views.plotpanel'¶
- Call the
-
class
fsleyes.views.plotpanel.OverlayPlotPanel(*args, **kwargs)¶ Bases:
fsleyes.views.plotpanel.PlotPanelThe
OverlayPlotPanelis aPlotPanelwhich contains some extra logic for creating, storing, and drawingDataSeriesinstances for each overlay in theOverlayList.Subclass requirements
Sub-classes must:
- Implement the
createDataSeries()method, so it creates aDataSeriesinstance for a specified overlay. - Implement the
PlotPanel.draw()method so it calls thePlotPanel.drawDataSeries(), passingDataSeriesinstances for all overlays whereDisplay.enabledisTrue. - Optionally implement the
prepareDataSeries()method to perform any custom preprocessing.
The internal data series store
The
OverlayPlotPanelmaintains a store ofDataSeriesinstances, one for each compatible overlay in theOverlayList. TheOverlayPlotPanelmanages the property listeners that must be registered with each of theseDataSeriesto refresh the plot. These instances are created by thecreateDataSeries()method, which is implemented by sub-classes. The following methods are available to sub-classes, for managing the internal store ofDataSeriesinstances:getDataSeriesReturns the DataSeriesinstance associated with the specified overlay, orNoneif there is noDataSeriesinstance.getDataSeriesToPlotConvenience method which returns a list of overlays which have DataSeriesthat should be plotted.clearDataSeriesDestroys the internally cached DataSeriesfor the given overlay.updateDataSeriesMakes sure that a DataSeriesinstance has been created for every compatible overlay, and that property listeners are correctly registered, so the plot can be refreshed when needed.addDataSeriesEvery DataSerieswhich is currently plotted, and has not been added to thePlotPanel.dataSerieslist, is added to said list.removeDataSeriesRemoves the most recently added DataSeriesfrom thisOverlayPlotPanel.Proxy images
The
OverlayPlotPanelwill replace allProxyImageinstances with their base images. This functionality was originally added to support theHistogramSeries.showOverlayfunctionality - it adds a mask image to theOverlayListto display the histogram range. Sub-classes may wish to adhere to the same logic (replacingProxyImageinstances with their bases)Control panels
The
PlotControlPanel,PlotListPanel, andOverlayListPanelare FSLeyes control panels which work with theOverlayPlotPanel. ThePlotControlPanelis not intended to be used directly - plot-specific sub-classes are used instead. The following actions can be used to toggle control panels on anOverlayPlotPanel:toggleOverlayListShows/hides an OverlayListPanel.togglePlotListShows/hides a PlotListPanel.Sub-classes
The
OverlayPlotPanelis the base class for:TimeSeriesPanelThe TimeSeriesPanelis anOverlayPlotPanelwhich plots time series data from overlays.HistogramPanelAn OverlayPlotPanelwhich plots histograms fromImageoverlay data.PowerSpectrumPanelThe PowerSpectrumPanelclass is anOverlayPlotPanelwhich plots power spectra of overlay data.-
plotColours= {}¶ This dictionary is used to store a collection of
{overlay : colour}mappings. It is shared across allOverlayPlotPanelinstances, so that the same (initial) colour is used for the same overlay, across multiple plots.Sub-classes should use the
getOverlayPlotColour()method to retrieve the initial colour to use for a given overlay.
-
__init__(*args, **kwargs)¶ Create an
OverlayPlotPanel.Parameters: initialState – Must be passed as a keyword argument. Allows you to specify the initial enabled/disabled state for each overlay. See updateDataSeries(). If not provided, only the data series for the currently selected overlay is shown (if possible).All other argumenst are passed through to
PlotPanel.__init__().
-
_OverlayPlotPanel__dataSeriesChanged(*a)¶ Called when the
dataSerieslist changes. Enables/disables theremoveDataSeries()action accordingly.
-
_OverlayPlotPanel__overlayListChanged(*a, **kwa)¶ Called when the
OverlayListchanges. Makes sure that there are noDataSeriesinstances in thePlotPanel.dataSerieslist, or in the internal cache, which refer to overlays that no longer exist.Parameters: initialState – Must be passed as a keyword argument. If provided, passed through to the updateDataSeries()method.
-
__module__= 'fsleyes.views.plotpanel'¶
-
destroy()¶ Must be called when this
OverlayPlotPanelis no longer needed. Removes some property listeners, and callsPlotPanel.destroy().
-
getDataSeriesToPlot()¶ Convenience method which returns a list of overlays which have
DataSeriesthat should be plotted.
-
getDataSeries(overlay)¶ Returns the
DataSeriesinstance associated with the specified overlay, orNoneif there is noDataSeriesinstance.
-
getOverlayPlotColour(overlay)¶ Returns an initial colour to use for plots associated with the given overlay. If a colour is present in the
plotColoursdictionary, it is returned. Otherwise a random colour is generated, added toplotColours, and returned.
-
addDataSeries()¶ Every
DataSerieswhich is currently plotted, and has not been added to thePlotPanel.dataSerieslist, is added to said list.
-
removeDataSeries(*a)¶ Removes the most recently added
DataSeriesfrom thisOverlayPlotPanel.
-
createDataSeries(overlay)¶ This method must be implemented by sub-classes. It must create and return a
DataSeriesinstance for the specified overlay.Note
Sub-class implementations should set the
DataSeries.colourproperty to that returned by thegetOverlayPlotColour()method.Different
DataSeriestypes need to be re-drawn when different properties change. For example, aVoxelTimeSeries`instance needs to be redrawn when theDisplayContext.locationproperty changes, whereas aMelodicTimeSeriesinstance needs to be redrawn when theVolumeOpts.volumeproperty changes.Therefore, in addition to creating and returning a
DataSeriesinstance for the given overlay, sub-class implementations must also specify the properties which affect the state of theDataSeriesinstance. These must be specified as two lists:- the targets list, a list of objects which own the dependant
properties (e.g. the
DisplayContextorVolumeOptsinstance). - The properties list, a list of names, each specifying the property on the corresponding target.
This method must therefore return a tuple containing:
- A
DataSeriesinstance, orNoneif the overlay is incompatible. - A list of target instances.
- A list of property names.
The target and property name lists must have the same length.
- the targets list, a list of objects which own the dependant
properties (e.g. the
-
clearDataSeries(overlay)¶ Destroys the internally cached
DataSeriesfor the given overlay.
-
updateDataSeries(initialState=None)¶ Makes sure that a
DataSeriesinstance has been created for every compatible overlay, and that property listeners are correctly registered, so the plot can be refreshed when needed.Parameters: initialState – If provided, must be a dictof{ overlay : bool }mappings, specifying the initial value of theDataSeries.enabledproperty for newly created instances. If not provided, only the data series for the currently selected overlay (if it has been newly added) is initially enabled.
-
toggleOverlayList()¶ Shows/hides an
OverlayListPanel. SeeViewPanel.togglePanel().
-
togglePlotList(floatPane=False)¶ Shows/hides a
PlotListPanel. SeeViewPanel.togglePanel().
- Implement the