Example #1
0
def test_background_color(hide_window):
    """Test setting background color"""
    with ExperimentController(*std_args,
                              participant='foo',
                              session='01',
                              output_dir=None,
                              version='dev') as ec:
        print((ec.window.width, ec.window.height))
        ec.set_background_color('red')
        ss = ec.screenshot()[:, :, :3]
        red_mask = (ss == [255, 0, 0]).all(axis=-1)
        assert (red_mask.all())
        ec.set_background_color('white')
        ss = ec.screenshot()[:, :, :3]
        white_mask = (ss == [255] * 3).all(axis=-1)
        assert (white_mask.all())
        ec.flip()
        ec.set_background_color('0.5')
        visual.Rectangle(ec, [0, 0, 1, 1], fill_color='black').draw()
        ss = ec.screenshot()[:, :, :3]
        gray_mask = ((ss == [127] * 3).all(axis=-1) |
                     (ss == [128] * 3).all(axis=-1))
        assert (gray_mask.any())
        black_mask = (ss == [0] * 3).all(axis=-1)
        assert (black_mask.any())
        assert (np.logical_or(gray_mask, black_mask).all())
def test_background_color():
    """Test setting background color"""
    with ExperimentController(*std_args,
                              participant='foo',
                              session='01',
                              output_dir=None,
                              version='dev') as ec:
        ec.set_background_color('red')
        ec.screen_text('red', color='white')
        ss = ec.screenshot()[:, :, :3]
        white_mask = (ss == [255] * 3)
        assert_true(white_mask.any())
        red_mask = (ss == [255, 0, 0])
        assert_true(red_mask.any())
        assert_true(np.logical_or(white_mask, red_mask).all())
        ec.flip()

        ec.set_background_color('0.5')
        visual.Rectangle(ec, [0, 0, 1, 1], fill_color='black').draw()
        ss = ec.screenshot()[:, :, :3]
        gray_mask = (ss == [127] * 3) | (ss == [128] * 3)
        assert_true(gray_mask.any())
        black_mask = (ss == [0] * 3)
        assert_true(black_mask.any())
        assert_true(np.logical_or(gray_mask, black_mask).all())
Example #3
0
def test_mouse_clicks(hide_window):
    """Test EC mouse click support."""
    with ExperimentController(*std_args,
                              participant='foo',
                              session='01',
                              output_dir=None,
                              version='dev') as ec:
        rect = visual.Rectangle(ec, [0, 0, 2, 2])
        fake_mouse_click(ec, [1, 2], delay=0.3)
        assert_equal(
            ec.wait_for_click_on(rect, 1.5, timestamp=False)[0],
            ('left', 1, 2))
        pytest.raises(TypeError, ec.wait_for_click_on, (rect, rect), 1.5)
        fake_mouse_click(ec, [2, 1], 'middle', delay=0.3)
        out = ec.wait_one_click(1.5, 0., ['middle'], timestamp=True)
        assert (out[3] < 1.5)
        assert_equal(out[:3], ('middle', 2, 1))
        fake_mouse_click(ec, [3, 2], 'left', delay=0.3)
        fake_mouse_click(ec, [4, 5], 'right', delay=0.3)
        out = ec.wait_for_clicks(1.5, timestamp=False)
        assert_equal(len(out), 2)
        assert (any(o == ('left', 3, 2) for o in out))
        assert (any(o == ('right', 4, 5) for o in out))
        out = ec.wait_for_clicks(0.1)
        assert_equal(len(out), 0)
