updated examples to use normal axis order, fixed a few ROI bugs

This commit is contained in:
Luke Campagnola 2016-05-03 09:13:25 -07:00
parent bee5878915
commit e740cb4b49
3 changed files with 22 additions and 14 deletions

View File

@ -11,6 +11,7 @@ import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui from pyqtgraph.Qt import QtCore, QtGui
import numpy as np import numpy as np
pg.setConfigOptions(imageAxisOrder='normal')
## Create image to display ## Create image to display
arr = np.ones((100, 100), dtype=float) arr = np.ones((100, 100), dtype=float)
@ -24,6 +25,11 @@ arr[:, 50] = 10
arr += np.sin(np.linspace(0, 20, 100)).reshape(1, 100) arr += np.sin(np.linspace(0, 20, 100)).reshape(1, 100)
arr += np.random.normal(size=(100,100)) arr += np.random.normal(size=(100,100))
# add an arrow for asymmetry
arr[10, :50] = 10
arr[9:12, 44:48] = 10
arr[8:13, 44:46] = 10
## create GUI ## create GUI
app = QtGui.QApplication([]) app = QtGui.QApplication([])

View File

@ -58,10 +58,10 @@ win.show()
# Generate image data # Generate image data
data = np.random.normal(size=(100, 200)) data = np.random.normal(size=(200, 100))
data[20:80, 20:80] += 2. data[20:80, 20:80] += 2.
data = pg.gaussianFilter(data, (3, 3)) data = pg.gaussianFilter(data, (3, 3))
data += np.random.normal(size=(100, 200)) * 0.1 data += np.random.normal(size=(200, 100)) * 0.1
img.setImage(data) img.setImage(data)
hist.setLevels(data.min(), data.max()) hist.setLevels(data.min(), data.max())
@ -80,7 +80,7 @@ p1.autoRange()
def updatePlot(): def updatePlot():
global img, roi, data, p2 global img, roi, data, p2
selected = roi.getArrayRegion(data, img) selected = roi.getArrayRegion(data, img)
p2.plot(selected.mean(axis=1), clear=True) p2.plot(selected.mean(axis=0), clear=True)
roi.sigRegionChanged.connect(updatePlot) roi.sigRegionChanged.connect(updatePlot)
updatePlot() updatePlot()

View File

@ -1020,11 +1020,8 @@ class ROI(GraphicsObject):
If the slice can not be computed (usually because the scene/transforms are not properly If the slice can not be computed (usually because the scene/transforms are not properly
constructed yet), then the method returns None. constructed yet), then the method returns None.
""" """
#print "getArraySlice"
## Determine shape of array along ROI axes ## Determine shape of array along ROI axes
dShape = (data.shape[axes[0]], data.shape[axes[1]]) dShape = (data.shape[axes[0]], data.shape[axes[1]])
#print " dshape", dShape
## Determine transform that maps ROI bounding box to image coordinates ## Determine transform that maps ROI bounding box to image coordinates
try: try:
@ -1033,25 +1030,28 @@ class ROI(GraphicsObject):
return None return None
## Modify transform to scale from image coords to data coords ## Modify transform to scale from image coords to data coords
#m = QtGui.QTransform() axisOrder = getConfigOption('imageAxisOrder')
if axisOrder == 'normal':
tr.scale(float(dShape[1]) / img.width(), float(dShape[0]) / img.height())
else:
tr.scale(float(dShape[0]) / img.width(), float(dShape[1]) / img.height()) tr.scale(float(dShape[0]) / img.width(), float(dShape[1]) / img.height())
#tr = tr * m
## Transform ROI bounds into data bounds ## Transform ROI bounds into data bounds
dataBounds = tr.mapRect(self.boundingRect()) dataBounds = tr.mapRect(self.boundingRect())
#print " boundingRect:", self.boundingRect()
#print " dataBounds:", dataBounds
## Intersect transformed ROI bounds with data bounds ## Intersect transformed ROI bounds with data bounds
if axisOrder == 'normal':
intBounds = dataBounds.intersected(QtCore.QRectF(0, 0, dShape[1], dShape[0]))
else:
intBounds = dataBounds.intersected(QtCore.QRectF(0, 0, dShape[0], dShape[1])) intBounds = dataBounds.intersected(QtCore.QRectF(0, 0, dShape[0], dShape[1]))
#print " intBounds:", intBounds
## Determine index values to use when referencing the array. ## Determine index values to use when referencing the array.
bounds = ( bounds = (
(int(min(intBounds.left(), intBounds.right())), int(1+max(intBounds.left(), intBounds.right()))), (int(min(intBounds.left(), intBounds.right())), int(1+max(intBounds.left(), intBounds.right()))),
(int(min(intBounds.bottom(), intBounds.top())), int(1+max(intBounds.bottom(), intBounds.top()))) (int(min(intBounds.bottom(), intBounds.top())), int(1+max(intBounds.bottom(), intBounds.top())))
) )
#print " bounds:", bounds if axisOrder == 'normal':
bounds = bounds[::-1]
if returnSlice: if returnSlice:
## Create slice objects ## Create slice objects
@ -1650,6 +1650,8 @@ class MultiRectROI(QtGui.QGraphicsObject):
## make sure orthogonal axis is the same size ## make sure orthogonal axis is the same size
## (sometimes fp errors cause differences) ## (sometimes fp errors cause differences)
if getConfigOption('imageAxisOrder') == 'normal':
axes = axes[::-1]
ms = min([r.shape[axes[1]] for r in rgns]) ms = min([r.shape[axes[1]] for r in rgns])
sl = [slice(None)] * rgns[0].ndim sl = [slice(None)] * rgns[0].ndim
sl[axes[1]] = slice(0,ms) sl[axes[1]] = slice(0,ms)