Debian Codegraph @

pymeasure/experiment/experiment.py

raw
1 #
2 # This file is part of the PyMeasure package.
3 #
4 # Copyright (c) 2013-2024 PyMeasure Developers
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # THE SOFTWARE.
23 #
24
25 import logging
26 import time
27 import tempfile
28 import gc
29
30 import numpy as np
31
32 from .results import unique_filename
33 from .config import get_config, set_mpl_rcparams
34 from pymeasure.log import setup_logging, console_log
35 from pymeasure.experiment import Results, Worker
36
37 log = logging.getLogger()
38 log.addHandler(logging.NullHandler())
39
40 try:
41 from IPython import display
42 except ImportError:
43 log.warning("IPython could not be imported")
44
45
46 def get_array(start, stop, step):
47 """Returns a numpy array from start to stop"""
48 step = np.sign(stop - start) * abs(step)
49 return np.arange(start, stop + step, step)
50
51
52 def get_array_steps(start, stop, numsteps):
53 """Returns a numpy array from start to stop in numsteps"""
54 return get_array(start, stop, (abs(stop - start) / numsteps))
55
56
57 def get_array_zero(maxval, step):
58 """Returns a numpy array from 0 to maxval to -maxval to 0"""
59 return np.concatenate((np.arange(0, maxval, step), np.arange(maxval, -maxval, -step),
60 np.arange(-maxval, 0, step)))
61
62
63 def create_filename(title):
64 """
65 Create a new filename according to the style defined in the config file.
66 If no config is specified, create a temporary file.
67 """
68 config = get_config()
69 if 'Filename' in config._sections.keys():
70 filename = unique_filename(suffix='_%s' % title, **config._sections['Filename'])
71 else:
72 filename = tempfile.mktemp()
73 return filename
74
75
76 class Experiment:
77 """ Class which starts logging and creates/runs the results and worker processes.
78
79 .. code-block:: python
80
81 procedure = Procedure()
82 experiment = Experiment(title, procedure)
83 experiment.start()
84 experiment.plot_live('x', 'y', style='.-')
85
86 for a multi-subplot graph:
87
88 import pylab as pl
89 ax1 = pl.subplot(121)
90 experiment.plot('x','y',ax=ax1)
91 ax2 = pl.subplot(122)
92 experiment.plot('x','z',ax=ax2)
93 experiment.plot_live()
94
95 :var value: The value of the parameter
96
97 :param title: The experiment title
98 :param procedure: The procedure object
99 :param analyse: Post-analysis function, which takes a pandas dataframe as input and
100 returns it with added (analysed) columns. The analysed results are accessible via
101 experiment.data, as opposed to experiment.results.data for the 'raw' data.
102 :param _data_timeout: Time limit for how long live plotting should wait for datapoints.
103 """
104
105 def __init__(self, title, procedure, analyse=(lambda x: x)):
106 self.title = title
107 self.procedure = procedure
108 self.measlist = []
109 self.port = 5888
110 self.plots = []
111 self.figs = []
112 self._data = []
113 self.analyse = analyse
114 self._data_timeout = 10
115
116 config = get_config()
117 set_mpl_rcparams(config)
118 if 'Logging' in config._sections.keys():
119 self.scribe = setup_logging(log, **config._sections['Logging'])
120 else:
121 self.scribe = console_log(log)
122 self.scribe.start()
123
124 self.filename = create_filename(self.title)
125 log.info("Using data file: %s" % self.filename)
126
127 self.results = Results(self.procedure, self.filename)
128 log.info("Set up Results")
129
130 self.worker = Worker(self.results, self.scribe.queue, logging.DEBUG)
131 log.info("Create worker")
132
133 def start(self):
134 """Start the worker"""
135 log.info("Starting worker...")
136 self.worker.start()
137
138 @property
139 def data(self):
140 """Data property which returns analysed data, if an analyse function
141 is defined, otherwise returns the raw data."""
142 self._data = self.analyse(self.results.data.copy())
143 return self._data
144
145 def wait_for_data(self):
146 """Wait for the data attribute to fill with datapoints."""
147 t = time.time()
148 while self.data.empty:
149 time.sleep(.1)
150 if (time.time() - t) > self._data_timeout:
151 log.warning('Timeout, no data received for liveplot')
152 return False
153 return True
154
155 def plot_live(self, *args, **kwargs):
156 """Live plotting loop for jupyter notebook, which automatically updates
157 (an) in-line matplotlib graph(s). Will create a new plot as specified by input
158 arguments, or will update (an) existing plot(s)."""
159 if self.wait_for_data():
160 if not (self.plots):
161 self.plot(*args, **kwargs)
162 while not self.worker.should_stop():
163 self.update_plot()
164 display.clear_output(wait=True)
165 if self.worker.is_alive():
166 self.worker.terminate()
167 self.scribe.stop()
168
169 def plot(self, *args, **kwargs):
170 """Plot the results from the experiment.data pandas dataframe. Store the
171 plots in a plots list attribute."""
172 if self.wait_for_data():
173 kwargs['title'] = self.title
174 ax = self.data.plot(*args, **kwargs)
175 self.plots.append({'type': 'plot', 'args': args, 'kwargs': kwargs, 'ax': ax})
176 if ax.get_figure() not in self.figs:
177 self.figs.append(ax.get_figure())
178 self._user_interrupt = False
179
180 def clear_plot(self):
181 """Clear the figures and plot lists."""
182 for fig in self.figs:
183 fig.clf()
184 for pl in self.plots:
185 pl.close()
186 self.figs = []
187 self.plots = []
188 gc.collect()
189
190 def update_plot(self):
191 """Update the plots in the plots list with new data from the experiment.data
192 pandas dataframe."""
193 try:
194 self.data
195 for plot in self.plots:
196 ax = plot['ax']
197 if plot['type'] == 'plot':
198 x, y = plot['args'][0], plot['args'][1]
199 if type(y) == str:
200 y = [y]
201 for yname, line in zip(y, ax.lines):
202 self.update_line(ax, line, x, yname)
203
204 display.clear_output(wait=True)
205 display.display(*self.figs)
206 time.sleep(0.1)
207 except KeyboardInterrupt:
208 display.clear_output(wait=True)
209 display.display(*self.figs)
210 self._user_interrupt = True
211
212 def update_line(self, ax, hl, xname, yname):
213 """Update a line in a matplotlib graph with new data."""
214 del hl._xorig, hl._yorig
215 hl.set_xdata(self._data[xname])
216 hl.set_ydata(self._data[yname])
217 ax.relim()
218 ax.autoscale()
219 gc.collect()
220
221 def __del__(self):
222 self.scribe.stop()
223 if self.worker.is_alive():
224 self.worker.recorder_queue.put(None)
225 self.worker.monitor_queue.put(None)
226 self.worker.stop()