diff --git a/bin/peus-plot b/bin/peus-plot old mode 100644 new mode 100755 index 76181b2..22044a4 --- a/bin/peus-plot +++ b/bin/peus-plot @@ -1,19 +1,24 @@ #!/usr/bin/env python3 import sys +from pathlib import Path +# Add import path for inplace usage +sys.path.insert(0, str(Path(__file__).absolute().parents[1])) + from frappy.client.interactive import Client from frappy_psi.iqplot import Plot import numpy as np import matplotlib.pyplot as plt + if len(sys.argv) < 2: - print('Usage: python peusplot.py ') + print('Usage: peus-plot ') def get_modules(name): return list(filter(None, (globals().get(name % i) for i in range(10)))) -secnode = Client('localhost:5000') +secnode = Client('pc13252:5000') time_size = {'time', 'size'} int_mods = [u] + get_modules('roi%d') t_rois = get_modules('roi%d') @@ -35,12 +40,11 @@ try: while True: curves = np.array(u.get_curves()) iqplot.plot(curves, - rois=[(r.time, r.time + r.size) for r in int_mods], + rois=[(r.time - r.size * 0.5, r.time + r.size * 0.5) for r in int_mods], average=([r.time for r in t_rois], [r.value for r in i_rois], [r.value for r in q_rois])) - plt.pause(0.5) - if iqplot.fig is None: # graph window closed + if not iqplot.pause(0.5): break except KeyboardInterrupt: iqplot.close() diff --git a/bin/us-plot b/bin/us-plot old mode 100644 new mode 100755 index 7421c59..8c44fc1 --- a/bin/us-plot +++ b/bin/us-plot @@ -1,8 +1,13 @@ #!/usr/bin/env python3 import sys +from pathlib import Path +# Add import path for inplace usage +sys.path.insert(0, str(Path(__file__).absolute().parents[1])) + from frappy.client.interactive import Client import numpy as np import matplotlib.pyplot as plt +from frappy.iqplot import Pause Client('pc13252:5000') @@ -40,7 +45,7 @@ else: npoints = float(sys.argv[3]) fig, ax = plt.subplots(figsize=(15,3)) - fig.canvas.mpl_connect('close_event', on_close) + pause = Pause(fig) try: get_signal = iq.get_signal print('plotting RUS signal') @@ -52,8 +57,7 @@ else: lines = [plot(s, ax, '-', xs) for s in signal] - while True: - plt.pause(0.5) + while pause(0.5): plt.draw() xs, signal = get_signal(start, end, npoints) for line, sig in zip(lines, signal): diff --git a/frappy_psi/iqplot.py b/frappy_psi/iqplot.py index 280a7ed..5e9baec 100644 --- a/frappy_psi/iqplot.py +++ b/frappy_psi/iqplot.py @@ -6,10 +6,40 @@ Created on Tue Feb 4 11:07:56 2020 import numpy as np import matplotlib.pyplot as plt +# disable the behaviour of raising the window to the front each time it is updated +plt.rcParams["figure.raise_window"] = False + NAN = float('nan') +class Pause: + """allows to leave the plot loop when the window is closed + + Usage: + pause = Pause(fig) + + # do initial plots + plt.show() + while pause(0.5): + # do plot updates + plt.draw() + """ + def __init__(self, fig): + fig.canvas.mpl_connect('close_event', self.on_close) + self.running = True + + def on_close(self, event): + self.running = False + + def __call__(self, interval): + try: + plt.pause(interval) + except Exception: + pass + return self.running + + def rect(x1, x2, y1, y2): return np.array([[x1,x2,x2,x1,x1],[y1,y1,y2,y2,y1]]) @@ -29,6 +59,10 @@ class Plot: self.first = True self.fig = None + def pause(self, interval): + """will be overridden when figure is created""" + return False + def set_line(self, iax, name, data, fmt, **kwds): """ plot or update a line @@ -57,9 +91,6 @@ class Plot: self.fig = None self.first = True - def on_close(self, event): - self.fig = None - def plot(self, curves, rois=None, average=None): boxes = rects(rois[1:], self.yaxis[0]) pbox = rect(*rois[0], *self.yaxis[1]) @@ -74,7 +105,7 @@ class Plot: if self.first: plt.ion() self.fig, axleft = plt.subplots(figsize=(15,7)) - self.fig.canvas.mpl_connect('close_event', self.on_close) + self.pause = Pause(self.fig) plt.title("I/Q", fontsize=14) axleft.set_xlim(0, curves[0][-1]) self.ax = [axleft, axleft.twinx()] @@ -104,5 +135,6 @@ class Plot: self.first = False plt.draw() + # TODO: do not know why this is needed: self.fig.canvas.draw() self.fig.canvas.flush_events()