{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Process Tabular Data with Pandas\n", "\n", "In this notebook you will:\n", "\n", "* Load a data from a scan.\n", "* Visualize it.\n", "* Explore the dataset, performing basic filtering and statistic operations using ``pandas``.\n", "\n", "Recommended Prerequisites:\n", "\n", "* [Hello Bluesky](./Hello%20Bluesky.ipynb)\n", "\n", "## Configuration\n", "Below, we will connect to EPICS IOC(s) controlling simulated hardware in lieu of actual motors, detectors. The IOCs should already be running in the background. Run this command to verify that they are running: it should produce output with RUNNING on each line. In the event of a problem, edit this command to replace status with restart all and run again.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!supervisorctl -c supervisor/supervisord.conf status" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%run scripts/beamline_configuration.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Quickly access and explore the data\n", "\n", "Let's take sample data." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "nbshpinx": "hidden" }, "outputs": [], "source": [ "# Creating a figure explicitly in advance helps with the\n", "# top-to-bottom flow of this notebook, but it is not necessary.\n", "# If this is omitted, bluesky will cause a figure to appear\n", "# during the RE(...) execution below.\n", "plt.figure('I slit_det vs motor_slit')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "RE(scan([slit, I], motor_slit, -10, 10, 15))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.gcf() # Display a snapshot of the current state of the figure." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Previously we used the syntax ``db(...)`` to search for Headers. We looped through each Header and extracted some of the metadata encapsulated in it. We use the shorthand ``db[-N]`` to retrieve exactly _one_ header, the one corresponding to ``N`` runs ago. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "header = db[-1] # Header for the most recent one (i.e. 1 run ago)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can access the data as a table. This object is called a ``pandas.DataFrame``, a high-performance, spreadsheet-like object in Python." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = header.table()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It display prettily, truncating if needed." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For large tables, it's handy to know how to ask for just the first or last couple rows." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is easy to visualize." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data.plot(x='motor_slit', y='slit_det')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.gcf() # Display a snapshot of the current state of the figure." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data.tail()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Statistics on columns are simple to compute." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data.mean()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data.min()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or just ask for all the common stats at once:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data.describe()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Do analysis, and plot\n", "\n", "### Normalize by beam current" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can do math on the columns, and we can add new columns." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data['normalized'] = data['slit_det'] / data['I'] * data['I'].mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can plot them conveniently, addressing the columns by name." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.figure()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot('motor_slit', 'slit_det', data=data, label='raw')\n", "plt.plot('motor_slit', 'normalized', data=data, label='normalized')\n", "plt.legend()\n", "plt.gcf()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Select only rows with high beam current" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Select rows where the current is above `500`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data[data['I'] > 500]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...or, where the the current is above the 30th percentile." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data[data['I'] > data['I'].quantile(0.30)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "...and plot just those rows, using a marker so it is easy to see which points have been disqualified due to low current." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data[data['I'] > data['I'].quantile(0.30)].plot(x='motor_slit', y='slit_det', marker='o')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Histogram the current" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.figure()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.hist(data['I'], bins='auto')\n", "plt.xlabel('current')\n", "plt.ylabel('count')\n", "plt.gcf()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Not enough data to be interesting. Try collecting a bunch of current data by itself and then try this again." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Creating a figure explicitly in advance helps with the\n", "# top-to-bottom flow of this notebook, but it is not necessary.\n", "# If this is omitted, bluesky will cause a figure to appear\n", "# during the RE(...) execution below.\n", "plt.figure(\"I vs time\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "RE(count([I], num=200, delay=0.1))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.gcf() # Display a snapshot of the current state of the figure." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.figure()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "more_current_data = db[-1].table()\n", "plt.hist(more_current_data['I'], bins='auto')\n", "plt.xlabel('current')\n", "plt.ylabel('count')\n", "plt.gcf()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Spend some time reading [10 minutes to pandas](https://pandas.pydata.org/pandas-docs/stable/10min.html), and use this notebook to try some of the examples there." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Hack away...." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.7.3" } }, "nbformat": 4, "nbformat_minor": 4 }