In this notebook you will:
pandas
.Recommended Prerequisites:
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.
!supervisorctl -c supervisor/supervisord.conf status
%run scripts/beamline_configuration.py
Let's take sample data.
# Creating a figure explicitly in advance helps with the
# top-to-bottom flow of this notebook, but it is not necessary.
# If this is omitted, bluesky will cause a figure to appear
# during the RE(...) execution below.
plt.figure('I slit_det vs motor_slit')
RE(scan([slit, I], motor_slit, -10, 10, 15))
plt.gcf() # Display a snapshot of the current state of the figure.
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.
header = db[-1] # Header for the most recent one (i.e. 1 run ago)
We can access the data as a table. This object is called a pandas.DataFrame
, a high-performance, spreadsheet-like object in Python.
data = header.table()
It display prettily, truncating if needed.
data
For large tables, it's handy to know how to ask for just the first or last couple rows.
It is easy to visualize.
data.plot(x='motor_slit', y='slit_det')
plt.gcf() # Display a snapshot of the current state of the figure.
data.head()
data.tail()
Statistics on columns are simple to compute.
data.mean()
data.min()
Or just ask for all the common stats at once:
data.describe()
We can do math on the columns, and we can add new columns.
data['normalized'] = data['slit_det'] / data['I'] * data['I'].mean()
We can plot them conveniently, addressing the columns by name.
plt.figure()
plt.plot('motor_slit', 'slit_det', data=data, label='raw')
plt.plot('motor_slit', 'normalized', data=data, label='normalized')
plt.legend()
plt.gcf()
Select rows where the current is above 500
.
data[data['I'] > 500]
...or, where the the current is above the 30th percentile.
data[data['I'] > data['I'].quantile(0.30)]
...and plot just those rows, using a marker so it is easy to see which points have been disqualified due to low current.
data[data['I'] > data['I'].quantile(0.30)].plot(x='motor_slit', y='slit_det', marker='o')
plt.figure()
plt.hist(data['I'], bins='auto')
plt.xlabel('current')
plt.ylabel('count')
plt.gcf()
Not enough data to be interesting. Try collecting a bunch of current data by itself and then try this again.
# Creating a figure explicitly in advance helps with the
# top-to-bottom flow of this notebook, but it is not necessary.
# If this is omitted, bluesky will cause a figure to appear
# during the RE(...) execution below.
plt.figure("I vs time")
RE(count([I], num=200, delay=0.1))
plt.gcf() # Display a snapshot of the current state of the figure.
plt.figure()
more_current_data = db[-1].table()
plt.hist(more_current_data['I'], bins='auto')
plt.xlabel('current')
plt.ylabel('count')
plt.gcf()
Spend some time reading 10 minutes to pandas, and use this notebook to try some of the examples there.
# Hack away....