Example #4
0
def test_visuals(hide_window):
    """Test EC visual methods."""
    with ExperimentController('test', **std_kwargs) as ec:
        pytest.raises(TypeError, visual.Circle, ec, n_edges=3.5)
        pytest.raises(ValueError, visual.Circle, ec, n_edges=3)
        circ = visual.Circle(ec)
        circ.draw()
        pytest.raises(ValueError, circ.set_radius, [1, 2, 3])
        pytest.raises(ValueError, circ.set_pos, [1])
        pytest.raises(ValueError, visual.Triangle, ec, [5, 6])
        tri = visual.Triangle(ec, [[-1, 0, 1], [-1, 1, -1]],
                              units='deg',
                              line_width=1.0)
        tri.draw()
        rect = visual.Rectangle(ec, [0, 0, 1, 1], line_width=1.0)
        rect.draw()
        diamond = visual.Diamond(ec, [0, 0, 1, 1], line_width=1.0)
        diamond.draw()
        pytest.raises(TypeError, visual.ConcentricCircles, ec, colors=dict())
        pytest.raises(TypeError,
                      visual.ConcentricCircles,
                      ec,
                      colors=np.array([]))
        pytest.raises(ValueError, visual.ConcentricCircles, ec, radii=[[1]])
        pytest.raises(ValueError, visual.ConcentricCircles, ec, radii=[1])
        fix = visual.ConcentricCircles(ec,
                                       radii=[1, 2, 3],
                                       colors=['w', 'k', 'y'])
        fix.set_pos([0.5, 0.5])
        fix.set_radius(0.1, 1)
        fix.set_radii([0.1, 0.2, 0.3])
        fix.set_color('w', 1)
        fix.set_colors(['w', 'k', 'k'])
        fix.set_colors(('w', 'k', 'k'))
        pytest.raises(IndexError, fix.set_color, 'w', 3)
        pytest.raises(ValueError, fix.set_colors, ['w', 'k'])
        pytest.raises(ValueError, fix.set_colors, np.array(['w', 'k', 'k']))
        pytest.raises(IndexError, fix.set_radius, 0.1, 3)
        pytest.raises(ValueError, fix.set_radii, [0.1, 0.2])
        fix.draw()
        fix_2 = visual.FixationDot(ec)
        fix_2.draw()
        pytest.raises(ValueError, rect.set_pos, [0, 1, 2])
        for shape in ((3, 3, 3), (3, 3, 4), (3, 3), (3, ), (3, ) * 4):
            data = np.ones(shape)
            if len(shape) not in (2, 3):
                pytest.raises(RuntimeError, visual.RawImage, ec, data)
            else:
                img = visual.RawImage(ec, data)
            print(img.bounds)  # test bounds
            assert_equal(img.scale, 1)
            # test get_rect
            imgrect = visual.Rectangle(ec, img.get_rect())
            assert_equal(imgrect._points['fill'][(0, 2, 0, 1), (0, 0, 1, 1)],
                         img.bounds)
            img.draw()
        line = visual.Line(ec, [[0, 1], [1, 0]])
        line.draw()
        pytest.raises(ValueError, line.set_line_width, 100)
        line.set_line_width(2)
        line.draw()
        pytest.raises(ValueError, line.set_coords, [0])
        line.set_coords([0, 1])
        ec.set_background_color('black')
        text = visual.Text(ec,
                           'Hello {color (255 0 0 255)}Everybody!',
                           pos=[0, 0],
                           color=[1, 1, 1],
                           wrap=False)
        text.draw()
        text.set_color(None)
        text.draw()
        text = visual.Text(ec,
                           'Thank you, come again.',
                           pos=[0, 0],
                           color='white',
                           attr=False)
        text.draw()
        text.set_color('red')
        text.draw()
        bar = visual.ProgressBar(ec, [0, 0, 1, .2])
        bar = visual.ProgressBar(ec, [0, 0, 1, 1], units='pix')
        bar.update_bar(.5)
        bar.draw()
        pytest.raises(ValueError,
                      visual.ProgressBar,
                      ec, [0, 0, 1, .1],
                      units='deg')
        pytest.raises(ValueError,
                      visual.ProgressBar,
                      ec, [0, 0, 1, .1],
                      colors=['w'])
        pytest.raises(ValueError, bar.update_bar, 500)
Example #5
0
bg_color = [0.2] * 3
fix_color = [0.5] * 3
v_off = 2.

with ExperimentController('standard_setup',
                          full_screen=False,
                          stim_db=75,
                          noise_db=45,
                          participant='s',
                          session='0',
                          output_dir=None,
                          suppress_resamp=True,
                          check_rms=None,
                          version='9c4bcea') as ec:
    square = visual.Rectangle(ec, [0, 0, 10, 10], units='deg')
    ec.screen_text('expy_standard_setup_test.', wrap=False)
    ec.set_background_color(bg_color)
    fix = visual.FixationDot(ec, colors=[fix_color, bg_color])
    circ = visual.Circle(ec,
                         radius=1,
                         units='deg',
                         line_color=fix_color,
                         fill_color=None)
    strs = ',   '.join(token.capitalize() for token in p['tokens'])
    targ_text = visual.Text(ec,
                            strs,
                            pos=[0, v_off],
                            units='deg',
                            color=fix_color)
