class TestNamedScatterAlphaSlider(TestCaseQt): def setUp(self): super(TestNamedScatterAlphaSlider, self).setUp() self.plot = PlotWidget() self.aslider = AlphaSlider.NamedScatterAlphaSlider(plot=self.plot) self.aslider.setOrientation(qt.Qt.Horizontal) toolbar = qt.QToolBar("plot", self.plot) toolbar.addWidget(self.aslider) self.plot.addToolBar(toolbar) self.plot.show() self.qWaitForWindowExposed(self.plot) self.mouseMove(self.plot) # Move to center self.qapp.processEvents() def tearDown(self): self.qapp.processEvents() self.plot.setAttribute(qt.Qt.WA_DeleteOnClose) self.plot.close() del self.plot del self.aslider super(TestNamedScatterAlphaSlider, self).tearDown() def testWidgetEnabled(self): # no Scatter set initially, slider must be deactivate self.assertFalse(self.aslider.isEnabled()) self.plot.addScatter([0, 1, 2], [2, 3, 4], [5, 6, 7], legend="1") self.aslider.setLegend("1") # now we have an image set self.assertTrue(self.aslider.isEnabled()) def testGetScatter(self): self.plot.addScatter([0, 1, 2], [2, 3, 4], [5, 6, 7], legend="1") self.plot.addScatter([0, 10, 20], [20, 30, 40], [50, 60, 70], legend="2") self.aslider.setLegend("1") self.assertEqual(self.plot.getScatter("1"), self.aslider.getItem()) self.aslider.setLegend("2") self.assertEqual(self.plot.getScatter("2"), self.aslider.getItem()) def testGetAlpha(self): self.plot.addScatter([0, 10, 20], [20, 30, 40], [50, 60, 70], legend="1") self.aslider.setLegend("1") self.aslider.setValue(128) self.assertAlmostEqual(self.aslider.getAlpha(), 128. / 255)
class TestNamedScatterAlphaSlider(TestCaseQt): def setUp(self): super(TestNamedScatterAlphaSlider, self).setUp() self.plot = PlotWidget() self.aslider = AlphaSlider.NamedScatterAlphaSlider(plot=self.plot) self.aslider.setOrientation(qt.Qt.Horizontal) toolbar = qt.QToolBar("plot", self.plot) toolbar.addWidget(self.aslider) self.plot.addToolBar(toolbar) self.plot.show() self.qWaitForWindowExposed(self.plot) self.mouseMove(self.plot) # Move to center self.qapp.processEvents() def tearDown(self): self.qapp.processEvents() self.plot.setAttribute(qt.Qt.WA_DeleteOnClose) self.plot.close() del self.plot del self.aslider super(TestNamedScatterAlphaSlider, self).tearDown() def testWidgetEnabled(self): # no Scatter set initially, slider must be deactivate self.assertFalse(self.aslider.isEnabled()) self.plot.addScatter([0, 1, 2], [2, 3, 4], [5, 6, 7], legend="1") self.aslider.setLegend("1") # now we have an image set self.assertTrue(self.aslider.isEnabled()) def testGetScatter(self): self.plot.addScatter([0, 1, 2], [2, 3, 4], [5, 6, 7], legend="1") self.plot.addScatter([0, 10, 20], [20, 30, 40], [50, 60, 70], legend="2") self.aslider.setLegend("1") self.assertEqual(self.plot.getScatter("1"), self.aslider.getItem()) self.aslider.setLegend("2") self.assertEqual(self.plot.getScatter("2"), self.aslider.getItem()) def testGetAlpha(self): self.plot.addScatter([0, 10, 20], [20, 30, 40], [50, 60, 70], legend="1") self.aslider.setLegend("1") self.aslider.setValue(128) self.assertAlmostEqual(self.aslider.getAlpha(), 128. / 255)
class MaskScatterWidget(qt.QMainWindow): """Simple plot widget designed to display a scatter plot on top of a background image. A transparency slider is provided to adjust the transparency of the scatter points. A mask tools widget is provided to select/mask points of the scatter plot. """ def __init__(self, parent=None): super(MaskScatterWidget, self).__init__(parent=parent) self._activeScatterLegend = "active scatter" self._bgImageLegend = "background image" # widgets centralWidget = qt.QWidget(self) self._plot = PlotWidget(parent=centralWidget) self._maskToolsWidget = ScatterMaskToolsWidget.ScatterMaskToolsWidget( plot=self._plot, parent=centralWidget) self._alphaSlider = NamedScatterAlphaSlider(parent=self, plot=self._plot) self._alphaSlider.setOrientation(qt.Qt.Horizontal) self._alphaSlider.setToolTip("Adjust scatter opacity") # layout layout = qt.QVBoxLayout(centralWidget) layout.addWidget(self._plot) layout.addWidget(self._alphaSlider) layout.addWidget(self._maskToolsWidget) centralWidget.setLayout(layout) self.setCentralWidget(centralWidget) def setSelectionMask(self, mask, copy=True): """Set the mask to a new array. :param numpy.ndarray mask: The array to use for the mask. Mask type: array of uint8 of dimension 1, Array of other types are converted. :param bool copy: True (the default) to copy the array, False to use it as is if possible. :return: None if failed, shape of mask as 1-tuple if successful. """ return self._maskToolsWidget.setSelectionMask(mask, copy=copy) def getSelectionMask(self, copy=True): """Get the current mask as a 1D array. :param bool copy: True (default) to get a copy of the mask. If False, the returned array MUST not be modified. :return: The array of the mask with dimension of the scatter data. If there is no scatter data, an empty array is returned. :rtype: 1D numpy.ndarray of uint8 """ return self._maskToolsWidget.getSelectionMask(copy=copy) def setBackgroundImage(self, image, xscale=(0, 1.), yscale=(0, 1.), colormap=None): """Set a background image :param image: 2D image, array of shape (nrows, ncolumns) or (nrows, ncolumns, 3) or (nrows, ncolumns, 4) RGB(A) pixmap :param xscale: Factors for polynomial scaling for x-axis, *(a, b)* such as :math:`x \mapsto a + bx` :param yscale: Factors for polynomial scaling for y-axis """ self._plot.addImage(image, legend=self._bgImageLegend, origin=(xscale[0], yscale[0]), scale=(xscale[1], yscale[1]), z=0, replace=False, colormap=colormap) def setScatter(self, x, y, v=None, info=None, colormap=None): """Set the scatter data, by providing its data as a 1D array or as a pixmap. The scatter plot set through this method is associated with the transparency slider. :param x: 1D array of x coordinates :param y: 1D array of y coordinates :param v: Array of values for each point, represented as the color of the point on the plot. """ self._plot.addScatter(x, y, v, legend=self._activeScatterLegend, info=info, colormap=colormap) # the mask is associated with the active scatter self._plot._setActiveItem(kind="scatter", legend=self._activeScatterLegend) self._alphaSlider.setLegend(self._activeScatterLegend)
class MaskScatterWidget(qt.QMainWindow): """Simple plot widget designed to display a scatter plot on top of a background image. A transparency slider is provided to adjust the transparency of the scatter points. A mask tools widget is provided to select/mask points of the scatter plot. """ def __init__(self, parent=None): super(MaskScatterWidget, self).__init__(parent=parent) self._activeScatterLegend = "active scatter" self._bgImageLegend = "background image" # widgets centralWidget = qt.QWidget(self) self._plot = PlotWidget(parent=centralWidget) self._maskToolsWidget = ScatterMaskToolsWidget.ScatterMaskToolsWidget( plot=self._plot, parent=centralWidget) self._alphaSlider = NamedScatterAlphaSlider(parent=self, plot=self._plot) self._alphaSlider.setOrientation(qt.Qt.Horizontal) self._alphaSlider.setToolTip("Adjust scatter opacity") # layout layout = qt.QVBoxLayout(centralWidget) layout.addWidget(self._plot) layout.addWidget(self._alphaSlider) layout.addWidget(self._maskToolsWidget) centralWidget.setLayout(layout) self.setCentralWidget(centralWidget) def setSelectionMask(self, mask, copy=True): """Set the mask to a new array. :param numpy.ndarray mask: The array to use for the mask. Mask type: array of uint8 of dimension 1, Array of other types are converted. :param bool copy: True (the default) to copy the array, False to use it as is if possible. :return: None if failed, shape of mask as 1-tuple if successful. """ return self._maskToolsWidget.setSelectionMask(mask, copy=copy) def getSelectionMask(self, copy=True): """Get the current mask as a 1D array. :param bool copy: True (default) to get a copy of the mask. If False, the returned array MUST not be modified. :return: The array of the mask with dimension of the scatter data. If there is no scatter data, None is returned. :rtype: 1D numpy.ndarray of uint8 """ return self._maskToolsWidget.getSelectionMask(copy=copy) def setBackgroundImage(self, image, xscale=(0, 1.), yscale=(0, 1.), colormap=None): """Set a background image :param image: 2D image, array of shape (nrows, ncolumns) or (nrows, ncolumns, 3) or (nrows, ncolumns, 4) RGB(A) pixmap :param xscale: Factors for polynomial scaling for x-axis, *(a, b)* such as :math:`x \mapsto a + bx` :param yscale: Factors for polynomial scaling for y-axis """ self._plot.addImage(image, legend=self._bgImageLegend, origin=(xscale[0], yscale[0]), scale=(xscale[1], yscale[1]), z=0, colormap=colormap) def setScatter(self, x, y, v=None, info=None, colormap=None): """Set the scatter data, by providing its data as a 1D array or as a pixmap. The scatter plot set through this method is associated with the transparency slider. :param x: 1D array of x coordinates :param y: 1D array of y coordinates :param v: Array of values for each point, represented as the color of the point on the plot. """ self._plot.addScatter(x, y, v, legend=self._activeScatterLegend, info=info, colormap=colormap) # the mask is associated with the active scatter self._plot._setActiveItem(kind="scatter", legend=self._activeScatterLegend) self._alphaSlider.setLegend(self._activeScatterLegend)