|
| 1 | +""" |
| 2 | +Distributed under the terms of the BSD 3-Clause License. |
| 3 | +
|
| 4 | +The full license is in the file LICENSE, distributed with this software. |
| 5 | +
|
| 6 | +Author: Jun Zhu <[email protected]> |
| 7 | +Copyright (C) European X-Ray Free-Electron Laser Facility GmbH. |
| 8 | +All rights reserved. |
| 9 | +""" |
| 10 | +import time |
| 11 | +from enum import IntEnum |
| 12 | + |
| 13 | +import numpy as np |
| 14 | + |
| 15 | +from PyQt5.QtCore import QTimer |
| 16 | + |
| 17 | +from extra_foam.gui import mkQApp |
| 18 | +from extra_foam.gui.plot_widgets import PlotWidgetF |
| 19 | + |
| 20 | +app = mkQApp() |
| 21 | + |
| 22 | + |
| 23 | +class PlotType(IntEnum): |
| 24 | + Line = 0 |
| 25 | + Bar = 1 |
| 26 | + StatisticsBar = 2 |
| 27 | + Scatter = 3 |
| 28 | + |
| 29 | + |
| 30 | +class BenchmarkPlotItemSpeed: |
| 31 | + def __init__(self, plot_type=PlotType.Line): |
| 32 | + self._timer = QTimer() |
| 33 | + self._timer.timeout.connect(self.update) |
| 34 | + |
| 35 | + self._widget = PlotWidgetF() |
| 36 | + |
| 37 | + if plot_type == PlotType.Line: |
| 38 | + self._graph = self._widget.plotCurve() |
| 39 | + n_pts = 5000 |
| 40 | + elif plot_type == PlotType.Bar: |
| 41 | + self._graph = self._widget.plotBar() |
| 42 | + n_pts = 300 |
| 43 | + elif plot_type == PlotType.StatisticsBar: |
| 44 | + self._graph = self._widget.plotStatisticsBar() |
| 45 | + self._graph.setBeam(1) |
| 46 | + n_pts = 500 |
| 47 | + elif plot_type == PlotType.Scatter: |
| 48 | + self._graph = self._widget.plotScatter() |
| 49 | + n_pts = 3000 |
| 50 | + else: |
| 51 | + raise ValueError(f"Unknown plot type: {plot_type}") |
| 52 | + |
| 53 | + self._x = np.arange(n_pts) |
| 54 | + self._data = 100 * np.random.normal(size=(50, n_pts)) |
| 55 | + if plot_type == PlotType.StatisticsBar: |
| 56 | + self._y_min = self._data - 20 |
| 57 | + self._y_max = self._data + 20 |
| 58 | + self._plot_type = plot_type |
| 59 | + |
| 60 | + self._prev_t = None |
| 61 | + self._count = 0 |
| 62 | + |
| 63 | + self._widget.show() |
| 64 | + |
| 65 | + def start(self): |
| 66 | + self._prev_t = time.time() |
| 67 | + self._timer.start(0) |
| 68 | + |
| 69 | + def update(self): |
| 70 | + idx = self._count % 10 |
| 71 | + if self._plot_type == PlotType.StatisticsBar: |
| 72 | + self._graph.setData(self._x, self._data[idx], |
| 73 | + y_min=self._y_min[idx], y_max=self._y_max[idx]) |
| 74 | + else: |
| 75 | + self._graph.setData(self._x, self._data[idx]) |
| 76 | + |
| 77 | + self._count += 1 |
| 78 | + |
| 79 | + now = time.time() |
| 80 | + dt = now - self._prev_t |
| 81 | + self._prev_t = now |
| 82 | + fps = 1.0 / dt |
| 83 | + |
| 84 | + self._widget.setTitle(f"{fps:.2f} fps") |
| 85 | + |
| 86 | + app.processEvents() # force complete redraw for every plot |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + bench = BenchmarkPlotItemSpeed(PlotType.Line) |
| 91 | + bench.start() |
| 92 | + app.exec_() |
0 commit comments