minor updates / fixes

This commit is contained in:
Luke Campagnola 2010-07-25 00:33:26 -04:00
parent 2ca08c69ce
commit 39cf791fba
4 changed files with 70 additions and 49 deletions

View File

@ -9,12 +9,11 @@ from pyqtgraph.graphicsWindows import *
from pyqtgraph.graphicsItems import * from pyqtgraph.graphicsItems import *
from pyqtgraph.widgets import * from pyqtgraph.widgets import *
from pyqtgraph.PlotWidget import * from pyqtgraph.PlotWidget import *
from pyqtgraph.functions import mkPen
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([]) app = QtGui.QApplication([])
#i = PlotWindow(array([0,1,2,1,2]), parent=None, title='')
class Win(QtGui.QMainWindow): class Win(QtGui.QMainWindow):
pass pass
@ -26,12 +25,8 @@ v.enableMouse(True)
v.autoPixelScale = False v.autoPixelScale = False
w.setCentralWidget(v) w.setCentralWidget(v)
#s = QtGui.QGraphicsScene()
#v.setScene(s)
s = v.scene() s = v.scene()
v.setRange(QtCore.QRect(-2, -2, 220, 220))
#p = Plot(array([0,2,1,3,4]), copy=False)
#s.addItem(p)
arr = ones((100, 100), dtype=float) arr = ones((100, 100), dtype=float)
arr[45:55, 45:55] = 0 arr[45:55, 45:55] = 0
@ -56,41 +51,57 @@ s.addItem(im4)
im4.moveBy(110, 130) im4.moveBy(110, 130)
im4.setZValue(10) im4.setZValue(10)
pi1 = PlotItem()
s.addItem(pi1)
pi1.scale(0.5, 0.5)
pi1.setGeometry(0, 170, 300, 100)
#g = Grid(view=v, bounds=QtCore.QRectF(0.1, 0.1, 0.8, 0.8)) lastRoi = None
#g = Grid(view=v)
#s.addItem(g)
#wid = RectROI([0, 0], [2, 2], maxBounds=QtCore.QRectF(-1, -1, 5, 5)) def updateRoi(roi):
roi = TestROI([0, 0], [20, 20], maxBounds=QtCore.QRectF(-10, -10, 230, 140)) global im1, im2, im3, im4, arr, lastRoi
s.addItem(roi) if roi is None:
roi2 = LineROI([0, 0], [20, 20], width=5) return
s.addItem(roi2) lastRoi = roi
mlroi = MultiLineROI([[0, 50], [50, 60], [60, 30]], width=5) arr1 = roi.getArrayRegion(im1.image, img=im1)
s.addItem(mlroi) im3.updateImage(arr1, autoRange=True)
elroi = EllipseROI([110, 10], [30, 20]) arr2 = roi.getArrayRegion(im2.image, img=im2)
s.addItem(elroi) im4.updateImage(arr2, autoRange=True)
croi = CircleROI([110, 50], [20, 20]) updateRoiPlot(roi, arr1)
s.addItem(croi)
troi = PolygonROI([[0,0], [1,0], [0,1]]) def updateRoiPlot(roi, data=None):
s.addItem(troi) if data is None:
data = roi.getArrayRegion(im1.image, img=im1)
if data is not None:
roi.curve.updateData(data.mean(axis=1))
#def updatePlot(roi)
def updateImg(roi): rois = []
global im1, im2, im3, im4, arr rois.append(TestROI([0, 0], [20, 20], maxBounds=QtCore.QRectF(-10, -10, 230, 140), pen=mkPen(0)))
arr1 = roi.getArrayRegion(arr, img=im1) rois.append(LineROI([0, 0], [20, 20], width=5, pen=mkPen(1)))
im3.updateImage(arr1, autoRange=True) rois.append(MultiLineROI([[0, 50], [50, 60], [60, 30]], width=5, pen=mkPen(2)))
arr2 = roi.getArrayRegion(arr, img=im2) rois.append(EllipseROI([110, 10], [30, 20], pen=mkPen(3)))
im4.updateImage(arr2, autoRange=True) rois.append(CircleROI([110, 50], [20, 20], pen=mkPen(4)))
rois.append(PolygonROI([[2,0], [2.1,0], [2,.1]], pen=mkPen(5)))
for r in rois:
s.addItem(r)
c = pi1.plot(pen=r.pen)
r.curve = c
r.connect(r, QtCore.SIGNAL('regionChanged'), updateRoi)
roi.connect(roi, QtCore.SIGNAL('regionChanged'), lambda: updateImg(roi)) def updateImage():
roi2.connect(roi2, QtCore.SIGNAL('regionChanged'), lambda: updateImg(roi2)) global im1, arr, lastRoi
croi.connect(croi, QtCore.SIGNAL('regionChanged'), lambda: updateImg(croi)) r = abs(random.normal(loc=0, scale=(arr.max()-arr.min())*0.1, size=arr.shape))
elroi.connect(elroi, QtCore.SIGNAL('regionChanged'), lambda: updateImg(elroi)) im1.updateImage(arr + r)
mlroi.connect(mlroi, QtCore.SIGNAL('regionChanged'), lambda: updateImg(mlroi)) updateRoi(lastRoi)
for r in rois:
updateRoiPlot(r)
v.setRange(QtCore.QRect(-2, -2, 220, 220))
t = QtCore.QTimer()
t.connect(t, QtCore.SIGNAL('timeout()'), updateImage)
t.start(50)
w.show() w.show()
app.exec_() app.exec_()

