def testGetImage(self): """PlotWidget.getImage and PlotWidget.getActiveImage tests""" plot = PlotWidget(backend='none') # No image image = plot.getImage() self.assertIsNone(image) plot.addImage(((0, 1), (2, 3)), legend='image 0') plot.addImage(((0, 1), (2, 3)), legend='image 1') # Active image active = plot.getActiveImage() self.assertEqual(active.getLegend(), 'image 0') image = plot.getImage() self.assertEqual(image.getLegend(), 'image 0') # No active image plot.addImage(((0, 1), (2, 3)), legend='image 2') plot.setActiveImage(None) active = plot.getActiveImage() self.assertIsNone(active) image = plot.getImage() self.assertEqual(image.getLegend(), 'image 2') # Active image plot.setActiveImage('image 1') active = plot.getActiveImage() self.assertEqual(active.getLegend(), 'image 1') image = plot.getImage() self.assertEqual(image.getLegend(), 'image 1')
def testDataRangeImageNegativeScaleY(self): """image data range, negative scale""" origin = (-10, 25) scale = (3., -8.) image = numpy.arange(100.).reshape(20, 5) plot = PlotWidget(backend='none') plot.addImage(image, origin=origin, scale=scale) xRange = numpy.array([0., image.shape[1] * scale[0]]) + origin[0] yRange = numpy.array([0., image.shape[0] * scale[1]]) + origin[1] yRange.sort() # negative scale! ranges = {(False, False): (xRange, yRange), (True, False): (None, None), (True, True): (None, None), (False, True): (None, None)} for logX, logY in ((False, False), (True, False), (True, True), (False, True), (False, False)): with self.subTest(logX=logX, logY=logY): plot.getXAxis()._setLogarithmic(logX) plot.getYAxis()._setLogarithmic(logY) dataRange = plot.getDataRange() xRange, yRange = ranges[logX, logY] self.assertTrue(numpy.array_equal(dataRange.x, xRange), msg='{0} != {1}'.format(dataRange.x, xRange)) self.assertTrue(numpy.array_equal(dataRange.y, yRange), msg='{0} != {1}'.format(dataRange.y, yRange)) self.assertIsNone(dataRange.yright)
def testDataRangeImageNegativeScaleY(self): """image data range, negative scale""" origin = (-10, 25) scale = (3., -8.) image = numpy.arange(100.).reshape(20, 5) plot = PlotWidget(backend='none') plot.addImage(image, origin=origin, scale=scale) xRange = numpy.array([0., image.shape[1] * scale[0]]) + origin[0] yRange = numpy.array([0., image.shape[0] * scale[1]]) + origin[1] yRange.sort() # negative scale! ranges = { (False, False): (xRange, yRange), (True, False): (None, None), (True, True): (None, None), (False, True): (None, None) } for logX, logY in ((False, False), (True, False), (True, True), (False, True), (False, False)): with self.subTest(logX=logX, logY=logY): plot.getXAxis()._setLogarithmic(logX) plot.getYAxis()._setLogarithmic(logY) dataRange = plot.getDataRange() xRange, yRange = ranges[logX, logY] self.assertTrue(numpy.array_equal(dataRange.x, xRange), msg='{0} != {1}'.format(dataRange.x, xRange)) self.assertTrue(numpy.array_equal(dataRange.y, yRange), msg='{0} != {1}'.format(dataRange.y, yRange)) self.assertIsNone(dataRange.yright)
def testAddNoRemove(self): """add objects to the Plot""" plot = PlotWidget(backend='none') plot.addCurve(x=(1, 2, 3), y=(3, 2, 1)) plot.addImage(numpy.arange(100.).reshape(10, -1)) plot.addItem( numpy.array((1., 10.)), numpy.array((10., 10.)), shape="rectangle") plot.addXMarker(10.)
def testDataRangeCurveImage(self): """right+left+image axis range""" # overlapping ranges : # image sets x min and y max # plot_left sets y min # plot_right sets x max (and yright) plot = PlotWidget(backend='none') origin = (-10, 5) scale = (3., 8.) image = numpy.arange(100.).reshape(20, 5) plot.addImage(image, origin=origin, scale=scale, legend='image') xData_l = numpy.arange(10) - 0.9 # range : -0.9 , 8.1 yData_l = numpy.arange(10) - 1.9 # range : -1.9 , 7.1 plot.addCurve(x=xData_l, y=yData_l, legend='plot_l', yaxis='left') xData_r = numpy.arange(10) + 4.1 # range : 4.1 , 13.1 yData_r = numpy.arange(10) - 0.9 # range : -0.9 , 8.1 plot.addCurve(x=xData_r, y=yData_r, legend='plot_r', yaxis='right') imgXRange = numpy.array([0., image.shape[1] * scale[0]]) + origin[0] imgYRange = numpy.array([0., image.shape[0] * scale[1]]) + origin[1] for logX, logY in ((False, False), (True, False), (True, True), (False, True), (False, False)): with self.subTest(logX=logX, logY=logY): plot.getXAxis()._setLogarithmic(logX) plot.getYAxis()._setLogarithmic(logY) dataRange = plot.getDataRange() xRangeL, yRangeL = self._getRanges([xData_l, yData_l], [logX, logY]) xRangeR, yRangeR = self._getRanges([xData_r, yData_r], [logX, logY]) if logX or logY: xRangeLR = self._getRangesMinmax([xRangeL, xRangeR]) else: xRangeLR = self._getRangesMinmax([xRangeL, xRangeR, imgXRange]) yRangeL = self._getRangesMinmax([yRangeL, imgYRange]) self.assertSequenceEqual(dataRange.x, xRangeLR) self.assertSequenceEqual(dataRange.y, yRangeL) self.assertSequenceEqual(dataRange.yright, yRangeR)
def testAddNoRemove(self): """add objects to the Plot""" plot = PlotWidget(backend='none') plot.addCurve(x=(1, 2, 3), y=(3, 2, 1)) plot.addImage(numpy.arange(100.).reshape(10, -1)) plot.addShape(numpy.array((1., 10.)), numpy.array((10., 10.)), shape="rectangle") plot.addXMarker(10.)
def testGetAllImages(self): """PlotWidget.getAllImages test""" plot = PlotWidget(backend='none') # No image images = plot.getAllImages() self.assertEqual(len(images), 0) # 2 images data = numpy.arange(100).reshape(10, 10) plot.addImage(data, legend='1') plot.addImage(data, origin=(10, 10), legend='2') images = plot.getAllImages(just_legend=True) self.assertEqual(list(images), ['1', '2']) images = plot.getAllImages(just_legend=False) self.assertEqual(len(images), 2) self.assertEqual(images[0].getLegend(), '1') self.assertEqual(images[1].getLegend(), '2')
def testGetImageOldApi(self): """PlotWidget.getImage and PlotWidget.getActiveImage old API tests""" plot = PlotWidget(backend='none') # No image image = plot.getImage() self.assertIsNone(image) image = numpy.arange(10).astype(numpy.float32) image.shape = 5, 2 plot.addImage(image, legend='image 0', info=["Hi!"]) # Active image data, legend, info, something, params = plot.getActiveImage() self.assertEqual(legend, 'image 0') self.assertEqual(info, ["Hi!"]) self.assertTrue(numpy.allclose(data, image), "image 0 data not correct")
class ColorBarShower(qt.QWidget): """ Simple widget displaying n image with different colormap, an active colorbar """ def __init__(self): qt.QWidget.__init__(self) self.setLayout(qt.QHBoxLayout()) self._buildActiveImageSelector() self.layout().addWidget(self.image_selector) self._buildPlot() self.layout().addWidget(self.plot) self._buildColorbar() self.layout().addWidget(self.colorbar) self._buildColormapEditors() self.layout().addWidget(self._cmpEditor) # connect radio button with the plot self.image_selector._qr1.toggled.connect(self.activateImageChanged) self.image_selector._qr2.toggled.connect(self.activateImageChanged) self.image_selector._qr3.toggled.connect(self.activateImageChanged) self.image_selector._qr4.toggled.connect(self.activateImageChanged) self.image_selector._qr5.toggled.connect(self.activateImageChanged) self.image_selector._qr6.toggled.connect(self.activateImageChanged) def _buildActiveImageSelector(self): """Build the image selector widget""" self.image_selector = qt.QGroupBox(parent=self) self.image_selector.setLayout(qt.QVBoxLayout()) self.image_selector._qr1 = qt.QRadioButton('image1') self.image_selector._qr1.setChecked(True) self.image_selector._qr2 = qt.QRadioButton('image2') self.image_selector._qr3 = qt.QRadioButton('image3') self.image_selector._qr4 = qt.QRadioButton('image4') self.image_selector._qr5 = qt.QRadioButton('image5') self.image_selector._qr6 = qt.QRadioButton('image6') self.image_selector.layout().addWidget(self.image_selector._qr6) self.image_selector.layout().addWidget(self.image_selector._qr5) self.image_selector.layout().addWidget(self.image_selector._qr4) self.image_selector.layout().addWidget(self.image_selector._qr3) self.image_selector.layout().addWidget(self.image_selector._qr2) self.image_selector.layout().addWidget(self.image_selector._qr1) def activateImageChanged(self): if self.image_selector._qr1.isChecked(): self.plot.setActiveImage('image1') if self.image_selector._qr2.isChecked(): self.plot.setActiveImage('image2') if self.image_selector._qr3.isChecked(): self.plot.setActiveImage('image3') if self.image_selector._qr4.isChecked(): self.plot.setActiveImage('image4') if self.image_selector._qr5.isChecked(): self.plot.setActiveImage('image5') if self.image_selector._qr6.isChecked(): self.plot.setActiveImage('image6') def _buildColorbar(self): self.colorbar = ColorBarWidget(parent=self) self.colorbar.setPlot(self.plot) def _buildPlot(self): image1 = numpy.exp(numpy.random.rand(IMG_WIDTH, IMG_WIDTH) * 10) image2 = numpy.linspace(-100, 1000, IMG_WIDTH * IMG_WIDTH).reshape( IMG_WIDTH, IMG_WIDTH) image3 = numpy.linspace(-1, 1, IMG_WIDTH * IMG_WIDTH).reshape( IMG_WIDTH, IMG_WIDTH) image4 = numpy.linspace(-20, 50, IMG_WIDTH * IMG_WIDTH).reshape( IMG_WIDTH, IMG_WIDTH) image5 = image3 image6 = image4 # viridis colormap self.colormap1 = Colormap.Colormap(name='green', normalization='log', vmin=None, vmax=None) self.plot = PlotWidget(parent=self) self.plot.addImage(data=image1, origin=(0, 0), legend='image1', colormap=self.colormap1) self.plot.addImage(data=image2, origin=(100, 0), legend='image2', colormap=self.colormap1) # red colormap self.colormap2 = Colormap.Colormap(name='red', normalization='linear', vmin=None, vmax=None) self.plot.addImage(data=image3, origin=(0, 100), legend='image3', colormap=self.colormap2) self.plot.addImage(data=image4, origin=(100, 100), legend='image4', colormap=self.colormap2) # gray colormap self.colormap3 = Colormap.Colormap(name='gray', normalization='linear', vmin=1.0, vmax=20.0) self.plot.addImage(data=image5, origin=(0, 200), legend='image5', colormap=self.colormap3) self.plot.addImage(data=image6, origin=(100, 200), legend='image6', colormap=self.colormap3) def _buildColormapEditors(self): self._cmpEditor = qt.QWidget(parent=self) self._cmpEditor.setLayout(qt.QVBoxLayout()) self._cmpEditor.layout().addWidget(_ColormapEditor(self.colormap3)) self._cmpEditor.layout().addWidget(_ColormapEditor(self.colormap2)) self._cmpEditor.layout().addWidget(_ColormapEditor(self.colormap1))
associated with a :class:`~silx.gui.plot.PlotWidget`. """ __authors__ = ["P. Knobel"] __license__ = "MIT" __date__ = "28/06/2017" from silx.gui import qt from silx.gui.plot.PlotWidget import PlotWidget from silx.gui.plot.ItemsSelectionDialog import ItemsSelectionDialog app = qt.QApplication([]) pw = PlotWidget() pw.addCurve([0, 1, 2], [3, 2, 1], "A curve") pw.addScatter([0, 1, 2.5], [3, 2.5, 0.9], [8, 9, 72], "A scatter") pw.addHistogram([0, 1, 2.5], [0, 1, 2, 3], "A histogram") pw.addImage([[0, 1, 2], [3, 2, 1]], "An image") pw.show() isd = ItemsSelectionDialog(plot=pw) isd.setItemsSelectionMode(qt.QTableWidget.ExtendedSelection) result = isd.exec_() if result: for item in isd.getSelectedItems(): print(item.getName(), type(item)) else: print("Selection cancelled") app.exec_()
class _ImagePreview(qt.QWidget): """Provide a preview of the selected image""" def __init__(self, parent=None): super(_ImagePreview, self).__init__(parent) self.__data = None self.__plot = PlotWidget(self) self.__plot.setAxesDisplayed(False) self.__plot.setKeepDataAspectRatio(True) layout = qt.QVBoxLayout() layout.setContentsMargins(0, 0, 0, 0) layout.addWidget(self.__plot) self.setLayout(layout) def resizeEvent(self, event): self.__updateConstraints() return qt.QWidget.resizeEvent(self, event) def sizeHint(self): return qt.QSize(200, 200) def plot(self): return self.__plot def setData(self, data, fromDataSelector=False): if data is None: self.clear() return resetzoom = not fromDataSelector previousImage = self.data() if previousImage is not None and data.shape != previousImage.shape: resetzoom = True self.__plot.addImage(legend="data", data=data, resetzoom=resetzoom) self.__data = data self.__updateConstraints() def __updateConstraints(self): """ Update the constraints depending on the size of the widget """ image = self.data() if image is None: return size = self.size() if size.width() == 0 or size.height() == 0: return heightData, widthData = image.shape widthContraint = heightData * size.width() / size.height() if widthContraint > widthData: heightContraint = heightData else: heightContraint = heightData * size.height() / size.width() widthContraint = widthData midWidth, midHeight = widthData * 0.5, heightData * 0.5 heightContraint, widthContraint = heightContraint * 0.5, widthContraint * 0.5 axis = self.__plot.getXAxis() axis.setLimitsConstraints(midWidth - widthContraint, midWidth + widthContraint) axis = self.__plot.getYAxis() axis.setLimitsConstraints(midHeight - heightContraint, midHeight + heightContraint) def __imageItem(self): image = self.__plot.getImage("data") return image def data(self): if self.__data is not None: if hasattr(self.__data, "name"): # in case of HDF5 if self.__data.name is None: # The dataset was closed self.__data = None return self.__data def colormap(self): image = self.__imageItem() if image is not None: return image.getColormap() return self.__plot.getDefaultColormap() def setColormap(self, colormap): self.__plot.setDefaultColormap(colormap) def clear(self): self.__data = None image = self.__imageItem() if image is not None: self.__plot.removeImage(legend="data")
associated with a :class:`~silx.gui.plot.PlotWidget`. """ __authors__ = ["P. Knobel"] __license__ = "MIT" __date__ = "28/06/2017" from silx.gui import qt from silx.gui.plot.PlotWidget import PlotWidget from silx.gui.plot.ItemsSelectionDialog import ItemsSelectionDialog app = qt.QApplication([]) pw = PlotWidget() pw.addCurve([0, 1, 2], [3, 2, 1], "A curve") pw.addScatter([0, 1, 2.5], [3, 2.5, 0.9], [8, 9, 72], "A scatter") pw.addHistogram([0, 1, 2.5], [0, 1, 2, 3], "A histogram") pw.addImage([[0, 1, 2], [3, 2, 1]], "An image") pw.show() isd = ItemsSelectionDialog(plot=pw) isd.setItemsSelectionMode(qt.QTableWidget.ExtendedSelection) result = isd.exec_() if result: for item in isd.getSelectedItems(): print(item.getLegend(), type(item)) else: print("Selection cancelled") app.exec_()