ibex_bluesky_core.callbacks

ISIS-specific bluesky callbacks.

Submodules

reflectometry

Reflectometry-specific callbacks.

Members

ISISCallbacks

A collection of ISIS standard callbacks.

class ibex_bluesky_core.callbacks.CentreOfMass(x: str, y: str)[source]

Bases: CollectThenCompute

Compute centre of mass after a run finishes.

This callback calculates the centre of mass of the 2D region bounded by min(y), min(x), max(x), and straight-line segments joining (x, y) data points with their nearest neighbours along the x axis.

Parameters:
  • x – Name of independent variable in event data

  • y – Name of dependent variable in event data

property result: float | None

The centre-of-mass calculated by this callback.

The returned position is a float along the x-axis, or None if no centre of mass has been calculated (for example if the bluesky run has not yet finished).

class ibex_bluesky_core.callbacks.ChainedLiveFit(method: FitMethod, y: list[str], x: str, *, yerr: list[str] | None = None, ax: list[Axes] | None = None)[source]

Bases: CallbackBase

Chained live fitting manages a series of sequential fits.

This callback manages multiple LiveFit instances, where the parameters from each completed fit serve as the initial guesses for the next fit.

Optional plotting is built in, using LiveFitPlot callbacks.

The LiveFit and LiveFitPlot callbacks should not be subscribed directly, but rather only via this callback.

Parameters:
  • methodFitMethod instance for fitting

  • y – List of y-axis variable names

  • x – x-axis variable name

  • yerr – Optional list of error values corresponding to y variables

  • ax – A list of Axes to plot fits on to. Creates LiveFitPlot instances.

property live_fit_plots: list[LiveFitPlot]

Return a list of the LiveFitPlot instances used by this callback.

property live_fits: list[LiveFit]

Return a list of the LiveFit instances used by this callback.

class ibex_bluesky_core.callbacks.DocLoggingCallback[source]

Bases: object

Log bluesky documents to line-delimited JSON files.

Tip

This callback is automatically subscribed to the RunEngine by get_run_engine. Manually creating this callback is unnecessary.

class ibex_bluesky_core.callbacks.HumanReadableFileCallback(fields: list[str], *, output_dir: Path | None, postfix: str = '')[source]

Bases: CallbackBase

Write human-readable files for each bluesky run.

If fields are specified, just output those, otherwise output all hinted signals.

Parameters:
  • fields – a list of field names to include in output files

  • output_dir – file path into which to write output files

  • postfix – optional postfix to append to output file names

class ibex_bluesky_core.callbacks.ISISCallbacks(*, x: str, y: str, yerr: str | None = None, measured_fields: list[str] | None = None, add_table_cb: bool = True, fields_for_live_table: list[str] | None = None, add_human_readable_file_cb: bool = True, fields_for_hr_file: list[str] | None = None, human_readable_file_output_dir: str | PathLike[str] | None = None, add_plot_cb: bool = True, ax: Axes | None = None, fit: FitMethod | None = None, show_fit_on_plot: bool = True, add_peak_stats: bool = True, add_centre_of_mass: bool = True, add_live_fit_logger: bool = True, live_fit_logger_output_dir: str | PathLike[str] | None = None, live_fit_logger_postfix: str = '', human_readable_file_postfix: str = '', save_plot_to_png: bool = True, plot_png_output_dir: str | PathLike[str] | None = None, plot_png_postfix: str = '', live_fit_update_every: int | None = 1, live_plot_update_on_every_event: bool = True)[source]

Bases: object

A collection of ISIS standard callbacks.

This callback collection represents a common set of callbacks used for many scans across ISIS instruments, which are bundled together in this callback collection for convenience. However, for fine-grained control over the exact set of callbacks to be used, individual callbacks may be more appropriate.

By default, the following callbacks are included and enabled:

Results can be accessed from the live_fit, com and peak_stats properties.

This can be defined in a plan and then as a decorator if results are needed:

def dae_scan_plan():
    ...
    icc = ISISCallbacks(
        x=block.name,
        y=reducer.intensity.name,
        yerr=reducer.intensity_stddev.name,
        fit=Linear.fit(),
        ...
    )
    ...

    @icc
    def _inner():
        yield from ...
        ...
        print(icc.live_fit.result.fit_report())
        print(f"COM: {icc.peak_stats['com']}")
