{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# How to use Plottr with QCoDeS for live plotting?\n", "\n", "Note: This notebook assumes that the user has a `conda` environment on their system with latest versions of `plottr` installed. It is not necessary to have plottr installed in measurements environment itself. Plottr can be in a separate environment as well. If you do not have plottr installed, please follow [Plottr Quickstart (Installation)](https://github.com/toolsforexperiments/plottr#quickstart) instructions.\n", "\n", "Let's begin!\n", "\n", "First, make necessary imports." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-12-07T12:38:25.294819Z", "start_time": "2020-12-07T12:38:21.906894Z" } }, "outputs": [], "source": [ "import os\n", "\n", "import numpy as np\n", "\n", "import qcodes as qc\n", "from qcodes.dataset import (\n", " Measurement,\n", " do1d,\n", " initialise_or_create_database_at,\n", " load_or_create_experiment,\n", ")\n", "from qcodes.instrument_drivers.mock_instruments import (\n", " DummyInstrument,\n", " DummyInstrumentWithMeasurement,\n", ")\n", "\n", "qc.logger.start_all_logging()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Mock Set-up\n", "\n", "For this notebook, we create a mock station setup. " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2020-12-07T12:38:35.103026Z", "start_time": "2020-12-07T12:38:35.092493Z" } }, "outputs": [], "source": [ "dac = DummyInstrument('dac', gates=['ch1', 'ch2'])\n", "dmm = DummyInstrumentWithMeasurement(name='dmm', setter_instr=dac)\n", "\n", "station = qc.Station(dmm, dac)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Initialize/Create Database\n", "\n", "Database needs to be initialized or created if it doesn't exist already, using `initialize_or_create_database` method. Furthermore, datasets are associated with experiments and by default the run is appended to the latest existing experiment. We can load or create an experiment using `load_or_create_experiment` function. " ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2020-12-07T12:38:48.235500Z", "start_time": "2020-12-07T12:38:48.166689Z" } }, "outputs": [], "source": [ "db_file_path = os.path.join(os.getcwd(), 'plottr_for_live_plotting_tutorial.db')\n", "initialise_or_create_database_at(db_file_path)\n", "exp = load_or_create_experiment(experiment_name='plottr_for_live_plotting_with_subsecond_refresh_rate',\n", " sample_name=\"no sample\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Launch Plottr Inspectr \n", "\n", "Open an anaconda prompt and activate your conda environment where plottr is installed. Start Inspectr GUI with `plottr-inspectr` command (more details for launching Inspectr are [here](https://github.com/toolsforexperiments/plottr#inspectr-qcodes-dataset-inspection-and-live-plotting). You will see a window as show below. In this window, set `Refresh interval (s)` to the desired value and check `Auto-plot new`. Using `File` button, load the database initialized/created above (Alternatively, database can be dragged and dropped on the Inspectr window).\n", "\n", "![Plottr-inspectr Window](../files/plottr.png)\n", "\n", "On selection, refresh interval and auto-plot new checkbox will look like as follows.\n", "\n", "![Refresh Interval and Autoplot](../files/plottr_set_refresh_interval_and_autoplot.png)\n", "\n", "### If plottr is installed in same environment as measurements environment\n", "\n", "Plottr-inspectr can also be launched with required DB as follows." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2020-12-07T12:39:53.719816Z", "start_time": "2020-12-07T12:39:52.824390Z" } }, "outputs": [ { "data": { "text/plain": [ ">" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import IPython.lib.backgroundjobs as bg\n", "from plottr.apps import inspectr\n", "\n", "jobs = bg.BackgroundJobManager()\n", "jobs.new(inspectr.main, db_file_path)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set refresh interval and auto-plot new as mentioned above. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Measurement\n", "\n", "Measurements can be run in two ways.\n", "\n", "### Measurement with run context manager\n", "\n", "Notice `write_period = 0.1` . This sets the measurement write period to 0.1s and is recommended to be used with sub-second refresh interval for plottr." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2020-12-07T12:41:40.283199Z", "start_time": "2020-12-07T12:41:40.215924Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Starting experimental run with id: 3. \n" ] } ], "source": [ "meas = Measurement(exp=exp)\n", "meas.register_parameter(dac.ch1)\n", "meas.register_parameter(dmm.v1, setpoints=(dac.ch1,))\n", "\n", "meas.write_period = 0.1\n", "\n", "with meas.run() as datasaver:\n", "\n", " for set_v in np.linspace(0, 25, 100):\n", " dac.ch1.set(set_v)\n", " get_v = dmm.v1.get()\n", " datasaver.add_result((dac.ch1, set_v),\n", " (dmm.v1, get_v))\n", "\n", " dataset = datasaver.dataset" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Measurement with doNd" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-12-07T12:42:07.870434Z", "start_time": "2020-12-07T12:42:06.117442Z" } }, "outputs": [], "source": [ "do1d(dac.ch1, 0, 25, 100, 0.01, dmm.v1, dmm.v2, write_period=0.1, do_plot=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On starting these measurements, a plottr plot window (as shown below) will open automatically. This plot will keep refreshing at the interval rate set by you till the measurement runs." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Live Plot\n", "\n", "![Live Plot](../files/live_plot.gif)\n", "\n", "For more details about Plottr, head to [Plottr Documentation](https://plottr.readthedocs.io/en/latest/index.html)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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.7.9" }, "nbsphinx": { "execute": "never" } }, "nbformat": 4, "nbformat_minor": 4 }