Hello Bluesky: Reading detectors and scanning#

In this notebook you will:

  • Connect to some simulated hardware.

  • Acquire some data via two common experimental procedures (“plans”), count and scan.

  • Write a custom plan.

Recommend Prerequisites:

Configuration#

Below, we will connect to EPICS IOC(s) controlling simulated hardware in lieu of actual motors and detectors. An EPICS IOC is control system software that allows communication with a wide variety of hardware using a common interface. The IOCs should already be running in the background. Run this command to verify that they are running: it should produce output with RUNNING on each line. In the event of a problem, edit this command to replace status with restart all and run again.

!supervisor/start_supervisor.sh status
decay                            RUNNING   pid 2695, uptime 0:01:00
mini_beamline                    RUNNING   pid 2696, uptime 0:01:00
random_walk                      RUNNING   pid 2697, uptime 0:01:00
random_walk_horiz                RUNNING   pid 2698, uptime 0:01:00
random_walk_vert                 RUNNING   pid 2699, uptime 0:01:00
simple                           RUNNING   pid 2700, uptime 0:01:00
thermo_sim                       RUNNING   pid 2701, uptime 0:01:00
trigger_with_pc                  RUNNING   pid 2702, uptime 0:01:00
from bluesky_tutorial_utils.beamline_configuration import *
OBJECT CACHE: Will use up to 1_092_190_617 bytes (15% of total physical RAM)
INFO:tiled.server.object_cache:Will use up to 1_092_190_617 bytes (15% of total physical RAM)

Check that we can communicate with the hardware. If this doesn’t raise an error, it worked.

det.wait_for_connection()

Data Acquisition#

Executing a count plan with various parameters#

In the example below, the Bluesky run engine is the interpreter of experiment plans and count is an experiment plan used here to acquire one reading from a point detector.

from bluesky.plans import count
RE(count([det]))
Transient Scan ID: 1     Time: 2023-04-22 21:09:18

Persistent Unique Scan ID: '634b5760-fde8-4a41-990d-8a72de2bf011'

WARNING:matplotlib.legend:No artists with labels found to put in legend.  Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
New stream: 'primary'

+-----------+------------+------------+

|   seq_num |       time |     ph_det |

+-----------+------------+------------+

|         1 | 21:09:19.1 |     100891 |

+-----------+------------+------------+

generator count ['634b5760'] (scan num: 1)




('634b5760-fde8-4a41-990d-8a72de2bf011',)

The return value is a list of the run IDs that uniquely identify this data set. The “scan num” is easier to remember but is not good for long-term reference because it may not be unique.

Let’s looks at the documentation for count to see what our other options are.

help(count)  # or, equiavently, type count? or ?count
Help on function count in module bluesky.plans:

count(detectors, num=1, delay=None, *, per_shot=None, md=None)
    Take one or more readings from detectors.
    
    Parameters
    ----------
    detectors : list
        list of 'readable' objects
    num : integer, optional
        number of readings to take; default is 1
    
        If None, capture data until canceled
    delay : iterable or scalar, optional
        Time delay in seconds between successive readings; default is 0.
    per_shot : callable, optional
        hook for customizing action of inner loop (messages per step)
        Expected signature ::
    
           def f(detectors: Iterable[OphydObj]) -> Generator[Msg]:
               ...
    
    md : dict, optional
        metadata
    
    Notes
    -----
    If ``delay`` is an iterable, it must have at least ``num - 1`` entries or
    the plan will raise a ``ValueError`` during iteration.

Executing the next cell will display an empty widget. In the sections below, the scans that we run will add figures to this widget.

If you are reading this in JupyterLab, right-click somewhere in the output area below and choose “Create New View for Output”. This will display a up-to-date copy of the figures off to the side of this notebook, and avoid the need for frequent scrolling between this widget and the code that follows.

auto_plot_view
# five consecutive readings
RE(count([det], num=5))
Transient Scan ID: 2     Time: 2023-04-22 21:09:19

Persistent Unique Scan ID: 'a8d569bf-afc4-4cee-9ded-03950989744f'

New stream: 'primary'

+-----------+------------+------------+

