From bdb6ff88a2412407e38bb97bd89d15a94b610855 Mon Sep 17 00:00:00 2001 From: Luke Campagnola <> Date: Wed, 4 Apr 2012 12:22:43 -0400 Subject: [PATCH] Updates to IsocurveItem, added isocurve example minor updates for other examples --- examples/Arrow.py | 2 -- examples/VideoSpeedTest.py | 18 +++++++---- examples/__main__.py | 9 +++++- examples/isocurve.py | 58 +++++++++++++++++++++++++++++++++++ graphicsItems/IsocurveItem.py | 44 ++++++++++++++++++++------ 5 files changed, 112 insertions(+), 19 deletions(-) create mode 100644 examples/isocurve.py diff --git a/examples/Arrow.py b/examples/Arrow.py index 86f5c8c7..ae118507 100755 --- a/examples/Arrow.py +++ b/examples/Arrow.py @@ -7,8 +7,6 @@ ## To place a static arrow anywhere in a scene, use ArrowItem. ## To attach other types of item to a curve, use CurvePoint. - -## Add path to library (just for examples; you do not need this) import initExample ## Add path to library (just for examples; you do not need this) import numpy as np diff --git a/examples/VideoSpeedTest.py b/examples/VideoSpeedTest.py index 57d8aacc..1ec28a3c 100644 --- a/examples/VideoSpeedTest.py +++ b/examples/VideoSpeedTest.py @@ -1,8 +1,13 @@ -#!/usr/bin/python # -*- coding: utf-8 -*- -## Add path to library (just for examples; you do not need this) -import sys, os, time -sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..')) +""" +Tests the speed of image updates for an ImageItem and RawImageWidget. +The speed will generally depend on the type of data being shown, whether +it is being scaled and/or converted by lookup table, and whether OpenGL +is used by the view widget +""" + + +import initExample ## Add path to library (just for examples; you do not need this) from pyqtgraph.Qt import QtGui, QtCore @@ -136,6 +141,7 @@ timer.start(0) -## Start Qt event loop unless running in interactive mode. -if sys.flags.interactive != 1: +## Start Qt event loop unless running in interactive mode or using pyside. +import sys +if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): app.exec_() diff --git a/examples/__main__.py b/examples/__main__.py index b58e50ee..8511327d 100644 --- a/examples/__main__.py +++ b/examples/__main__.py @@ -12,9 +12,13 @@ examples = OrderedDict([ ('Basic Plotting', 'Plotting.py'), ('ImageView', 'ImageView.py'), ('ParameterTree', '../parametertree'), + ('Crosshair / Mouse interaction', 'crosshair.py'), + ('Video speed test', 'VideoSpeedTest.py'), + ('Plot speed test', 'PlotSpeedTest.py'), ('GraphicsItems', OrderedDict([ ('Scatter Plot', 'ScatterPlot.py'), #('PlotItem', 'PlotItem.py'), + ('IsocurveItem', 'isocurve.py'), ('ImageItem - video', 'ImageItem.py'), ('ImageItem - draw', 'Draw.py'), ('Region-of-Interest', 'ROItypes.py'), @@ -90,7 +94,10 @@ class ExampleLoader(QtGui.QMainWindow): fn = self.currentFile() if fn is None: return - os.spawnl(os.P_NOWAIT, sys.executable, sys.executable, '"' + fn + '"') + if sys.platform.startswith('win'): + os.spawnl(os.P_NOWAIT, sys.executable, sys.executable, '"' + fn + '"') + else: + os.spawnl(os.P_NOWAIT, sys.executable, sys.executable, fn) def showFile(self): diff --git a/examples/isocurve.py b/examples/isocurve.py new file mode 100644 index 00000000..05316373 --- /dev/null +++ b/examples/isocurve.py @@ -0,0 +1,58 @@ +# -*- coding: utf-8 -*- +""" +Tests use of IsoCurve item displayed with image +""" + + +import initExample ## Add path to library (just for examples; you do not need this) + + +from pyqtgraph.Qt import QtGui, QtCore +import numpy as np +import pyqtgraph as pg +import scipy.ndimage as ndi + +app = QtGui.QApplication([]) + +## make pretty looping data +frames = 200 +data = np.random.normal(size=(frames,30,30), loc=0, scale=100) +data = np.concatenate([data, data], axis=0) +data = ndi.gaussian_filter(data, (10, 10, 10))[frames/2:frames + frames/2] + +win = pg.GraphicsWindow() +vb = win.addViewBox() +img = pg.ImageItem(data[0]) +vb.addItem(img) +vb.setAspectLocked() + +## generate empty curves +curves = [] +levels = np.linspace(data.min(), data.max(), 10) +for i in range(len(levels)): + v = levels[i] + ## generate isocurve with automatic color selection + c = pg.IsocurveItem(level=v, pen=(i, len(levels)*1.5)) + c.setParentItem(img) ## make sure isocurve is always correctly displayed over image + c.setZValue(10) + curves.append(c) + +## animate! +ptr = 0 +imgLevels = (data.min(), data.max() * 2) +def update(): + global data, curves, img, ptr, imgLevels + ptr = (ptr + 1) % data.shape[0] + data[ptr] + img.setImage(data[ptr], levels=imgLevels) + for c in curves: + c.setData(data[ptr]) + +timer = QtCore.QTimer() +timer.timeout.connect(update) +timer.start(50) + +## Start Qt event loop unless running in interactive mode or using pyside. +import sys +if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): + app.exec_() diff --git a/graphicsItems/IsocurveItem.py b/graphicsItems/IsocurveItem.py index 62e582fc..eb87418a 100644 --- a/graphicsItems/IsocurveItem.py +++ b/graphicsItems/IsocurveItem.py @@ -2,7 +2,7 @@ from GraphicsObject import * import pyqtgraph.functions as fn -from pyqtgraph.Qt import QtGui +from pyqtgraph.Qt import QtGui, QtCore class IsocurveItem(GraphicsObject): @@ -13,26 +13,50 @@ class IsocurveItem(GraphicsObject): call isocurve.setParentItem(image) """ - def __init__(self, data, level, pen='w'): + def __init__(self, data=None, level=0, pen='w'): GraphicsObject.__init__(self) - - lines = fn.isocurve(data, level) - - self.path = QtGui.QPainterPath() + self.level = 0 + self.data = None + self.path = None + self.setData(data, level) self.setPen(pen) - for line in lines: - self.path.moveTo(*line[0]) - self.path.lineTo(*line[1]) - + + def setData(self, data, level=None): + if level is None: + level = self.level + self.level = level + self.data = data + self.path = None + self.prepareGeometryChange() + self.update() + + def setLevel(self, level): + self.level = level + self.path = None + self.update() + def setPen(self, *args, **kwargs): self.pen = fn.mkPen(*args, **kwargs) self.update() def boundingRect(self): + if self.path is None: + return QtCore.QRectF() return self.path.boundingRect() + def generatePath(self): + self.path = QtGui.QPainterPath() + if self.data is None: + return + lines = fn.isocurve(self.data, self.level) + for line in lines: + self.path.moveTo(*line[0]) + self.path.lineTo(*line[1]) + def paint(self, p, *args): + if self.path is None: + self.generatePath() p.setPen(self.pen) p.drawPath(self.path) \ No newline at end of file