Run UDFs on DECTRIS live streams

  • Make sure to adjust the network setup, nav shape and trigger mode below to match the data source!

  • This notebook requires the bqplot extra of LiberTEM: pip install libertem[bqplot]

[1]:
import os
import logging

# adjust accordingly to your network setup:
DCU_API_HOST = os.environ.get('DCU_API_HOST', '127.0.0.1')
DCU_API_PORT = os.environ.get('DCU_API_PORT', 8910)
DCU_DATA_HOST = os.environ.get('DCU_DATA_HOST', '127.0.0.1')
DCU_DATA_PORT = os.environ.get('DCU_DATA_PORT', 9999)

# logging config for `libertem_dectris`:
# (replace 'ERROR' with 'INFO' or 'DEBUG' to get verbose logging)
os.environ.setdefault('LIBERTEM_DECTRIS_LOG_LEVEL', 'ERROR')

# to get basic logging from Python code, replace ERROR with INFO or similar:
logging.basicConfig(level=logging.ERROR)
[2]:
from libertem_live.api import LiveContext, Hooks
from libertem.udf.sumsigudf import SumSigUDF
from libertem.viz.bqp import BQLive2DPlot
[3]:
ctx = LiveContext(plot_class=BQLive2DPlot)
[4]:
class MyHooks(Hooks):
    def on_ready_for_data(self, env):
        # can trigger the microscope here, for demonstration purposes
        # we just print out the size of the scan:
        print(f"starting scan of size {env.aq.shape.nav}")
[5]:
# open a connection to the DECTRIS detector control unit (DCU).
# note that by default, this connection stays open, so if you try to
# connect using other software, make sure to either close the notebook,
# or call `conn.close()`.
conn = ctx.make_connection('dectris').open(
    api_host=DCU_API_HOST,
    api_port=DCU_API_PORT,
    data_host=DCU_DATA_HOST,
    data_port=DCU_DATA_PORT,
)
[6]:
aq = ctx.make_acquisition(
    conn=conn,
    nav_shape=(128, 128),  # adjust `nav_shape`
    controller=conn.get_active_controller(trigger_mode='exte'),
    hooks=MyHooks(),
    frames_per_partition=256,
)
[7]:
res = ctx.run_udf(dataset=aq, udf=SumSigUDF(), plots=True)
starting scan of size (128, 128)

Example output (because bqplot plots aren’t saved in the notebook file); you should get something like the following:

Screenshot%202022-08-24%20124739.png