def initUI(self, ylim):
        self.figure = plt.figure()
        self.canvas = FigureCanvas(self.figure)
        self.toolbar = NavigationToolbar(self.canvas, self)

        self.tracelist = TraceList()

        self.ax = self.figure.add_subplot(111)
        self.ani = animation.FuncAnimation(self.figure,
                                           self.update_figure,
                                           self.should_continue,
                                           init_func=self.init,
                                           interval=25,
                                           blit=False)

        self.ax.set_xlim([0, 100])
        self.ax.set_ylim(ylim)
        self.ax.set_title(self.name)

        hbox = QtGui.QHBoxLayout()
        hbox.addWidget(self.tracelist)
        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.toolbar)
        vbox.addWidget(self.canvas)
        hbox.addLayout(vbox)
        self.setLayout(hbox)
Beispiel #2
0
 def initUI(self):
     self.tracelist = TraceList(self)
     self.pw = pg.PlotWidget()
     self.coords = QtGui.QLabel('')
     self.title = QtGui.QLabel(self.name)
     frame = QtGui.QFrame()
     splitter = QtGui.QSplitter()
     splitter.addWidget(self.tracelist)
     hbox = QtGui.QHBoxLayout()
     vbox = QtGui.QVBoxLayout()
     vbox.addWidget(self.title)
     vbox.addWidget(self.pw)
     vbox.addWidget(self.coords)
     frame.setLayout(vbox)
     splitter.addWidget(frame)
     hbox.addWidget(splitter)
     self.setLayout(hbox)
     #self.legend = self.pw.addLegend()
     self.tracelist.itemChanged.connect(self.checkboxChanged)
     self.pw.plot([], [])
     vb = self.pw.plotItem.vb
     self.img = pg.ImageItem()
     vb.addItem(self.img)
     self.pw.scene().sigMouseMoved.connect(self.mouseMoved)
     self.pw.sigRangeChanged.connect(self.rangeChanged)
    def initUI(self):
        self.tracelist = TraceList(self)
        self.pw = pg.PlotWidget()
        self.coords = QtGui.QLabel('')
        self.title = QtGui.QLabel(self.name)
	frame = QtGui.QFrame()
	splitter = QtGui.QSplitter()
        splitter.addWidget(self.tracelist)
	hbox = QtGui.QHBoxLayout()
        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.title)
        vbox.addWidget(self.pw)
        vbox.addWidget(self.coords)
	frame.setLayout(vbox)
	splitter.addWidget(frame)
        hbox.addWidget(splitter)
        self.setLayout(hbox)
        #self.legend = self.pw.addLegend()
        self.tracelist.itemChanged.connect(self.checkboxChanged)
        self.pw.plot([],[])
        vb = self.pw.plotItem.vb
        self.img = pg.ImageItem()
        vb.addItem(self.img)
        self.pw.scene().sigMouseMoved.connect(self.mouseMoved)
	self.pw.sigRangeChanged.connect(self.rangeChanged)
Beispiel #4
0
    def initUI(self):
        self.tracelist = TraceList(self)
        self.pw = pg.PlotWidget()
        if self.vline_name:
            self.inf = pg.InfiniteLine(movable=True,
                                       angle=90,
                                       label=self.vline_name + '{value:0.0f}',
                                       labelOpts={
                                           'position': 0.9,
                                           'color': (200, 200, 100),
                                           'fill': (200, 200, 200, 50),
                                           'movable': True
                                       })
            init_value = yield self.get_init_vline()
            self.inf.setValue(init_value)
            self.inf.setPen(width=5.0)
        self.coords = QtWidgets.QLabel('')
        self.title = QtWidgets.QLabel(self.name)
        frame = QtWidgets.QFrame()
        splitter = QtWidgets.QSplitter()
        splitter.addWidget(self.tracelist)
        hbox = QtWidgets.QHBoxLayout()
        vbox = QtWidgets.QVBoxLayout()
        vbox.addWidget(self.title)
        vbox.addWidget(self.pw)
        vbox.addWidget(self.coords)
        frame.setLayout(vbox)
        splitter.addWidget(frame)
        hbox.addWidget(splitter)
        self.setLayout(hbox)
        #self.legend = self.pw.addLegend()
        self.tracelist.itemChanged.connect(self.checkboxChanged)
        self.pw.plot([], [])
        vb = self.pw.plotItem.vb
        self.img = pg.ImageItem()
        vb.addItem(self.img)
        if self.vline_name:
            vb.addItem(self.inf)
            self.inf.sigPositionChangeFinished.connect(self.vline_changed)

        self.pw.scene().sigMouseMoved.connect(self.mouseMoved)
        self.pw.sigRangeChanged.connect(self.rangeChanged)
    def initUI(self, ylim):
        self.figure = plt.figure()
        self.canvas = FigureCanvas(self.figure)
        self.toolbar = NavigationToolbar(self.canvas, self)

        self.tracelist = TraceList()

        self.ax = self.figure.add_subplot(111)
        self.ani = animation.FuncAnimation(self.figure, self.update_figure, self.should_continue, init_func=self.init, interval=25, blit=False)

        self.ax.set_xlim([0, 100])
        self.ax.set_ylim(ylim)
        self.ax.set_title(self.name)

        hbox = QtGui.QHBoxLayout()
        hbox.addWidget(self.tracelist)
        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.toolbar)
        vbox.addWidget(self.canvas)
        hbox.addLayout(vbox)
        self.setLayout(hbox)
