artiq/examples/artiq_ipython_notebook.ipynb

342 lines
26 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Populating the interactive namespace from numpy and matplotlib\n"
]
}
],
"source": [
"%pylab inline\n",
"\n",
"import os\n",
"import logging\n",
"import time\n",
"import asyncio\n",
"import datetime\n",
"import glob\n",
"\n",
"import numpy as np\n",
"np.set_printoptions(precision=3)\n",
"import matplotlib.pyplot as plt\n",
"import seaborn\n",
"seaborn.set_style(\"whitegrid\")\n",
"import pandas as pd\n",
"import h5py\n",
"\n",
"from artiq.protocols.pc_rpc import (Client, AsyncioClient,\n",
" BestEffortClient, AutoTarget)\n",
"from artiq.master.databases import DeviceDB\n",
"from artiq.master.worker_db import DeviceManager"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# let's assume artiq_master and artiq_ctlmgr are already running\n",
"# move to a location where we have our artiq setup\n",
"os.chdir(os.path.expanduser(\"~/work/nist/artiq/run\"))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# you can directly use the artiq controller infrastructure\n",
"# and access any artiq device\n",
"\n",
"# you can have artiq prepare that for you:\n",
"\n",
"ddb = DeviceDB(\"device_db.pyon\")\n",
"devmgr = DeviceManager(ddb)\n",
"lda = devmgr.get(\"lda\")\n",
"lda.set_attenuation(42)\n",
"assert lda.get_attenuation() == 42\n",
"\n",
"# ... or you can wire it up yourself if you know where it is\n",
"assert ddb.get(\"lda\")[\"host\"] == \"::1\"\n",
"assert ddb.get(\"lda\")[\"port\"] == 3253\n",
"\n",
"# synchronous\n",
"lda = Client(\"::1\", 3253)\n",
"assert lda.get_attenuation() == 42\n",
"\n",
"# asyncio\n",
"lda = AsyncioClient()\n",
"async def test_lda():\n",
" await lda.connect_rpc(\"::1\", 3253, AutoTarget)\n",
" return await lda.get_attenuation()\n",
"assert asyncio.get_event_loop().run_until_complete(test_lda()) == 42\n",
"\n",
"# best effort\n",
"lda = BestEffortClient(\"::1\", 3253, AutoTarget)\n",
"assert lda.get_attenuation() == 42"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"current schedule {}\n",
"experiments ['__pycache__/', 'flopping_f_simulation.py', 'notebook_test.py', '.git/', 'idle.elf', 'transport.py', 'idle.py', 'speed_benchmark.py', 'test_raise.py']\n"
]
}
],
"source": [
"# let's connect to the master\n",
"\n",
"schedule, exps, datasets = [\n",
" Client(\"::1\", 3251, \"master_\" + i) for i in\n",
" \"schedule experiment_db dataset_db\".split()]\n",
"\n",
"print(\"current schedule\", schedule.get_status())\n",
"print(\"experiments\", exps.list_directory(\"repository\"))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"current schedule {131: {'priority': 0, 'status': 'preparing', 'repo_msg': None, 'pipeline': 'main', 'due_date': None, 'flush': False, 'expid': {'file': 'repository/flopping_f_simulation.py', 'arguments': {'noise_amplitude': 0.1, 'F0': 1500}, 'log_level': 30, 'class_name': 'FloppingF'}}}\n"
]
}
],
"source": [
"# we can submit experiments to be run:\n",
"\n",
"expid = dict(\n",
" file=\"repository/flopping_f_simulation.py\",\n",
" class_name=\"FloppingF\",\n",
" log_level=logging.WARNING,\n",
" arguments=dict(\n",
" F0=1500,\n",
" noise_amplitude=.1,\n",
" ),\n",
")\n",
"if not schedule.get_status():\n",
" rid = schedule.submit(pipeline_name=\"main\", expid=expid,\n",
" priority=0, due_date=None, flush=False)\n",
"print(\"current schedule\", schedule.get_status())"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# wait for experiment to finish\n",
"# this can be written nicer by subscribing and reacting to scheduler changes\n",
"while rid in schedule.get_status():\n",
" time.sleep(.1)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"flopping_f 1499.996784076909\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAecAAAFVCAYAAADVDycqAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xl0FNeBLvCvqqv31oYQi1gkmc0gg+0ICI4NZhKTgTjz\nMtjwJmATE/PyMEwyOSY42AYPBJPxJHGenclzjs1zJh6L2DjxMsnYWWawEzAgB5DZ1wEjNi1sWnrv\nqq56f7S6BUZSt6Tu1i3p+52jgyV1V11dd9fXd6l7JcMwDBAREZEw5N4uABEREV2P4UxERCQYhjMR\nEZFgGM5ERESCYTgTEREJhuFMREQkGCXZAzRNw6pVq3DhwgUoioKnn34aoVAIS5cuRWlpKQBgwYIF\nmDNnTqbLSkRE1C9Iye5zfv/99/Huu+/iueeew86dO7F582ZMnz4dfr8fixcvzlIxiYiI+o+kLefS\n0lJEo1EYhgGv1wur1YrDhw/j9OnT2LJlC0pKSrB69Wq4XK5slJeIiKjPS9pyrq+vx/Lly+H3+9HU\n1ISXXnoJp0+fxrhx4zBhwgS8+OKLaG5uxqpVq7JVZiIioj4tacv5lVdewfTp0/Hoo4+ioaEBX/va\n1/Daa6+hsLAQADBr1ixs2LCh02NUV1enp7REREQmUlFR0a3nJQ3nvLw8KErsYTk5OVBVFY888gie\neuopTJo0CVVVVSgvL89YASl11dXVrOcMYx1nHus4O1jPmdeThmnScH7ooYfw5JNP4oEHHoCmaVi5\nciXKysqwfv16WK1WFBUVYf369d0uABEREV0vaTi7XC48//zzN/z89ddfz0iBiIiI+jsuQkJERCQY\nhjMREZFgGM5ERESCYTgTEREJhuFMREQkGIYzERGRYBjOREREgmE4ExERCYbhTEREJBiGMxERkWAY\nzkRERIJhOBMREQmG4UxERCQYhjM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Rl8UjT4NjT4N/pAOl12G2y7D7bDA7ZBhUyTIkgRZjr0wLTLg\nsMpw2GJfVkvsE1lUB9SogYimQ9WM2Fc09m9YM9Ds13DFq+Fq61dEM5DvtiDfo8T+dStw2WU4W4/r\ntMnQDQMtgShaAlE0B6Jo9kdx1Rd7fkug7Q2lWCQMybdiSIEVRXlWWCyxkNT12AW+vcpVLBIUiwRr\n67/x+mjxx87XEowiGNYRCOtQW0NfloCiPCuKB1gxdIANg/KtcNnidRE7VjCiX1enDU0qzlwMwxvs\n+E3rccgYlB8r+6A8K4ryFNitMmSprc4jmgFva7m8wSiutGg4WReCP6S3/j2A22GBPxSF9qlrjdUi\noXSwHaOHOjAwT0n8zYpFgqoZOH85grOXwjhzKQxfazndDhmTR7sxeYwHOc7rWwpa1MDBmgCqjnlx\nsTn2abp4gBXFrXUyKN8Kh1XC76ubcOZiBE67jHsn5+OWEleHdfBpUd3Ae7ub8PEpf+JnQwqsGJir\n4NCZIIYWWLHki4OgWHoeUle9Gl78fQMimoEFdxdi3DBnj4+Zbv+xqxHVJ2N1cd8dAzCpLPW67Egw\nomPjHxrQ6IuieIAVdY1q4uLvccr4u+mFGDEweYsuLhCO4k8HWrDnpB+GAYwpdmBiqQsXLkdQczGM\nhqa2Hg+HVULZEAdGD7WjdLADBR4L5E994KhrjGDfqQAO1AQQjMRel7IEjCiyYcRAO4IRHU1+DU3+\nKJr92nWve8UCFOVaIcux64BuxF5TV30a9GveigNzFQwtsGJwgRWD860YnG+D3SohqscaHFHDQCCk\no75RRV1jBHVXVTQ0qdCiBiQp9oFcggSrIiHfbUGBR0FB6zUtx2mBy9F6LbXLsMgSQqqOUERHKGLA\nH4qirlHF+csRXLgSSfy
"text/plain": [
"<matplotlib.figure.Figure at 0x7f0d5095f2b0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# now that the experiment has completed, we can get the\n",
"# current value of the (live) dataset and plot it\n",
"# had we done this earlier, the dataset would have been incomplete\n",
"fig, ax = plt.subplots()\n",
"d = datasets.get(\"flopping_f_brightness\")\n",
"ax.plot(d)\n",
"print(\"flopping_f\", datasets.get(\"flopping_freq\"))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# this is how you would clear all pipelines\n",
"for i in schedule.get_status():\n",
" schedule.delete(i)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"available datasets ['artiq_version', 'flopping_f_brightness']\n"
]
}
],
"source": [
"# we can easily find and use the data that was saved as part\n",
"# of the experiment\n",
"\n",
"t = datetime.datetime.now()\n",
"f = os.path.join(\n",
" \"results\", t.strftime(\"%Y-%m-%d\"), #t.strftime(\"%H-%M\"),\n",
" \"*\", \"{:09d}-FloppingF.h5\".format(rid))\n",
"\n",
"# we would usually like to use pandas but our data doe not comply\n",
"# with the pandas metadata\n",
"#d = pd.HDFStore(glob.glob(f)[0])\n",
"\n",
"with h5py.File(glob.glob(f)[0]) as f:\n",
" print(\"available datasets\", list(f))\n",
" assert np.allclose(f[\"flopping_f_brightness\"], d)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting repository/notebook_test.py\n"
]
}
],
"source": [
"%%writefile repository/notebook_test.py\n",
"\n",
"# we can also write experiments in the notebook ans submit them\n",
"# we don't have submit-by-content yet (and there would be questions\n",
"# about other modules that would need to be imported) so we just export\n",
"# this cell and submit it by filename\n",
"\n",
"from artiq.experiment import *\n",
"\n",
"class Hello(EnvExperiment):\n",
" def build(self):\n",
" pass\n",
" \n",
" def run(self):\n",
" print(\"Hello world!\")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"133\n"
]
}
],
"source": [
"expid = dict(\n",
" file=\"repository/notebook_test.py\",\n",
" class_name=\"Hello\",\n",
" log_level=logging.WARNING,\n",
" arguments=dict(),\n",
")\n",
"rid = schedule.submit(pipeline_name=\"misc\", expid=expid,\n",
" priority=1, due_date=None, flush=False)\n",
"print(rid)\n",
"# on the master you should see the message."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}