Example #6
0
def test_visuals():
    """Test EC visual methods
    """
    with ExperimentController('test', **std_kwargs) as ec:
        assert_raises(TypeError, visual.Circle, ec, n_edges=3.5)
        assert_raises(ValueError, visual.Circle, ec, n_edges=3)
        circ = visual.Circle(ec)
        circ.draw()
        assert_raises(ValueError, circ.set_radius, [1, 2, 3])
        assert_raises(ValueError, circ.set_pos, [1])
        assert_raises(ValueError, visual.Triangle, ec, [5, 6])
        tri = visual.Triangle(ec, [[-1, 0, 1], [-1, 1, -1]],
                              units='deg',
                              line_width=1.0)
        tri.draw()
        rect = visual.Rectangle(ec, [0, 0, 1, 1], line_width=1.0)
        rect.draw()
        diamond = visual.Diamond(ec, [0, 0, 1, 1], line_width=1.0)
        diamond.draw()
        assert_raises(TypeError, visual.ConcentricCircles, ec, colors=dict())
        assert_raises(TypeError,
                      visual.ConcentricCircles,
                      ec,
                      colors=np.array([]))
        assert_raises(ValueError, visual.ConcentricCircles, ec, radii=[[1]])
        assert_raises(ValueError, visual.ConcentricCircles, ec, radii=[1])
        fix = visual.ConcentricCircles(ec,
                                       radii=[1, 2, 3],
                                       colors=['w', 'k', 'y'])
        fix.set_pos([0.5, 0.5])
        fix.set_radius(0.1, 1)
        fix.set_radii([0.1, 0.2, 0.3])
        fix.set_color('w', 1)
        fix.set_colors(['w', 'k', 'k'])
        fix.set_colors(('w', 'k', 'k'))
        assert_raises(IndexError, fix.set_color, 'w', 3)
        assert_raises(ValueError, fix.set_colors, ['w', 'k'])
        assert_raises(ValueError, fix.set_colors, np.array(['w', 'k', 'k']))
        assert_raises(IndexError, fix.set_radius, 0.1, 3)
        assert_raises(ValueError, fix.set_radii, [0.1, 0.2])
        fix.draw()
        fix_2 = visual.FixationDot(ec)
        fix_2.draw()
        assert_raises(ValueError, rect.set_pos, [0, 1, 2])
        img = visual.RawImage(ec, np.ones((3, 3, 4)))
        print(img.bounds)  # test bounds
        assert_equal(img.scale, 1)
        img.draw()
        line = visual.Line(ec, [[0, 1], [1, 0]])
        line.draw()
        assert_raises(ValueError, line.set_line_width, 100)
        line.set_line_width(2)
        line.draw()
        assert_raises(ValueError, line.set_coords, [0])
        line.set_coords([0, 1])
        ec.set_background_color('black')
        text = visual.Text(ec,
                           'Hello {color (255 0 0 255)}Everybody!',
                           pos=[0, 0],
                           color=[1, 1, 1],
                           wrap=False)
        text.draw()
        text.set_color(None)
        text.draw()
        text = visual.Text(ec,
                           'Thank you, come again.',
                           pos=[0, 0],
                           color='white',
                           attr=False)
        text.draw()
        text.set_color('red')
        text.draw()

    # test video
    std_kwargs.update(dict(enable_video=True, window_size=(640, 480)))
    video_path = fetch_data_file('video/example-video.mp4')
    with ExperimentController('test', **std_kwargs) as ec:
        ec.load_video(video_path)
        ec.video.play()
        assert_raises(ValueError, ec.video.set_scale, 'foo')
        assert_raises(ValueError, ec.video.set_scale, -1)
        ec.video.set_scale('fill')
        ec.video.set_scale('fit')
        ec.video.set_scale(0.5)
        ec.video.set_pos(pos=(0.1, 0), units='norm')
        ec.wait_secs(0.2)
        ec.video.pause()
        ec.delete_video()
