Experiment class for live in-line plotting with jupyter

This example uses the Experiment class to create a measurement from a procedure object.


In [1]:
%%writefile procedures.py
import random
from time import sleep

import logging
log = logging.getLogger('')
log.addHandler(logging.NullHandler())

from pymeasure.experiment import Procedure, IntegerParameter, Parameter, FloatParameter

class TestProcedure(Procedure):

    iterations = IntegerParameter('Loop Iterations', default=100)
    delay = FloatParameter('Delay Time', units='s', default=0.2)
    seed = Parameter('Random Seed', default='12345')

    DATA_COLUMNS = ['Iteration', 'Random Number']

    def startup(self):
        log.info("Setting up random number generator")
        random.seed(self.seed)

    def execute(self):
        log.info("Starting to generate numbers")
        for i in range(self.iterations):
            data = {
                'Iteration': i,
                'Random Number': random.random()
            }
            log.debug("Produced numbers: %s" % data)
            self.emit('results', data)
            self.emit('progress', 100.*i/self.iterations)
            sleep(self.delay)
            if self.should_stop():
                log.warning("Catch stop command in procedure")
                break

    def shutdown(self):
        log.info("Finished")


Writing procedures.py

In [2]:
from pymeasure.experiment import Experiment
from procedures import TestProcedure
%matplotlib inline

In [3]:
experiment = Experiment('test', TestProcedure(iterations=100, delay=.1))


01:30:01 PM: Set up console logging (root, INFO)
01:30:01 PM: Set up file logging (root, INFO)
01:30:01 PM: Starting logger scribe (pymeasure.log, INFO)
01:30:01 PM: Using data file: /Users/Guen/Development/pymeasure/examples/data/DATA2016-02-10_1_test.csv (root, INFO)
01:30:01 PM: Set up Results (root, INFO)
01:30:01 PM: Create worker (root, INFO)

In [4]:
experiment.start()
experiment.plot_live('Iteration', 'Random Number')



In [ ]: