Hello, World#

Test the new bluesky installation with the Hello, World! code in the ~/bluesky directory. If you need help to get there, follow those links:

Follow these steps to load the bluesky and databroker packages for data collection activities.

[1]:
import bluesky
import databroker
from bluesky.callbacks.best_effort import BestEffortCallback

cat = databroker.temp().v2
RE = bluesky.RunEngine()
RE.subscribe(cat.v1.insert)
RE.subscribe(BestEffortCallback())
[1]:
1

Line by line explanation

  • my_path = ... define a variable my_path as the “bluesky” directory located in the user’s home directory

  • sys.path.append(my_path) adds this directory to the list of directories that Python will search when you try to import a module.

  • cat = databroker.temp().v2 creates an instance of a databroker temporary catalog (a catalog which is deleted when the python session is exited) and assigns it to the variable cat. This allows you to interact with the databroker instance and access its functionality, such as querying for data, creating runs, or accessing metadata.

  • RE = bluesky.RunEngine() creates a new instance of the RunEngine class and assigns it to the variable RE. The RunEngine is the central component of Bluesky, responsible for managing the execution of experimental plans and coordinating communication between other components, such as detectors and data acquisition devices.

  • RE.subscribe(cat.v1.insert) sets up a subscription to the RunEngine instance (RE) that will insert data into a databroker instance (using cat.v1) as data is collected. This ensures that even partially collected data is stored and available for later analysis and processing.

  • RE.subscribe(BestEffortCallback()) sets up a subscription to the RunEngine instance (RE) that:

    • show a chart during the run

    • print a table of the run data during the run

    • print a summary at the end of the run.

We can now load the quick_hello.py file located in the user directory and execute its contents in the current Python environment.

[2]:
%run -i ~/bluesky/user/quick_hello.py
Loading 'Hello, World!' example.

Note: The Loading 'Hello, World!' example. text came from a print() statement in the file.

This also loads hello_world(), a demonstration bluesky plan. We can run the hello_world() function in the RE RunEngine instance, which will execute the corresponding experimental plan.

[3]:
RE(hello_world())


Transient Scan ID: 1     Time: 2023-05-10 23:00:42
Persistent Unique Scan ID: '5d1dca55-ed86-47c4-a2d9-6b20e6c76672'
New stream: 'primary'
+-----------+------------+------------+
|   seq_num |       time |      hello |
+-----------+------------+------------+
|         1 | 23:00:42.9 |          1 |
+-----------+------------+------------+
generator count ['5d1dca55'] (scan num: 1)



[3]:
('5d1dca55-ed86-47c4-a2d9-6b20e6c76672',)

Since we have created a temporary catalog, the data is only available until the end of the IPython session.

When working with a regular (“not temporary”) catalog, more details about working with the data can be found here:

In any case, we can retrieve data for the most recent run stored in the cat databroker instance, and assign it to the a variable run.

[4]:
run = cat[-1]
metadata=run.metadata
data=run.primary.read()
  • run is a dictionary-like object that can be used to access the data run.primary and metadata run.metadata from the most recent run: we use [-1], meaning “1 run ago”.

  • metadata is a python dictionary containing the information about an experimental run, such as the plan used, the detectors used, the start and end time of the run, and any other relevant experimental conditions. This metadata can be used to help manage and organize experimental data.

  • data uses the read() method to read the actual data collected during the run from the primary stream.

[5]:
run
[5]:
BlueskyRun
  uid='5d1dca55-ed86-47c4-a2d9-6b20e6c76672'
  exit_status='success'
  2023-05-10 23:00:42.934 -- 2023-05-10 23:00:42.979
  Streams:
    * primary

[6]:
metadata
[6]:
{'start': Start({'detectors': ['hello'],
 'hints': {'dimensions': [[['time'], 'primary']]},
 'num_intervals': 0,
 'num_points': 1,
 'plan_args': {'detectors': ["HelloDevice(prefix='', name='hello', "
                             "read_attrs=['number', 'text'], "
                             'configuration_attrs=[])'],
               'num': 1},
 'plan_name': 'count',
 'plan_type': 'generator',
 'scan_id': 1,
 'time': 1683777642.9343226,
 'title': 'test QS',
 'uid': '5d1dca55-ed86-47c4-a2d9-6b20e6c76672',
 'versions': {'bluesky': '1.6.7', 'ophyd': '1.6.0'}}),
 'stop': Stop({'exit_status': 'success',
 'num_events': {'primary': 1},
 'reason': '',
 'run_start': '5d1dca55-ed86-47c4-a2d9-6b20e6c76672',
 'time': 1683777642.9791105,
 'uid': '789202b8-bc55-460f-a4af-9f6f5b6db404'}),
 'catalog_dir': None}
[7]:
data
[7]:
<xarray.Dataset>
Dimensions:     (time: 1)
Coordinates:
  * time        (time) float64 1.684e+09
Data variables:
    hello       (time) int64 1
    hello_text  (time) <U13 'Hello, World!'

Congratulations, you’ve tested your new bluesky installation!