{ "cells": [ { "cell_type": "markdown", "id": "12fb5aef", "metadata": {}, "source": [ "# WIP: save reflection information in stream\n", "See: https://github.com/bluesky/hklpy/issues/158" ] }, { "cell_type": "code", "execution_count": 1, "id": "2dcbeb2c", "metadata": {}, "outputs": [], "source": [ "from bluesky import RunEngine\n", "from bluesky.callbacks.best_effort import BestEffortCallback\n", "import bluesky.plans as bp\n", "import bluesky.plan_stubs as bps\n", "import bluesky.preprocessors as bpp\n", "import databroker\n", "import hkl\n", "from hkl import * # TODO: wildcard import, yikes!\n", "import numpy as np\n", "import pyRestTable\n", "from ophyd import Component, Device, EpicsSignal, Signal\n", "from ophyd.signal import AttributeSignal, ArrayAttributeSignal\n", "from ophyd.sim import *\n", "import pandas as pd\n", "\n", "bec = BestEffortCallback()\n", "bec.disable_plots()\n", "cat = databroker.temp().v2\n", "\n", "RE = RunEngine({})\n", "RE.subscribe(bec)\n", "RE.subscribe(cat.v1.insert)\n", "RE.md[\"notebook\"] = \"tst_UB_in_stream\"\n", "RE.md[\"objective\"] = \"Demonstrate UB matrix save & restore in stream of bluesky run\"" ] }, { "cell_type": "code", "execution_count": 2, "id": "c7fbcbc7", "metadata": {}, "outputs": [], "source": [ "from collections import namedtuple\n", "\n", "Lattice = namedtuple(\"LatticeTuple\", \"a b c alpha beta gamma\")\n", "Position = namedtuple(\"PositionTuple\", \"omega chi phi tth\")\n", "Reflection = namedtuple(\"ReflectionTuple\", \"h k l position\")\n", "\n", "class Holder:\n", " samples = {}\n", "\n", "class Reflections:\n", " reflections = []\n", "\n", "class MyDevice(Device):\n", " uptime = Component(EpicsSignal, \"gp:UPTIME\", kind=\"normal\")\n", " apple = Component(Signal, value=\"Fuji\", kind=\"omitted\")\n", " orange = Component(Signal, value=\"Valencia\", kind=\"omitted\")\n", " octopus = Component(Signal, value=\"spotted\", kind=\"omitted\")\n", "\n", " stream_name = Component(AttributeSignal, attr=\"_stream_name\", doc=\"stream name\", kind=\"omitted\")\n", " stream_attrs = Component(AttributeSignal, attr=\"_stream_attrs\", doc=\"attributes in stream\", kind=\"omitted\")\n", "\n", " _samples = {}\n", " _stream_name = \"bozo\"\n", " # _stream_attrs = \"orange octopus samples stream_name stream_attrs\".split()\n", " _stream_attrs = \"orange octopus stream_name stream_attrs\".split()\n", "\n", " # cannot make AttributeSignal from these that can be written by bluesky\n", " paddle = Holder()\n", " spots = Reflections()\n", "\n", " def other_streams(self, label=None):\n", " label = label or self._stream_name\n", " yield from bps.create(name=label)\n", " for attr in self._stream_attrs:\n", " yield from bps.read(getattr(self, attr))\n", " yield from bps.save()\n", "\n", " yield from bps.create(\"fruit\")\n", " yield from bps.read(self.apple)\n", " yield from bps.read(self.orange)\n", " yield from bps.save()\n", "\n", " yield from bps.create(\"animal\")\n", " yield from bps.read(self.octopus)\n", " yield from bps.save()\n", "\n", "\n", "nitwit = MyDevice(\"\", name=\"nitwit\")\n", "nitwit.paddle.samples[\"main\"] = Lattice(1,1,1,30,60,90)\n", "nitwit.paddle.samples[\"second\"] = Lattice(2,2,2, 2,2,2)\n", "\n", "def try_it():\n", " yield from bps.open_run()\n", "\n", " yield from bps.create()\n", " yield from bps.read(nitwit)\n", " yield from bps.save()\n", "\n", " yield from nitwit.other_streams()\n", "\n", " yield from bps.close_run()" ] }, { "cell_type": "code", "execution_count": 3, "id": "0af36aa4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "OrderedDict([('nitwit_uptime',\n", " {'value': '11 days, 06:21:15', 'timestamp': 1655096814.592612})])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nitwit.wait_for_connection()\n", "nitwit.read()" ] }, { "cell_type": "code", "execution_count": 4, "id": "69cc74b2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "Transient Scan ID: 1 Time: 2022-06-13 00:06:55\n", "Persistent Unique Scan ID: '8abd6968-1ce9-443d-a4ef-f9ad98f52d6c'\n", "New stream: 'primary'\n", "+-----------+------------+\n", "| seq_num | time |\n", "+-----------+------------+\n", "| 1 | 00:06:55.2 |\n", "New stream: 'bozo'\n", "New stream: 'fruit'\n", "New stream: 'animal'\n", "+-----------+------------+\n", "generator try_it ['8abd6968'] (scan num: 1)\n", "\n", "\n", "\n" ] }, { "data": { "text/plain": [ "('8abd6968-1ce9-443d-a4ef-f9ad98f52d6c',)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# RE(bp.count([nitwit]))\n", "RE(try_it())" ] }, { "cell_type": "code", "execution_count": 5, "id": "26b80f9a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "BlueskyRun\n", " uid='8abd6968-1ce9-443d-a4ef-f9ad98f52d6c'\n", " exit_status='success'\n", " 2022-06-13 00:06:55.235 -- 2022-06-13 00:06:55.253\n", " Streams:\n", " * animal\n", " * fruit\n", " * primary\n", " * bozo\n" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cat[-1]" ] }, { "cell_type": "code", "execution_count": 6, "id": "8e616824", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "stream = 'animal'\n", "\n", "Dimensions: (time: 1)\n", "Coordinates:\n", " * time (time) float64 1.655e+09\n", "Data variables:\n", " nitwit_octopus (time) \n", "Dimensions: (time: 1)\n", "Coordinates:\n", " * time (time) float64 1.655e+09\n", "Data variables:\n", " nitwit_orange (time) \n", "Dimensions: (time: 1)\n", "Coordinates:\n", " * time (time) float64 1.655e+09\n", "Data variables:\n", " nitwit_uptime (time) \n", "Dimensions: (time: 1, dim_0: 4)\n", "Coordinates:\n", " * time (time) float64 1.655e+09\n", "Dimensions without coordinates: dim_0\n", "Data variables:\n", " nitwit_stream_attrs (time, dim_0) \n", "Dimensions: (time: 1, dim_0: 6, dim_1: 6, dim_2: 6)\n", "Coordinates:\n", " * time (time) float64 1.655e+09\n", "Dimensions without coordinates: dim_0, dim_1, dim_2\n", "Data variables:\n", " main (time, dim_0) int64 1 1 1 30 60 90\n", " _keys (time, dim_1) \n", "Dimensions: (time: 1, dim_0: 4, dim_1: 3, dim_2: 4, dim_3: 2, dim_4: 4,\n", " dim_5: 3, dim_6: 4)\n", "Coordinates:\n", " * time (time) float64 1.655e+09\n", "Dimensions without coordinates: dim_0, dim_1, dim_2, dim_3, dim_4, dim_5, dim_6\n", "Data variables:\n", " r1 (time, dim_0) object 4.0 ... [-145.451, 0.0, 0.0, 69.0966]\n", " r2_hkl (time, dim_1) float64 0.0 4.0 0.0\n", " r2 (time, dim_2) object 0.0 ... [-145.451, 0.0, 90.0, 69.0966]\n", " _keys (time, dim_3)