1D plugins¶
Adding 1D plugins¶
Overview¶
A 1D plugin is a module that can be added to the PyMca 1D window in order to perform user defined operations of the plotted 1D data.
Plugins can be automatically installed provided they are in the appropriate place:
- In the user home directory (POSIX systems): ${HOME}/.pymca/plugins or ${HOME}/PyMca5/plugins (older PyMca installation)
- In “My Documents\PyMca\plugins” (Windows)
A plugin inherit the Plugin1DBase
class and implement the methods:
Plugin1DBase.getMethods()
Plugin1DBase.getMethodToolTip()
(optional but convenient)Plugin1DBase.getMethodPixmap()
(optional)Plugin1DBase.applyMethod()
and modify the static module variable MENU_TEXT
and the static module function
getPlugin1DInstance()
according to the defined plugin.
These plugins will be compatible with any 1D-plot window that implements the Plot1D interface. The plot window interface is described in the Plot1DBase class.
The main items are reproduced here and can be directly accessed as plugin methods.
Plugin1DBase.addCurve()
Plugin1DBase.getActiveCurve()
Plugin1DBase.getAllCurves()
Plugin1DBase.getGraphXLimits()
Plugin1DBase.getGraphYLimits()
Plugin1DBase.getGraphTitle()
Plugin1DBase.getGraphXLabel()
Plugin1DBase.getGraphYLabel()
Plugin1DBase.removeCurve()
Plugin1DBase.setActiveCurve()
Plugin1DBase.setGraphTitle()
Plugin1DBase.setGraphXLimits()
Plugin1DBase.setGraphYLimits()
Plugin1DBase.setGraphXLabel()
Plugin1DBase.setGraphYLabel()
A simple plugin example, normalizing each curve to its maximum and vertically shifting the curves.
from PyMca5 import Plugin1DBase
class Shifting(Plugin1DBase.Plugin1DBase):
def getMethods(self, plottype=None):
return ["Shift"]
def getMethodToolTip(self, methodName):
if methodName != "Shift":
raise InvalidArgument("Method %s not valid" % methodName)
return "Subtract minimum, normalize to maximum, and shift up by 0.1"
def applyMethod(self, methodName):
if methodName != "Shift":
raise ValueError("Method %s not valid" % methodName)
allCurves = self.getAllCurves()
increment = 0.1
for i in range(len(allCurves)):
x, y, legend, info = allCurves[i][:4]
delta = float(y.max() - y.min())
if delta < 1.0e-15:
delta = 1.0
y = (y - y.min())/delta + i * increment
if i == (len(allCurves) - 1):
replot = True
else:
replot = False
if i == 0:
replace = True
else:
replace = False
self.addCurve(x, y, legend=legend + " %.2f" % (i * increment),
info=info, replace=replace, replot=replot)
MENU_TEXT="Simple Shift Example"
def getPlugin1DInstance(plotWindow, **kw):
ob = Shifting(plotWindow)
return ob
1D plugin API¶
-
class
PyMca5.PyMcaCore.Plugin1DBase.
Plugin1DBase
(plotWindow, **kw)[source]¶ -
addCurve
(x, y, legend=None, info=None, replace=False, replot=True, **kw)[source]¶ Add the 1D curve given by x an y to the graph.
Parameters: - x (list or numpy.ndarray) – The data corresponding to the x axis
- y (list or numpy.ndarray) – The data corresponding to the y axis
- legend (string or None) – The legend to be associated to the curve
- info (dict or None) – Dictionary of information associated to the curve
- replace (boolean default False) – Flag to indicate if already existing curves are to be deleted
- replot (boolean default True) – Flag to indicate plot is to be immediately updated
- kw – Additional keywords recognized by the plot window. Beware that the keywords recognized by silx and PyMca plot windows may differ.
-
getActiveCurve
(just_legend=False)[source]¶ Parameters: just_legend (boolean) – Flag to specify the type of output required Returns: legend of the active curve or list [x, y, legend, info]
Return type: string or list Function to access the graph currently active curve. It returns None in case of not having an active curve.
Default output has the form:
xvalues, yvalues, legend, dict
where dict is a dictionary containing curve info. For the time being, only the plot labels associated to the curve are warranted to be present under the keys xlabel, ylabel.
If just_legend is True:
The legend of the active curve (or None) is returned.
-
getAllCurves
(just_legend=False)[source]¶ Parameters: just_legend (boolean) – Flag to specify the type of output required Returns: legend of the curves or list [[x, y, legend, info], ...]
Return type: list of strings or list of curves It returns an empty list in case of not having any curve.
If just_legend is False, it returns a list of the form:
[[xvalues0, yvalues0, legend0, dict0], [xvalues1, yvalues1, legend1, dict1], [...], [xvaluesn, yvaluesn, legendn, dictn]]
If just_legend is True, it returns a list of the form:
[legend0, legend1, ..., legendn]
-
getGraphYLimits
()[source]¶ Get the graph Y (left) limits.
Returns: Two floats with the Y (left) axis limits
-
getMethodPixmap
(name)[source]¶ Parameters: name – The method for which a pixmap is asked Return type: QPixmap or None
-
getMethodToolTip
(name)[source]¶ Returns the help associated to the particular method name or None.
Parameters: name – The method for which a tooltip is asked Return type: string
-
getMethods
(plottype=None)[source]¶ Parameters: plottype – string or None for the case the plugin only support one type of plots. Implemented values “SCAN”, “MCA” or None Returns: A list with the NAMES associated to the callable methods that are applicable to the specified type plot. The list can be empty. Return type: list[string]
-
getMonotonicCurves
()[source]¶ Convenience method that calls
getAllCurves()
and makes sure that all of the X values are strictly increasing.It returns a list of the form:
[[xvalues0, yvalues0, legend0, dict0], [xvalues1, yvalues1, legend1, dict1], [...], [xvaluesn, yvaluesn, legendn, dictn]]
-
removeCurve
(legend, replot=True)[source]¶ Remove the curve associated to the supplied legend from the graph. The graph will be updated if replot is true.
Parameters: - legend (string or None) – The legend associated to the curve to be deleted
- replot (boolean default True) – Flag to indicate plot is to be immediately updated
-
setActiveCurve
(legend)[source]¶ Funtion to request the plot window to set the curve with the specified legend as the active curve.
Parameters: legend (string) – The legend associated to the curve
-
-
PyMca5.PyMcaCore.Plugin1DBase.
getPlugin1DInstance
(plotWindow, **kw)[source]¶ This function will be called by the plot window instantiating and calling the plugins. It passes itself as first argument, but the default implementation of the base class only keeps a weak reference to prevent circular references.
Builtin 1D plugins¶
Background subtraction tools¶
This plugin provides 3 methods:
- subtract a SNIP1D background from the active curve
- apply a Savitsky-Golay filter on the active curve
- smooth and replace current curve by its SNIP1D background (deglitch)
Median filter average¶
This plugin provides methods to replace curves by their median filter average. 3-, 5-, 7- or 9-points filters are provided. The filter can be applied on the data in its original order, or in a randomized order.
Simple vertical shift¶
This plugin replaces all curves with a normalized and shifted curve. The minimum is subtracted, than the data is normalized to the maximum, and finally it is shifted up by i*0.1 (i being the curve index).
Alignment plugin¶
This plugin aligns all curves with the active curve, using the FFT.
Remove glitches from curves¶
This plugin uses a median filter smoothing to remove glitches from curves. A configuration widget can be used to configure the median filter (width and threshold), and the user can choose to apply it on the active curve or on all curves.
Built-in Math¶
This plugin provide simple math functions, to derivate or invert the active curve.
Normalization¶
This plugin provides methods to normalize all curves.
Following normalization methods are available:
- Normalize to maximum y / max(y)
- Subtract offset and normalize to new maximum (y-min(y))/(max(y)-min(y))
- Subtract offset and normalize to integrated area (y-min(y))/trapz(max(y)-min(y),x)
- Subtract offset and normalize to counts (y-min(y))/sum(max(y)-min(y))
- Divide all curves by active curve
Fit all curves¶
This plugin allows to perform a fit on all curves in the plot. A widget is provided to configure the fit parameters and to specify the output file. The fit results are saved as a NeXus HDF5 file, with one entry per fitted curve.
Motor Info¶
This plugin opens a widget displaying values of various motors associated with each spectrum, if the curve originates from a file whose format provides this information.