2
0
mirror of https://github.com/m-labs/artiq.git synced 2024-12-05 01:36:39 +08:00
artiq/examples/artiq_ipython_notebook.ipynb

371 lines
24 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"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",
"from pprint import pprint\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": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# let's assume artiq_master and artiq_ctlmgr are already running\n",
"# then 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": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# we can directly use the artiq controller infrastructure\n",
"# and access any artiq device\n",
"\n",
"# we can have artiq prepare that connection for us:\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 we can wire it up ourselves if you know where it is\n",
"assert ddb.get(\"lda\")[\"host\"] == \"::1\"\n",
"assert ddb.get(\"lda\")[\"port\"] == 3253\n",
"\n",
"# there are different Client types tailored to different use cases:\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": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"current schedule\n",
"{}\n",
"experiments:\n",
"['ex/',\n",
" 'test_analyzer.py',\n",
" 'notebook_test.py',\n",
" 'speed_benchmark.py',\n",
" 'histograms.py',\n",
" 'arguments_demo.py',\n",
" '.git/',\n",
" '__pycache__/',\n",
" 'flopping_f_simulation.py',\n",
" 'test_crash.py',\n",
" 'run_forever.py',\n",
" 'transport.py',\n",
" 'pdq2_simple.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\")\n",
"pprint(schedule.get_status())\n",
"print(\"experiments:\")\n",
"pprint(exps.list_directory(\"repository\"))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"current schedule\n",
"{4722: {'due_date': None,\n",
" 'expid': {'arguments': {'F0': 1500, 'noise_amplitude': 0.3},\n",
" 'class_name': 'FloppingF',\n",
" 'file': 'repository/flopping_f_simulation.py',\n",
" 'log_level': 30},\n",
" 'flush': False,\n",
" 'pipeline': 'main',\n",
" 'priority': 0,\n",
" 'repo_msg': None,\n",
" 'status': 'preparing'}}\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=.3,\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\")\n",
"pprint(schedule.get_status())"
]
},
{
"cell_type": "code",
"execution_count": 7,
"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": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"flopping_f: 1499.944285221012\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAECCAYAAAAFL5eMAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xt4VOWBP/DvOXOfTC4ECJckJCFcE4FKEsyqIKuywtbd\nLRZXYWVlpf1x2fpYeFCUi6GR1a3bZ+tPH9tKf219DF2wVfyt9de1NdIahSgwBeRiuIdLICGEXOY+\nZ+ac3x9JJkRCZpLMBOad7+d5eB4yM+ecN2/OfOed97znfSVN0zQQEZFQ5JtdACIiij6GOxGRgBju\nREQCYrgTEQmI4U5EJCCGOxGRgPThXhAIBLB27VrU1dVBr9fjhRdegNfrxbJly5CbmwsAWLhwIebN\nmxfrshIRUYSkcOPcP/74Y3zwwQf48Y9/jN27d2P79u2YOXMmXC4XlixZMkjFJCKivgjbcs/NzUUw\nGISmaXA4HDAYDDhy5AjOnDmDyspK5OTkYP369bBarYNRXiIiikDYlnt9fT1WrlwJl8uFlpYWvPHG\nGzhz5gwmTpyIgoIC/OxnP0NrayvWrl07WGUmIqIwwl5QffPNNzFz5kz84Q9/wPvvv4+1a9di1qxZ\nKCgoAADMmTMHNTU1MS8oERFFLmy3TGp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h3TlSiCo4fwVH05e8uFM\nvReTsi2YVZgSer6/4R62/Z+amgq9vv1lycnJCAQCKCgowJ49ezBjxgxUVVWhtLQ07IF6K+Dsu9u/\nIp6ua0FmRjJslu5dBa1OH/yKiuFD+v5G+rpZYZ6va3TCZNBhWNqNj3XHNf8PqhoarrrQ3OZD5nAb\n0pK7QsbrD6D2UhsMOhl
"text/plain": [
"<matplotlib.figure.Figure at 0x7fc5187c8668>"
]
},
"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": 9,
"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": 10,
"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 does not have\n",
"# the metadata pandas want\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": 11,
"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 and 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": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4724\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
}