def handle_new_stream(self, run, stream_name):
        if stream_name != "primary":
            # Nothing to do for this stream.
            return

        # Look for the scan_type='dichro' hint
        scan_type = run.metadata["start"]["hints"].get('scan_type', None)
        if scan_type != 'dichro':
            return

        # Detect x variable from hints in metadata.
        first_scan_dimension = run.metadata["start"]["hints"]["dimensions"][0]
        scanning_fields, _ = first_scan_dimension
        x = scanning_fields[0]

        # Collect monitor and detector
        if "monitor" in run.metadata["start"]["hints"].keys():
            self._monitor = run.metadata["start"]["hints"]["monitor"]
        # TODO: This will only use the first detector, but maybe could
        # expand to all detectors.
        if "detectors" in run.metadata["start"]["hints"].keys():
            self._detector = run.metadata["start"]["hints"]["detectors"][0]

        # If we already have a figure for this x, reuse it (over-plot).
        try:
            (xanes_lines, xmcd_lines) = self._x_to_lines[x]
        except KeyError:
            # We don't have a figure for this x. Make one.
            xanes_axes = Axes(x_label=x, title="XANES")
            xmcd_axes = Axes(x_label=x, title="XMCD")
            figure = Figure((xanes_axes, xmcd_axes), title="XANES and XMCD")
            # Set up objects that will select the approriate data and do the
            # desired transformation for plotting.
            xanes_lines = Lines(
                x=lambda primary: downsampled(primary[x]),
                ys=[lambda primary: xanes(primary[self._monitor],
                                          primary[self._detector],
                                          self.fluo)],
                axes=xanes_axes,
            )
            xmcd_lines = Lines(
                x=lambda primary: downsampled(primary[x]),
                ys=[lambda primary: xmcd(primary[self._monitor],
                                         primary[self._detector],
                                         self.fluo)],
                axes=xmcd_axes,
            )
            self._x_to_lines[x] = (xanes_lines, xmcd_lines)
            # Keep track of these plot builders to enable *removing* runs from
            # them.
            self.plot_builders.append(xanes_lines)
            self.plot_builders.append(xmcd_lines)
            # Appending this figures list will trigger to view to show our new
            # figure.
            self.figures.append(figure)
        # Add this Run to the figure.
        xanes_lines.add_run(run)
        xmcd_lines.add_run(run)
Example #2
0
    def _on_new_button_clicked(self):
        axes = Axes()
        figure = Figure((axes, ), title="")
        line = Lines(x=self.x_selector.currentText(),
                     ys=[self.y_selector.currentText()],
                     axes=axes,
                     max_runs=3)

        if self.model.search.active_run:
            line.add_run(self.model.search.active_run)

        self.model.auto_plot_builder.plot_builders.append(line)
        self.model.auto_plot_builder.figures.append(figure)
In [1]: %run -m bluesky_widgets.examples.ipy_qt_figure
"""
from bluesky import RunEngine
from bluesky.plans import scan
from ophyd.sim import motor, det

from bluesky_widgets.utils.streaming import stream_documents_into_runs
from bluesky_widgets.models.plot_builders import Lines
from bluesky_widgets.qt.figures import QtFigure
from bluesky_widgets.examples.utils.generate_msgpack_data import get_catalog

model = Lines("motor", ["det"], max_runs=3)
view = QtFigure(model.figure)
view.show()

RE = RunEngine()
RE.subscribe(stream_documents_into_runs(model.add_run))

catalog = get_catalog()
scans = catalog.search({"plan_name": "scan"})
model.add_run(scans[-1], pinned=True)


def plan():
    for i in range(1, 5):
        yield from scan([det], motor, -1, 1, 1 + 2 * i)


RE(plan())