class Graph_PyQtGraph(QtGui.QWidget):
    def __init__(self, config, reactor, parent=None):
        super(Graph_PyQtGraph, self).__init__(parent)
        self.reactor = reactor
        self.artists = {}
        self.should_stop = False
        self.name = config.name
        self.show_points = config.show_points

        self.dataset_queue = Queue.Queue(config.max_datasets)
        
        self.live_update_loop = LoopingCall(self.update_figure)
        self.live_update_loop.start(0)

        colors = ['r', 'g', 'y', 'c', 'm', 'w']
        self.colorChooser = itertools.cycle(colors)
        self.initUI()

    def initUI(self):
        self.tracelist = TraceList(self)
        self.pw = pg.PlotWidget()
        self.coords = QtGui.QLabel('')
        self.title = QtGui.QLabel(self.name)
        hbox = QtGui.QHBoxLayout()
        hbox.addWidget(self.tracelist)
        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.title)
        vbox.addWidget(self.pw)
        vbox.addWidget(self.coords)
        hbox.addLayout(vbox)
        self.setLayout(hbox)
        #self.legend = self.pw.addLegend()
        self.tracelist.itemChanged.connect(self.checkboxChanged)
        self.pw.plot([],[])
        vb = self.pw.plotItem.vb
        self.img = pg.ImageItem()
        vb.addItem(self.img)
        self.pw.scene().sigMouseMoved.connect(self.mouseMoved)

    def update_figure(self):
        for ident, params in self.artists.iteritems():
            if params.shown:
                try:
                    ds = params.dataset
                    index = params.index
                    current_update = ds.updateCounter
                    if params.last_update < current_update:
                        x = ds.data[:,0]
                        y = ds.data[:,index+1]
                        params.last_update = current_update
                        params.artist.setData(x,y)
                except: pass

    def add_artist(self, ident, dataset, index):
        new_color = self.colorChooser.next()
        if self.show_points:
            line = self.pw.plot([], [], symbol='o', symbolBrush = new_color, pen = new_color, name = ident)
        else:
            line = self.pw.plot([], [], pen = new_color, name = ident)
        self.artists[ident] = artistParameters(line, dataset, index, True)
        self.tracelist.addTrace(ident)

    def remove_artist(self, ident):
        try:
            artist = self.artists[ident].artist
            self.pw.removeItem(artist)
            #self.legend.removeItem(ident)
            self.tracelist.removeTrace(ident)
        except:
            print "remove failed"

    def display(self, ident, shown):
        try:
            artist = self.artists[ident].artist
            if shown:
                self.pw.addItem(artist)
                self.artists[ident].shown = True
            else:
                self.pw.removeItem(artist)
                #self.legend.removeItem(ident)
                self.artists[ident].shown = False
        except KeyError:
            raise Exception('404 Artist not found')

    def checkboxChanged(self):
        for ident, item in self.tracelist.trace_dict.iteritems():
            if item.checkState() and not self.artists[ident].shown:
               self.display(ident, True)
            if not item.checkState() and self.artists[ident].shown:
                self.display(ident, False)

    @inlineCallbacks
    def add_dataset(self, dataset):
        try:
            self.dataset_queue.put(dataset, block=False)
        except Queue.Full:
            remove_ds = self.dataset_queue.get()
            self.remove_dataset(remove_ds)
            self.dataset_queue.put(dataset, block=False)
        labels = yield dataset.getLabels()
        for i, label in enumerate(labels):
            self.add_artist(label, dataset, i)

    @inlineCallbacks
    def remove_dataset(self, dataset):
        labels = yield dataset.getLabels()
        for label in labels:
            self.remove_artist(label)

    def set_xlimits(self, limits):
        self.pw.setXRange(limits[0], limits[1])
        self.current_limits = limits

    def set_ylimits(self, limits):
        self.pw.setYRange(limits[0],limits[1])

    def mouseMoved(self, pos):
        #print "Image position:", self.img.mapFromScene(pos)
        pnt = self.img.mapFromScene(pos)
        string = '(' + str(pnt.x()) + ' , ' + str(pnt.y()) + ')'
        self.coords.setText(string)
