From 36abba5c0ae2f47c4ca133747a2146c246e5d400 Mon Sep 17 00:00:00 2001 From: "remi.clement@inrae.fr" <arnaud.watlet@umons.ac.be> Date: Wed, 19 Apr 2023 13:49:34 +0000 Subject: [PATCH] Adds plots module and plot_realtime_fulldata capability in run_sequence --- ohmpi.py | 18 ++++++++++++++++-- plots.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 plots.py diff --git a/ohmpi.py b/ohmpi.py index b0dbd309..791a69b3 100644 --- a/ohmpi.py +++ b/ohmpi.py @@ -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' diff --git a/plots.py b/plots.py new file mode 100644 index 00000000..6cef71da --- /dev/null +++ b/plots.py @@ -0,0 +1,57 @@ +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 -- GitLab