Parameters:
  • x – The signal name to use for X within plots and fits.

  • y – The signal name to use for Y within plots and fits.

  • yerr – The signal name to use for the Y uncertainty within plots and fits.

  • measured_fields – the fields to use for both the live table and human-readable file.

  • add_table_cb – whether to add a table callback.

  • fields_for_live_table – the fields to measure for the live table (in addition to measured_fields).

  • add_human_readable_file_cb – whether to add a human-readable file callback.

  • fields_for_hr_file – the fields to measure for the human-readable file (in addition to measured_fields).

  • human_readable_file_output_dir – the output directory for human-readable files. can be blank and will default.

  • add_plot_cb – whether to add a plot callback.

  • ax – An optional axes object to use for plotting.

  • fit – The fit method to use when fitting.

  • show_fit_on_plot – whether to show fit on plot.

  • add_peak_stats – whether to add a peak stats callback.

  • add_centre_of_mass – whether to add a centre of mass callback.

  • add_live_fit_logger – whether to add a live fit logger.

  • live_fit_logger_output_dir – the output directory for live fit logger.

  • live_fit_logger_postfix – the postfix to add to live fit logger.

  • human_readable_file_postfix – optional postfix to add to human-readable file logger.

  • save_plot_to_png – whether to save the plot to a PNG file.

  • plot_png_output_dir – the output directory for plotting PNG files.

  • plot_png_postfix – the postfix to add to PNG plot files.

  • live_fit_update_every – How often, in points, to recompute the fit. If None, do not compute until the end.

  • live_plot_update_on_every_event – whether to show the live plot on every event, or just at the end.

property com: CentreOfMass

The CentreOfMass callback.

property live_fit: LiveFit

The live fit callback, containing fitting results.

property peak_stats: PeakStats

The peak stats callback, containing simple peak statistics such as min/max.

property subs: list[CallbackBase]

The list of all subscribed callbacks.

class ibex_bluesky_core.callbacks.LiveFit(method: FitMethod, y: str, x: str, *, update_every: int | None = 1, yerr: str | None = None)[source]

Bases: LiveFit

bluesky.callbacks.LiveFit, with support for uncertainties and dynamic guesses.

This callback extends the functionality of bluesky.callbacks.LiveFit by adding:

  • Support for weighting fits by uncertainties. The weights are passed through to the underlying lmfit.model.Model.fit routine as 1/stddev, if provided.

  • Support for dynamic fit guesses, as performed by the models defined in ibex_bluesky_core.fitting.

Parameters:
  • method (FitMethod) – The FitMethod (Model & Guess) to use when fitting.

  • y (str) – The name of the dependent variable.

  • x (str) – The name of the independent variable.

  • update_every (int, optional) – How often, in points, to update the fit.

  • yerr (str or None, optional) – Name of field in the Event document that provides standard deviation for each Y value. None meaning do not use uncertainties in fit.

class ibex_bluesky_core.callbacks.LiveFitLogger(livefit: LiveFit, y: str, x: str, postfix: str, output_dir: str | PathLike[str] | None, yerr: str | None = None)[source]

Bases: CallbackBase

Write data files containing the results of a fit.

Parameters:
  • livefit (LiveFit) – A reference to LiveFit callback to collect fit info from.

  • y (str) – The name of the signal pointing to y counts data.

  • x (str) – The name of the signal pointing to x counts data.

  • output_dir (str) – A path to where the fitting file should be stored.

  • postfix (str) – A small string that should be placed at the end of the filename to disambiguate multiple fits and avoid overwriting.

  • yerr (str) – The name of the signal pointing to y count uncertainties data.

class ibex_bluesky_core.callbacks.LivePColorMesh(*, y: str, x: str, x_coord: ndarray[tuple[Any, ...], dtype[float64]], ax: Axes, x_name: str | None = None, **kwargs: Any)[source]

Bases: QtAwareCallback

Live PColorMesh-based heatmap.

This callback displays one row of data in the heatmap per scan point. The x signal is therefore expected to contain array data, which should be of the same length for every measurement.

Parameters:
  • y – the name of the signal appearing along the y-axis.

  • x – the name of the signal appearing along the x-axis. This signal is expected to be an array, representing rows of the heatmap.

  • x_coord – coordinates along the x-axis. This is expected to have the same length as each row of the heatmap.

  • ax – a set of Axes on which to plot.

  • x_name – A display name for the x-axis. Defaults to the same as x if not provided.

  • **kwargs – Arbitrary keyword arguments are passed through to matplotlib.pyplot.pcolormesh

class ibex_bluesky_core.callbacks.LivePlot(y: str, x: str | None = None, yerr: str | None = None, *args: Any, update_on_every_event: bool = True, **kwargs: Any)[source]

Bases: LivePlot

bluesky.callbacks.mpl_plotting.LivePlot with support for uncertainties.

This callback is an extension of bluesky.callbacks.mpl_plotting.LivePlot with the following additional features:

  • Support for rendering uncertainties as error-bars on the plot.

  • Support for automatically calling matplotlib.pyplot.show when needed, if using the IBEX matplotlib backend.

Parameters:
class ibex_bluesky_core.callbacks.PlotPNGSaver(x: str, y: str, ax: Axes, postfix: str, output_dir: str | PathLike[str] | None = None)[source]

Bases: QtAwareCallback

Save plots to PNG files on a run end.

Parameters:
  • x – The name of the signal for x.

  • y – The name of the signal for y.

  • ax – The subplot to save to a file.

  • postfix – The file postfix.

  • output_dir – The output directory for PNGs.

ibex_bluesky_core.callbacks.show_plot() None[source]

Call matplotlib.pyplot.show if IBEX matplotlib backend is in use.

If a different matplotlib backend is in use, do nothing.