Beispiel #7
0
class Graph_PyQtGraph(QtGui.QWidget):
    def __init__(self, config, reactor, parent=None):
        super(Graph_PyQtGraph, self).__init__(parent)
        self.reactor = reactor
        self.artists = {}
        self.should_stop = False
        self.name = config.name
        self.show_points = config.show_points

        self.dataset_queue = Queue.Queue(config.max_datasets)

        self.live_update_loop = LoopingCall(self.update_figure)
        self.live_update_loop.start(0)

        colors = ['r', 'g', 'y', 'c', 'm', 'w']
        self.colorChooser = itertools.cycle(colors)
        self.initUI()

    def initUI(self):
        self.tracelist = TraceList(self)
        self.pw = pg.PlotWidget()
        self.coords = QtGui.QLabel('')
        self.title = QtGui.QLabel(self.name)
        hbox = QtGui.QHBoxLayout()
        hbox.addWidget(self.tracelist)
        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.title)
        vbox.addWidget(self.pw)
        vbox.addWidget(self.coords)
        hbox.addLayout(vbox)
        self.setLayout(hbox)
        #self.legend = self.pw.addLegend()
        self.tracelist.itemChanged.connect(self.checkboxChanged)
        self.pw.plot([], [])
        vb = self.pw.plotItem.vb
        self.img = pg.ImageItem()
        vb.addItem(self.img)
        self.pw.scene().sigMouseMoved.connect(self.mouseMoved)

    def update_figure(self):
        for ident, params in self.artists.iteritems():
            if params.shown:
                try:
                    ds = params.dataset
                    index = params.index
                    current_update = ds.updateCounter
                    if params.last_update < current_update:
                        x = ds.data[:, 0]
                        y = ds.data[:, index + 1]
                        params.last_update = current_update
                        params.artist.setData(x, y)
                except:
                    pass

    def add_artist(self, ident, dataset, index):
        new_color = self.colorChooser.next()
        if self.show_points:
            line = self.pw.plot([], [],
                                symbol='o',
                                symbolBrush=new_color,
                                pen=new_color,
                                name=ident)
        else:
            line = self.pw.plot([], [], pen=new_color, name=ident)
        self.artists[ident] = artistParameters(line, dataset, index, True)
        self.tracelist.addTrace(ident)

    def remove_artist(self, ident):
        try:
            artist = self.artists[ident].artist
            self.pw.removeItem(artist)
            #self.legend.removeItem(ident)
            self.tracelist.removeTrace(ident)
        except:
            print "remove failed"

    def display(self, ident, shown):
        try:
            artist = self.artists[ident].artist
            if shown:
                self.pw.addItem(artist)
                self.artists[ident].shown = True
            else:
                self.pw.removeItem(artist)
                #self.legend.removeItem(ident)
                self.artists[ident].shown = False
        except KeyError:
            raise Exception('404 Artist not found')

    def checkboxChanged(self):
        for ident, item in self.tracelist.trace_dict.iteritems():
            if item.checkState() and not self.artists[ident].shown:
                self.display(ident, True)
            if not item.checkState() and self.artists[ident].shown:
                self.display(ident, False)

    @inlineCallbacks
    def add_dataset(self, dataset):
        try:
            self.dataset_queue.put(dataset, block=False)
        except Queue.Full:
            remove_ds = self.dataset_queue.get()
            self.remove_dataset(remove_ds)
            self.dataset_queue.put(dataset, block=False)
        labels = yield dataset.getLabels()
        for i, label in enumerate(labels):
            self.add_artist(label, dataset, i)

    @inlineCallbacks
    def remove_dataset(self, dataset):
        labels = yield dataset.getLabels()
        for label in labels:
            self.remove_artist(label)

    def set_xlimits(self, limits):
        self.pw.setXRange(limits[0], limits[1])
        self.current_limits = limits

    def set_ylimits(self, limits):
        self.pw.setYRange(limits[0], limits[1])

    def mouseMoved(self, pos):
        #print "Image position:", self.img.mapFromScene(pos)
        pnt = self.img.mapFromScene(pos)
        string = '(' + str(pnt.x()) + ' , ' + str(pnt.y()) + ')'
        self.coords.setText(string)
Beispiel #8
0
class Graph_PyQtGraph(QtWidgets.QWidget):
    def __init__(self, config, reactor, cxn=None, parent=None):
        super(Graph_PyQtGraph, self).__init__(parent)
        from labrad.units import WithUnit as U
        self.U = U
        self.cxn = cxn
        self.pv = self.cxn.parametervault
        self.reactor = reactor
        self.artists = {}
        self.should_stop = False
        self.name = config.name
        self.vline_name = config.vline
        self.vline_param = config.vline_param
        self.hline_name = config.hline
        self.hline_param = config.hline_param
        self.show_points = config.show_points
        self.grid_on = config.grid_on
        self.scatter_plot = config.scatter_plot

        self.dataset_queue = queue.Queue(config.max_datasets)

        self.live_update_loop = LoopingCall(self.update_figure)
        self.live_update_loop.start(0)

        colors = ['r', 'g', 'y', 'c', 'm', 'w']
        self.colorChooser = itertools.cycle(colors)
        self.initUI()

    @inlineCallbacks
    def initUI(self):
        self.tracelist = TraceList(self)
        self.pw = pg.PlotWidget()
        self._set_axes_font(20)
        if self.vline_name:
            self.inf = pg.InfiniteLine(movable=True,
                                       angle=90,
                                       label=self.vline_name + '{value:0.0f}',
                                       labelOpts={
                                           'position': 0.9,
                                           'color': (200, 200, 100),
                                           'fill': (200, 200, 200, 50),
                                           'movable': True
                                       })
            init_value = yield self.get_init_vline()
            self.inf.setValue(init_value)
            self.inf.setPen(width=5.0)

        if self.hline_name:
            self.inf = pg.InfiniteLine(movable=True,
                                       angle=0,
                                       label=self.hline_name + '{value:0.0f}',
                                       labelOpts={
                                           'position': 0.9,
                                           'color': (200, 200, 100),
                                           'fill': (200, 200, 200, 50),
                                           'movable': True
                                       })
            init_value = yield self.get_init_hline()
            self.inf.setValue(init_value)
            self.inf.setPen(width=5.0)

        self.coords = QtGui.QLabel('')
        self.title = QtGui.QLabel(self.name)
        frame = QtGui.QFrame()
        splitter = QtGui.QSplitter()
        splitter.addWidget(self.tracelist)
        hbox = QtGui.QHBoxLayout()
        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.title)
        vbox.addWidget(self.pw)
        vbox.addWidget(self.coords)
        frame.setLayout(vbox)
        splitter.addWidget(frame)
        hbox.addWidget(splitter)
        self.setLayout(hbox)
        self.tracelist.itemChanged.connect(self.checkboxChanged)
        self.pw.plot([], [])
        vb = self.pw.plotItem.vb
        self.img = pg.ImageItem()
        vb.addItem(self.img)

        if self.vline_name:
            vb.addItem(self.inf)
            self.inf.sigPositionChangeFinished.connect(self.vline_changed)

        if self.hline_name:
            vb.addItem(self.inf)
            self.inf.sigPositionChangeFinished.connect(self.hline_changed)

        self.pw.scene().sigMouseMoved.connect(self.mouseMoved)
        self.pw.sigRangeChanged.connect(self.rangeChanged)

    def _set_axes_font(self, font_size):
        font = QtGui.QFont()
        font.setPixelSize(font_size)
        left_axis = self.pw.plotItem.getAxis("left")
        left_axis.tickFont = font
        left_axis.setWidth(font_size * 7)
        left_axis.setStyle(tickTextOffset=int(font_size / 2))
        left_axis.setStyle(textFillLimits=[(0, 0.6), (2, 0.4), (4,
                                                                0.2), (6,
                                                                       0.0)])
        bottom_axis = self.pw.plotItem.getAxis("bottom")
        bottom_axis.tickFont = font
        bottom_axis.setHeight(font_size * 2)
        bottom_axis.setStyle(tickTextOffset=int(font_size / 2))
        bottom_axis.setStyle(textFillLimits=[(0, 0.6), (2, 0.4), (4,
                                                                  0.2), (6,
                                                                         0.0)])

    def getItemColor(self, color):
        color_dict = {
            "r": QtGui.QColor(QtCore.Qt.red).lighter(130),
            "g": QtGui.QColor(QtCore.Qt.green),
            "y": QtGui.QColor(QtCore.Qt.yellow),
            "c": QtGui.QColor(QtCore.Qt.cyan),
            "m": QtGui.QColor(QtCore.Qt.magenta).lighter(120),
            "w": QtGui.QColor(QtCore.Qt.white)
        }
        return color_dict[color]

    def update_figure(self):
        for ident, params in self.artists.items():
            if params.shown:
                try:
                    ds = params.dataset
                    index = params.index
                    current_update = ds.updateCounter
                    if params.last_update < current_update:
                        x = ds.data[:, 0]
                        y = ds.data[:, index + 1]
                        params.last_update = current_update
                        params.artist.setData(x, y)
                except:
                    pass

    def _check_artist_exist(self, ident):
        if ident in self.artists.keys():
            counter = 1
            while ident + str(counter) in self.artists.keys():
                counter += 1
            ident += str(counter)
        return ident

    def add_artist(self, ident, dataset, index, no_points=False):
        '''
        no_points is an override parameter to the global show_points setting.
        It is to allow data fits to be plotted without points
        '''
        new_color = next(self.colorChooser)
        ident = self._check_artist_exist(ident)
        if self.show_points and not no_points:
            line = self.pw.plot([], [],
                                symbol='o',
                                symbolBrush=self.getItemColor(new_color),
                                name=ident,
                                pen=self.getItemColor(new_color),
                                connect=self.scatter_plot)
        else:
            line = self.pw.plot([], [],
                                pen=self.getItemColor(new_color),
                                name=ident)
        if self.grid_on:
            self.pw.showGrid(x=True, y=True)
        self.artists[ident] = artistParameters(line, dataset, index, True)
        self.tracelist.addTrace(ident, new_color)

    def remove_artist(self, ident):
        try:
            artist = self.artists[ident].artist
            self.pw.removeItem(artist)
            self.tracelist.removeTrace(ident)
            self.artists[ident].shown = False
            try:
                del self.artists[ident]
            except KeyError:
                pass
        except:
            print("remove failed")

    def display(self, ident, shown):
        try:
            artist = self.artists[ident].artist
            if shown:
                self.pw.addItem(artist)
                self.artists[ident].shown = True
            else:
                self.pw.removeItem(artist)
                #self.legend.removeItem(ident)
                self.artists[ident].shown = False
        except KeyError:
            raise Exception('404 Artist not found')

    def checkboxChanged(self):
        for ident, item in self.tracelist.trace_dict.items():
            try:
                if item.checkState() and not self.artists[ident].shown:
                    self.display(ident, True)
                if not item.checkState() and self.artists[ident].shown:
                    self.display(ident, False)
            except KeyError:  # this means the artist has been deleted.
                pass

    def rangeChanged(self):

        lims = self.pw.viewRange()
        self.pointsToKeep = lims[0][1] - lims[0][0]
        self.current_limits = [lims[0][0], lims[0][1]]

    @inlineCallbacks
    def add_dataset(self, dataset):
        try:
            self.dataset_queue.put(dataset, block=False)
        except queue.Full:
            remove_ds = self.dataset_queue.get()
            self.remove_dataset(remove_ds)
            self.dataset_queue.put(dataset, block=False)
        labels = yield dataset.getLabels()
        for i, label in enumerate(labels):
            self.add_artist(label, dataset, i)

    @inlineCallbacks
    def remove_dataset(self, dataset):
        labels = yield dataset.getLabels()
        for label in labels:
            self.remove_artist(label)

    def set_xlimits(self, limits):
        self.pw.setXRange(limits[0], limits[1])
        self.current_limits = limits

    def set_ylimits(self, limits):
        self.pw.setYRange(limits[0], limits[1])

    def mouseMoved(self, pos):
        pnt = self.img.mapFromScene(pos)
        string = '(' + str(pnt.x()) + ' , ' + str(pnt.y()) + ')'
        self.coords.setText(string)

    @inlineCallbacks
    def get_init_vline(self):
        init_vline = yield self.pv.get_parameter(self.vline_param[0],
                                                 self.vline_param[1])
        returnValue(init_vline)

    @inlineCallbacks
    def get_init_hline(self):
        init_hline = yield self.pv.get_parameter(self.hline_param[0],
                                                 self.hline_param[1])
        returnValue(init_hline)

    @inlineCallbacks
    def vline_changed(self, sig):
        val = self.inf.value()
        param = yield self.pv.get_parameter(self.vline_param[0],
                                            self.vline_param[1])
        units = param.units
        val = self.U(val, units)
        yield self.pv.set_parameter(self.vline_param[0], self.vline_param[1],
                                    val)

    @inlineCallbacks
    def hline_changed(self, sig):
        val = self.inf.value()
        param = yield self.pv.get_parameter(self.hline_param[0],
                                            self.hline_param[1])
        units = param.units
        val = self.U(val, units)
        yield self.pv.set_parameter(self.hline_param[0], self.hline_param[1],
                                    val)