View File

@ -61,6 +61,8 @@ def mkColor(*args):
(r, g, b, a) = args[0] (r, g, b, a) = args[0]
else: else:
raise Exception(err) raise Exception(err)
if type(args[0]) == int:
return intColor(args[0])
else: else:
raise Exception(err) raise Exception(err)
elif len(args) == 3: elif len(args) == 3:

View File

@ -12,8 +12,11 @@ from PyQt4 import QtGui, QtCore
from ObjectWorkaround import * from ObjectWorkaround import *
#tryWorkaround(QtCore, QtGui) #tryWorkaround(QtCore, QtGui)
from numpy import * from numpy import *
import scipy.weave as weave try:
from scipy.weave import converters import scipy.weave as weave
from scipy.weave import converters
except:
pass
from scipy.fftpack import fft from scipy.fftpack import fft
from scipy.signal import resample from scipy.signal import resample
import scipy.stats import scipy.stats
@ -509,8 +512,6 @@ class PlotCurveItem(GraphicsObject):
elif data.ndim == 1: elif data.ndim == 1:
y = data y = data
if x.shape != y.shape:
raise Exception("X and Y arrays must be the same shape--got %s and %s." % (str(x.shape), str(y.shape)))
self.prepareGeometryChange() self.prepareGeometryChange()
if copy: if copy:
self.yData = y.copy() self.yData = y.copy()
@ -523,8 +524,11 @@ class PlotCurveItem(GraphicsObject):
self.xData = x self.xData = x
if x is None: if x is None:
self.xData = arange(0, self.y.shape[0]) self.xData = arange(0, self.yData.shape[0])
if self.xData.shape != self.yData.shape:
raise Exception("X and Y arrays must be the same shape--got %s and %s." % (str(x.shape), str(y.shape)))
self.path = None self.path = None
#self.specPath = None #self.specPath = None
self.xDisp = self.yDisp = None self.xDisp = self.yDisp = None

View File