|   seq_num |       time |     ph_det |

+-----------+------------+------------+

|         1 | 21:09:19.7 |     104448 |

|         2 | 21:09:19.8 |     104658 |

|         3 | 21:09:19.9 |     104658 |

|         4 | 21:09:20.0 |     104658 |

|         5 | 21:09:20.1 |     104658 |

+-----------+------------+------------+

generator count ['a8d569bf'] (scan num: 2)




('a8d569bf-afc4-4cee-9ded-03950989744f',)
# five sequential readings separated by a 1-second delay
RE(count([det], num=5, delay=1))
Transient Scan ID: 3     Time: 2023-04-22 21:09:20

Persistent Unique Scan ID: '980ed57e-963e-461b-a0c5-a9889d41dabe'

New stream: 'primary'

+-----------+------------+------------+

|   seq_num |       time |     ph_det |

+-----------+------------+------------+

|         1 | 21:09:20.7 |     102570 |

|         2 | 21:09:21.5 |      96293 |

|         3 | 21:09:22.5 |      96965 |

|         4 | 21:09:23.5 |     103688 |

|         5 | 21:09:24.5 |     103595 |

+-----------+------------+------------+

generator count ['980ed57e'] (scan num: 3)




('980ed57e-963e-461b-a0c5-a9889d41dabe',)

Scan#

Scan motor from -10 to 10, stopping at 15 equally-spaced points along the way and reading det.

RE(scan([det], motor, -10, 10, 15))
Transient Scan ID: 4     Time: 2023-04-22 21:09:25

Persistent Unique Scan ID: 'a49637fc-3eb6-4f05-b9d1-1f14b2170027'

WARNING:matplotlib.legend:No artists with labels found to put in legend.  Note that artists whose label start with an underscore are ignored when legend() is called with no argument.
New stream: 'primary'

+-----------+------------+------------+------------+

|   seq_num |       time |   motor_ph |     ph_det |

+-----------+------------+------------+------------+

|         1 | 21:09:36.0 |    -10.000 |      14638 |

|         2 | 21:09:37.5 |     -8.571 |      19642 |

|         3 | 21:09:39.1 |     -7.143 |      33127 |

|         4 | 21:09:40.6 |     -5.714 |      48725 |

|         5 | 21:09:42.1 |     -4.286 |      61339 |

|         6 | 21:09:43.6 |     -2.857 |      84372 |

|         7 | 21:09:45.2 |     -1.429 |      93036 |

|         8 | 21:09:46.7 |      0.000 |      96751 |

|         9 | 21:09:48.2 |      1.429 |      98781 |

|        10 | 21:09:49.7 |      2.857 |      80502 |

|        11 | 21:09:51.2 |      4.286 |      72425 |

|        12 | 21:09:52.8 |      5.714 |      51546 |

|        13 | 21:09:54.3 |      7.143 |      35302 |

|        14 | 21:09:55.8 |      8.571 |      24969 |

|        15 | 21:09:57.4 |     10.000 |      13439 |

+-----------+------------+------------+------------+

generator scan ['a49637fc'] (scan num: 4)




('a49637fc-3eb6-4f05-b9d1-1f14b2170027',)

Simulators#

Bluesky includes utilities to inspecting plans before they are run. You can imagine various reasons you might want to do this. Example:

from bluesky.simulators import summarize_plan

summarize_plan(scan([det], motor, -1, 1, 3))
=================================== Open Run ===================================

motor_ph -> -1.0

  Read ['ph', 'motor_ph']

motor_ph -> 0.0

  Read ['ph', 'motor_ph']

motor_ph -> 1.0

  Read ['ph', 'motor_ph']

================================== Close Run ===================================

Custom plan#

Define a custom “plan”, using the Python syntax yield from to dispatch out to built-in plans.

# The plan_stubs module contains smaller plans.
# They can be used alone or as buildling blocks for larger plans.
from bluesky.plan_stubs import mv


def sweep_exposure_time(times):
    "Multiple scans: one per exposure time setting."
    for t in times:
        yield from mv(det.exp, t)
        yield from scan([det], motor, -10, 10, 5)

Before we run, let’s make our simulated motor move faster, just to save time in this example.