class Graph(QtGui.QWidget):
    def __init__(self, name, reactor, parent=None, ylim=[0, 1]):
        super(Graph, self).__init__(parent)
        self.reactor = reactor
        self.artists = {}
        self.datasets = {}  # a single dataset might have multiple traces
        self.should_stop = False
        self.name = name
        self.initUI(ylim)

    def initUI(self, ylim):
        self.figure = plt.figure()
        self.canvas = FigureCanvas(self.figure)
        self.toolbar = NavigationToolbar(self.canvas, self)

        self.tracelist = TraceList()

        self.ax = self.figure.add_subplot(111)
        self.ani = animation.FuncAnimation(self.figure,
                                           self.update_figure,
                                           self.should_continue,
                                           init_func=self.init,
                                           interval=25,
                                           blit=False)

        self.ax.set_xlim([0, 100])
        self.ax.set_ylim(ylim)
        self.ax.set_title(self.name)

        hbox = QtGui.QHBoxLayout()
        hbox.addWidget(self.tracelist)
        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.toolbar)
        vbox.addWidget(self.canvas)
        hbox.addLayout(vbox)
        self.setLayout(hbox)
        #self.draw_stuff()

    def init(self):
        line, = self.ax.plot([], [])
        return line,

    def update_figure(self, _input=None):
        artists = []
        for ident, (artist, dataset, index) in self.artists.iteritems():
            x = dataset.data[:, 0]
            y = dataset.data[:, index + 1]
            artist.set_data((x, y))
            artists.append(artist)
        return artists

    def add_artist(self, ident, dataset, index):
        line, = self.ax.plot([], [], '-o', markersize=1.0, label=ident)
        #self.ax.legend()
        # dataset is the dataset object
        # index is the position in the dataset object this trace lives
        self.artists[ident] = [line, dataset, index]
        self.tracelist.addTrace(ident)

        # connect the checkbox in the tracelist
        self.tracelist.itemChanged.connect(self.checkboxChanged)
        #cb = self.tracelist.trace_dict[ident]
        #cb.itemChanged.connect(self.checkboxChanged)

    def display(self, ident, shown):
        try:
            self.artists[ident][0].set_visible(shown)
        except KeyError:
            raise Exception('404 Artist not found')
        self.canvas.draw()

    def checkboxChanged(self, state):
        for ident, item in self.tracelist.trace_dict.iteritems():
            if item.checkState():
                self.display(ident, True)
            else:
                self.display(ident, False)

    def should_continue(self):
        while True:
            if self.should_stop: return
            yield True

    @inlineCallbacks
    def add_dataset(self, dataset):
        labels = yield dataset.getLabels()
        for i, label in enumerate(labels):
            self.add_artist(label, dataset, i)

    def set_xlimits(self, limits):
        self.ax.set_xlim(limits)
        self.current_limits = limits

    def set_ylimits(self, limits):
        self.ax.set_ylim(limits)

    def redraw(self):
        self.canvas.draw()