Example #7
0
                          output_dir=None, version='dev') as ec:
    ec.screen_text('hello')

    # make an image with  alpha the x-dimension (columns), RGB upward
    img_buffer = np.zeros((120, 100, 4))
    img_buffer[:, :50, 3] = 1.0
    img_buffer[:, 50:, 3] = 0.5
    img_buffer[0] = 1
    for ii in range(3):
        img_buffer[ii * 40:(ii + 1) * 40, :, ii] = 1.0
    img = visual.RawImage(ec, img_buffer, scale=2.)

    # make a line, rectangle, diamond, and circle
    line = visual.Line(ec, [[-2, 2, 2, -2], [-2, 2, -2, -2]], units='deg',
                       line_color='w', line_width=2.0)
    rect = visual.Rectangle(ec, [0, 0, 2, 2], units='deg', fill_color='y')
    diamond = visual.Diamond(ec, [0, 0, 4, 4], units='deg', fill_color=None,
                             line_color='gray', line_width=2.0)
    circle = visual.Circle(ec, 1, units='deg', line_color='w', fill_color='k',
                           line_width=2.0)

    # do the drawing, then flip
    for obj in [img, line, rect, diamond, circle]:
        obj.draw()

    screenshot = ec.screenshot()  # must be called *before* the flip
    ec.flip()
    ec.wait_for_presses(0.5)

plt.ion()
ea.plot_screen(screenshot)
Example #8
0
def test_visuals():
    """Test EC visual methods."""
    with ExperimentController('test', **std_kwargs) as ec:
        assert_raises(TypeError, visual.Circle, ec, n_edges=3.5)
        assert_raises(ValueError, visual.Circle, ec, n_edges=3)
        circ = visual.Circle(ec)
        circ.draw()
        assert_raises(ValueError, circ.set_radius, [1, 2, 3])
        assert_raises(ValueError, circ.set_pos, [1])
        assert_raises(ValueError, visual.Triangle, ec, [5, 6])
        tri = visual.Triangle(ec, [[-1, 0, 1], [-1, 1, -1]], units='deg',
                              line_width=1.0)
        tri.draw()
        rect = visual.Rectangle(ec, [0, 0, 1, 1], line_width=1.0)
        rect.draw()
        diamond = visual.Diamond(ec, [0, 0, 1, 1], line_width=1.0)
        diamond.draw()
        assert_raises(TypeError, visual.ConcentricCircles, ec, colors=dict())
        assert_raises(TypeError, visual.ConcentricCircles, ec,
                      colors=np.array([]))
        assert_raises(ValueError, visual.ConcentricCircles, ec, radii=[[1]])
        assert_raises(ValueError, visual.ConcentricCircles, ec, radii=[1])
        fix = visual.ConcentricCircles(ec, radii=[1, 2, 3],
                                       colors=['w', 'k', 'y'])
        fix.set_pos([0.5, 0.5])
        fix.set_radius(0.1, 1)
        fix.set_radii([0.1, 0.2, 0.3])
        fix.set_color('w', 1)
        fix.set_colors(['w', 'k', 'k'])
        fix.set_colors(('w', 'k', 'k'))
        assert_raises(IndexError, fix.set_color, 'w', 3)
        assert_raises(ValueError, fix.set_colors, ['w', 'k'])
        assert_raises(ValueError, fix.set_colors, np.array(['w', 'k', 'k']))
        assert_raises(IndexError, fix.set_radius, 0.1, 3)
        assert_raises(ValueError, fix.set_radii, [0.1, 0.2])
        fix.draw()
        fix_2 = visual.FixationDot(ec)
        fix_2.draw()
        assert_raises(ValueError, rect.set_pos, [0, 1, 2])
        img = visual.RawImage(ec, np.ones((3, 3, 4)))
        print(img.bounds)  # test bounds
        assert_equal(img.scale, 1)
        img.draw()
        line = visual.Line(ec, [[0, 1], [1, 0]])
        line.draw()
        assert_raises(ValueError, line.set_line_width, 100)
        line.set_line_width(2)
        line.draw()
        assert_raises(ValueError, line.set_coords, [0])
        line.set_coords([0, 1])
        ec.set_background_color('black')
        text = visual.Text(ec, 'Hello {color (255 0 0 255)}Everybody!',
                           pos=[0, 0], color=[1, 1, 1], wrap=False)
        text.draw()
        text.set_color(None)
        text.draw()
        text = visual.Text(ec, 'Thank you, come again.', pos=[0, 0],
                           color='white', attr=False)
        text.draw()
        text.set_color('red')
        text.draw()