motor.delay = 0
RE(sweep_exposure_time([0.01, 0.1, 1]))
Transient Scan ID: 5     Time: 2023-04-22 21:09:57

Persistent Unique Scan ID: 'c0b585da-2056-4bc4-a248-209f355b464b'

New stream: 'primary'

+-----------+------------+------------+------------+

|   seq_num |       time |   motor_ph |     ph_det |

+-----------+------------+------------+------------+

|         1 | 21:10:18.2 |    -10.000 |        151 |

|         2 | 21:10:23.3 |     -5.000 |        601 |

|         3 | 21:10:28.3 |      0.000 |       1033 |

|         4 | 21:10:33.4 |      5.000 |        581 |

|         5 | 21:10:38.4 |     10.000 |        133 |

+-----------+------------+------------+------------+

generator scan ['c0b585da'] (scan num: 5)




Transient Scan ID: 6     Time: 2023-04-22 21:10:38

Persistent Unique Scan ID: '11a790f8-96b1-4c8f-bb51-c7d5019c8f8c'

New stream: 'primary'

+-----------+------------+------------+------------+

|   seq_num |       time |   motor_ph |     ph_det |

+-----------+------------+------------+------------+

|         1 | 21:10:59.2 |    -10.000 |       1464 |

|         2 | 21:11:04.3 |     -5.000 |       5920 |

|         3 | 21:11:09.3 |      0.000 |       9466 |

|         4 | 21:11:14.4 |      5.000 |       5956 |

|         5 | 21:11:19.5 |     10.000 |       1500 |

+-----------+------------+------------+------------+

generator scan ['11a790f8'] (scan num: 6)




Transient Scan ID: 7     Time: 2023-04-22 21:11:19

Persistent Unique Scan ID: '507c2cc4-d51b-4878-92b5-cc8c6c476f28'

New stream: 'primary'

+-----------+------------+------------+------------+

|   seq_num |       time |   motor_ph |     ph_det |

+-----------+------------+------------+------------+

|         1 | 21:11:40.2 |    -10.000 |      14077 |

|         2 | 21:11:45.3 |     -5.000 |      55532 |

|         3 | 21:11:50.4 |      0.000 |      94654 |

|         4 | 21:11:55.4 |      5.000 |      63272 |

|         5 | 21:12:00.5 |     10.000 |      13926 |

+-----------+------------+------------+------------+

generator scan ['507c2cc4'] (scan num: 7)




('c0b585da-2056-4bc4-a248-209f355b464b',
 '11a790f8-96b1-4c8f-bb51-c7d5019c8f8c',
 '507c2cc4-d51b-4878-92b5-cc8c6c476f28')

Exercises#

Q1: Above we ran a count with multiple readings separated by a fixed delay. The delay parameter also accepts a list of values. Try a count with a variable delay.

# Try your solution here. Fill in the blank:
# RE(count(____)))

Execute the following cell to reveal a solution:

%load solutions/count_variable_delay.py

Q2: Write a custom plan that scans the same region twice, first with coarse steps and then with fine steps.

# Try your solution here. Fill in the blank:
# def coarse_and_fine(detectors, motor, start, stop):
#     yield from scan(___)
#     yield from scan(___)
#
# RE(coarse_and_fine([det], motor, -10, 10))
%load solutions/scan_coarse_and_fine.py

Q3. All of the usages of scan we have seen so far scan from negative to positive. Scan from positive to negative.

# Try your solution here.
%load solutions/scan_positive_to_negative.py

Q4: The scan plan samples equally-spaced points. To sample arbitrary points, you can use list_scan. Import it from the same module that we imported scan from, then use list_scan? to view its documentation and figure out how to use it. Scan the positions [1, 1, 2, 3, 5, 8].

# Try your solution here.
%load solutions/scan_fibonacci.py

Q5: What’s wrong with this? (What does it do?)

# Broken example
def sweep_exposure_time(times):
    "Multiple scans: one per exposure time setting."
    for t in times:
        mv(det.exp, t)
        scan([det], motor, -10, 10, 15)
%load solutions/broken_sweep_exposure_time_explanation.txt