|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "0", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# TBL: Orca image normalization workflow\n", |
| 9 | + "\n", |
| 10 | + "This notebook shows how to use the workflow to compute normalized images recorded by the Orca detector on the TBL instrument." |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": null, |
| 16 | + "id": "1", |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [], |
| 19 | + "source": [ |
| 20 | + "import ess.tbl.data # noqa: F401\n", |
| 21 | + "from ess import tbl\n", |
| 22 | + "from ess.imaging.types import *\n", |
| 23 | + "import scipp as sc\n", |
| 24 | + "import plopp as pp\n", |
| 25 | + "\n", |
| 26 | + "%matplotlib widget" |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "markdown", |
| 31 | + "id": "2", |
| 32 | + "metadata": {}, |
| 33 | + "source": [ |
| 34 | + "## Workflow setup" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "code", |
| 39 | + "execution_count": null, |
| 40 | + "id": "3", |
| 41 | + "metadata": {}, |
| 42 | + "outputs": [], |
| 43 | + "source": [ |
| 44 | + "wf = tbl.OrcaNormalizedImagesWorkflow()\n", |
| 45 | + "\n", |
| 46 | + "wf[Filename[SampleRun]] = tbl.data.tbl_lego_sample_run()\n", |
| 47 | + "wf[Filename[DarkBackgroundRun]] = tbl.data.tbl_lego_dark_run()\n", |
| 48 | + "wf[Filename[OpenBeamRun]] = tbl.data.tbl_lego_openbeam_run()\n", |
| 49 | + "wf[NeXusDetectorName] = 'orca_detector'\n", |
| 50 | + "\n", |
| 51 | + "wf[MaskingRules] = {} # No masks to begin with\n", |
| 52 | + "wf[UncertaintyBroadcastMode] = UncertaintyBroadcastMode.upper_bound" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "code", |
| 57 | + "execution_count": null, |
| 58 | + "id": "4", |
| 59 | + "metadata": {}, |
| 60 | + "outputs": [], |
| 61 | + "source": [ |
| 62 | + "wf.visualize(NormalizedImage, compact=True, graph_attr={\"rankdir\": \"LR\"})" |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "markdown", |
| 67 | + "id": "5", |
| 68 | + "metadata": {}, |
| 69 | + "source": [ |
| 70 | + "## Run the workflow\n", |
| 71 | + "\n", |
| 72 | + "We compute the final normalized image:" |
| 73 | + ] |
| 74 | + }, |
| 75 | + { |
| 76 | + "cell_type": "code", |
| 77 | + "execution_count": null, |
| 78 | + "id": "6", |
| 79 | + "metadata": {}, |
| 80 | + "outputs": [], |
| 81 | + "source": [ |
| 82 | + "image = wf.compute(NormalizedImage)\n", |
| 83 | + "image" |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "code", |
| 88 | + "execution_count": null, |
| 89 | + "id": "7", |
| 90 | + "metadata": {}, |
| 91 | + "outputs": [], |
| 92 | + "source": [ |
| 93 | + "pp.slicer(image, autoscale=False)" |
| 94 | + ] |
| 95 | + }, |
| 96 | + { |
| 97 | + "cell_type": "markdown", |
| 98 | + "id": "8", |
| 99 | + "metadata": {}, |
| 100 | + "source": [ |
| 101 | + "## Adding masks\n", |
| 102 | + "\n", |
| 103 | + "If we want to mask some part of the image, we update the masking rules.\n", |
| 104 | + "For example, here we mask the upper part of the image:" |
| 105 | + ] |
| 106 | + }, |
| 107 | + { |
| 108 | + "cell_type": "code", |
| 109 | + "execution_count": null, |
| 110 | + "id": "9", |
| 111 | + "metadata": {}, |
| 112 | + "outputs": [], |
| 113 | + "source": [ |
| 114 | + "wf[MaskingRules] = {'y_pixel_offset': lambda x: x > sc.scalar(0.082, unit='m')}\n", |
| 115 | + "\n", |
| 116 | + "pp.slicer(wf.compute(NormalizedImage), autoscale=False)" |
| 117 | + ] |
| 118 | + }, |
| 119 | + { |
| 120 | + "cell_type": "markdown", |
| 121 | + "id": "10", |
| 122 | + "metadata": {}, |
| 123 | + "source": [ |
| 124 | + "## Intermediate results\n", |
| 125 | + "\n", |
| 126 | + "We can also inspect intermediate results, which is useful for debugging:" |
| 127 | + ] |
| 128 | + }, |
| 129 | + { |
| 130 | + "cell_type": "code", |
| 131 | + "execution_count": null, |
| 132 | + "id": "11", |
| 133 | + "metadata": {}, |
| 134 | + "outputs": [], |
| 135 | + "source": [ |
| 136 | + "results = wf.compute([\n", |
| 137 | + " RawDetector[SampleRun],\n", |
| 138 | + " CorrectedDetector[SampleRun],\n", |
| 139 | + " BackgroundSubtractedDetector[SampleRun]\n", |
| 140 | + "])\n", |
| 141 | + "\n", |
| 142 | + "fig = pp.tiled(2, 2, hspace=0.3, wspace=0.3)\n", |
| 143 | + "fig[0, 0] = results[RawDetector[SampleRun]]['time', 0].plot(title='Raw data')\n", |
| 144 | + "fig[0, 1] = results[CorrectedDetector[SampleRun]]['time', 0].plot(title='Masks applied')\n", |
| 145 | + "fig[1, 0] = results[BackgroundSubtractedDetector[SampleRun]]['time', 0].plot(title='Background subtracted')\n", |
| 146 | + "fig[1, 1] = image['time', 0].plot(title='Final image')\n", |
| 147 | + "fig" |
| 148 | + ] |
| 149 | + } |
| 150 | + ], |
| 151 | + "metadata": { |
| 152 | + "kernelspec": { |
| 153 | + "display_name": "Python 3 (ipykernel)", |
| 154 | + "language": "python", |
| 155 | + "name": "python3" |
| 156 | + }, |
| 157 | + "language_info": { |
| 158 | + "codemirror_mode": { |
| 159 | + "name": "ipython", |
| 160 | + "version": 3 |
| 161 | + }, |
| 162 | + "file_extension": ".py", |
| 163 | + "mimetype": "text/x-python", |
| 164 | + "name": "python", |
| 165 | + "nbconvert_exporter": "python", |
| 166 | + "pygments_lexer": "ipython3", |
| 167 | + "version": "3.12.7" |
| 168 | + } |
| 169 | + }, |
| 170 | + "nbformat": 4, |
| 171 | + "nbformat_minor": 5 |
| 172 | +} |
0 commit comments