@ -33,7 +33,7 @@ def rectStr(r):
class ROI(QtGui.QGraphicsItem, QObjectWorkaround): class ROI(QtGui.QGraphicsItem, QObjectWorkaround):
def __init__(self, pos, size=Point(1, 1), angle=0.0, invertible=False, maxBounds=None, snapSize=1.0, scaleSnap=False, translateSnap=False, rotateSnap=False, parent=None): def __init__(self, pos, size=Point(1, 1), angle=0.0, invertible=False, maxBounds=None, snapSize=1.0, scaleSnap=False, translateSnap=False, rotateSnap=False, parent=None, pen=None):
QObjectWorkaround.__init__(self) QObjectWorkaround.__init__(self)
QtGui.QGraphicsItem.__init__(self, parent) QtGui.QGraphicsItem.__init__(self, parent)
pos = Point(pos) pos = Point(pos)
@ -41,7 +41,10 @@ class ROI(QtGui.QGraphicsItem, QObjectWorkaround):
self.aspectLocked = False self.aspectLocked = False
self.translatable = True self.translatable = True
self.pen = QtGui.QPen(QtGui.QColor(255, 255, 255)) if pen is None:
self.pen = QtGui.QPen(QtGui.QColor(255, 255, 255))
else:
self.pen = pen
self.handlePen = QtGui.QPen(QtGui.QColor(150, 255, 255)) self.handlePen = QtGui.QPen(QtGui.QColor(150, 255, 255))
self.handles = [] self.handles = []
self.state = {'pos': pos, 'size': size, 'angle': angle} self.state = {'pos': pos, 'size': size, 'angle': angle}
@ -755,14 +758,15 @@ class LineROI(ROI):
class MultiLineROI(QtGui.QGraphicsItem, QObjectWorkaround): class MultiLineROI(QtGui.QGraphicsItem, QObjectWorkaround):
def __init__(self, points, width, **args): def __init__(self, points, width, pen=None, **args):
QObjectWorkaround.__init__(self) QObjectWorkaround.__init__(self)
QtGui.QGraphicsItem.__init__(self) QtGui.QGraphicsItem.__init__(self)
self.pen = pen
self.roiArgs = args self.roiArgs = args
if len(points) < 2: if len(points) < 2:
raise Exception("Must start with at least 2 points") raise Exception("Must start with at least 2 points")
self.lines = [] self.lines = []
self.lines.append(ROI([0, 0], [1, 5], parent=self)) self.lines.append(ROI([0, 0], [1, 5], parent=self, pen=pen, **args))
self.lines[-1].addScaleHandle([0.5, 1], [0.5, 0.5]) self.lines[-1].addScaleHandle([0.5, 1], [0.5, 0.5])
h = self.lines[-1].addScaleRotateHandle([0, 0.5], [1, 0.5]) h = self.lines[-1].addScaleRotateHandle([0, 0.5], [1, 0.5])
h.movePoint(points[0]) h.movePoint(points[0])
@ -770,7 +774,7 @@ class MultiLineROI(QtGui.QGraphicsItem, QObjectWorkaround):
for i in range(1, len(points)): for i in range(1, len(points)):
h = self.lines[-1].addScaleRotateHandle([1, 0.5], [0, 0.5]) h = self.lines[-1].addScaleRotateHandle([1, 0.5], [0, 0.5])
if i < len(points)-1: if i < len(points)-1:
self.lines.append(ROI([0, 0], [1, 5], parent=self)) self.lines.append(ROI([0, 0], [1, 5], parent=self, pen=pen, **args))
self.lines[-1].addScaleRotateHandle([0, 0.5], [1, 0.5], item=h) self.lines[-1].addScaleRotateHandle([0, 0.5], [1, 0.5], item=h)
h.movePoint(points[i]) h.movePoint(points[i])
h.movePoint(points[i]) h.movePoint(points[i])
@ -857,8 +861,8 @@ class CircleROI(EllipseROI):
self.addScaleHandle([0.5*2.**-0.5 + 0.5, 0.5*2.**-0.5 + 0.5], [0.5, 0.5]) self.addScaleHandle([0.5*2.**-0.5 + 0.5, 0.5*2.**-0.5 + 0.5], [0.5, 0.5])
class PolygonROI(ROI): class PolygonROI(ROI):
def __init__(self, positions): def __init__(self, positions, **args):
ROI.__init__(self, [0,0], [100,100]) ROI.__init__(self, [0,0], [100,100], **args)
for p in positions: for p in positions:
self.addFreeHandle(p) self.addFreeHandle(p)