{ "cells": [ { "cell_type": "markdown", "id": "35b784c0", "metadata": {}, "source": [ "# Locate peak on 2-D area detector image\n", "\n", "From *APS Python Training for Bluesky Data Acquisition*.\n", "\n", "**Objective**\n", "\n", "Measure a simulated diffraction spot with an EPICS area detector\n", "and find its centroid and width.\n", "\n", "Note: The position, width, and peak intensity of the simulated is randomized and will be different each time this note book is run. In fact, the EPICS support applies a small amount of jitter to the position to better simulate a real signal.\n", "\n", "## Start the `instrument` package\n", "\n", "Our instrument package is in the `bluesky` subdirectory here so we add that to the search path before importing it." ] }, { "cell_type": "code", "execution_count": 1, "id": "f373fb2a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/home/prjemian/bluesky/instrument/_iconfig.py\n", "Activating auto-logging. Current session state plus future input saved.\n", "Filename : /home/prjemian/Documents/projects/BCDA-APS/bluesky_training/docs/source/howto/.logs/ipython_console.log\n", "Mode : rotate\n", "Output logging : True\n", "Raw input log : False\n", "Timestamping : True\n", "State : active\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "I Fri-15:42:25 - ############################################################ startup\n", "I Fri-15:42:25 - logging started\n", "I Fri-15:42:25 - logging level = 10\n", "I Fri-15:42:25 - /home/prjemian/bluesky/instrument/session_logs.py\n", "I Fri-15:42:25 - /home/prjemian/bluesky/instrument/collection.py\n", "I Fri-15:42:25 - CONDA_PREFIX = /home/prjemian/.conda/envs/bluesky_2023_2\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Exception reporting mode: Minimal\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "I Fri-15:42:25 - xmode exception level: 'Minimal'\n", "I Fri-15:42:25 - /home/prjemian/bluesky/instrument/mpl/notebook.py\n", "I Fri-15:42:25 - #### Bluesky Framework ####\n", "I Fri-15:42:25 - /home/prjemian/bluesky/instrument/framework/check_python.py\n", "I Fri-15:42:25 - /home/prjemian/bluesky/instrument/framework/check_bluesky.py\n", "I Fri-15:42:25 - /home/prjemian/bluesky/instrument/framework/initialize.py\n", "I Fri-15:42:25 - RunEngine metadata saved in directory: /home/prjemian/Bluesky_RunEngine_md\n", "I Fri-15:42:25 - using databroker catalog 'training'\n", "I Fri-15:42:25 - using ophyd control layer: pyepics\n", "I Fri-15:42:25 - /home/prjemian/bluesky/instrument/framework/metadata.py\n", "I Fri-15:42:25 - /home/prjemian/bluesky/instrument/epics_signal_config.py\n", "I Fri-15:42:25 - Using RunEngine metadata for scan_id\n", "I Fri-15:42:25 - #### Devices ####\n", "I Fri-15:42:25 - /home/prjemian/bluesky/instrument/devices/area_detector.py\n", "I Fri-15:42:25 - /home/prjemian/bluesky/instrument/devices/calculation_records.py\n", "I Fri-15:42:27 - /home/prjemian/bluesky/instrument/devices/fourc_diffractometer.py\n", "I Fri-15:42:28 - /home/prjemian/bluesky/instrument/devices/ioc_stats.py\n", "I Fri-15:42:28 - /home/prjemian/bluesky/instrument/devices/kohzu_monochromator.py\n", "I Fri-15:42:28 - /home/prjemian/bluesky/instrument/devices/motors.py\n", "I Fri-15:42:28 - /home/prjemian/bluesky/instrument/devices/noisy_detector.py\n", "I Fri-15:42:28 - /home/prjemian/bluesky/instrument/devices/scaler.py\n", "I Fri-15:42:29 - /home/prjemian/bluesky/instrument/devices/shutter_simulator.py\n", "I Fri-15:42:29 - /home/prjemian/bluesky/instrument/devices/simulated_fourc.py\n", "I Fri-15:42:29 - /home/prjemian/bluesky/instrument/devices/simulated_kappa.py\n", "I Fri-15:42:29 - /home/prjemian/bluesky/instrument/devices/slits.py\n", "I Fri-15:42:29 - /home/prjemian/bluesky/instrument/devices/sixc_diffractometer.py\n", "I Fri-15:42:29 - /home/prjemian/bluesky/instrument/devices/temperature_signal.py\n", "I Fri-15:42:29 - #### Callbacks ####\n", "I Fri-15:42:29 - /home/prjemian/bluesky/instrument/callbacks/spec_data_file_writer.py\n", "I Fri-15:42:29 - #### Plans ####\n", "I Fri-15:42:29 - /home/prjemian/bluesky/instrument/plans/lup_plan.py\n", "I Fri-15:42:29 - /home/prjemian/bluesky/instrument/plans/peak_finder_example.py\n", "I Fri-15:42:29 - /home/prjemian/bluesky/instrument/utils/image_analysis.py\n", "I Fri-15:42:29 - #### Utilities ####\n", "I Fri-15:42:29 - writing to SPEC file: /home/prjemian/Documents/projects/BCDA-APS/bluesky_training/docs/source/howto/20230414-154229.dat\n", "I Fri-15:42:29 - >>>> Using default SPEC file name <<<<\n", "I Fri-15:42:29 - file will be created when bluesky ends its next scan\n", "I Fri-15:42:29 - to change SPEC file, use command: newSpecFile('title')\n", "I Fri-15:42:29 - #### Startup is complete. ####\n" ] } ], "source": [ "import pathlib, sys\n", "sys.path.append(str(pathlib.Path.home() / \"bluesky\"))\n", "from instrument.collection import *" ] }, { "cell_type": "markdown", "id": "b220c99a", "metadata": {}, "source": [ "Count the detector : record an image." ] }, { "cell_type": "code", "execution_count": 2, "id": "6c9d788f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "Transient Scan ID: 970 Time: 2023-04-14 15:42:30\n", "Persistent Unique Scan ID: '8923a036-6063-4115-8763-ad6ce324df07'\n", "New stream: 'label_start_motor'\n", "New stream: 'primary'\n", "+-----------+------------+\n", "| seq_num | time |\n", "+-----------+------------+\n", "| 1 | 15:42:31.6 |\n", "+-----------+------------+\n", "generator count ['8923a036'] (scan num: 970)\n" ] }, { "data": { "text/plain": [ "('8923a036-6063-4115-8763-ad6ce324df07',)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "RE(bp.count([adsimdet], md={\"motive\": \"locate_image_peak\"}))" ] }, { "cell_type": "markdown", "id": "6c371fda", "metadata": {}, "source": [ "## View the image\n", "\n", "Since area detector images are typically quite large, they are never \n", "read into Python memory during data acquisition. To view the image \n", "here, we need to use the *databroker* which reads the run data. The \n", "run data includes the reference to the file that contains the image.\n", "\n", "### Get the run\n", "\n", "First, get the most recent run from the `catalog` of runs." ] }, { "cell_type": "code", "execution_count": 3, "id": "d7fed850", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "BlueskyRun\n", " uid='8923a036-6063-4115-8763-ad6ce324df07'\n", " exit_status='success'\n", " 2023-04-14 15:42:30.957 -- 2023-04-14 15:42:31.658\n", " Streams:\n", " * primary\n", " * label_start_motor\n" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "run = cat[-1]\n", "run" ] }, { "cell_type": "markdown", "id": "c67ea36a", "metadata": {}, "source": [ "Look at the run's metadata. Confirm it has the `{\"motive\": \"locate_image_peak\"}` metadata we added. That was stored in the `start` document." ] }, { "cell_type": "code", "execution_count": 4, "id": "8740b3c4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "locate_image_peak\n" ] } ], "source": [ "print(run.metadata[\"start\"][\"motive\"])" ] }, { "cell_type": "markdown", "id": "32b874bf", "metadata": {}, "source": [ "### Primary stream\n", "The image was collected in the primary data stream. Get the primary stream data from the database." ] }, { "cell_type": "code", "execution_count": 5, "id": "408c36e7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.Dataset>\n", "Dimensions: (time: 1, dim_0: 100, dim_1: 1024, dim_2: 1024)\n", "Coordinates:\n", " * time (time) float64 1.682e+09\n", "Dimensions without coordinates: dim_0, dim_1, dim_2\n", "Data variables:\n", " adsimdet_image (time, dim_0, dim_1, dim_2) uint8 17 8 19 21 ... 10 21 8 17
<xarray.DataArray 'adsimdet_image' (time: 1, dim_0: 100, dim_1: 1024,\n", " dim_2: 1024)>\n", "array([[[[17, 8, 19, ..., 10, 19, 21],\n", " [19, 15, 23, ..., 8, 15, 14],\n", " [ 7, 26, 15, ..., 12, 10, 8],\n", " ...,\n", " [11, 21, 16, ..., 20, 16, 7],\n", " [ 9, 12, 19, ..., 10, 7, 14],\n", " [14, 20, 9, ..., 19, 22, 8]],\n", "\n", " [[25, 18, 25, ..., 17, 11, 15],\n", " [14, 13, 25, ..., 26, 16, 25],\n", " [ 8, 10, 20, ..., 19, 10, 18],\n", " ...,\n", " [18, 15, 9, ..., 18, 24, 12],\n", " [17, 18, 8, ..., 21, 21, 17],\n", " [24, 16, 20, ..., 18, 14, 22]],\n", "\n", " [[10, 13, 23, ..., 8, 12, 13],\n", " [18, 19, 22, ..., 10, 7, 23],\n", " [ 9, 11, 8, ..., 22, 19, 20],\n", " ...,\n", "...\n", " ...,\n", " [ 9, 24, 14, ..., 16, 16, 24],\n", " [17, 13, 13, ..., 21, 21, 21],\n", " [ 7, 20, 11, ..., 9, 14, 15]],\n", "\n", " [[14, 24, 21, ..., 24, 23, 23],\n", " [19, 24, 22, ..., 25, 22, 24],\n", " [14, 15, 14, ..., 20, 25, 7],\n", " ...,\n", " [11, 15, 13, ..., 10, 13, 18],\n", " [23, 7, 12, ..., 11, 22, 15],\n", " [26, 15, 9, ..., 22, 25, 17]],\n", "\n", " [[13, 23, 22, ..., 16, 7, 10],\n", " [21, 11, 16, ..., 15, 22, 18],\n", " [23, 13, 7, ..., 11, 13, 10],\n", " ...,\n", " [ 9, 23, 21, ..., 20, 16, 21],\n", " [16, 7, 18, ..., 21, 25, 9],\n", " [20, 18, 24, ..., 21, 8, 17]]]], dtype=uint8)\n", "Coordinates:\n", " * time (time) float64 1.682e+09\n", "Dimensions without coordinates: dim_0, dim_1, dim_2\n", "Attributes:\n", " object: adsimdet
<xarray.DataArray 'adsimdet_image' (dim_1: 1024, dim_2: 1024)>\n", "array([[17, 8, 19, ..., 10, 19, 21],\n", " [19, 15, 23, ..., 8, 15, 14],\n", " [ 7, 26, 15, ..., 12, 10, 8],\n", " ...,\n", " [11, 21, 16, ..., 20, 16, 7],\n", " [ 9, 12, 19, ..., 10, 7, 14],\n", " [14, 20, 9, ..., 19, 22, 8]], dtype=uint8)\n", "Coordinates:\n", " time float64 1.682e+09\n", "Dimensions without coordinates: dim_1, dim_2\n", "Attributes:\n", " object: adsimdet