Beispiel #10
0
class Hist_PyQtGraph(QtGui.QWidget):
    def __init__(self, config, reactor, cxn=None, parent=None):
        super(Hist_PyQtGraph, self).__init__(parent)
        self.cxn = cxn
        self.pv = self.cxn.parametervault
        self.reactor = reactor
        self.artists = {}
        self.should_stop = False
        self.name = config.name
        self.vline_name = config.vline
        self.vline_param = config.vline_param

        self.dataset_queue = Queue.Queue(config.max_datasets)

        self.live_update_loop = LoopingCall(self.update_figure)
        self.live_update_loop.start(0)

        colors = [(255, 0, 0, 80),
                  (0, 255, 0, 80),
                  (255, 255, 0, 80),
                  (0, 255, 255, 80),
                  (255, 0, 255, 80),
                  (255, 255, 255, 80)]
        self.colorChooser = itertools.cycle(colors)
        self.initUI()

    @inlineCallbacks
    def initUI(self):
        self.tracelist = TraceList(self)
        self.pw = pg.PlotWidget()
        if self.vline_name:
            self.inf = pg.InfiniteLine(movable=True, angle=90,
                                       label=self.vline_name + '{value:0.0f}',
                                       labelOpts={'position': 0.9,
                                                  'color': (200, 200, 100),
                                                  'fill': (200, 200, 200, 50),
                                                  'movable': True})
            init_value = yield self.get_init_vline()
            self.inf.setValue(init_value)
            self.inf.setPen(width=5.0)
        self.coords = QtGui.QLabel('')
        self.title = QtGui.QLabel(self.name)
        frame = QtGui.QFrame()
        splitter = QtGui.QSplitter()
        splitter.addWidget(self.tracelist)
        hbox = QtGui.QHBoxLayout()
        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.title)
        vbox.addWidget(self.pw)
        vbox.addWidget(self.coords)
        frame.setLayout(vbox)
        splitter.addWidget(frame)
        hbox.addWidget(splitter)
        self.setLayout(hbox)
        #self.legend = self.pw.addLegend()
        self.tracelist.itemChanged.connect(self.checkboxChanged)
        self.pw.plot([],[])
        vb = self.pw.plotItem.vb
        self.img = pg.ImageItem()
        vb.addItem(self.img)
        if self.vline_name:
            vb.addItem(self.inf)
            self.inf.sigPositionChangeFinished.connect(self.vline_changed)

        self.pw.scene().sigMouseMoved.connect(self.mouseMoved)
        self.pw.sigRangeChanged.connect(self.rangeChanged)

    def getItemColor(self, color):

        color_dict = {(255, 0, 0, 80): QtGui.QColor(QtCore.Qt.red).lighter(130),
                      (0, 255, 0, 80): QtGui.QColor(QtCore.Qt.green),
                      (255, 255, 0, 80): QtGui.QColor(QtCore.Qt.yellow),
                      (0, 255, 255, 80): QtGui.QColor(QtCore.Qt.cyan),
                      (255, 0, 255, 80): QtGui.QColor(QtCore.Qt.magenta).lighter(120),
                      (255, 255, 255, 80): QtGui.QColor(QtCore.Qt.white)}
        return color_dict[color]
        
    def update_figure(self):
        for ident, params in self.artists.iteritems():
            if params.shown:
                try:
                    ds = params.dataset
                    index = params.index
                    current_update = ds.updateCounter
                    if params.last_update < current_update:
                        x = ds.data[:,0]
                        x = list(x) + [x[-1] + 1]
                        y = ds.data[:,index+1]
                        params.last_update = current_update
                        params.artist.setData(x,y)
                except: pass

    def add_artist(self, ident, dataset, index, no_points = False):
        '''
        no_points is an override parameter to the global show_points setting.
        It is to allow data fits to be plotted without points
        '''
        new_color = self.colorChooser.next()
        hist = pg.PlotCurveItem([0,1],[1], stepMode=True, fillLevel=0, brush=new_color, pen=None)
        self.artists[ident] = artistParameters(hist, dataset, index, True)
        self.pw.addItem(hist)
        self.tracelist.addTrace(ident, new_color)

    def remove_artist(self, ident):
        try:
            artist = self.artists[ident].artist
            self.pw.removeItem(artist)
            self.tracelist.removeTrace(ident)
            self.artists[ident].shown = False
            try:
                del self.artists[ident]
            except KeyError:
                pass
        except:
            print "remove failed"

    def display(self, ident, shown):
        try:
            artist = self.artists[ident].artist
            if shown:
                self.pw.addItem(artist)
                self.artists[ident].shown = True
            else:
                self.pw.removeItem(artist)
                self.artists[ident].shown = False
        except KeyError:
            raise Exception('404 Artist not found')

    def checkboxChanged(self):
        for ident, item in self.tracelist.trace_dict.iteritems():
            try:
                if item.checkState() and not self.artists[ident].shown:
                    self.display(ident, True)
                if not item.checkState() and self.artists[ident].shown:
                    self.display(ident, False)
            except KeyError:  # this means the artist has been deleted.
                pass

    def rangeChanged(self):

        lims = self.pw.viewRange()
        self.pointsToKeep = lims[0][1] - lims[0][0]
        self.current_limits = [lims[0][0], lims[0][1]]

    @inlineCallbacks
    def add_dataset(self, dataset):
        try:
            self.dataset_queue.put(dataset, block=False)
        except Queue.Full:
            remove_ds = self.dataset_queue.get()
            self.remove_dataset(remove_ds)
            self.dataset_queue.put(dataset, block=False)
        labels = yield dataset.getLabels()
        for i, label in enumerate(labels):
            self.add_artist(label, dataset, i)

    @inlineCallbacks
    def remove_dataset(self, dataset):
        labels = yield dataset.getLabels()
        for label in labels:
            self.remove_artist(label)

    def set_xlimits(self, limits):
        self.pw.setXRange(limits[0], limits[1])
        self.current_limits = limits

    def set_ylimits(self, limits):
        self.pw.setYRange(limits[0], limits[1])

    def mouseMoved(self, pos):
        pnt = self.img.mapFromScene(pos)
        string = '(' + str(pnt.x()) + ' , ' + str(pnt.y()) + ')'
        self.coords.setText(string)

    @inlineCallbacks
    def get_init_vline(self):
        init_vline = yield self.pv.get_parameter(self.vline_param[0],
                                                 self.vline_param[1])
        print init_vline
        returnValue(init_vline)

    @inlineCallbacks
    def vline_changed(self, sig):
        val = self.inf.value()
        val = int(round(val))
        yield self.pv.set_parameter(self.vline_param[0],
                                    self.vline_param[1], val)
