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 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 _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")