Commit 36abba5c authored by Arnaud WATLET's avatar Arnaud WATLET
Browse files

Adds plots module and plot_realtime_fulldata capability in run_sequence

No related merge requests found
Showing with 73 additions and 2 deletions
+73 -2
......@@ -24,6 +24,7 @@ import threading
from logging_setup import setup_loggers
from config import MQTT_CONTROL_CONFIG, OHMPI_CONFIG, EXEC_LOGGING_CONFIG
from logging import DEBUG
from plots import *
# finish import (done only when class is instantiated as some libs are only available on arm64 platform)
try:
......@@ -1235,7 +1236,7 @@ class OhmPi(object):
self.thread = threading.Thread(target=func)
self.thread.start()
def run_sequence(self, cmd_id=None, **kwargs):
def run_sequence(self, cmd_id=None, plot_realtime_fulldata=False, **kwargs):
"""Runs sequence synchronously (=blocking on main thread).
Additional arguments are passed to run_measurement().
......@@ -1314,7 +1315,6 @@ class OhmPi(object):
# switch mux off
self.switch_mux_off(quad)
# add command_id in dataset
acquired_data.update({'cmd_id': cmd_id})
# log data to the data logger
......@@ -1323,6 +1323,20 @@ class OhmPi(object):
self.append_and_save(filename, acquired_data)
self.exec_logger.debug(f'quadrupole {i + 1:d}/{n:d}')
if plot_realtime_fulldata:
realtime_plot_window = 10
plt.ion()
last_measurement = acquired_data["fulldata"][~np.isnan(acquired_data["fulldata"][:, 2])]
if i==0:
xlim = [last_measurement[:, 2][-1] - realtime_plot_window, last_measurement[:, 2][-1]]
fig, (ax1,ax2), (line1,line2) = plot_fulldata(last_measurement, realtime=True,xlim=xlim)
acquired_dataset = last_measurement
else:
fig,(ax1,ax2), (line1,line2), acquired_dataset = \
update_realtime_fulldata_plot(last_measurement, acquired_dataset, (line1,line2),(ax1,ax2), fig, x_window=realtime_plot_window )
if plot_realtime_fulldata:
return fig,(ax1,ax2), (line1,line2), acquired_dataset
self.switch_dps('off')
self.status = 'idle'
......
plots.py 0 → 100644
import matplotlib.pyplot as plt
import numpy as np
def plot_fulldata(fulldata, axes=None, fig=None, save=False, output="fulldata.png", show=False, realtime=False, xlim=None):
if axes is None:
fig, (ax1,ax2) = plt.subplots(2, sharex=True)
else:
(ax1,ax2) = axes
line1, = ax1.plot(fulldata[:, 2], fulldata[:, 0], 'r.-', label='current [mA]')
ax1.set_ylabel('Current (mA)')
ax1.set_title('Current')
line2, = ax2.plot(fulldata[:, 2], fulldata[:, 1], '.-', label='Voltage [mV]',alpha=.5)
ax2.set_xlabel('Time (s)')
ax2.set_ylabel('Voltage (mV)')
ax2.set_title('Voltage')
if xlim is not None:
ax1.set_xlim(xlim)
ax2.set_xlim(xlim)
if save:
fig.savefig(output,dpi=300,bbox_inches='tight')
if show:
plt.show()
if realtime:
fig.canvas.draw()
fig.canvas.flush_events()
return fig, (ax1,ax2), (line1,line2)
else:
return fig, (ax1, ax2)
def update_realtime_fulldata_plot(last_measurement, acquired_dataset, lines, axes, fig, x_window=10):
(line1, line2) = lines
(ax1,ax2) = axes
t = np.append(acquired_dataset[:,2], last_measurement[:, 2][~np.isnan(last_measurement[:, 2])] + acquired_dataset[:,2][-1])
i_rt = np.append(acquired_dataset[:,0], last_measurement[:, 0][~np.isnan(last_measurement[:, 2])])
u_rt = np.append(acquired_dataset[:,1], last_measurement[:, 1][~np.isnan(last_measurement[:, 2])])
line1.set_ydata(i_rt)
line1.set_xdata(t)
line2.set_ydata(u_rt)
line2.set_xdata(t)
ax1.relim()
ax2.relim()
ax1.autoscale_view(scalex=False)
ax2.autoscale_view(scalex=False)
ax1.set_xlim([t[-1] - x_window, t[-1]])
ax2.set_xlim([t[-1] - x_window, t[-1]])
fig.canvas.draw()
fig.canvas.flush_events()
return fig,(ax1,ax2), (line1,line2), np.array([i_rt,u_rt,t]).T
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment