2020-02-22 00:24:33 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-05-09 16:00:58 +00:00
|
|
|
import numpy as np
|
|
|
|
import pytest
|
2016-05-03 16:20:23 +00:00
|
|
|
import pyqtgraph as pg
|
2021-05-28 04:57:07 +00:00
|
|
|
import platform
|
2021-01-31 15:13:54 +00:00
|
|
|
from pyqtgraph.Qt import QtCore, QtGui, QtTest
|
2021-05-28 04:57:07 +00:00
|
|
|
from tests.image_testing import assertImageApproved
|
|
|
|
from tests.ui_testing import mouseMove, mouseDrag, mouseClick, resizeWindow
|
2016-05-09 16:00:58 +00:00
|
|
|
|
|
|
|
app = pg.mkQApp()
|
2020-10-14 05:33:00 +00:00
|
|
|
pg.setConfigOption("mouseRateLimit", 0)
|
2016-05-03 16:20:23 +00:00
|
|
|
|
2016-08-26 01:18:15 +00:00
|
|
|
def test_getArrayRegion(transpose=False):
|
2016-05-18 01:04:52 +00:00
|
|
|
pr = pg.PolyLineROI([[0, 0], [27, 0], [0, 28]], closed=True)
|
|
|
|
pr.setPos(1, 1)
|
2016-05-09 16:00:58 +00:00
|
|
|
rois = [
|
|
|
|
(pg.ROI([1, 1], [27, 28], pen='y'), 'baseroi'),
|
|
|
|
(pg.RectROI([1, 1], [27, 28], pen='y'), 'rectroi'),
|
|
|
|
(pg.EllipseROI([1, 1], [27, 28], pen='y'), 'ellipseroi'),
|
2016-05-18 01:04:52 +00:00
|
|
|
(pr, 'polylineroi'),
|
2016-05-09 16:00:58 +00:00
|
|
|
]
|
|
|
|
for roi, name in rois:
|
2016-05-18 01:04:52 +00:00
|
|
|
# For some ROIs, resize should not be used.
|
|
|
|
testResize = not isinstance(roi, pg.PolyLineROI)
|
|
|
|
|
2016-08-23 16:04:07 +00:00
|
|
|
origMode = pg.getConfigOption('imageAxisOrder')
|
|
|
|
try:
|
2016-08-26 01:18:15 +00:00
|
|
|
if transpose:
|
|
|
|
pg.setConfigOptions(imageAxisOrder='row-major')
|
|
|
|
check_getArrayRegion(roi, 'roi/'+name, testResize, transpose=True)
|
|
|
|
else:
|
|
|
|
pg.setConfigOptions(imageAxisOrder='col-major')
|
|
|
|
check_getArrayRegion(roi, 'roi/'+name, testResize)
|
2016-08-23 16:04:07 +00:00
|
|
|
finally:
|
|
|
|
pg.setConfigOptions(imageAxisOrder=origMode)
|
2016-05-09 16:00:58 +00:00
|
|
|
|
2016-08-26 01:18:15 +00:00
|
|
|
def test_getArrayRegion_axisorder():
|
|
|
|
test_getArrayRegion(transpose=True)
|
|
|
|
|
2016-05-09 16:00:58 +00:00
|
|
|
|
2016-08-23 16:04:07 +00:00
|
|
|
def check_getArrayRegion(roi, name, testResize=True, transpose=False):
|
2021-05-28 04:57:07 +00:00
|
|
|
# on windows, edges corner pixels seem to be slightly different from other platforms
|
|
|
|
# giving a pxCount=2 for a fudge factor
|
|
|
|
if isinstance(roi, (pg.ROI, pg.RectROI)) and platform.system() == "Windows":
|
|
|
|
pxCount = 2
|
|
|
|
else:
|
|
|
|
pxCount=-1
|
|
|
|
|
|
|
|
|
2016-05-18 01:04:52 +00:00
|
|
|
initState = roi.getState()
|
|
|
|
|
2016-06-17 00:34:39 +00:00
|
|
|
win = pg.GraphicsView()
|
2016-05-09 16:00:58 +00:00
|
|
|
win.show()
|
2016-12-14 16:35:33 +00:00
|
|
|
resizeWindow(win, 200, 400)
|
2021-05-28 04:57:07 +00:00
|
|
|
# Don't use Qt's layouts for testing--these generate unpredictable results.
|
2016-06-17 00:34:39 +00:00
|
|
|
# Instead, place the viewboxes manually
|
|
|
|
vb1 = pg.ViewBox()
|
|
|
|
win.scene().addItem(vb1)
|
|
|
|
vb1.setPos(6, 6)
|
|
|
|
vb1.resize(188, 191)
|
|
|
|
|
|
|
|
vb2 = pg.ViewBox()
|
|
|
|
win.scene().addItem(vb2)
|
|
|
|
vb2.setPos(6, 203)
|
|
|
|
vb2.resize(188, 191)
|
|
|
|
|
2016-08-26 01:18:15 +00:00
|
|
|
img1 = pg.ImageItem(border='w')
|
|
|
|
img2 = pg.ImageItem(border='w')
|
2016-08-23 16:04:07 +00:00
|
|
|
|
2016-05-09 16:00:58 +00:00
|
|
|
vb1.addItem(img1)
|
|
|
|
vb2.addItem(img2)
|
|
|
|
|
|
|
|
np.random.seed(0)
|
|
|
|
data = np.random.normal(size=(7, 30, 31, 5))
|
|
|
|
data[0, :, :, :] += 10
|
|
|
|
data[:, 1, :, :] += 10
|
|
|
|
data[:, :, 2, :] += 10
|
|
|
|
data[:, :, :, 3] += 10
|
|
|
|
|
2016-08-26 01:18:15 +00:00
|
|
|
if transpose:
|
|
|
|
data = data.transpose(0, 2, 1, 3)
|
|
|
|
|
2016-05-09 16:00:58 +00:00
|
|
|
img1.setImage(data[0, ..., 0])
|
|
|
|
vb1.setAspectLocked()
|
|
|
|
vb1.enableAutoRange(True, True)
|
|
|
|
|
|
|
|
roi.setZValue(10)
|
|
|
|
vb1.addItem(roi)
|
|
|
|
|
2016-08-26 01:18:15 +00:00
|
|
|
if isinstance(roi, pg.RectROI):
|
|
|
|
if transpose:
|
|
|
|
assert roi.getAffineSliceParams(data, img1, axes=(1, 2)) == ([28.0, 27.0], ((1.0, 0.0), (0.0, 1.0)), (1.0, 1.0))
|
|
|
|
else:
|
|
|
|
assert roi.getAffineSliceParams(data, img1, axes=(1, 2)) == ([27.0, 28.0], ((1.0, 0.0), (0.0, 1.0)), (1.0, 1.0))
|
|
|
|
|
2016-05-09 16:00:58 +00:00
|
|
|
rgn = roi.getArrayRegion(data, img1, axes=(1, 2))
|
2016-08-23 16:04:07 +00:00
|
|
|
#assert np.all((rgn == data[:, 1:-2, 1:-2, :]) | (rgn == 0))
|
2016-05-09 16:00:58 +00:00
|
|
|
img2.setImage(rgn[0, ..., 0])
|
|
|
|
vb2.setAspectLocked()
|
|
|
|
vb2.enableAutoRange(True, True)
|
|
|
|
|
|
|
|
app.processEvents()
|
2021-05-28 04:57:07 +00:00
|
|
|
assertImageApproved(win, name+'/roi_getarrayregion', 'Simple ROI region selection.', pxCount=pxCount)
|
2016-05-09 16:00:58 +00:00
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
2016-05-18 01:04:52 +00:00
|
|
|
roi.setPos(0, False)
|
2016-05-09 16:00:58 +00:00
|
|
|
|
|
|
|
roi.setPos([0.5, 1.5])
|
|
|
|
rgn = roi.getArrayRegion(data, img1, axes=(1, 2))
|
|
|
|
img2.setImage(rgn[0, ..., 0])
|
|
|
|
app.processEvents()
|
2021-05-28 04:57:07 +00:00
|
|
|
assertImageApproved(win, name+'/roi_getarrayregion_halfpx', 'Simple ROI region selection, 0.5 pixel shift.', pxCount=pxCount)
|
2016-05-09 16:00:58 +00:00
|
|
|
|
|
|
|
roi.setAngle(45)
|
|
|
|
roi.setPos([3, 0])
|
|
|
|
rgn = roi.getArrayRegion(data, img1, axes=(1, 2))
|
|
|
|
img2.setImage(rgn[0, ..., 0])
|
|
|
|
app.processEvents()
|
2021-05-28 04:57:07 +00:00
|
|
|
assertImageApproved(win, name+'/roi_getarrayregion_rotate', 'Simple ROI region selection, rotation.', pxCount=pxCount)
|
2016-05-09 16:00:58 +00:00
|
|
|
|
2016-05-18 01:04:52 +00:00
|
|
|
if testResize:
|
|
|
|
roi.setSize([60, 60])
|
|
|
|
rgn = roi.getArrayRegion(data, img1, axes=(1, 2))
|
|
|
|
img2.setImage(rgn[0, ..., 0])
|
|
|
|
app.processEvents()
|
2021-05-28 04:57:07 +00:00
|
|
|
assertImageApproved(win, name+'/roi_getarrayregion_resize', 'Simple ROI region selection, resized.', pxCount=pxCount)
|
2016-05-09 16:00:58 +00:00
|
|
|
|
|
|
|
img1.setPos(0, img1.height())
|
2021-01-31 15:13:54 +00:00
|
|
|
img1.setTransform(QtGui.QTransform().scale(1, -1).rotate(20), True)
|
2016-05-09 16:00:58 +00:00
|
|
|
rgn = roi.getArrayRegion(data, img1, axes=(1, 2))
|
|
|
|
img2.setImage(rgn[0, ..., 0])
|
|
|
|
app.processEvents()
|
2021-05-28 04:57:07 +00:00
|
|
|
assertImageApproved(win, name+'/roi_getarrayregion_img_trans', 'Simple ROI region selection, image transformed.', pxCount=pxCount)
|
2016-05-09 16:00:58 +00:00
|
|
|
|
|
|
|
vb1.invertY()
|
|
|
|
rgn = roi.getArrayRegion(data, img1, axes=(1, 2))
|
|
|
|
img2.setImage(rgn[0, ..., 0])
|
|
|
|
app.processEvents()
|
2019-07-02 10:57:45 +00:00
|
|
|
assertImageApproved(win, name+'/roi_getarrayregion_inverty', 'Simple ROI region selection, view inverted.', pxCount=pxCount)
|
2016-05-09 16:00:58 +00:00
|
|
|
|
2016-05-18 01:04:52 +00:00
|
|
|
roi.setState(initState)
|
2016-05-09 16:00:58 +00:00
|
|
|
img1.setPos(0, 0)
|
2021-01-31 15:13:54 +00:00
|
|
|
img1.setTransform(QtGui.QTransform.fromScale(1, 0.5))
|
2016-05-09 16:00:58 +00:00
|
|
|
rgn = roi.getArrayRegion(data, img1, axes=(1, 2))
|
|
|
|
img2.setImage(rgn[0, ..., 0])
|
|
|
|
app.processEvents()
|
2021-05-28 04:57:07 +00:00
|
|
|
assertImageApproved(win, name+'/roi_getarrayregion_anisotropic', 'Simple ROI region selection, image scaled anisotropically.', pxCount=pxCount)
|
2016-08-23 16:04:07 +00:00
|
|
|
|
|
|
|
# allow the roi to be re-used
|
|
|
|
roi.scene().removeItem(roi)
|
2016-05-09 16:00:58 +00:00
|
|
|
|
2019-08-17 05:32:06 +00:00
|
|
|
win.hide()
|
2016-05-09 16:00:58 +00:00
|
|
|
|
2021-05-27 00:14:08 +00:00
|
|
|
|
|
|
|
def test_mouseClickEvent():
|
|
|
|
plt = pg.GraphicsView()
|
|
|
|
plt.show()
|
|
|
|
resizeWindow(plt, 200, 200)
|
|
|
|
vb = pg.ViewBox()
|
|
|
|
plt.scene().addItem(vb)
|
|
|
|
vb.resize(200, 200)
|
|
|
|
QtTest.QTest.qWaitForWindowExposed(plt)
|
|
|
|
QtTest.QTest.qWait(100)
|
|
|
|
|
|
|
|
roi = pg.RectROI((0, 0), (10, 20), removable=True)
|
|
|
|
vb.addItem(roi)
|
|
|
|
app.processEvents()
|
|
|
|
|
|
|
|
mouseClick(plt, roi.mapToScene(pg.Point(2, 2)), QtCore.Qt.LeftButton)
|
|
|
|
|
|
|
|
|
2016-05-31 01:00:19 +00:00
|
|
|
def test_PolyLineROI():
|
|
|
|
rois = [
|
|
|
|
(pg.PolyLineROI([[0, 0], [10, 0], [0, 15]], closed=True, pen=0.3), 'closed'),
|
|
|
|
(pg.PolyLineROI([[0, 0], [10, 0], [0, 15]], closed=False, pen=0.3), 'open')
|
|
|
|
]
|
2016-06-17 00:34:39 +00:00
|
|
|
|
|
|
|
#plt = pg.plot()
|
|
|
|
plt = pg.GraphicsView()
|
|
|
|
plt.show()
|
2016-12-14 16:35:33 +00:00
|
|
|
resizeWindow(plt, 200, 200)
|
2016-07-18 15:13:25 +00:00
|
|
|
vb = pg.ViewBox()
|
|
|
|
plt.scene().addItem(vb)
|
|
|
|
vb.resize(200, 200)
|
|
|
|
#plt.plotItem = pg.PlotItem()
|
|
|
|
#plt.scene().addItem(plt.plotItem)
|
|
|
|
#plt.plotItem.resize(200, 200)
|
|
|
|
|
2016-05-09 16:00:58 +00:00
|
|
|
|
2016-05-31 01:00:19 +00:00
|
|
|
plt.scene().minDragTime = 0 # let us simulate mouse drags very quickly.
|
|
|
|
|
|
|
|
# seemingly arbitrary requirements; might need longer wait time for some platforms..
|
2021-02-04 03:27:17 +00:00
|
|
|
QtTest.QTest.qWaitForWindowExposed(plt)
|
2016-05-31 01:00:19 +00:00
|
|
|
QtTest.QTest.qWait(100)
|
2016-05-03 16:20:23 +00:00
|
|
|
|
2016-05-31 01:00:19 +00:00
|
|
|
for r, name in rois:
|
2016-07-18 15:13:25 +00:00
|
|
|
vb.clear()
|
|
|
|
vb.addItem(r)
|
|
|
|
vb.autoRange()
|
2016-05-31 01:00:19 +00:00
|
|
|
app.processEvents()
|
|
|
|
|
|
|
|
assertImageApproved(plt, 'roi/polylineroi/'+name+'_init', 'Init %s polyline.' % name)
|
|
|
|
initState = r.getState()
|
|
|
|
assert len(r.getState()['points']) == 3
|
|
|
|
|
|
|
|
# hover over center
|
|
|
|
center = r.mapToScene(pg.Point(3, 3))
|
|
|
|
mouseMove(plt, center)
|
|
|
|
assertImageApproved(plt, 'roi/polylineroi/'+name+'_hover_roi', 'Hover mouse over center of ROI.')
|
|
|
|
|
|
|
|
# drag ROI
|
|
|
|
mouseDrag(plt, center, center + pg.Point(10, -10), QtCore.Qt.LeftButton)
|
|
|
|
assertImageApproved(plt, 'roi/polylineroi/'+name+'_drag_roi', 'Drag mouse over center of ROI.')
|
|
|
|
|
|
|
|
# hover over handle
|
|
|
|
pt = r.mapToScene(pg.Point(r.getState()['points'][2]))
|
|
|
|
mouseMove(plt, pt)
|
|
|
|
assertImageApproved(plt, 'roi/polylineroi/'+name+'_hover_handle', 'Hover mouse over handle.')
|
|
|
|
|
|
|
|
# drag handle
|
|
|
|
mouseDrag(plt, pt, pt + pg.Point(5, 20), QtCore.Qt.LeftButton)
|
|
|
|
assertImageApproved(plt, 'roi/polylineroi/'+name+'_drag_handle', 'Drag mouse over handle.')
|
|
|
|
|
|
|
|
# hover over segment
|
|
|
|
pt = r.mapToScene((pg.Point(r.getState()['points'][2]) + pg.Point(r.getState()['points'][1])) * 0.5)
|
|
|
|
mouseMove(plt, pt+pg.Point(0, 2))
|
|
|
|
assertImageApproved(plt, 'roi/polylineroi/'+name+'_hover_segment', 'Hover mouse over diagonal segment.')
|
|
|
|
|
|
|
|
# click segment
|
|
|
|
mouseClick(plt, pt, QtCore.Qt.LeftButton)
|
|
|
|
assertImageApproved(plt, 'roi/polylineroi/'+name+'_click_segment', 'Click mouse over segment.')
|
2017-10-18 04:22:55 +00:00
|
|
|
|
|
|
|
# drag new handle
|
|
|
|
mouseMove(plt, pt+pg.Point(10, -10)) # pg bug: have to move the mouse off/on again to register hover
|
|
|
|
mouseDrag(plt, pt, pt + pg.Point(10, -10), QtCore.Qt.LeftButton)
|
|
|
|
assertImageApproved(plt, 'roi/polylineroi/'+name+'_drag_new_handle', 'Drag mouse over created handle.')
|
2016-05-31 01:00:19 +00:00
|
|
|
|
2017-10-18 04:22:55 +00:00
|
|
|
# clear all points
|
2016-05-31 01:00:19 +00:00
|
|
|
r.clearPoints()
|
|
|
|
assertImageApproved(plt, 'roi/polylineroi/'+name+'_clear', 'All points cleared.')
|
|
|
|
assert len(r.getState()['points']) == 0
|
|
|
|
|
2017-10-18 04:22:55 +00:00
|
|
|
# call setPoints
|
2016-05-31 01:00:19 +00:00
|
|
|
r.setPoints(initState['points'])
|
|
|
|
assertImageApproved(plt, 'roi/polylineroi/'+name+'_setpoints', 'Reset points to initial state.')
|
|
|
|
assert len(r.getState()['points']) == 3
|
|
|
|
|
2017-10-18 04:22:55 +00:00
|
|
|
# call setState
|
2016-05-31 01:00:19 +00:00
|
|
|
r.setState(initState)
|
|
|
|
assertImageApproved(plt, 'roi/polylineroi/'+name+'_setstate', 'Reset ROI to initial state.')
|
|
|
|
assert len(r.getState()['points']) == 3
|
2019-08-17 05:32:06 +00:00
|
|
|
|
|
|
|
plt.hide()
|
|
|
|
|