Ejemplo n.º 1
0
def configure_base(user_ns,
                   broker_name,
                   *,
                   bec=True,
                   epics_context=True,
                   magics=True,
                   mpl=True,
                   ophyd_logging=True,
                   pbar=True):
    """
    Perform base setup and instantiation of important objects.

    This factory function instantiates the following and adds them to the
    namespace:

    * ``RE`` -- a RunEngine
    * ``db`` -- a Broker (from "databroker"), subscribe to ``RE``
    * ``bec`` -- a BestEffortCallback, subscribed to ``RE``
    * ``peaks`` -- an alias for ``bec.peaks``
    * ``sd`` -- a SupplementalData preprocessor, added to ``RE.preprocessors``
    * ``pbar_maanger`` -- a ProgressBarManager, set as the ``RE.waiting_hook``

    And it performs some low-level configuration:

    * creates a context in ophyd's control layer (``ophyd.setup_ophyd()``)
    * turns out interactive plotting (``matplotlib.pyplot.ion()``)
    * bridges the RunEngine and Qt event loops
      (``bluesky.utils.install_kicker()``)
    * logs ERROR-level log message from ophyd to the standard out

    Parameters
    ----------
    user_ns: dict
        a namespace --- for example, ``get_ipython().user_ns``
    broker_name : Union[str, Broker]
        Name of databroker configuration or a Broker instance.
    bec : boolean, optional
        True by default. Set False to skip BestEffortCallback.
    epics_context : boolean, optional
        True by default. Set False to skip ``setup_ophyd()``.
    magics : boolean, optional
        True by default. Set False to skip registration of custom IPython
        magics.
    mpl : boolean, optional
        True by default. Set False to skip matplotlib ``ion()`` at event-loop
        bridging.
    ophyd_logging : boolean, optional
        True by default. Set False to skip ERROR-level log configuration for
        ophyd.
    pbar : boolean, optional
        True by default. Set false to skip ProgressBarManager.

    Returns
    -------
    names : list
        list of names added to the namespace

    Examples
    --------
    Configure IPython for CHX.

    >>>> configure_base(get_ipython().user_ns, 'chx');
    """
    ns = {}  # We will update user_ns with this at the end.

    # Test if we are in Jupyter or IPython:
    in_jupyter = user_ns['get_ipython']().has_trait('kernel')

    # Set up a RunEngine and use metadata backed by a sqlite file.
    from bluesky import RunEngine
    from bluesky.utils import get_history
    # if RunEngine already defined grab it
    # useful when users make their own custom RunEngine
    if 'RE' in user_ns:
        RE = user_ns['RE']
    else:
        RE = RunEngine(get_history())
        ns['RE'] = RE

    # Set up SupplementalData.
    # (This is a no-op until devices are added to it,
    # so there is no need to provide a 'skip_sd' switch.)
    from bluesky import SupplementalData
    sd = SupplementalData()
    RE.preprocessors.append(sd)
    ns['sd'] = sd

    if isinstance(broker_name, str):
        # Set up a Broker.
        from databroker import Broker
        db = Broker.named(broker_name)
        ns['db'] = db
    else:
        db = broker_name

    RE.subscribe(db.insert)

    if pbar and not in_jupyter:
        # Add a progress bar.
        from bluesky.utils import ProgressBarManager
        pbar_manager = ProgressBarManager()
        RE.waiting_hook = pbar_manager
        ns['pbar_manager'] = pbar_manager

    if magics:
        # Register bluesky IPython magics.
        from bluesky.magics import BlueskyMagics
        get_ipython().register_magics(BlueskyMagics)

    if bec:
        # Set up the BestEffortCallback.
        from bluesky.callbacks.best_effort import BestEffortCallback
        _bec = BestEffortCallback()
        bec = _bec
        RE.subscribe(_bec)
        if in_jupyter:
            _bec.disable_plots()
        ns['bec'] = _bec
        ns['peaks'] = _bec.peaks  # just as alias for less typing

    if mpl:
        # Import matplotlib and put it in interactive mode.
        import matplotlib.pyplot as plt
        ns['plt'] = plt
        plt.ion()

        # Commented to allow more intelligent setting of kickers (for Jupyter and IPython):
        ## Make plots update live while scans run.
        # from bluesky.utils import install_kicker
        # install_kicker()

        # Make plots update live while scans run.
        if in_jupyter:
            from bluesky.utils import install_nb_kicker
            install_nb_kicker()
        else:
            from bluesky.utils import install_qt_kicker
            install_qt_kicker()

    if not ophyd_logging:
        # Turn on error-level logging, particularly useful for knowing when
        # pyepics callbacks fail.
        import logging
        import ophyd.ophydobj
        ch = logging.StreamHandler()
        ch.setLevel(logging.ERROR)
        ophyd.ophydobj.logger.addHandler(ch)

    # convenience imports
    # some of the * imports are for 'back-compatibility' of a sort -- we have
    # taught BL staff to expect LiveTable and LivePlot etc. to be in their
    # namespace
    import numpy as np
    ns['np'] = np

    import bluesky.callbacks
    ns['bc'] = bluesky.callbacks
    import_star(bluesky.callbacks, ns)

    import bluesky.plans
    ns['bp'] = bluesky.plans
    import_star(bluesky.plans, ns)

    import bluesky.plan_stubs
    ns['bps'] = bluesky.plan_stubs
    import_star(bluesky.plan_stubs, ns)
    # special-case the commonly-used mv / mvr and its aliases mov / movr4
    ns['mv'] = bluesky.plan_stubs.mv
    ns['mvr'] = bluesky.plan_stubs.mvr
    ns['mov'] = bluesky.plan_stubs.mov
    ns['movr'] = bluesky.plan_stubs.movr

    import bluesky.preprocessors
    ns['bpp'] = bluesky.preprocessors

    import bluesky.callbacks.broker
    import_star(bluesky.callbacks.broker, ns)
    import bluesky.simulators
    import_star(bluesky.simulators, ns)

    user_ns.update(ns)
    return list(ns)
