Updates to IsocurveItem, added isocurve example

minor updates for other examples
This commit is contained in:
Luke Campagnola 2012-04-04 12:22:43 -04:00
parent fffbd5548e
commit bdb6ff88a2
5 changed files with 112 additions and 19 deletions

View File

@ -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

View File

@ -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_()

View File

@ -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):

58
examples/isocurve.py Normal file
View File

@ -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_()

View File

@ -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)