diff --git a/examples/LoadImageFromDB-execution.ipynb b/examples/LoadImageFromDB-execution.ipynb new file mode 100644 index 0000000..00efe9e --- /dev/null +++ b/examples/LoadImageFromDB-execution.ipynb @@ -0,0 +1,1084 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import datetime\n", + "import h5py\n", + "import numpy as np\n", + "import imageio\n", + "import matplotlib.pyplot as plt\n", + "from matplotlib.colors import LogNorm\n", + "\n", + "from databroker import Broker\n", + "from eiger_io.fs_handler import EigerHandler" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "db = Broker.named('chx')\n", + "db.reg.register_handler('AD_EIGER2', EigerHandler, overwrite=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def save_hdf5(data, filename='data.h5', dataset='dataset', save_dir=None):\n", + " \"\"\"Saving data to an hdf5 file.\n", + " \n", + " Parameters\n", + " ----------\n", + " data: ndarray\n", + " data array to save into hdf5 file\n", + " filename: str\n", + " the name of the file\n", + " dataset: str\n", + " the name of the entry in the hdf5 layout\n", + " save_dir: None or str\n", + " a directory to save the file in\n", + " \n", + " Returns\n", + " -------\n", + " status: str\n", + " a status string\n", + " \"\"\"\n", + " if save_dir:\n", + " filename = os.path.join(save_dir, filename)\n", + " h5f = h5py.File(filename, 'w')\n", + " r = h5f.create_dataset(dataset, data=data)\n", + " status = '{} created: {}'.format(r, os.path.abspath(filename))\n", + " h5f.close()\n", + " return status" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Get data from Eiger detector" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "time: 201707312119\n", + "uid: f4fe7c56-3c23-4445-8bec-9a373066ead2\n", + "scan_id: 5068\n" + ] + } + ], + "source": [ + "eiger_uid = 'f4fe7c'\n", + "hdr_eiger = db[eiger_uid] # 'eiger4m_single_image' field for hdr.data()\n", + "time_e = datetime.datetime.fromtimestamp(timestamp=hdr_eiger['start']['time']).strftime('%Y%m%d%H%M')\n", + "uid_e = hdr_eiger['start']['uid']\n", + "scan_id_e = hdr_eiger['start']['scan_id']\n", + "print(f'time: {time_e}\\nuid: {uid_e}\\nscan_id: {scan_id_e}')" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
timeeiger4m_single_imageeiger4m_single_stats1_totaleiger4m_single_stats2_totaleiger4m_single_stats3_totaleiger4m_single_stats4_totaleiger4m_single_stats5_total
seq_num
12017-07-31 21:21:58.409980aee074a3-2e4e-4295-854c-da0b7f919853648629050.0252040143.08.590173e+102.576998e+102.130496e+15
\n", + "
" + ], + "text/plain": [ + " time eiger4m_single_image \\\n", + "seq_num \n", + "1 2017-07-31 21:21:58.409980 aee074a3-2e4e-4295-854c-da0b7f919853 \n", + "\n", + " eiger4m_single_stats1_total eiger4m_single_stats2_total \\\n", + "seq_num \n", + "1 648629050.0 252040143.0 \n", + "\n", + " eiger4m_single_stats3_total eiger4m_single_stats4_total \\\n", + "seq_num \n", + "1 8.590173e+10 2.576998e+10 \n", + "\n", + " eiger4m_single_stats5_total \n", + "seq_num \n", + "1 2.130496e+15 " + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "hdr_eiger.table()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'eiger4m_single_image',\n", + " 'eiger4m_single_stats1_total',\n", + " 'eiger4m_single_stats2_total',\n", + " 'eiger4m_single_stats3_total',\n", + " 'eiger4m_single_stats4_total',\n", + " 'eiger4m_single_stats5_total'}" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "hdr_eiger.fields()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Use the field with an '_image' occurrence" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "filepath : /XF11ID/data/2017/07/31/5c06c4d9-3743-4873-8ea9\n", + "got images_per_file\n" + ] + } + ], + "source": [ + "imgs_eiger = list(hdr_eiger.data('eiger4m_single_image'))" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2167, 2070)" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "imgs_eiger[0].shape" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "FigureCanvasNbAgg()" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "%matplotlib widget\n", + "plt.imshow(imgs_eiger[0][0], cmap='viridis', norm=LogNorm())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Get data from xray eye camera" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "time: 201801201715\n", + "uid: 44aae6fe-70b7-4fbf-a4ed-2297ea6b725f\n", + "scan_id: 10836\n" + ] + } + ], + "source": [ + "xeye_uid = '44aae6fe'\n", + "hdr_xeye = db[xeye_uid] # 'xray_eye3_image' field for hdr.data\n", + "time_x = datetime.datetime.fromtimestamp(timestamp=hdr_xeye['start']['time']).strftime('%Y%m%d%H%M')\n", + "uid_x = hdr_xeye['start']['uid']\n", + "scan_id_x = hdr_xeye['start']['scan_id']\n", + "print(f'time: {time_x}\\nuid: {uid_x}\\nscan_id: {scan_id_x}')" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
timexray_eye3_stats1_totalxray_eye3_stats2_totalxray_eye3_stats3_totalxray_eye3_stats4_totalxray_eye3_stats5_totalxray_eye3_image
seq_num
12018-01-20 17:17:00.270831125698.064832.0237824.018878.011772860.004de934d-9daa-4c1f-8890-4a9a4d55744e
\n", + "
" + ], + "text/plain": [ + " time xray_eye3_stats1_total \\\n", + "seq_num \n", + "1 2018-01-20 17:17:00.270831 125698.0 \n", + "\n", + " xray_eye3_stats2_total xray_eye3_stats3_total \\\n", + "seq_num \n", + "1 64832.0 237824.0 \n", + "\n", + " xray_eye3_stats4_total xray_eye3_stats5_total \\\n", + "seq_num \n", + "1 18878.0 11772860.0 \n", + "\n", + " xray_eye3_image \n", + "seq_num \n", + "1 04de934d-9daa-4c1f-8890-4a9a4d55744e " + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "hdr_xeye.table()" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'xray_eye3_image',\n", + " 'xray_eye3_stats1_total',\n", + " 'xray_eye3_stats2_total',\n", + " 'xray_eye3_stats3_total',\n", + " 'xray_eye3_stats4_total',\n", + " 'xray_eye3_stats5_total'}" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "hdr_xeye.fields()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Use the field with an '_image' occurrence" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "imgs_xeye = list(hdr_xeye.data('xray_eye3_image'))" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(100, 2050, 2448)" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "imgs_xeye[0].shape" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "54da83baba3a4d218ee6f8041c2cb9e2", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "FigureCanvasNbAgg()" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "%matplotlib widget\n", + "plt.imshow(np.mean(imgs_xeye[0], axis=0), cmap='gray')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Get tabular data" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "hdr_tab = db['635f98ba']" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
timemca_xs_channel1_rois_roi01_valuemca_xs_channel1_rois_roi01_value_sumsmp_ysmp_y_setpointsmp_y_actuate
seq_num
12018-01-20 16:56:52.6557528852.9501308852.9501301.632001.6320001
22018-01-20 16:56:54.7523649455.6840409455.6840401.634071.6340691
32018-01-20 16:56:56.3205279188.6743979188.6743971.636141.6361381
42018-01-20 16:56:58.6685099684.2937959684.2937951.638211.6382071
52018-01-20 16:57:01.07092610320.92636410320.9263641.640281.6402761
62018-01-20 16:57:02.61649111229.17372511229.1737251.642341.6423451
72018-01-20 16:57:07.24867911389.69575311389.6957531.644411.6444141
82018-01-20 16:57:10.69574312590.48366312590.4836631.646481.6464831
92018-01-20 16:57:14.32730916611.34600216611.3460021.648551.6485521
102018-01-20 16:57:20.60342520751.49583920751.4958391.650621.6506211
112018-01-20 16:57:25.91517625207.73361025207.7336101.652691.6526901
122018-01-20 16:57:27.63473238616.63325338616.6332531.654761.6547591
132018-01-20 16:57:31.62673479980.40619279980.4061921.656831.6568281
142018-01-20 16:57:33.480865155312.266491155312.2664911.658901.6588971
152018-01-20 16:57:35.916922227985.963766227985.9637661.660971.6609661
162018-01-20 16:57:37.890037256689.802487256689.8024871.663031.6630341
172018-01-20 16:57:39.938619215936.465360215936.4653601.665101.6651031
182018-01-20 16:57:42.086255136271.719730136271.7197301.667171.6671721
192018-01-20 16:57:53.62266273234.90591173234.9059111.669241.6692411
202018-01-20 16:57:58.38499639155.73310539155.7331051.671311.6713101
212018-01-20 16:58:00.09288328484.73536928484.7353691.673381.6733791
222018-01-20 16:58:02.58928824366.67208124366.6720811.675451.6754481
232018-01-20 16:58:04.89248319999.37375319999.3737531.677521.6775171
242018-01-20 16:58:08.03897016928.13022416928.1302241.679591.6795861
252018-01-20 16:58:09.77866116430.15099816430.1509981.681661.6816551
262018-01-20 16:58:11.92791415745.41623515745.4162351.683721.6837241
272018-01-20 16:58:13.62072514326.17755814326.1775581.685791.6857931
282018-01-20 16:58:15.72433314398.28987414398.2898741.687861.6878621
292018-01-20 16:58:17.35895714171.37079214171.3707921.689931.6899311
302018-01-20 16:58:23.89972114099.43412414099.4341241.692001.6920001
\n", + "
" + ], + "text/plain": [ + " time mca_xs_channel1_rois_roi01_value \\\n", + "seq_num \n", + "1 2018-01-20 16:56:52.655752 8852.950130 \n", + "2 2018-01-20 16:56:54.752364 9455.684040 \n", + "3 2018-01-20 16:56:56.320527 9188.674397 \n", + "4 2018-01-20 16:56:58.668509 9684.293795 \n", + "5 2018-01-20 16:57:01.070926 10320.926364 \n", + "6 2018-01-20 16:57:02.616491 11229.173725 \n", + "7 2018-01-20 16:57:07.248679 11389.695753 \n", + "8 2018-01-20 16:57:10.695743 12590.483663 \n", + "9 2018-01-20 16:57:14.327309 16611.346002 \n", + "10 2018-01-20 16:57:20.603425 20751.495839 \n", + "11 2018-01-20 16:57:25.915176 25207.733610 \n", + "12 2018-01-20 16:57:27.634732 38616.633253 \n", + "13 2018-01-20 16:57:31.626734 79980.406192 \n", + "14 2018-01-20 16:57:33.480865 155312.266491 \n", + "15 2018-01-20 16:57:35.916922 227985.963766 \n", + "16 2018-01-20 16:57:37.890037 256689.802487 \n", + "17 2018-01-20 16:57:39.938619 215936.465360 \n", + "18 2018-01-20 16:57:42.086255 136271.719730 \n", + "19 2018-01-20 16:57:53.622662 73234.905911 \n", + "20 2018-01-20 16:57:58.384996 39155.733105 \n", + "21 2018-01-20 16:58:00.092883 28484.735369 \n", + "22 2018-01-20 16:58:02.589288 24366.672081 \n", + "23 2018-01-20 16:58:04.892483 19999.373753 \n", + "24 2018-01-20 16:58:08.038970 16928.130224 \n", + "25 2018-01-20 16:58:09.778661 16430.150998 \n", + "26 2018-01-20 16:58:11.927914 15745.416235 \n", + "27 2018-01-20 16:58:13.620725 14326.177558 \n", + "28 2018-01-20 16:58:15.724333 14398.289874 \n", + "29 2018-01-20 16:58:17.358957 14171.370792 \n", + "30 2018-01-20 16:58:23.899721 14099.434124 \n", + "\n", + " mca_xs_channel1_rois_roi01_value_sum smp_y smp_y_setpoint \\\n", + "seq_num \n", + "1 8852.950130 1.63200 1.632000 \n", + "2 9455.684040 1.63407 1.634069 \n", + "3 9188.674397 1.63614 1.636138 \n", + "4 9684.293795 1.63821 1.638207 \n", + "5 10320.926364 1.64028 1.640276 \n", + "6 11229.173725 1.64234 1.642345 \n", + "7 11389.695753 1.64441 1.644414 \n", + "8 12590.483663 1.64648 1.646483 \n", + "9 16611.346002 1.64855 1.648552 \n", + "10 20751.495839 1.65062 1.650621 \n", + "11 25207.733610 1.65269 1.652690 \n", + "12 38616.633253 1.65476 1.654759 \n", + "13 79980.406192 1.65683 1.656828 \n", + "14 155312.266491 1.65890 1.658897 \n", + "15 227985.963766 1.66097 1.660966 \n", + "16 256689.802487 1.66303 1.663034 \n", + "17 215936.465360 1.66510 1.665103 \n", + "18 136271.719730 1.66717 1.667172 \n", + "19 73234.905911 1.66924 1.669241 \n", + "20 39155.733105 1.67131 1.671310 \n", + "21 28484.735369 1.67338 1.673379 \n", + "22 24366.672081 1.67545 1.675448 \n", + "23 19999.373753 1.67752 1.677517 \n", + "24 16928.130224 1.67959 1.679586 \n", + "25 16430.150998 1.68166 1.681655 \n", + "26 15745.416235 1.68372 1.683724 \n", + "27 14326.177558 1.68579 1.685793 \n", + "28 14398.289874 1.68786 1.687862 \n", + "29 14171.370792 1.68993 1.689931 \n", + "30 14099.434124 1.69200 1.692000 \n", + "\n", + " smp_y_actuate \n", + "seq_num \n", + "1 1 \n", + "2 1 \n", + "3 1 \n", + "4 1 \n", + "5 1 \n", + "6 1 \n", + "7 1 \n", + "8 1 \n", + "9 1 \n", + "10 1 \n", + "11 1 \n", + "12 1 \n", + "13 1 \n", + "14 1 \n", + "15 1 \n", + "16 1 \n", + "17 1 \n", + "18 1 \n", + "19 1 \n", + "20 1 \n", + "21 1 \n", + "22 1 \n", + "23 1 \n", + "24 1 \n", + "25 1 \n", + "26 1 \n", + "27 1 \n", + "28 1 \n", + "29 1 \n", + "30 1 " + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tbl = hdr_tab.table()\n", + "tbl" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "abca5475eb4e44e6ad63dfd375a52dcf", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "FigureCanvasNbAgg()" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tbl.plot(x='smp_y', y='mca_xs_channel1_rois_roi01_value_sum', grid=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "tbl.to_csv('/home/mrakitin/work/CHX/exported.csv')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Save Eiger data to TIFF and HDF5 files" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "201707312119_f4fe7c_5068.tiff\n", + "201707312119_f4fe7c_5068.h5\n" + ] + } + ], + "source": [ + "filename = '{}_{}_{}'.format(time_e, eiger_uid, scan_id_e)\n", + "filename_tiff= '{}.{}'.format(filename, 'tiff')\n", + "filename_h5 = '{}.{}'.format(filename, 'h5')\n", + "print(filename_tiff)\n", + "print(filename_h5)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "imageio.imwrite(os.path.join('/home/mrakitin/work/CHX/', filename_tiff), imgs_eiger[0][0])" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "collapsed": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " created: /home/mrakitin/work/CHX/201707312119_f4fe7c_5068.h5\n" + ] + } + ], + "source": [ + "status = save_hdf5(imgs_eiger, filename=filename_h5, dataset='dataset',\n", + " save_dir='/home/mrakitin/work/CHX/')\n", + "print(status)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "CHX (current)", + "language": "python", + "name": "analysis-2018-2.1_srv1" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/examples/LoadImageFromDB-template.ipynb b/examples/LoadImageFromDB-template.ipynb new file mode 100644 index 0000000..ae5456f --- /dev/null +++ b/examples/LoadImageFromDB-template.ipynb @@ -0,0 +1,334 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "import datetime\n", + "import h5py\n", + "import numpy as np\n", + "import imageio\n", + "import matplotlib.pyplot as plt\n", + "from matplotlib.colors import LogNorm\n", + "\n", + "from databroker import Broker\n", + "from eiger_io.fs_handler import EigerHandler" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "db = Broker.named('chx')\n", + "db.reg.register_handler('AD_EIGER2', EigerHandler, overwrite=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def save_hdf5(data, filename='data.h5', dataset='dataset', save_dir=None):\n", + " \"\"\"Saving data to an hdf5 file.\n", + " \n", + " Parameters\n", + " ----------\n", + " data: ndarray\n", + " data array to save into hdf5 file\n", + " filename: str\n", + " the name of the file\n", + " dataset: str\n", + " the name of the entry in the hdf5 layout\n", + " save_dir: None or str\n", + " a directory to save the file in\n", + " \n", + " Returns\n", + " -------\n", + " status: str\n", + " a status string\n", + " \"\"\"\n", + " if save_dir:\n", + " filename = os.path.join(save_dir, filename)\n", + " h5f = h5py.File(filename, 'w')\n", + " r = h5f.create_dataset(dataset, data=data)\n", + " status = '{} created: {}'.format(r, os.path.abspath(filename))\n", + " h5f.close()\n", + " return status" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Get data from Eiger detector" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "eiger_uid = 'f4fe7c'\n", + "hdr_eiger = db[eiger_uid] # 'eiger4m_single_image' field for hdr.data()\n", + "time_e = datetime.datetime.fromtimestamp(timestamp=hdr_eiger['start']['time']).strftime('%Y%m%d%H%M')\n", + "uid_e = hdr_eiger['start']['uid']\n", + "scan_id_e = hdr_eiger['start']['scan_id']\n", + "print(f'time: {time_e}\\nuid: {uid_e}\\nscan_id: {scan_id_e}')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "hdr_eiger.table()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "hdr_eiger.fields()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Use the field with an '_image' occurrence" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "imgs_eiger = list(hdr_eiger.data('eiger4m_single_image'))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "imgs_eiger[0].shape" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%matplotlib widget\n", + "plt.imshow(imgs_eiger[0][0], cmap='viridis', norm=LogNorm())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Get data from xray eye camera" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "xeye_uid = '44aae6fe'\n", + "hdr_xeye = db[xeye_uid] # 'xray_eye3_image' field for hdr.data\n", + "time_x = datetime.datetime.fromtimestamp(timestamp=hdr_xeye['start']['time']).strftime('%Y%m%d%H%M')\n", + "uid_x = hdr_xeye['start']['uid']\n", + "scan_id_x = hdr_xeye['start']['scan_id']\n", + "print(f'time: {time_x}\\nuid: {uid_x}\\nscan_id: {scan_id_x}')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "hdr_xeye.table()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "hdr_xeye.fields()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Use the field with an '_image' occurrence" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "imgs_xeye = list(hdr_xeye.data('xray_eye3_image'))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "imgs_xeye[0].shape" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%matplotlib widget\n", + "plt.imshow(np.mean(imgs_xeye[0], axis=0), cmap='gray')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Get tabular data" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "hdr_tab = db['635f98ba']" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "tbl = hdr_tab.table()\n", + "tbl" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "tbl.plot(x='smp_y', y='mca_xs_channel1_rois_roi01_value_sum', grid=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "tbl.to_csv('/home/mrakitin/work/CHX/exported.csv')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Save Eiger data to TIFF and HDF5 files" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "filename = '{}_{}_{}'.format(time_e, eiger_uid, scan_id_e)\n", + "filename_tiff= '{}.{}'.format(filename, 'tiff')\n", + "filename_h5 = '{}.{}'.format(filename, 'h5')\n", + "print(filename_tiff)\n", + "print(filename_h5)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "imageio.imwrite(os.path.join('/home/mrakitin/work/CHX/', filename_tiff), imgs_eiger[0][0])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "status = save_hdf5(imgs_eiger, filename=filename_h5, dataset='dataset',\n", + " save_dir='/home/mrakitin/work/CHX/')\n", + "print(status)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "CHX (current)", + "language": "python", + "name": "analysis-2018-2.1_srv1" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}