Fits and Plots¶
pswalker
uses callbacks to monitor live data, create fits and pipe this
back into scanning logic in order to reach desired positions. These all build
off of LiveBuild
in order to be used with the fitwalk()
. The
most prominent example of this in the repository is walk_to_pixel()
LiveBuild¶
-
class
pswalker.callbacks.
LiveBuild
(model, y, independent_vars, init_guess=None, update_every=1, filters=None, drop_missing=True, average=1)¶ Bases:
bluesky.callbacks.fitting.LiveFit
Base class for live model building in Skywalker
Parameters: - model (lmfit.Model) –
- y (string) – Key of dependent variable
- indpendent_vars (dict) – Map independent variables names to keys in the event document stream
- init_guess (dict, optional) – Initial guesses for other values if expected
- update_every (int or None, optional) – Update rate of the model. If set to None, the model will only be computed at the end of the run. By default, this is set to 1 i.e update on every new event
-
backsolve
(target, **kwargs)¶ Use the most recent fit to find the independent variables that create the requested dependent variable
..note:
For multivariable functions the user may have to specify which variable to solve for, and which to keep fixed
-
eval
(*args, **kwargs)¶ Estimate a point based on the current fit of the model. Reimplemented by subclasses
-
field_names
¶ Name of all the keys associated with the fit
-
install_filters
(filters)¶ Install additional filters
Parameters: filters (dict) – Filters are provided in a dictionary of key / callable pairs that take a single input from the data stream and return a boolean value.
-
name
¶ Name of the model
-
class
pswalker.callbacks.
LinearFit
(y, x, init_guess=None, update_every=1, name=None, average=1)¶ Bases:
pswalker.callbacks.LiveBuild
Model to fit a linear relationship between a single variable axis and a depended variable
Parameters: - y (str) – Keyword in the event document that reports the dependent variable
- x (str) – Keyword in the event document that reports the independent variable
- init_guess (dict, optional) – Initialization guess for the linear fit, available keys are
slope
andintercept
- name (optional , str) – Name for the contained model. When None (default) the name is the same as the model function
- update_every (int or None, optional) – Update rate of the model. If set to None, the model will only be computed at the end of the run. By default, this is set to 1 i.e update on every new event
-
backsolve
(target, **kwargs)¶ Find the
x
position that solves the reaches the given targetParameters: target (float) – Desired y
valueReturns: x – Variable name and floating value Return type: dict
-
eval
(**kwargs)¶ Evaluate the predicted outcome based on the most recent fit of the given information.
Parameters: - x (float or int, optional) – Independent variable to evaluate linear model
- kwargs – The value for the indepenedent variable can also be given as the field name in the event document
Returns: estimate – Y value as determined by current linear fit
Return type: float
Plots¶
-
class
pswalker.callbacks.
LivePlotWithGoal
(y, x=None, *, goal=0.0, tolerance=0.0, averages=None, **kwargs)¶ Bases:
bluesky.callbacks.mpl_plotting.LivePlot
Build a function that updates a plot from a stream of Events.
Parameters: - y (str) – the name of a data field in an Event
- x (str, optional) – the name of a data field in an Event, or ‘seq_num’ or ‘time’ If None, use the Event’s sequence number. Special case: If the Event’s data includes a key named ‘seq_num’ or ‘time’, that takes precedence over the standard ‘seq_num’ and ‘time’ recorded in every Event.
- goal (float) – the target pixel
- tolerance (float, optional) – the tolerance for the pixel
- averages (float, optional) – The number of images to average. If None is specified, every point is rendered as they come,
otherwise the graph will update every
`averages`
points. - legend_keys (list, optional) – The list of keys to extract from the RunStart document and format in the legend of the plot. The legend will always show the scan_id followed by a colon (“1: “). Each
- xlim (tuple, optional) – passed to Axes.set_xlim
- ylim (tuple, optional) – passed to Axes.set_ylim
- ax (Axes, optional) – matplotib Axes; if none specified, new figure and axes are made.
- fig (Figure, optional) – deprecated: use ax instead
- epoch ({'run', 'unix'}, optional) – If ‘run’ t=0 is the time recorded in the RunStart document. If ‘unix’, t=0 is 1 Jan 1970 (“the UNIX epoch”). Default is ‘run’.
- kwargs –
- additional keyword arguments are passed through to Axes.plot. (All) –
Notes
If your figure blocks the main thread when you are trying to scan with this callback, call plt.ion() in your IPython session.
Examples
>>> my_plotter = LivePlotWithGoals('det', 'motor', goal=10.0, tolerance=1.5, averages=None, legend_keys=['sample']) >>> RE(my_scan, my_plotter)