File: curves.py

package info (click to toggle)
python-pymeasure 0.14.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 8,788 kB
  • sloc: python: 47,201; makefile: 155
file content (224 lines) | stat: -rw-r--r-- 8,210 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#
# This file is part of the PyMeasure package.
#
# Copyright (c) 2013-2024 PyMeasure Developers
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

import logging

import numpy as np
import pyqtgraph as pg
from .Qt import QtCore, QtGui

log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler())


class ResultsCurve(pg.PlotDataItem):
    """ Creates a curve loaded dynamically from a file through the Results object. The data can
    be forced to fully reload on each update, useful for cases when the data is changing across
    the full file instead of just appending.
    """

    def __init__(self, results, x, y, force_reload=False, wdg=None, **kwargs):
        super().__init__(**kwargs)
        self.results = results
        self.wdg = wdg
        self.pen = kwargs.get('pen', None)
        self.x, self.y = x, y
        self.force_reload = force_reload
        self.color = self.opts['pen'].color()

    def update_data(self):
        """Updates the data by polling the results"""
        if self.force_reload:
            self.results.reload()
        data = self.results.data  # get the current snapshot

        # Set x-y data
        self.setData(data[self.x], data[self.y])

    def set_color(self, color):
        self.pen.setColor(color)
        self.color = self.opts['pen'].color()
        self.updateItems(styleUpdate=True)

# TODO: Add method for changing x and y


class ResultsImage(pg.ImageItem):
    """ Creates an image loaded dynamically from a file through the Results
    object."""

    def __init__(self, results, x, y, z, force_reload=False, wdg=None, **kwargs):
        self.results = results
        self.wdg = wdg
        self.x = x
        self.y = y
        self.z = z
        self.xstart = getattr(self.results.procedure, self.x + '_start')
        self.xend = getattr(self.results.procedure, self.x + '_end')
        self.xstep = getattr(self.results.procedure, self.x + '_step')
        self.xsize = int(np.ceil((self.xend - self.xstart) / self.xstep)) + 1
        self.ystart = getattr(self.results.procedure, self.y + '_start')
        self.yend = getattr(self.results.procedure, self.y + '_end')
        self.ystep = getattr(self.results.procedure, self.y + '_step')
        self.ysize = int(np.ceil((self.yend - self.ystart) / self.ystep)) + 1
        self.img_data = np.zeros((self.ysize, self.xsize, 4))
        self.force_reload = force_reload
        self.cm = pg.colormap.get('viridis')

        super().__init__(image=self.img_data)

        # Scale and translate image so that the pixels are in the correct
        # position in "data coordinates"
        tr = QtGui.QTransform()
        tr.scale(self.xstep, self.ystep)
        tr.translate(int(self.xstart / self.xstep) - 0.5,
                     int(self.ystart / self.ystep) - 0.5)  # 0.5 so pixels centered
        self.setTransform(tr)

    def update_data(self):
        if self.force_reload:
            self.results.reload()

        data = self.results.data
        zmin = data[self.z].min()
        zmax = data[self.z].max()

        # populate the image array with the new data
        for idx, row in data.iterrows():
            xdat = row[self.x]
            ydat = row[self.y]
            xidx, yidx = self.find_img_index(xdat, ydat)
            self.img_data[yidx, xidx, :] = self.colormap((row[self.z] - zmin) / (zmax - zmin))

        # set image data, need to transpose since pyqtgraph assumes column-major order
        self.setImage(image=np.transpose(self.img_data, axes=(1, 0, 2)))

    def find_img_index(self, x, y):
        """ Finds the integer image indices corresponding to the
        closest x and y points of the data given some x and y data.
        """

        indices = [self.xsize - 1, self.ysize - 1]  # default to the final pixel
        if self.xstart <= x <= self.xend:  # only change if within reasonable range
            indices[0] = self.round_up((x - self.xstart) / self.xstep)
        if self.ystart <= y <= self.yend:
            indices[1] = self.round_up((y - self.ystart) / self.ystep)

        return indices

    def round_up(self, x):
        """Convenience function since numpy rounds to even"""
        if x % 1 >= 0.5:
            return int(x) + 1
        else:
            return int(x)

    def colormap(self, x):
        """ Return mapped color as 0.0-1.0 floats RGBA """
        return self.cm.map(x, mode='float')

    # TODO: colormap selection


class BufferCurve(pg.PlotDataItem):
    """ Creates a curve based on a predefined buffer size and allows data to be added dynamically.
    """

    data_updated = QtCore.Signal()

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self._buffer = None

    def prepare(self, size, dtype=np.float32):
        """ Prepares the buffer based on its size, data type """
        self._buffer = np.empty((size, 2), dtype=dtype)
        self._ptr = 0

    def append(self, x, y):
        """ Appends data to the curve with optional errors """
        if self._buffer is None:
            raise Exception("BufferCurve buffer must be prepared")
        if len(self._buffer) <= self._ptr:
            raise Exception("BufferCurve overflow")

        # Set x-y data
        self._buffer[self._ptr, :2] = [x, y]
        self.setData(self._buffer[:self._ptr, :2])

        self._ptr += 1
        self.data_updated.emit()


class Crosshairs(QtCore.QObject):
    """ Attaches crosshairs to the a plot and provides a signal with the
    x and y graph coordinates
    """

    coordinates = QtCore.Signal(float, float)

    def __init__(self, plot, pen=None):
        """ Initiates the crosshars onto a plot given the pen style.

        Example pen:
        pen=pg.mkPen(color='#AAAAAA', style=QtCore.Qt.PenStyle.DashLine)
        """
        super().__init__()

        self.vertical = pg.InfiniteLine(angle=90, movable=False, pen=pen)
        self.horizontal = pg.InfiniteLine(angle=0, movable=False, pen=pen)
        plot.addItem(self.vertical, ignoreBounds=True)
        plot.addItem(self.horizontal, ignoreBounds=True)

        self.position = None
        self.proxy = pg.SignalProxy(plot.scene().sigMouseMoved, rateLimit=60,
                                    slot=self.mouseMoved)
        self.plot = plot

    def hide(self):
        self.vertical.hide()
        self.horizontal.hide()

    def show(self):
        self.vertical.show()
        self.horizontal.show()

    def update(self):
        """ Updates the mouse position based on the data in the plot. For
        dynamic plots, this is called each time the data changes to ensure
        the x and y values correspond to those on the display.
        """
        if self.position is not None:
            mouse_point = self.plot.vb.mapSceneToView(self.position)
            self.coordinates.emit(mouse_point.x(), mouse_point.y())
            self.vertical.setPos(mouse_point.x())
            self.horizontal.setPos(mouse_point.y())

    def mouseMoved(self, event=None):
        """ Updates the mouse position upon mouse movement """
        if event is not None:
            self.position = event[0]
            self.update()
        else:
            raise Exception("Mouse location not known")