Debian Codegraph @

pymeasure/display/widgets/plot_frame.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
27 import re
28 import pyqtgraph as pg
29
30 from ..curves import ResultsCurve, Crosshairs
31 from ..Qt import QtCore, QtWidgets
32 from ...experiment import Procedure
33
34 log = logging.getLogger(__name__)
35 log.addHandler(logging.NullHandler())
36
37
38 class PlotFrame(QtWidgets.QFrame):
39 """ Combines a PyQtGraph Plot with Crosshairs. Refreshes
40 the plot based on the refresh_time, and allows the axes
41 to be changed on the fly, which updates the plotted data
42 """
43
44 LABEL_STYLE = {'font-size': '10pt', 'font-family': 'Arial', 'color': '#000000'}
45 updated = QtCore.Signal()
46 ResultsClass = ResultsCurve
47 x_axis_changed = QtCore.Signal(str)
48 y_axis_changed = QtCore.Signal(str)
49
50 def __init__(self, x_axis=None, y_axis=None, refresh_time=0.2, check_status=True, parent=None):
51 super().__init__(parent)
52 self.refresh_time = refresh_time
53 self.check_status = check_status
54 self._setup_ui()
55 self.change_x_axis(x_axis)
56 self.change_y_axis(y_axis)
57
58 def _setup_ui(self):
59 self.setAutoFillBackground(False)
60 self.setStyleSheet("background: #fff")
61 self.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel)
62 self.setFrameShadow(QtWidgets.QFrame.Shadow.Sunken)
63 self.setMidLineWidth(1)
64
65 vbox = QtWidgets.QVBoxLayout(self)
66
67 self.plot_widget = pg.PlotWidget(self, background='#ffffff')
68 vbox.addWidget(self.plot_widget)
69 self.setLayout(vbox)
70
71 self.plot = self.plot_widget.getPlotItem()
72
73 style = dict(self.LABEL_STYLE, justify='right')
74 if "font-size" in style: # LabelItem wants the size as 'size' rather than 'font-size'
75 style["size"] = style.pop("font-size")
76 self.coordinates = pg.LabelItem("", parent=self.plot, **style)
77 self.coordinates.anchor(itemPos=(1, 1), parentPos=(1, 1), offset=(0, 3))
78
79 self.crosshairs = Crosshairs(self.plot,
80 pen=pg.mkPen(color='#AAAAAA',
81 style=QtCore.Qt.PenStyle.DashLine))
82 self.crosshairs.coordinates.connect(self.update_coordinates)
83
84 self.timer = QtCore.QTimer(self)
85 self.timer.timeout.connect(self.update_curves)
86 self.timer.timeout.connect(self.crosshairs.update)
87 self.timer.timeout.connect(self.updated)
88 self.timer.start(int(self.refresh_time * 1e3))
89
90 def update_coordinates(self, x, y):
91 self.coordinates.setText(f"({x:g}, {y:g})")
92
93 def update_curves(self):
94 for item in self.plot.items:
95 if isinstance(item, self.ResultsClass):
96 if self.check_status:
97 if item.results.procedure.status == Procedure.RUNNING:
98 item.update_data()
99 else:
100 item.update_data()
101
102 def parse_axis(self, axis):
103 """ Returns the units of an axis by searching the string
104 """
105 units_pattern = r"\((?P<units>\w+)\)"
106 try:
107 match = re.search(units_pattern, axis)
108 except TypeError:
109 match = None
110
111 if match:
112 if 'units' in match.groupdict():
113 label = re.sub(units_pattern, '', axis)
114 return label, match.groupdict()['units']
115 else:
116 return axis, None
117
118 def change_x_axis(self, axis):
119 for item in self.plot.items:
120 if isinstance(item, self.ResultsClass):
121 item.x = axis
122 item.update_data()
123 label, units = self.parse_axis(axis)
124 self.plot.setLabel('bottom', label, units=units, **self.LABEL_STYLE)
125 self.x_axis = axis
126 self.x_axis_changed.emit(axis)
127
128 def change_y_axis(self, axis):
129 for item in self.plot.items:
130 if isinstance(item, self.ResultsClass):
131 item.y = axis
132 item.update_data()
133 label, units = self.parse_axis(axis)
134 self.plot.setLabel('left', label, units=units, **self.LABEL_STYLE)
135 self.y_axis = axis
136 self.y_axis_changed.emit(axis)