class Graph(QtGui.QWidget):
    def __init__(self, name, reactor, parent=None, ylim=[0,1]):
        super(Graph, self).__init__(parent)
        self.reactor = reactor
        self.artists = {}
        self.datasets = {} # a single dataset might have multiple traces
        self.should_stop = False
        self.name = name
        self.initUI(ylim)

    def initUI(self, ylim):
        self.figure = plt.figure()
        self.canvas = FigureCanvas(self.figure)
        self.toolbar = NavigationToolbar(self.canvas, self)

        self.tracelist = TraceList()

        self.ax = self.figure.add_subplot(111)
        self.ani = animation.FuncAnimation(self.figure, self.update_figure, self.should_continue, init_func=self.init, interval=25, blit=False)

        self.ax.set_xlim([0, 100])
        self.ax.set_ylim(ylim)
        self.ax.set_title(self.name)

        hbox = QtGui.QHBoxLayout()
        hbox.addWidget(self.tracelist)
        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.toolbar)
        vbox.addWidget(self.canvas)
        hbox.addLayout(vbox)
        self.setLayout(hbox)
        #self.draw_stuff()

    def init(self):
        line, = self.ax.plot([], [])
        return line,

    def update_figure(self, _input = None):
        artists = []
        for ident, (artist, dataset, index) in self.artists.iteritems():
            x = dataset.data[:,0]
            y = dataset.data[:,index+1]
            artist.set_data((x,y))
            artists.append(artist)
        return artists

    def add_artist(self, ident, dataset, index):
        line, = self.ax.plot([], [], '-o', markersize = 1.0, label = ident)
        #self.ax.legend()
        # dataset is the dataset object
        # index is the position in the dataset object this trace lives
        self.artists[ident] = [line, dataset, index]
        self.tracelist.addTrace(ident)

        # connect the checkbox in the tracelist
        self.tracelist.itemChanged.connect(self.checkboxChanged)
        #cb = self.tracelist.trace_dict[ident]
        #cb.itemChanged.connect(self.checkboxChanged)

    def display(self, ident, shown):
        try:
            self.artists[ident][0].set_visible(shown)
        except KeyError:
            raise Exception('404 Artist not found')
        self.canvas.draw()

    def checkboxChanged(self, state):
        for ident, item in self.tracelist.trace_dict.iteritems():
            if item.checkState():
               self.display(ident, True)
            else:
                self.display(ident, False)

    def should_continue(self):
        while True:
            if self.should_stop: return
            yield True

    @inlineCallbacks
    def add_dataset(self, dataset):
        labels = yield dataset.getLabels()
        for i, label in enumerate(labels):
            self.add_artist(label, dataset, i)

    def set_xlimits(self, limits):
        self.ax.set_xlim(limits)
        self.current_limits = limits

    def set_ylimits(self, limits):
        self.ax.set_ylim(limits)

    def redraw(self):
        self.canvas.draw()