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)[source]¶ 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:Prompts the user to select a file name, then saves a screenshot of the current plot.
Imports data series from a text file.
Exports displayed data series to a text file.
-
dataSeries= <MagicMock name='mock.List()' id='281473495788224'>¶ 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='281473495788224'>¶ This list contains any
matplotlib.Artistinstances which are plotted every call todrawArtists().
-
legend= <MagicMock name='mock.Boolean()' id='281473497326640'>¶ If
True, a legend is added to the plot, with an entry for everyDataSeriesinstance in thedataSerieslist.
-
xAutoScale= <MagicMock name='mock.Boolean()' id='281473497326640'>¶ If
True, the plotlimitsfor the X axis are automatically updated to fit all plotted data.
-
yAutoScale= <MagicMock name='mock.Boolean()' id='281473497326640'>¶ If
True, the plotlimitsfor the Y axis are automatically updated to fit all plotted data.
-
xLogScale= <MagicMock name='mock.Boolean()' id='281473497326640'>¶ Toggle a \(log_{10}\) x axis scale.
-
yLogScale= <MagicMock name='mock.Boolean()' id='281473497326640'>¶ Toggle a \(log_{10}\) y axis scale.
-
invertX= <MagicMock name='mock.Boolean()' id='281473497326640'>¶ Invert the plot along the X axis.
-
invertY= <MagicMock name='mock.Boolean()' id='281473497326640'>¶ Invert the plot along the Y axis.
-
xScale= <MagicMock name='mock.Real()' id='281473497926576'>¶ Scale to apply to the X axis data.
-
yScale= <MagicMock name='mock.Real()' id='281473497926576'>¶ Scale to apply to the Y axis data.
-
xOffset= <MagicMock name='mock.Real()' id='281473497926576'>¶ Offset to apply to the X axis data.
-
yOffset= <MagicMock name='mock.Real()' id='281473497926576'>¶ Offset to apply to the Y axis data.
-
ticks= <MagicMock name='mock.Boolean()' id='281473497326640'>¶ Toggle axis ticks and tick labels on/off.
-
grid= <MagicMock name='mock.Boolean()' id='281473497326640'>¶ Toggle an axis grid on/off.
-
bgColour= <MagicMock name='mock.Colour()' id='281473498033216'>¶ Plot background colour.
-
smooth= <MagicMock name='mock.Boolean()' id='281473497326640'>¶ If
Trueall plotted data is up-sampled, and smoothed using spline interpolation.
-
xlabel= <MagicMock name='mock.String()' id='281473498257632'>¶ A label to show on the x axis.
-
ylabel= <MagicMock name='mock.String()' id='281473498257632'>¶ A label to show on the y axis.
-
limits= <MagicMock name='mock.Bounds()' id='281473494326624'>¶ The x/y axis limits. If
xAutoScaleandyAutoScaleareTrue, these limit values are automatically updated on every call todrawDataSeries().
-
__init__(parent, overlayList, displayCtx, frame)[source]¶ Create a
PlotPanel.- Parameters
parent – The
wxparent object.overlayList – An
OverlayListinstance.displayCtx – A
DisplayContextinstance.frame – The
FSLeyesFrameinstance.
-
draw(*a)[source]¶ 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)[source]¶ 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()[source]¶ Removes some property listeners, and then calls
ViewPanel.destroy().
-
screenshot(*a)[source]¶ Prompts the user to select a file name, then saves a screenshot of the current plot.
See the
ScreenshotAction.
-
importDataSeries(*a)[source]¶ Imports data series from a text file.
See the
ImportDataSeriesAction.
-
exportDataSeries(*args, **kwargs)[source]¶ Exports displayed data series to a text file.
See the
ExportDataSeriesAction.
-
message(msg, clear=True, border=False)[source]¶ Displays the given message in the centre of the figure.
This is a convenience method provided for use by subclasses.
-
getArtist(ds)[source]¶ Returns the
matplotlib.Artist(typically aLine2Dinstance) associated with the givenDataSeriesinstance. AKeyErroris raised if there is no such artist.
-
getDrawnDataSeries()[source]¶ Returns a list of tuples, each tuple containing the
(DataSeries, x, y)data for oneDataSeriesinstance as it is shown on the plot.
-
prepareDataSeries(ds)[source]¶ 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)[source]¶ 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)[source]¶ 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).
-
_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.
-
_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.
-
__module__= 'fsleyes.views.plotpanel'¶
-
class
fsleyes.views.plotpanel.OverlayPlotPanel(*args, **kwargs)[source]¶ 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:Returns the
DataSeriesinstance associated with the specified overlay, orNoneif there is noDataSeriesinstance.Convenience method which returns a list of overlays which have
DataSeriesthat should be plotted.Destroys the internally cached
DataSeriesfor the given overlay.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.Every
DataSerieswhich is currently plotted, and has not been added to thePlotPanel.dataSerieslist, is added to said list.Removes 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:Shows/hides an
OverlayListPanel.Shows/hides a
PlotListPanel.Sub-classes
The
OverlayPlotPanelis the base class for:The
TimeSeriesPanelis anOverlayPlotPanelwhich plots time series data from overlays.An
OverlayPlotPanelwhich plots histograms fromImageoverlay data.The
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.See also
plotStyles.Sub-classes should use the
getOverlayPlotColour()and :meth:`getOverlayPlotStyle`methods to retrieve the initial colour and linestyle to use for a given overlay.
-
_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'¶
-
plotStyles= {}¶ This dictionary is used to store a collection of
{overlay : colour}mappings - it is used in conjunction withplotColours.
-
__init__(*args, **kwargs)[source]¶ 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__().
-
destroy()[source]¶ Must be called when this
OverlayPlotPanelis no longer needed. Removes some property listeners, and callsPlotPanel.destroy().
-
getDataSeriesToPlot()[source]¶ Convenience method which returns a list of overlays which have
DataSeriesthat should be plotted.
-
getDataSeries(overlay)[source]¶ Returns the
DataSeriesinstance associated with the specified overlay, orNoneif there is noDataSeriesinstance.
-
getOverlayPlotColour(overlay)[source]¶ 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.
-
getOverlayPlotStyle(overlay)[source]¶ Returns an initial line style to use for plots associated with the given overlay. If a colour is present in the
plotStylesdictionary, it is returned. Otherwise a line style is generated, added toplotStyles, and returned.The format of the returned line style is suitable for use with the
linestyleargument of thematplotlib``plot``functions.
-
addDataSeries()[source]¶ Every
DataSerieswhich is currently plotted, and has not been added to thePlotPanel.dataSerieslist, is added to said list.
-
removeDataSeries(*a)[source]¶ Removes the most recently added
DataSeriesfrom thisOverlayPlotPanel.
-
createDataSeries(overlay)[source]¶ 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, and theDataSeries.lineStyleproperty to that returned by thegetOverlayPlotStyle()methodDifferent
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.
-
clearDataSeries(overlay)[source]¶ Destroys the internally cached
DataSeriesfor the given overlay.
-
updateDataSeries(initialState=None)[source]¶ 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()[source]¶ Shows/hides an
OverlayListPanel. SeeViewPanel.togglePanel().
-
togglePlotList(floatPane=False)[source]¶ Shows/hides a
PlotListPanel. SeeViewPanel.togglePanel().