Ejemplo n.º 2
0
dirname = os.path.expanduser('~/.data-cache/')
mds = MDS({'directory': dirname,
            'timezone': tzlocal.get_localzone().zone})
fs = FileStore({'dbpath': os.path.join(dirname, 'filestore.db')})
db = Broker(mds, fs)
del dirname, mds, fs, MDS, FileStore, Broker, tzlocal  # clean up namespace

from bluesky.global_state import gs
RE = gs.RE  # alias for convenience

# Subscribe metadatastore to documents.
# If this is removed, data is not saved to metadatastore.
gs.RE.subscribe('all', db.mds.insert)

# Import matplotlib and put it in interactive mode.
import matplotlib.pyplot as plt
plt.ion()

# Make plots live-update while scans run.
from bluesky.utils import install_nb_kicker
install_nb_kicker()
del install_nb_kicker

# For convenience, import some commonly-used functions and modules.
from bluesky.callbacks import *
from bluesky.spec_api import *
import bluesky.plans as bp
import time
import os
Ejemplo n.º 3
0
mds = MDS({"directory": dirname, "timezone": tzlocal.get_localzone().zone})
fs = FileStore({"dbpath": os.path.join(dirname, "filestore.db")})
db = Broker(mds, fs)
del dirname, mds, fs, MDS, FileStore, Broker, tzlocal  # clean up namespace

from bluesky.global_state import gs

RE = gs.RE  # alias for convenience

# Subscribe metadatastore to documents.
# If this is removed, data is not saved to metadatastore.
gs.RE.subscribe("all", db.mds.insert)

# Import matplotlib and put it in interactive mode.
import matplotlib.pyplot as plt

plt.ion()

# Make plots live-update while scans run.
from bluesky.utils import install_nb_kicker

install_nb_kicker()
del install_nb_kicker

# For convenience, import some commonly-used functions and modules.
from bluesky.callbacks import *
from bluesky.spec_api import *
import bluesky.plans as bp
import time
import os