def testImageDataChanged(self): """Test sigItemChanged for ImageData""" self.plot.addImage(numpy.arange(100).reshape(10, 10), legend='test') image = self.plot.getImage('test') listener = SignalListener() image.sigItemChanged.connect(listener) # ColormapMixIn colormap = self.plot.getDefaultColormap().copy() image.setColormap(colormap) image.getColormap().setName('viridis') # Test of signals in ImageBase class image.setOrigin(10) image.setScale(2) # Test of signals in ImageData class image.setData(numpy.ones((10, 10))) self.assertEqual(listener.arguments(argumentIndex=0), [ItemChangedType.COLORMAP, ItemChangedType.COLORMAP, ItemChangedType.POSITION, ItemChangedType.SCALE, ItemChangedType.DATA])
def test_data_event(self): listener = SignalListener() widget = self.create_widget() widget.dataChanged.connect(listener) widget.setData(10) widget.setData(None) self.assertEqual(listener.callCount(), 2)
def test_data_event(self): data = numpy.arange(3 * 3 * 3) widget = NumpyAxesSelector() listener = SignalListener() widget.dataChanged.connect(listener) widget.setData(data) widget.setData(None) self.assertEqual(listener.callCount(), 2)
def test_display_mode_event(self): listener = SignalListener() widget = self.create_widget() widget.displayedViewChanged.connect(listener) widget.setData(10) widget.setData(None) modes = [v.modeId() for v in listener.arguments(argumentIndex=0)] self.assertEqual(modes, [DataViews.RAW_MODE, DataViews.EMPTY_MODE]) listener.clear()
def test_event(self): listener = SignalListener() formatter = TextFormatter() formatter.formatChanged.connect(listener) formatter.setFloatFormat("%.3f") formatter.setIntegerFormat("%03i") formatter.setUseQuoteForText(False) formatter.setImaginaryUnit("z") self.assertEqual(listener.callCount(), 4)
def testStackFrameNumber(self): self.stackview.setStack(self.mystack) self.assertEqual(self.stackview.getFrameNumber(), 0) listener = SignalListener() self.stackview.sigFrameChanged.connect(listener) self.stackview.setFrameNumber(1) self.assertEqual(self.stackview.getFrameNumber(), 1) self.assertEqual(listener.arguments(), [(1,)])
def test_selected_data_event(self): data = numpy.arange(3 * 3 * 3) data.shape = 3, 3, 3 widget = NumpyAxesSelector() listener = SignalListener() widget.selectionChanged.connect(listener) widget.setData(data) widget.setAxisNames(["x"]) widget.setData(None) self.assertEqual(listener.callCount(), 3) listener.clear()
def testHistogramChanged(self): """Test sigItemChanged for Histogram""" self.plot.addHistogram( numpy.arange(10), edges=numpy.arange(11), legend='test') histogram = self.plot.getHistogram('test') listener = SignalListener() histogram.sigItemChanged.connect(listener) # Test signals in Histogram class histogram.setData(numpy.zeros(10), numpy.arange(11)) self.assertEqual(listener.arguments(argumentIndex=0), [ItemChangedType.DATA])
def testImageRgbaChanged(self): """Test sigItemChanged for ImageRgba""" self.plot.addImage(numpy.ones((10, 10, 3)), legend='rgb') image = self.plot.getImage('rgb') listener = SignalListener() image.sigItemChanged.connect(listener) # Test of signals in ImageRgba class image.setData(numpy.zeros((10, 10, 3))) self.assertEqual(listener.arguments(argumentIndex=0), [ItemChangedType.DATA])
def testHistogramChanged(self): """Test sigItemChanged for Histogram""" self.plot.addHistogram(numpy.arange(10), edges=numpy.arange(11), legend='test') histogram = self.plot.getHistogram('test') listener = SignalListener() histogram.sigItemChanged.connect(listener) # Test signals in Histogram class histogram.setData(numpy.zeros(10), numpy.arange(11)) self.assertEqual(listener.arguments(argumentIndex=0), [ItemChangedType.DATA])
def testShapeChanged(self): """Test sigItemChanged for shape""" data = numpy.array((1., 10.)) self.plot.addItem(data, data, legend='test', shape='rectangle') shape = self.plot._getItem(kind='item', legend='test') listener = SignalListener() shape.sigItemChanged.connect(listener) shape.setOverlay(True) shape.setPoints(((2., 2.), (3., 3.))) self.assertEqual(listener.arguments(), [(ItemChangedType.OVERLAY, ), (ItemChangedType.DATA, )])
def testCurveChanged(self): """Test sigItemChanged for curve""" self.plot.addCurve(numpy.arange(10), numpy.arange(10), legend='test') curve = self.plot.getCurve('test') listener = SignalListener() curve.sigItemChanged.connect(listener) # Test for signal in Item class curve.setVisible(False) curve.setVisible(True) curve.setZValue(100) # Test for signals in PointsBase class curve.setData(numpy.arange(100), numpy.arange(100)) # SymbolMixIn curve.setSymbol('Circle') curve.setSymbol('d') curve.setSymbolSize(20) # AlphaMixIn curve.setAlpha(0.5) # Test for signals in Curve class # ColorMixIn curve.setColor('yellow') # YAxisMixIn curve.setYAxis('right') # FillMixIn curve.setFill(True) # LineMixIn curve.setLineStyle(':') curve.setLineStyle(':') # Not sending event curve.setLineWidth(2) self.assertEqual(listener.arguments(argumentIndex=0), [ItemChangedType.VISIBLE, ItemChangedType.VISIBLE, ItemChangedType.ZVALUE, ItemChangedType.DATA, ItemChangedType.SYMBOL, ItemChangedType.SYMBOL, ItemChangedType.SYMBOL_SIZE, ItemChangedType.ALPHA, ItemChangedType.COLOR, ItemChangedType.YAXIS, ItemChangedType.FILL, ItemChangedType.LINE_STYLE, ItemChangedType.LINE_WIDTH])
def testCurveChanged(self): """Test sigItemChanged for curve""" self.plot.addCurve(numpy.arange(10), numpy.arange(10), legend='test') curve = self.plot.getCurve('test') listener = SignalListener() curve.sigItemChanged.connect(listener) # Test for signal in Item class curve.setVisible(False) curve.setVisible(True) curve.setZValue(100) # Test for signals in Points class curve.setData(numpy.arange(100), numpy.arange(100)) # SymbolMixIn curve.setSymbol('Circle') curve.setSymbol('d') curve.setSymbolSize(20) # AlphaMixIn curve.setAlpha(0.5) # Test for signals in Curve class # ColorMixIn curve.setColor('yellow') # YAxisMixIn curve.setYAxis('right') # FillMixIn curve.setFill(True) # LineMixIn curve.setLineStyle(':') curve.setLineStyle(':') # Not sending event curve.setLineWidth(2) self.assertEqual(listener.arguments(argumentIndex=0), [ItemChangedType.VISIBLE, ItemChangedType.VISIBLE, ItemChangedType.ZVALUE, ItemChangedType.DATA, ItemChangedType.SYMBOL, ItemChangedType.SYMBOL, ItemChangedType.SYMBOL_SIZE, ItemChangedType.ALPHA, ItemChangedType.COLOR, ItemChangedType.YAXIS, ItemChangedType.FILL, ItemChangedType.LINE_STYLE, ItemChangedType.LINE_WIDTH])
def testMaxROI(self): """Test Max ROI""" origin1 = numpy.array([1., 10.]) size1 = numpy.array([10., 10.]) origin2 = numpy.array([2., 3.]) size2 = numpy.array([10., 10.]) manager = roi.InteractiveRegionOfInterestManager(self.plot) self.roiTableWidget.setRegionOfInterestManager(manager) self.assertEqual(manager.getRois(), ()) changedListener = SignalListener() manager.sigRoiChanged.connect(changedListener) # Add two point item = roi_items.RectangleROI() item.setGeometry(origin=origin1, size=size1) manager.addRoi(item) item = roi_items.RectangleROI() item.setGeometry(origin=origin2, size=size2) manager.addRoi(item) self.qapp.processEvents() self.assertEqual(changedListener.callCount(), 2) self.assertEqual(len(manager.getRois()), 2) # Try to set max ROI to 1 while there is 2 ROIs with self.assertRaises(ValueError): manager.setMaxRois(1) manager.clear() self.assertEqual(len(manager.getRois()), 0) self.assertEqual(changedListener.callCount(), 3) # Set max limit to 1 manager.setMaxRois(1) # Add a point item = roi_items.RectangleROI() item.setGeometry(origin=origin1, size=size1) manager.addRoi(item) self.qapp.processEvents() self.assertEqual(changedListener.callCount(), 4) # Add a 2nd point while max ROI is 1 item = roi_items.RectangleROI() item.setGeometry(origin=origin1, size=size1) manager.addRoi(item) self.qapp.processEvents() self.assertEqual(changedListener.callCount(), 6) self.assertEqual(len(manager.getRois()), 1)
def testShapeChanged(self): """Test sigItemChanged for shape""" data = numpy.array((1., 10.)) self.plot.addItem(data, data, legend='test', shape='rectangle') shape = self.plot._getItem(kind='item', legend='test') listener = SignalListener() shape.sigItemChanged.connect(listener) shape.setOverlay(True) shape.setPoints(((2., 2.), (3., 3.))) self.assertEqual(listener.arguments(), [(ItemChangedType.OVERLAY,), (ItemChangedType.DATA,)])
def testEmitCurrentROI(self): """Test behavior of the CurvesROIWidget.sigROISignal""" roi = CurvesROIWidget.ROI(name='linear', fromdata=0, todata=5) self.widget.roiWidget.setRois((roi,)) signalListener = SignalListener() self.widget.roiWidget.sigROISignal.connect(signalListener.partial()) self.widget.show() self.qapp.processEvents() self.assertTrue(signalListener.callCount() is 0) self.assertTrue(self.widget.roiWidget.roiTable.activeRoi is roi) roi.setFrom(0.0) self.qapp.processEvents() self.assertTrue(signalListener.callCount() is 0) roi.setFrom(0.3) self.qapp.processEvents() self.assertTrue(signalListener.callCount() is 1)
def testFail(self): listener = SignalListener() button = ThreadPoolPushButton() button.setCallable(self._computeFail) button.beforeExecuting.connect(listener.partial(test="be")) button.started.connect(listener.partial(test="s")) button.succeeded.connect(listener.partial(test="Unexpected success")) button.failed.connect(listener.partial(test="exception")) button.finished.connect(listener.partial(test="f")) with TestLogging('silx.gui.widgets.ThreadPoolPushButton', error=1): button.executeCallable() self.qapp.processEvents() time.sleep(0.1) self.qapp.processEvents() result = listener.karguments(argumentName="test") self.assertListEqual(result, ["be", "s", "exception", "f"]) listener.clear()
def setUp(self): self.plot = Plot1D() x = range(20) y = range(20) self.plot.addCurve(x, y, legend='curve0') self.listener = SignalListener() self.curves_roi_widget = self.plot.getCurvesRoiWidget() self.curves_roi_widget.sigROISignal.connect(self.listener) assert self.curves_roi_widget.isVisible() is False assert self.listener.callCount() == 0 self.plot.show() self.qWaitForWindowExposed(self.plot) toolButton = getQToolButtonFromAction(self.plot.getRoiAction()) self.mouseClick(widget=toolButton, button=qt.Qt.LeftButton) self.curves_roi_widget.show() self.qWaitForWindowExposed(self.curves_roi_widget)
def testScatterChanged(self): """Test sigItemChanged for scatter""" data = numpy.arange(10) self.plot.addScatter(data, data, data, legend='test') scatter = self.plot.getScatter('test') listener = SignalListener() scatter.sigItemChanged.connect(listener) # ColormapMixIn scatter.getColormap().setName('viridis') data2 = data + 10 # Test of signals in Scatter class scatter.setData(data2, data2, data2) self.assertEqual(listener.arguments(), [(ItemChangedType.COLORMAP, ), (ItemChangedType.DATA, )])
def testScatterChanged(self): """Test sigItemChanged for scatter""" data = numpy.arange(10) self.plot.addScatter(data, data, data, legend='test') scatter = self.plot.getScatter('test') listener = SignalListener() scatter.sigItemChanged.connect(listener) # ColormapMixIn scatter.getColormap().setName('viridis') data2 = data + 10 # Test of signals in Scatter class scatter.setData(data2, data2, data2) self.assertEqual(listener.arguments(), [(ItemChangedType.COLORMAP,), (ItemChangedType.DATA,)])
def setUp(self): TestCaseQt.setUp(self) self.model = hdf5.Hdf5TreeModel() filename = _tmpDirectory + "/data.h5" self.h5 = h5py.File(filename) self.model.insertH5pyObject(self.h5) self.listener = SignalListener() self.model.sigH5pyObjectLoaded.connect(self.listener.partial(signal="loaded")) self.model.sigH5pyObjectRemoved.connect(self.listener.partial(signal="removed")) self.model.sigH5pyObjectSynchronized.connect(self.listener.partial(signal="synchronized"))
def testVisibleExtentTracking(self): """Test Item's visible extent tracking""" image = items.ImageData() image.setData(numpy.arange(6).reshape(2, 3)) listener = SignalListener() image._sigVisibleBoundsChanged.connect(listener) image._setVisibleBoundsTracking(True) self.assertTrue(image._isVisibleBoundsTracking()) self.plot.addItem(image) self.assertEqual(listener.callCount(), 1) self.plot.getXAxis().setLimits(0, 1) self.assertEqual(listener.callCount(), 2) self.plot.hide() self.qapp.processEvents() # No event here self.assertEqual(listener.callCount(), 2) self.plot.getXAxis().setLimits(1, 2) # No event since PlotWidget is hidden, delayed to PlotWidget show self.assertEqual(listener.callCount(), 2) self.plot.show() self.qapp.processEvents() # Receives delayed event now self.assertEqual(listener.callCount(), 3) image.setOrigin((-1, -1)) self.assertEqual(listener.callCount(), 4) image.setVisible(False) image.setOrigin((0, 0)) # No event since item is not visible self.assertEqual(listener.callCount(), 4) image.setVisible(True) # Receives delayed event now self.assertEqual(listener.callCount(), 5)
def testMarkerChanged(self): """Test sigItemChanged for markers""" self.plot.addMarker(10, 20, legend='test') marker = self.plot._getMarker('test') listener = SignalListener() marker.sigItemChanged.connect(listener) # Test signals in _BaseMarker marker.setPosition(10, 10) marker.setPosition(10, 10) # Not sending event marker.setText('toto') self.assertEqual(listener.arguments(argumentIndex=0), [ItemChangedType.POSITION, ItemChangedType.TEXT]) # XMarker self.plot.addXMarker(10, legend='x') marker = self.plot._getMarker('x') listener = SignalListener() marker.sigItemChanged.connect(listener) marker.setPosition(20, 20) self.assertEqual(listener.arguments(argumentIndex=0), [ItemChangedType.POSITION]) # YMarker self.plot.addYMarker(10, legend='x') marker = self.plot._getMarker('x') listener = SignalListener() marker.sigItemChanged.connect(listener) marker.setPosition(20, 20) self.assertEqual(listener.arguments(argumentIndex=0), [ItemChangedType.POSITION])
def testScatterChanged(self): """Test sigItemChanged for scatter""" data = numpy.arange(10) self.plot.addScatter(data, data, data, legend='test') scatter = self.plot.getScatter('test') listener = SignalListener() scatter.sigItemChanged.connect(listener) # ColormapMixIn scatter.getColormap().setName('viridis') # Test of signals in Scatter class scatter.setData((0, 1, 2), (1, 0, 2), (0, 1, 2)) # Visualization mode changed scatter.setVisualization(scatter.Visualization.SOLID) self.assertEqual(listener.arguments(), [(ItemChangedType.COLORMAP,), (ItemChangedType.DATA,), (ItemChangedType.VISUALIZATION_MODE,)])
def testSuccess(self): listener = SignalListener() button = ThreadPoolPushButton() button.setCallable(self._compute) button.beforeExecuting.connect(listener.partial(test="be")) button.started.connect(listener.partial(test="s")) button.succeeded.connect(listener.partial(test="result")) button.failed.connect(listener.partial(test="Unexpected exception")) button.finished.connect(listener.partial(test="f")) button.executeCallable() self.qapp.processEvents() time.sleep(0.1) self.qapp.processEvents() result = listener.karguments(argumentName="test") self.assertListEqual(result, ["be", "s", "result", "f"])
def testEmitCurrentROI(self): """Test behavior of the CurvesROIWidget.sigROISignal""" roi = CurvesROIWidget.ROI(name='linear', fromdata=0, todata=5) self.widget.roiWidget.setRois((roi, )) signalListener = SignalListener() self.widget.roiWidget.sigROISignal.connect(signalListener.partial()) self.widget.show() self.qapp.processEvents() self.assertTrue(signalListener.callCount() is 0) self.assertTrue(self.widget.roiWidget.roiTable.activeRoi is roi) roi.setFrom(0.0) self.qapp.processEvents() self.assertTrue(signalListener.callCount() is 0) roi.setFrom(0.3) self.qapp.processEvents() self.assertTrue(signalListener.callCount() is 1)
def test(self): """Test ROI of different shapes""" tests = ( # shape, points=[list of (x, y), list of (x, y)] (roi_items.PointROI, numpy.array(([(10., 15.)], [(20., 25.)]))), (roi_items.RectangleROI, numpy.array((((1., 10.), (11., 20.)), ((2., 3.), (12., 13.))))), (roi_items.PolygonROI, numpy.array((((0., 1.), (0., 10.), (10., 0.)), ((5., 6.), (5., 16.), (15., 6.))))), (roi_items.LineROI, numpy.array((((10., 20.), (10., 30.)), ((30., 40.), (30., 50.))))), (roi_items.HorizontalLineROI, numpy.array((((10., 20.), (10., 30.)), ((30., 40.), (30., 50.))))), (roi_items.VerticalLineROI, numpy.array((((10., 20.), (10., 30.)), ((30., 40.), (30., 50.))))), ) for roiClass, points in tests: with self.subTest(roiClass=roiClass): manager = roi.RegionOfInterestManager(self.plot) self.roiTableWidget.setRegionOfInterestManager(manager) manager.start(roiClass) self.assertEqual(manager.getRois(), ()) finishListener = SignalListener() manager.sigInteractiveModeFinished.connect(finishListener) changedListener = SignalListener() manager.sigRoiChanged.connect(changedListener) # Add a point manager.createRoi(roiClass, points[0]) self.qapp.processEvents() self.assertTrue(len(manager.getRois()), 1) self.assertEqual(changedListener.callCount(), 1) # Remove it manager.removeRoi(manager.getRois()[0]) self.assertEqual(manager.getRois(), ()) self.assertEqual(changedListener.callCount(), 2) # Add two point manager.createRoi(roiClass, points[0]) self.qapp.processEvents() manager.createRoi(roiClass, points[1]) self.qapp.processEvents() self.assertTrue(len(manager.getRois()), 2) self.assertEqual(changedListener.callCount(), 4) # Reset it result = manager.clear() self.assertTrue(result) self.assertEqual(manager.getRois(), ()) self.assertEqual(changedListener.callCount(), 5) changedListener.clear() # Add two point manager.createRoi(roiClass, points[0]) self.qapp.processEvents() manager.createRoi(roiClass, points[1]) self.qapp.processEvents() self.assertTrue(len(manager.getRois()), 2) self.assertEqual(changedListener.callCount(), 2) # stop result = manager.stop() self.assertTrue(result) self.assertTrue(len(manager.getRois()), 1) self.qapp.processEvents() self.assertEqual(finishListener.callCount(), 1) manager.clear()
class TestHdf5TreeModelSignals(TestCaseQt): def setUp(self): TestCaseQt.setUp(self) self.model = hdf5.Hdf5TreeModel() filename = _tmpDirectory + "/data.h5" self.h5 = h5py.File(filename) self.model.insertH5pyObject(self.h5) self.listener = SignalListener() self.model.sigH5pyObjectLoaded.connect(self.listener.partial(signal="loaded")) self.model.sigH5pyObjectRemoved.connect(self.listener.partial(signal="removed")) self.model.sigH5pyObjectSynchronized.connect(self.listener.partial(signal="synchronized")) def tearDown(self): self.signals = None ref = weakref.ref(self.model) self.model = None self.qWaitForDestroy(ref) self.h5.close() self.h5 = None TestCaseQt.tearDown(self) def waitForPendingOperations(self, model): for _ in range(10): if not model.hasPendingOperations(): break self.qWait(10) else: raise RuntimeError("Still waiting for a pending operation") def testInsert(self): filename = _tmpDirectory + "/data.h5" h5 = h5py.File(filename) self.model.insertH5pyObject(h5) self.assertEqual(self.listener.callCount(), 0) def testLoaded(self): filename = _tmpDirectory + "/data.h5" self.model.insertFile(filename) self.assertEqual(self.listener.callCount(), 1) self.assertEqual(self.listener.karguments(argumentName="signal")[0], "loaded") self.assertIsNot(self.listener.arguments(callIndex=0)[0], self.h5) self.assertEqual(self.listener.arguments(callIndex=0)[0].filename, filename) def testRemoved(self): self.model.removeH5pyObject(self.h5) self.assertEqual(self.listener.callCount(), 1) self.assertEqual(self.listener.karguments(argumentName="signal")[0], "removed") self.assertIs(self.listener.arguments(callIndex=0)[0], self.h5) def testSynchonized(self): self.model.synchronizeH5pyObject(self.h5) self.waitForPendingOperations(self.model) self.assertEqual(self.listener.callCount(), 1) self.assertEqual(self.listener.karguments(argumentName="signal")[0], "synchronized") self.assertIs(self.listener.arguments(callIndex=0)[0], self.h5) self.assertIsNot(self.listener.arguments(callIndex=0)[1], self.h5)
class TestImageStack(TestCaseQt): """Simple test of the Image stack""" def setUp(self): TestCaseQt.setUp(self) self.urls = OrderedDict() self._raw_data = {} self._folder = tempfile.mkdtemp() self._n_urls = 10 file_name = os.path.join(self._folder, 'test_inage_stack_file.h5') with h5py.File(file_name, 'w') as h5f: for i in range(self._n_urls): width = numpy.random.randint(10, 40) height = numpy.random.randint(10, 40) raw_data = numpy.random.random((width, height)) self._raw_data[i] = raw_data h5f[str(i)] = raw_data self.urls[i] = DataUrl(file_path=file_name, data_path=str(i), scheme='silx') self.widget = ImageStack() self.urlLoadedListener = SignalListener() self.widget.sigLoaded.connect(self.urlLoadedListener) self.currentUrlChangedListener = SignalListener() self.widget.sigCurrentUrlChanged.connect( self.currentUrlChangedListener) def tearDown(self): shutil.rmtree(self._folder) self.widget.setAttribute(qt.Qt.WA_DeleteOnClose, True) self.widget.close() TestCaseQt.setUp(self) def testControls(self): """Test that selection using the url table and the slider are working """ self.widget.show() self.assertEqual(self.widget.getCurrentUrl(), None) self.assertEqual(self.widget.getCurrentUrlIndex(), None) self.widget.setUrls(list(self.urls.values())) # wait for image to be loaded self._waitUntilUrlLoaded() self.assertEqual(self.widget.getCurrentUrl(), self.urls[0]) # make sure all image are loaded self.assertEqual(self.urlLoadedListener.callCount(), self._n_urls) numpy.testing.assert_array_equal( self.widget.getPlotWidget().getActiveImage( just_legend=False).getData(), self._raw_data[0]) self.assertEqual(self.widget._slider.value(), 0) self.widget._urlsTable.setUrl(self.urls[4]) numpy.testing.assert_array_equal( self.widget.getPlotWidget().getActiveImage( just_legend=False).getData(), self._raw_data[4]) self.assertEqual(self.widget._slider.value(), 4) self.assertEqual(self.widget.getCurrentUrl(), self.urls[4]) self.assertEqual(self.widget.getCurrentUrlIndex(), 4) self.widget._slider.setUrlIndex(6) numpy.testing.assert_array_equal( self.widget.getPlotWidget().getActiveImage( just_legend=False).getData(), self._raw_data[6]) self.assertEqual(self.widget._urlsTable.currentItem().text(), self.urls[6].path()) def testCurrentUrlSignals(self): """Test emission of 'currentUrlChangedListener'""" # check initialization self.assertEqual(self.currentUrlChangedListener.callCount(), 0) self.widget.setUrls(list(self.urls.values())) self.qapp.processEvents() time.sleep(0.5) self.qapp.processEvents() # once loaded the two signals should have been sended self.assertEqual(self.currentUrlChangedListener.callCount(), 1) # if the slider is stuck to the same position no signal should be # emitted self.qapp.processEvents() time.sleep(0.5) self.qapp.processEvents() self.assertEqual(self.widget._slider.value(), 0) self.assertEqual(self.currentUrlChangedListener.callCount(), 1) # if slider position is changed, one of each signal should have been # emitted self.widget._urlsTable.setUrl(self.urls[4]) self.qapp.processEvents() time.sleep(1.5) self.qapp.processEvents() self.assertEqual(self.currentUrlChangedListener.callCount(), 2) def testUtils(self): """Test that some utils functions are working""" self.widget.show() self.widget.setUrls(list(self.urls.values())) self.assertEqual(len(self.widget.getUrls()), len(self.urls)) # wait for image to be loaded self._waitUntilUrlLoaded() urls_values = list(self.urls.values()) self.assertEqual(urls_values[0], self.urls[0]) self.assertEqual(urls_values[7], self.urls[7]) self.assertEqual( self.widget._getNextUrl(urls_values[2]).path(), urls_values[3].path()) self.assertEqual(self.widget._getPreviousUrl(urls_values[0]), None) self.assertEqual( self.widget._getPreviousUrl(urls_values[6]).path(), urls_values[5].path()) self.assertEqual(self.widget._getNNextUrls(2, urls_values[0]), urls_values[1:3]) self.assertEqual(self.widget._getNNextUrls(5, urls_values[7]), urls_values[8:]) self.assertEqual(self.widget._getNPreviousUrls(3, urls_values[2]), urls_values[:2]) self.assertEqual(self.widget._getNPreviousUrls(5, urls_values[8]), urls_values[3:8]) def _waitUntilUrlLoaded(self, timeout=2.0): """Wait until all image urls are loaded""" loop_duration = 0.2 remaining_duration = timeout while (len(self.widget._loadingThreads) > 0 and remaining_duration > 0): remaining_duration -= loop_duration time.sleep(loop_duration) self.qapp.processEvents() if remaining_duration <= 0.0: remaining_urls = [] for thread_ in self.widget._loadingThreads: remaining_urls.append(thread_.url.path()) mess = 'All images are not loaded after the time out. ' \ 'Remaining urls are: ' + str(remaining_urls) raise TimeoutError(mess) return True
class TestRoiWidgetSignals(TestCaseQt): """Test Signals emitted by the RoiWidgetSignals""" def setUp(self): self.plot = Plot1D() x = range(20) y = range(20) self.plot.addCurve(x, y, legend='curve0') self.listener = SignalListener() self.curves_roi_widget = self.plot.getCurvesRoiWidget() self.curves_roi_widget.sigROISignal.connect(self.listener) assert self.curves_roi_widget.isVisible() is False assert self.listener.callCount() == 0 self.plot.show() self.qWaitForWindowExposed(self.plot) toolButton = getQToolButtonFromAction(self.plot.getRoiAction()) self.mouseClick(widget=toolButton, button=qt.Qt.LeftButton) self.curves_roi_widget.show() self.qWaitForWindowExposed(self.curves_roi_widget) def tearDown(self): self.plot = None def testSigROISignalAddRmRois(self): """Test SigROISignal when adding and removing ROIS""" print(self.listener.callCount()) self.assertEqual(self.listener.callCount(), 1) self.listener.clear() roi1 = CurvesROIWidget.ROI(name='linear', fromdata=0, todata=5) self.curves_roi_widget.roiTable.addRoi(roi1) self.assertEqual(self.listener.callCount(), 1) self.assertTrue(self.listener.arguments()[0][0]['current'] == 'linear') self.listener.clear() roi2 = CurvesROIWidget.ROI(name='linear2', fromdata=0, todata=5) self.curves_roi_widget.roiTable.addRoi(roi2) self.assertEqual(self.listener.callCount(), 1) self.assertTrue( self.listener.arguments()[0][0]['current'] == 'linear2') self.listener.clear() self.curves_roi_widget.roiTable.removeROI(roi2) self.assertEqual(self.listener.callCount(), 1) self.assertTrue(self.curves_roi_widget.roiTable.activeRoi == roi1) self.assertTrue(self.listener.arguments()[0][0]['current'] == 'linear') self.listener.clear() self.curves_roi_widget.roiTable.deleteActiveRoi() self.assertEqual(self.listener.callCount(), 1) self.assertTrue(self.curves_roi_widget.roiTable.activeRoi is None) self.assertTrue(self.listener.arguments()[0][0]['current'] is None) self.listener.clear() self.curves_roi_widget.roiTable.addRoi(roi1) self.assertEqual(self.listener.callCount(), 1) self.assertTrue(self.listener.arguments()[0][0]['current'] == 'linear') self.assertTrue(self.curves_roi_widget.roiTable.activeRoi == roi1) self.listener.clear() self.qapp.processEvents() self.curves_roi_widget.roiTable.removeROI(roi1) self.qapp.processEvents() self.assertEqual(self.listener.callCount(), 1) self.assertTrue(self.listener.arguments()[0][0]['current'] == 'ICR') self.listener.clear() def testSigROISignalModifyROI(self): """Test SigROISignal when modifying it""" self.curves_roi_widget.roiTable.setMiddleROIMarkerFlag(True) roi1 = CurvesROIWidget.ROI(name='linear', fromdata=2, todata=5) self.curves_roi_widget.roiTable.addRoi(roi1) self.curves_roi_widget.roiTable.setActiveRoi(roi1) # test modify the roi2 object self.listener.clear() roi1.setFrom(0.56) self.assertEqual(self.listener.callCount(), 1) self.listener.clear() roi1.setTo(2.56) self.assertEqual(self.listener.callCount(), 1) self.listener.clear() roi1.setName('linear2') self.assertEqual(self.listener.callCount(), 1) self.listener.clear() roi1.setType('new type') self.assertEqual(self.listener.callCount(), 1) # modify roi limits (from the gui) roi_marker_handler = self.curves_roi_widget.roiTable._markersHandler.getMarkerHandler( roi1.getID()) for marker_type in ('min', 'max', 'middle'): with self.subTest(marker_type=marker_type): self.listener.clear() marker = roi_marker_handler.getMarker(marker_type) self.qapp.processEvents() items_interaction = ItemsInteraction(plot=self.plot) x_pix, y_pix = self.plot.dataToPixel(marker.getXPosition(), 1) items_interaction.beginDrag(x_pix, y_pix) self.qapp.processEvents() items_interaction.endDrag(x_pix + 10, y_pix) self.qapp.processEvents() self.assertEqual(self.listener.callCount(), 1) def testSetActiveCurve(self): """Test sigRoiSignal when set an active curve""" roi1 = CurvesROIWidget.ROI(name='linear', fromdata=2, todata=5) self.curves_roi_widget.roiTable.addRoi(roi1) self.curves_roi_widget.roiTable.setActiveRoi(roi1) self.listener.clear() self.plot.setActiveCurve('curve0') self.assertEqual(self.listener.callCount(), 0)
def test(self): """Test ROI of different shapes""" tests = ( # shape, points=[list of (x, y), list of (x, y)] (roi_items.PointROI, numpy.array(([(10., 15.)], [(20., 25.)]))), (roi_items.RectangleROI, numpy.array((((1., 10.), (11., 20.)), ((2., 3.), (12., 13.))))), (roi_items.PolygonROI, numpy.array((((0., 1.), (0., 10.), (10., 0.)), ((5., 6.), (5., 16.), (15., 6.))))), (roi_items.LineROI, numpy.array( (((10., 20.), (10., 30.)), ((30., 40.), (30., 50.))))), (roi_items.HorizontalLineROI, numpy.array( (((10., 20.), (10., 30.)), ((30., 40.), (30., 50.))))), (roi_items.VerticalLineROI, numpy.array( (((10., 20.), (10., 30.)), ((30., 40.), (30., 50.))))), (roi_items.HorizontalLineROI, numpy.array( (((10., 20.), (10., 30.)), ((30., 40.), (30., 50.))))), ) for roiClass, points in tests: with self.subTest(roiClass=roiClass): manager = roi.RegionOfInterestManager(self.plot) self.roiTableWidget.setRegionOfInterestManager(manager) manager.start(roiClass) self.assertEqual(manager.getRois(), ()) finishListener = SignalListener() manager.sigInteractiveModeFinished.connect(finishListener) changedListener = SignalListener() manager.sigRoiChanged.connect(changedListener) # Add a point r = roiClass() r.setFirstShapePoints(points[0]) manager.addRoi(r) self.qapp.processEvents() self.assertTrue(len(manager.getRois()), 1) self.assertEqual(changedListener.callCount(), 1) # Remove it manager.removeRoi(manager.getRois()[0]) self.assertEqual(manager.getRois(), ()) self.assertEqual(changedListener.callCount(), 2) # Add two point r = roiClass() r.setFirstShapePoints(points[0]) manager.addRoi(r) self.qapp.processEvents() r = roiClass() r.setFirstShapePoints(points[1]) manager.addRoi(r) self.qapp.processEvents() self.assertTrue(len(manager.getRois()), 2) self.assertEqual(changedListener.callCount(), 4) # Reset it result = manager.clear() self.assertTrue(result) self.assertEqual(manager.getRois(), ()) self.assertEqual(changedListener.callCount(), 5) changedListener.clear() # Add two point r = roiClass() r.setFirstShapePoints(points[0]) manager.addRoi(r) self.qapp.processEvents() r = roiClass() r.setFirstShapePoints(points[1]) manager.addRoi(r) self.qapp.processEvents() self.assertTrue(len(manager.getRois()), 2) self.assertEqual(changedListener.callCount(), 2) # stop result = manager.stop() self.assertTrue(result) self.assertTrue(len(manager.getRois()), 1) self.qapp.processEvents() self.assertEqual(finishListener.callCount(), 1) manager.clear()