Esempio n. 1
0
def init_viewbox():
    """Helper function to init the ViewBox
    """
    global win, vb

    win = pg.GraphicsWindow()
    win.ci.layout.setContentsMargins(0, 0, 0, 0)
    win.resize(200, 200)
    win.show()
    vb = win.addViewBox()

    # set range before viewbox is shown
    vb.setRange(xRange=[0, 10], yRange=[0, 10], padding=0)

    # required to make mapFromView work properly.
    qtest.qWaitForWindowShown(win)

    g = pg.GridItem()
    vb.addItem(g)

    app.processEvents()
Esempio n. 2
0
def test_plotscene():
    tempfilename = tempfile.NamedTemporaryFile(suffix='.svg').name
    print("using %s as a temporary file" % tempfilename)
    pg.setConfigOption('foreground', (0, 0, 0))
    w = pg.GraphicsWindow()
    w.show()
    p1 = w.addPlot()
    p2 = w.addPlot()
    p1.plot([1, 3, 2, 3, 1, 6, 9, 8, 4, 2, 3, 5, 3], pen={'color': 'k'})
    p1.setXRange(0, 5)
    p2.plot([1, 5, 2, 3, 4, 6, 1, 2, 4, 2, 3, 5, 3],
            pen={
                'color': 'k',
                'cosmetic': False,
                'width': 0.3
            })
    app.processEvents()
    app.processEvents()

    ex = pg.exporters.SVGExporter(w.scene())
    ex.export(fileName=tempfilename)
    # clean up after the test is done
    os.unlink(tempfilename)
def test_PlotCurveItem():
    p = pg.GraphicsWindow()
    p.ci.layout.setContentsMargins(4, 4, 4, 4)  # default margins vary by platform
    v = p.addViewBox()
    p.resize(200, 150)
    data = np.array([1,4,2,3,np.inf,5,7,6,-np.inf,8,10,9,np.nan,-1,-2,0])
    c = pg.PlotCurveItem(data)
    v.addItem(c)
    v.autoRange()
    
    # Check auto-range works. Some platform differences may be expected..
    checkRange = np.array([[-1.1457564053237301, 16.145756405323731], [-3.076811473165955, 11.076811473165955]])
    assert np.allclose(v.viewRange(), checkRange)
    
    assertImageApproved(p, 'plotcurveitem/connectall', "Plot curve with all points connected.")
    
    c.setData(data, connect='pairs')
    assertImageApproved(p, 'plotcurveitem/connectpairs', "Plot curve with pairs connected.")
    
    c.setData(data, connect='finite')
    assertImageApproved(p, 'plotcurveitem/connectfinite', "Plot curve with finite points connected.")
    
    c.setData(data, connect=np.array([1,1,1,0,1,1,0,0,1,0,0,0,1,1,0,0]))
    assertImageApproved(p, 'plotcurveitem/connectarray', "Plot curve with connection array.")
def test_ImageItem(transpose=False):

    w = pg.GraphicsWindow()
    view = pg.ViewBox()
    w.setCentralWidget(view)
    w.resize(200, 200)
    w.show()
    img = TransposedImageItem(border=0.5, transpose=transpose)

    view.addItem(img)

    # test mono float
    np.random.seed(0)
    data = np.random.normal(size=(20, 20))
    dmax = data.max()
    data[:10, 1] = dmax + 10
    data[1, :10] = dmax + 12
    data[3, :10] = dmax + 13
    img.setImage(data)

    QtTest.QTest.qWaitForWindowShown(w)
    time.sleep(0.1)
    app.processEvents()
    assertImageApproved(
        w, 'imageitem/init',
        'Init image item. View is auto-scaled, image axis 0 marked by 1 line, axis 1 is marked by 2 lines. Origin in bottom-left.'
    )

    # ..with colormap
    cmap = pg.ColorMap([0, 0.25, 0.75, 1],
                       [[0, 0, 0, 255], [255, 0, 0, 255], [255, 255, 0, 255],
                        [255, 255, 255, 255]])
    img.setLookupTable(cmap.getLookupTable())
    assertImageApproved(w, 'imageitem/lut', 'Set image LUT.')

    # ..and different levels
    img.setLevels([dmax + 9, dmax + 13])
    assertImageApproved(w, 'imageitem/levels1', 'Levels show only axis lines.')

    img.setLookupTable(None)

    # test mono int
    data = np.fromfunction(lambda x, y: x + y * 10,
                           (129, 128)).astype(np.int16)
    img.setImage(data)
    assertImageApproved(w, 'imageitem/gradient_mono_int', 'Mono int gradient.')

    img.setLevels([640, 641])
    assertImageApproved(w, 'imageitem/gradient_mono_int_levels',
                        'Mono int gradient w/ levels to isolate diagonal.')

    # test mono byte
    data = np.fromfunction(lambda x, y: x + y, (129, 128)).astype(np.ubyte)
    img.setImage(data)
    assertImageApproved(w, 'imageitem/gradient_mono_byte',
                        'Mono byte gradient.')

    img.setLevels([127, 128])
    assertImageApproved(w, 'imageitem/gradient_mono_byte_levels',
                        'Mono byte gradient w/ levels to isolate diagonal.')

    # test monochrome image
    data = np.zeros((10, 10), dtype='uint8')
    data[:5, :5] = 1
    data[5:, 5:] = 1
    img.setImage(data)
    assertImageApproved(w, 'imageitem/monochrome',
                        'Ubyte image with only 0,1 values.')

    # test bool
    data = data.astype(bool)
    img.setImage(data)
    assertImageApproved(w, 'imageitem/bool', 'Boolean mask.')

    # test RGBA byte
    data = np.zeros((100, 100, 4), dtype='ubyte')
    data[..., 0] = np.linspace(0, 255, 100).reshape(100, 1)
    data[..., 1] = np.linspace(0, 255, 100).reshape(1, 100)
    data[..., 3] = 255
    img.setImage(data)
    assertImageApproved(w, 'imageitem/gradient_rgba_byte',
                        'RGBA byte gradient.')

    img.setLevels([[128, 129], [128, 255], [0, 1], [0, 255]])
    assertImageApproved(
        w, 'imageitem/gradient_rgba_byte_levels',
        'RGBA byte gradient. Levels set to show x=128 and y>128.')

    # test RGBA float
    data = data.astype(float)
    img.setImage(data / 1e9)
    assertImageApproved(w, 'imageitem/gradient_rgba_float',
                        'RGBA float gradient.')

    # checkerboard to test alpha
    img2 = TransposedImageItem(transpose=transpose)
    img2.setImage(np.fromfunction(lambda x, y: (x + y) % 2, (10, 10)),
                  levels=[-1, 2])
    view.addItem(img2)
    img2.scale(10, 10)
    img2.setZValue(-10)

    data[..., 0] *= 1e-9
    data[..., 1] *= 1e9
    data[..., 3] = np.fromfunction(lambda x, y: np.sin(0.1 * (x + y)),
                                   (100, 100))
    img.setImage(data, levels=[[0, 128e-9], [0, 128e9], [0, 1], [-1, 1]])
    assertImageApproved(w, 'imageitem/gradient_rgba_float_alpha',
                        'RGBA float gradient with alpha.')

    # test composition mode
    img.setCompositionMode(QtGui.QPainter.CompositionMode_Plus)
    assertImageApproved(
        w, 'imageitem/gradient_rgba_float_additive',
        'RGBA float gradient with alpha and additive composition mode.')

    img2.hide()
    img.setCompositionMode(QtGui.QPainter.CompositionMode_SourceOver)

    # test downsampling
    data = np.fromfunction(lambda x, y: np.cos(0.002 * x**2), (800, 100))
    img.setImage(data, levels=[-1, 1])
    assertImageApproved(w, 'imageitem/resolution_without_downsampling',
                        'Resolution test without downsampling.')

    img.setAutoDownsample(True)
    assertImageApproved(w, 'imageitem/resolution_with_downsampling_x',
                        'Resolution test with downsampling axross x axis.')
    assert img._lastDownsample == (4, 1)

    img.setImage(data.T, levels=[-1, 1])
    assertImageApproved(w, 'imageitem/resolution_with_downsampling_y',
                        'Resolution test with downsampling across y axis.')
    assert img._lastDownsample == (1, 4)

    view.hide()
 def mkobjs():
     w = pg.GraphicsWindow()
     p1 = w.addPlot()
     v1 = w.addViewBox()
     return mkrefs(w, p1, v1)