def test_ec(ac=None, rd=None):
    """Test EC methods
    """
    if ac is None:
        # test type/value checking for audio_controller
        assert_raises(TypeError, ExperimentController, *std_args,
                      audio_controller=1, stim_fs=44100, **std_kwargs)
        assert_raises(ValueError, ExperimentController, *std_args,
                      audio_controller='foo', stim_fs=44100, **std_kwargs)
        assert_raises(ValueError, ExperimentController, *std_args,
                      audio_controller=dict(TYPE='foo'), stim_fs=44100,
                      **std_kwargs)

        # test type checking for 'session'
        std_kwargs['session'] = 1
        assert_raises(TypeError, ExperimentController, *std_args,
                      audio_controller='pyglet', stim_fs=44100, **std_kwargs)
        std_kwargs['session'] = '01'

        # test value checking for trigger controller
        assert_raises(ValueError, ExperimentController, *std_args,
                      audio_controller='pyglet', trigger_controller='foo',
                      stim_fs=44100, **std_kwargs)

        # test value checking for RMS checker
        assert_raises(ValueError, ExperimentController, *std_args,
                      audio_controller='pyglet', check_rms=True, stim_fs=44100,
                      **std_kwargs)

        # run rest of test with audio_controller == 'pyglet'
        this_ac = 'pyglet'
        this_rd = 'keyboard'
        this_tc = 'dummy'
        this_fs = 44100
    else:
        assert ac == 'tdt'
        # run rest of test with audio_controller == 'tdt'
        this_ac = ac
        this_rd = rd
        this_tc = ac
        this_fs = get_tdt_rates()['25k']
    with ExperimentController(*std_args, audio_controller=this_ac,
                              response_device=this_rd,
                              trigger_controller=this_tc,
                              stim_fs=this_fs, **std_kwargs) as ec:
        assert_true(ec.participant == std_kwargs['participant'])
        assert_true(ec.session == std_kwargs['session'])
        assert_true(ec.exp_name == std_args[0])
        stamp = ec.current_time
        ec.write_data_line('hello')
        ec.wait_until(stamp + 0.02)
        ec.screen_prompt('test', 0.01, 0, None)
        ec.screen_prompt('test', 0.01, 0, ['1'])
        ec.screen_prompt(['test', 'ing'], 0.01, 0, ['1'])
        assert_raises(ValueError, ec.screen_prompt, 'foo', np.inf, 0, [])
        assert_raises(TypeError, ec.screen_prompt, 3, 0.01, 0, None)
        assert_equal(ec.wait_one_press(0.01), (None, None))
        assert_true(ec.wait_one_press(0.01, timestamp=False) is None)
        assert_equal(ec.wait_for_presses(0.01), [])
        assert_equal(ec.wait_for_presses(0.01, timestamp=False), [])
        assert_raises(ValueError, ec.get_presses)
        ec.listen_presses()
        assert_equal(ec.get_presses(), [])
        ec.clear_buffer()
        ec.set_noise_db(0)
        ec.set_stim_db(20)
        # test buffer data handling
        ec.load_buffer([0, 0, 0, 0, 0, 0])
        assert_raises(ValueError, ec.load_buffer, [0, 2, 0, 0, 0, 0])
        ec.load_buffer(np.zeros((100,)))
        ec.load_buffer(np.zeros((100, 1)))
        ec.load_buffer(np.zeros((100, 2)))
        ec.load_buffer(np.zeros((1, 100)))
        ec.load_buffer(np.zeros((2, 100)))
        assert_raises(ValueError, ec.stamp_triggers, 'foo')
        assert_raises(ValueError, ec.stamp_triggers, 0)
        assert_raises(ValueError, ec.stamp_triggers, 3)
        assert_raises(ValueError, ec.stamp_triggers, 1, check='foo')
        ec.stamp_triggers(3, check='int4')
        ec.stamp_triggers(2)
        ec.stamp_triggers([2, 4, 8])
        assert_raises(ValueError, ec.load_buffer, np.zeros((100, 3)))
        assert_raises(ValueError, ec.load_buffer, np.zeros((3, 100)))
        assert_raises(ValueError, ec.load_buffer, np.zeros((1, 1, 1)))

        # test RMS checking
        assert_raises(ValueError, ec.set_rms_checking, 'foo')
        # click: RMS 0.0135, should pass 'fullfile' and fail 'windowed'
        click = np.zeros((int(ec.fs / 4),))  # 250 ms
        click[len(click) // 2] = 1.
        click[len(click) // 2 + 1] = -1.
        # noise: RMS 0.03, should fail both 'fullfile' and 'windowed'
        noise = np.random.normal(scale=0.03, size=(int(ec.fs / 4),))
        ec.set_rms_checking(None)
        ec.load_buffer(click)  # should go unchecked
        ec.load_buffer(noise)  # should go unchecked
        ec.set_rms_checking('wholefile')
        with warnings.catch_warnings(record=True) as w:
            ec.load_buffer(click)  # should pass
            assert_equal(len(w), 0)
            ec.load_buffer(noise)
            assert_equal(len(w), 1)
            ec.set_rms_checking('windowed')
            ec.load_buffer(click)
            assert_equal(len(w), 2)
            ec.load_buffer(noise)
            assert_equal(len(w), 3)

        ec.stop()
        ec.call_on_every_flip(dummy_print, 'called start stimuli')

        #
        # First: identify_trial
        #
        assert_raises(RuntimeError, ec.start_stimulus)  # order violation
        ec.start_stimulus(start_of_trial=False)         # should work
        assert_raises(RuntimeError, ec.trial_ok)        # order violation
        ec.stop()
        # only binary for TTL
        assert_raises(KeyError, ec.identify_trial, ec_id='foo')  # need ttl_id
        assert_raises(TypeError, ec.identify_trial, ec_id='foo', ttl_id='bar')
        assert_raises(ValueError, ec.identify_trial, ec_id='foo', ttl_id=[2])
        ec.identify_trial(ec_id='foo', ttl_id=[0, 1])
        #
        # Second: start_stimuli
        #
        assert_raises(RuntimeError, ec.identify_trial, ec_id='foo', ttl_id=[0])
        assert_raises(RuntimeError, ec.trial_ok)        # order violation
        ec.start_stimulus(flip=False, when=-1)
        assert_raises(RuntimeError, ec.play)  # already played, must stop
        ec.stop()
        #
        # Third: trial_ok
        #
        assert_raises(RuntimeError, ec.start_stimulus)  # order violation
        assert_raises(RuntimeError, ec.identify_trial)  # order violation
        ec.trial_ok()
        # double-check
        assert_raises(RuntimeError, ec.start_stimulus)  # order violation
        ec.start_stimulus(start_of_trial=False)         # should work
        assert_raises(RuntimeError, ec.trial_ok)        # order violation
        ec.stop()

        ec.flip()
        ec.estimate_screen_fs()
        ec.play()
        ec.call_on_every_flip(None)
        ec.call_on_next_flip(ec.start_noise())
        ec.stop()
        ec.start_stimulus(start_of_trial=False)
        ec.call_on_next_flip(ec.stop_noise())
        ec.stop()
        ec.start_stimulus(start_of_trial=False)
        ec.get_mouse_position()
        ec.listen_clicks()
        ec.get_clicks()
        ec.toggle_cursor(False)
        ec.toggle_cursor(True, True)
        ec.wait_secs(0.001)
        print(ec.id_types)
        print(ec.stim_db)
        print(ec.noise_db)
        print(ec.on_next_flip_functions)
        print(ec.on_every_flip_functions)
        print(ec.window)
        data = ec.screenshot()
        assert_allclose(data.shape[:2], std_kwargs['window_size'])
        print(ec.fs)  # test fs support
        wait_secs(0.01)
        test_pix = (11.3, 0.5, 110003)
        print(test_pix)
        # test __repr__
        assert all([x in repr(ec) for x in ['foo', '"test"', '01']])
    del ec
Example #2
0
def test_ec(ac=None, rd=None):
    """Test EC methods."""
    if ac is None:
        # test type/value checking for audio_controller
        assert_raises(TypeError,
                      ExperimentController,
                      *std_args,
                      audio_controller=1,
                      stim_fs=44100,
                      **std_kwargs)
        assert_raises(ValueError,
                      ExperimentController,
                      *std_args,
                      audio_controller='foo',
                      stim_fs=44100,
                      **std_kwargs)
        assert_raises(ValueError,
                      ExperimentController,
                      *std_args,
                      audio_controller=dict(TYPE='foo'),
                      stim_fs=44100,
                      **std_kwargs)
        # monitor, etc.
        assert_raises(TypeError,
                      ExperimentController,
                      *std_args,
                      monitor='foo',
                      **std_kwargs)
        assert_raises(KeyError,
                      ExperimentController,
                      *std_args,
                      monitor=dict(),
                      **std_kwargs)
        assert_raises(ValueError,
                      ExperimentController,
                      *std_args,
                      response_device='foo',
                      **std_kwargs)
        std_kwargs.update(window_size=10.)
        assert_raises(ValueError, ExperimentController, *std_args,
                      **std_kwargs)
        std_kwargs.update(window_size=(1, 1))
        assert_raises(ValueError,
                      ExperimentController,
                      *std_args,
                      audio_controller='pyglet',
                      response_device='tdt',
                      **std_kwargs)
        assert_raises(ValueError,
                      ExperimentController,
                      *std_args,
                      audio_controller='pyglet',
                      response_device='keyboard',
                      trigger_controller='tdt',
                      **std_kwargs)

        # test type checking for 'session'
        std_kwargs['session'] = 1
        assert_raises(TypeError,
                      ExperimentController,
                      *std_args,
                      audio_controller='pyglet',
                      stim_fs=44100,
                      **std_kwargs)
        std_kwargs['session'] = '01'

        # test value checking for trigger controller
        assert_raises(ValueError,
                      ExperimentController,
                      *std_args,
                      audio_controller='pyglet',
                      trigger_controller='foo',
                      stim_fs=44100,
                      **std_kwargs)

        # test value checking for RMS checker
        assert_raises(ValueError,
                      ExperimentController,
                      *std_args,
                      audio_controller='pyglet',
                      check_rms=True,
                      stim_fs=44100,
                      **std_kwargs)

        # run rest of test with audio_controller == 'pyglet'
        this_ac = 'pyglet'
        this_rd = 'keyboard'
        this_tc = 'dummy'
        this_fs = 44100
    else:
        assert ac == 'tdt'
        # run rest of test with audio_controller == 'tdt'
        this_ac = ac
        this_rd = rd
        this_tc = ac
        this_fs = get_tdt_rates()['25k']
        assert_raises(ValueError,
                      ExperimentController,
                      *std_args,
                      audio_controller=dict(TYPE=this_ac, TDT_MODEL='foo'),
                      **std_kwargs)
    with warnings.catch_warnings(record=True) as w:
        for suppress in (True, False):
            with ExperimentController(*std_args,
                                      audio_controller=this_ac,
                                      response_device=this_rd,
                                      trigger_controller=this_tc,
                                      stim_fs=100.,
                                      suppress_resamp=suppress,
                                      **std_kwargs) as ec:
                pass
    with ExperimentController(*std_args,
                              audio_controller=this_ac,
                              response_device=this_rd,
                              trigger_controller=this_tc,
                              stim_fs=this_fs,
                              **std_kwargs) as ec:
        assert_true(ec.participant == std_kwargs['participant'])
        assert_true(ec.session == std_kwargs['session'])
        assert_true(ec.exp_name == std_args[0])
        stamp = ec.current_time
        ec.write_data_line('hello')
        ec.wait_until(stamp + 0.02)
        ec.screen_prompt('test', 0.01, 0, None)
        ec.screen_prompt('test', 0.01, 0, ['1'])
        ec.screen_prompt(['test', 'ing'], 0.01, 0, ['1'])
        ec.screen_prompt('test', 1e-3, click=True)
        assert_raises(ValueError, ec.screen_prompt, 'foo', np.inf, 0, [])
        assert_raises(TypeError, ec.screen_prompt, 3, 0.01, 0, None)
        assert_equal(ec.wait_one_press(0.01), (None, None))
        assert_true(ec.wait_one_press(0.01, timestamp=False) is None)
        assert_equal(ec.wait_for_presses(0.01), [])
        assert_equal(ec.wait_for_presses(0.01, timestamp=False), [])
        assert_raises(ValueError, ec.get_presses)
        ec.listen_presses()
        assert_equal(ec.get_presses(), [])
        assert_equal(ec.get_presses(kind='presses'), [])
        assert_raises(ValueError, ec.get_presses, kind='foo')
        if this_rd == 'tdt':
            # TDT does not have key release events, so should raise an
            # exception if asked for them:
            assert_raises(RuntimeError, ec.get_presses, kind='releases')
            assert_raises(RuntimeError, ec.get_presses, kind='both')
        else:
            assert_equal(ec.get_presses(kind='both'), [])
            assert_equal(ec.get_presses(kind='releases'), [])
        ec.set_noise_db(0)
        ec.set_stim_db(20)
        # test buffer data handling
        ec.set_rms_checking(None)
        ec.load_buffer([0, 0, 0, 0, 0, 0])
        assert_raises(ValueError, ec.load_buffer, [0, 2, 0, 0, 0, 0])
        ec.load_buffer(np.zeros((100, )))
        ec.load_buffer(np.zeros((100, 1)))
        ec.load_buffer(np.zeros((100, 2)))
        ec.load_buffer(np.zeros((1, 100)))
        ec.load_buffer(np.zeros((2, 100)))
        data = np.zeros(int(5e6), np.float32)  # too long for TDT
        if this_fs == get_tdt_rates()['25k']:
            assert_raises(RuntimeError, ec.load_buffer, data)
        else:
            ec.load_buffer(data)
        ec.load_buffer(np.zeros(2))
        del data
        assert_raises(ValueError, ec.stamp_triggers, 'foo')
        assert_raises(ValueError, ec.stamp_triggers, 0)
        assert_raises(ValueError, ec.stamp_triggers, 3)
        assert_raises(ValueError, ec.stamp_triggers, 1, check='foo')
        print(ec._tc)  # test __repr__
        if this_tc == 'dummy':
            assert_equal(ec._tc._trigger_list, [])
        ec.stamp_triggers(3, check='int4')
        ec.stamp_triggers(2)
        ec.stamp_triggers([2, 4, 8])
        if this_tc == 'dummy':
            assert_equal(ec._tc._trigger_list, [3, 2, 2, 4, 8])
            ec._tc._trigger_list = list()
        assert_raises(ValueError, ec.load_buffer, np.zeros((100, 3)))
        assert_raises(ValueError, ec.load_buffer, np.zeros((3, 100)))
        assert_raises(ValueError, ec.load_buffer, np.zeros((1, 1, 1)))

        # test RMS checking
        assert_raises(ValueError, ec.set_rms_checking, 'foo')
        # click: RMS 0.0135, should pass 'fullfile' and fail 'windowed'
        click = np.zeros((int(ec.fs / 4), ))  # 250 ms
        click[len(click) // 2] = 1.
        click[len(click) // 2 + 1] = -1.
        # noise: RMS 0.03, should fail both 'fullfile' and 'windowed'
        noise = np.random.normal(scale=0.03, size=(int(ec.fs / 4), ))
        ec.set_rms_checking(None)
        ec.load_buffer(click)  # should go unchecked
        ec.load_buffer(noise)  # should go unchecked
        ec.set_rms_checking('wholefile')
        with warnings.catch_warnings(record=True) as w:
            ec.load_buffer(click)  # should pass
            assert_equal(len(w), 0)
            ec.load_buffer(noise)
            assert_equal(len(w), 1)
            ec.set_rms_checking('windowed')
            ec.load_buffer(click)
            assert_equal(len(w), 2)
            ec.load_buffer(noise)
            assert_equal(len(w), 3)

        ec.stop()
        ec.set_visible()
        ec.set_visible(False)
        ec.call_on_every_flip(partial(dummy_print, 'called start stimuli'))

        # Note: we put some wait_secs in here because otherwise the delay in
        # play start (e.g. for trigdel and onsetdel) can
        # mess things up! So we probably eventually should add
        # some safeguard against stopping too quickly after starting...

        #
        # First: identify_trial
        #
        noise = np.random.normal(scale=0.01, size=(int(ec.fs), ))
        ec.load_buffer(noise)
        assert_raises(RuntimeError, ec.start_stimulus)  # order violation
        assert_true(ec._playing is False)
        if this_tc == 'dummy':
            assert_equal(ec._tc._trigger_list, [])
        ec.start_stimulus(start_of_trial=False)  # should work
        if this_tc == 'dummy':
            assert_equal(ec._tc._trigger_list, [1])
        ec.wait_secs(0.05)
        assert_true(ec._playing is True)
        assert_raises(RuntimeError, ec.trial_ok)  # order violation
        ec.stop()
        assert_true(ec._playing is False)
        # only binary for TTL
        assert_raises(KeyError, ec.identify_trial, ec_id='foo')  # need ttl_id
        assert_raises(TypeError, ec.identify_trial, ec_id='foo', ttl_id='bar')
        assert_raises(ValueError, ec.identify_trial, ec_id='foo', ttl_id=[2])
        assert_true(ec._playing is False)
        if this_tc == 'dummy':
            ec._tc._trigger_list = list()
        ec.identify_trial(ec_id='foo', ttl_id=[0, 1])
        assert_true(ec._playing is False)
        #
        # Second: start_stimuli
        #
        assert_raises(RuntimeError, ec.identify_trial, ec_id='foo', ttl_id=[0])
        assert_true(ec._playing is False)
        assert_raises(RuntimeError, ec.trial_ok)  # order violation
        assert_true(ec._playing is False)
        ec.start_stimulus(flip=False, when=-1)
        if this_tc == 'dummy':
            assert_equal(ec._tc._trigger_list, [4, 8, 1])
        if ac != 'tdt':
            # dummy TDT version won't do this check properly, as
            # ec._ac._playing -> GetTagVal('playing') always gives False
            assert_raises(RuntimeError, ec.play)  # already played, must stop
        ec.wait_secs(0.05)
        ec.stop()
        assert_true(ec._playing is False)
        #
        # Third: trial_ok
        #
        assert_raises(RuntimeError, ec.start_stimulus)  # order violation
        assert_raises(RuntimeError, ec.identify_trial)  # order violation
        ec.trial_ok()
        # double-check
        assert_raises(RuntimeError, ec.start_stimulus)  # order violation
        ec.start_stimulus(start_of_trial=False)  # should work
        assert_raises(RuntimeError, ec.trial_ok)  # order violation
        ec.wait_secs(0.05)
        ec.stop()
        assert_true(ec._playing is False)

        ec.flip(-np.inf)
        assert_true(ec._playing is False)
        ec.estimate_screen_fs()
        assert_true(ec._playing is False)
        ec.play()
        ec.wait_secs(0.05)
        assert_true(ec._playing is True)
        ec.call_on_every_flip(None)
        ec.call_on_next_flip(ec.start_noise())
        ec.wait_secs(0.05)
        ec.stop()
        assert_true(ec._playing is False)
        ec.start_stimulus(start_of_trial=False)
        ec.call_on_next_flip(ec.stop_noise)
        ec.stop()
        ec.start_stimulus(start_of_trial=False)
        ec.get_mouse_position()
        ec.listen_clicks()
        ec.get_clicks()
        ec.toggle_cursor(False)
        ec.toggle_cursor(True, True)
        ec.wait_secs(0.001)
        print(ec.id_types)
        print(ec.stim_db)
        print(ec.noise_db)
        print(ec.on_next_flip_functions)
        print(ec.on_every_flip_functions)
        print(ec.window)
        data = ec.screenshot()
        assert_allclose(data.shape[:2], std_kwargs['window_size'])
        print(ec.fs)  # test fs support
        wait_secs(0.01)
        test_pix = (11.3, 0.5, 110003)
        print(test_pix)
        # test __repr__
        assert all([x in repr(ec) for x in ['foo', '"test"', '01']])
    del ec
def test_ec(ac=None, rd=None):
    """Test EC methods
    """
    if ac is None:
        # test type/value checking for audio_controller
        assert_raises(TypeError,
                      ExperimentController,
                      *std_args,
                      audio_controller=1,
                      stim_fs=44100,
                      **std_kwargs)
        assert_raises(ValueError,
                      ExperimentController,
                      *std_args,
                      audio_controller='foo',
                      stim_fs=44100,
                      **std_kwargs)
        assert_raises(ValueError,
                      ExperimentController,
                      *std_args,
                      audio_controller=dict(TYPE='foo'),
                      stim_fs=44100,
                      **std_kwargs)
        # monitor, etc.
        assert_raises(TypeError,
                      ExperimentController,
                      *std_args,
                      monitor='foo',
                      **std_kwargs)
        assert_raises(KeyError,
                      ExperimentController,
                      *std_args,
                      monitor=dict(),
                      **std_kwargs)
        assert_raises(ValueError,
                      ExperimentController,
                      *std_args,
                      response_device='foo',
                      **std_kwargs)
        std_kwargs.update(window_size=10.)
        assert_raises(ValueError, ExperimentController, *std_args,
                      **std_kwargs)
        std_kwargs.update(window_size=(1, 1))
        assert_raises(ValueError,
                      ExperimentController,
                      *std_args,
                      audio_controller='pyglet',
                      response_device='tdt',
                      **std_kwargs)
        assert_raises(ValueError,
                      ExperimentController,
                      *std_args,
                      audio_controller='pyglet',
                      response_device='keyboard',
                      trigger_controller='tdt',
                      **std_kwargs)

        # test type checking for 'session'
        std_kwargs['session'] = 1
        assert_raises(TypeError,
                      ExperimentController,
                      *std_args,
                      audio_controller='pyglet',
                      stim_fs=44100,
                      **std_kwargs)
        std_kwargs['session'] = '01'

        # test value checking for trigger controller
        assert_raises(ValueError,
                      ExperimentController,
                      *std_args,
                      audio_controller='pyglet',
                      trigger_controller='foo',
                      stim_fs=44100,
                      **std_kwargs)

        # test value checking for RMS checker
        assert_raises(ValueError,
                      ExperimentController,
                      *std_args,
                      audio_controller='pyglet',
                      check_rms=True,
                      stim_fs=44100,
                      **std_kwargs)

        # run rest of test with audio_controller == 'pyglet'
        this_ac = 'pyglet'
        this_rd = 'keyboard'
        this_tc = 'dummy'
        this_fs = 44100
    else:
        assert ac == 'tdt'
        # run rest of test with audio_controller == 'tdt'
        this_ac = ac
        this_rd = rd
        this_tc = ac
        this_fs = get_tdt_rates()['25k']
    with warnings.catch_warnings(record=True) as w:
        for suppress in (True, False):
            with ExperimentController(*std_args,
                                      audio_controller=this_ac,
                                      response_device=this_rd,
                                      trigger_controller=this_tc,
                                      stim_fs=100.,
                                      suppress_resamp=suppress,
                                      **std_kwargs) as ec:
                pass
    warnings.simplefilter('ignore')  # ignore dummy TDT warning
    with ExperimentController(*std_args,
                              audio_controller=this_ac,
                              response_device=this_rd,
                              trigger_controller=this_tc,
                              stim_fs=this_fs,
                              **std_kwargs) as ec:
        warnings.simplefilter('always')
        assert_true(ec.participant == std_kwargs['participant'])
        assert_true(ec.session == std_kwargs['session'])
        assert_true(ec.exp_name == std_args[0])
        stamp = ec.current_time
        ec.write_data_line('hello')
        ec.wait_until(stamp + 0.02)
        ec.screen_prompt('test', 0.01, 0, None)
        ec.screen_prompt('test', 0.01, 0, ['1'])
        ec.screen_prompt(['test', 'ing'], 0.01, 0, ['1'])
        assert_raises(ValueError, ec.screen_prompt, 'foo', np.inf, 0, [])
        assert_raises(TypeError, ec.screen_prompt, 3, 0.01, 0, None)
        assert_equal(ec.wait_one_press(0.01), (None, None))
        assert_true(ec.wait_one_press(0.01, timestamp=False) is None)
        assert_equal(ec.wait_for_presses(0.01), [])
        assert_equal(ec.wait_for_presses(0.01, timestamp=False), [])
        assert_raises(ValueError, ec.get_presses)
        ec.listen_presses()
        assert_equal(ec.get_presses(), [])
        ec.clear_buffer()
        ec.set_noise_db(0)
        ec.set_stim_db(20)
        # test buffer data handling
        ec.set_rms_checking(None)
        ec.load_buffer([0, 0, 0, 0, 0, 0])
        assert_raises(ValueError, ec.load_buffer, [0, 2, 0, 0, 0, 0])
        ec.load_buffer(np.zeros((100, )))
        ec.load_buffer(np.zeros((100, 1)))
        ec.load_buffer(np.zeros((100, 2)))
        ec.load_buffer(np.zeros((1, 100)))
        ec.load_buffer(np.zeros((2, 100)))
        assert_raises(ValueError, ec.stamp_triggers, 'foo')
        assert_raises(ValueError, ec.stamp_triggers, 0)
        assert_raises(ValueError, ec.stamp_triggers, 3)
        assert_raises(ValueError, ec.stamp_triggers, 1, check='foo')
        ec.stamp_triggers(3, check='int4')
        ec.stamp_triggers(2)
        ec.stamp_triggers([2, 4, 8])
        assert_raises(ValueError, ec.load_buffer, np.zeros((100, 3)))
        assert_raises(ValueError, ec.load_buffer, np.zeros((3, 100)))
        assert_raises(ValueError, ec.load_buffer, np.zeros((1, 1, 1)))

        # test RMS checking
        assert_raises(ValueError, ec.set_rms_checking, 'foo')
        # click: RMS 0.0135, should pass 'fullfile' and fail 'windowed'
        click = np.zeros((int(ec.fs / 4), ))  # 250 ms
        click[len(click) // 2] = 1.
        click[len(click) // 2 + 1] = -1.
        # noise: RMS 0.03, should fail both 'fullfile' and 'windowed'
        noise = np.random.normal(scale=0.03, size=(int(ec.fs / 4), ))
        ec.set_rms_checking(None)
        ec.load_buffer(click)  # should go unchecked
        ec.load_buffer(noise)  # should go unchecked
        ec.set_rms_checking('wholefile')
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            ec.load_buffer(click)  # should pass
            assert_equal(len(w), 0)
            ec.load_buffer(noise)
            assert_equal(len(w), 1)
            ec.set_rms_checking('windowed')
            ec.load_buffer(click)
            assert_equal(len(w), 2)
            ec.load_buffer(noise)
            assert_equal(len(w), 3)

        ec.stop()
        ec.set_visible()
        ec.set_visible(False)
        ec.call_on_every_flip(partial(dummy_print, 'called start stimuli'))

        #
        # First: identify_trial
        #
        assert_raises(RuntimeError, ec.start_stimulus)  # order violation
        ec.start_stimulus(start_of_trial=False)  # should work
        assert_raises(RuntimeError, ec.trial_ok)  # order violation
        ec.stop()
        # only binary for TTL
        assert_raises(KeyError, ec.identify_trial, ec_id='foo')  # need ttl_id
        assert_raises(TypeError, ec.identify_trial, ec_id='foo', ttl_id='bar')
        assert_raises(ValueError, ec.identify_trial, ec_id='foo', ttl_id=[2])
        ec.identify_trial(ec_id='foo', ttl_id=[0, 1])
        #
        # Second: start_stimuli
        #
        assert_raises(RuntimeError, ec.identify_trial, ec_id='foo', ttl_id=[0])
        assert_raises(RuntimeError, ec.trial_ok)  # order violation
        ec.start_stimulus(flip=False, when=-1)
        assert_raises(RuntimeError, ec.play)  # already played, must stop
        ec.stop()
        #
        # Third: trial_ok
        #
        assert_raises(RuntimeError, ec.start_stimulus)  # order violation
        assert_raises(RuntimeError, ec.identify_trial)  # order violation
        ec.trial_ok()
        # double-check
        assert_raises(RuntimeError, ec.start_stimulus)  # order violation
        ec.start_stimulus(start_of_trial=False)  # should work
        assert_raises(RuntimeError, ec.trial_ok)  # order violation
        ec.stop()

        ec.flip(-np.inf)
        ec.estimate_screen_fs()
        ec.play()
        ec.call_on_every_flip(None)
        ec.call_on_next_flip(ec.start_noise())
        ec.stop()
        ec.start_stimulus(start_of_trial=False)
        ec.call_on_next_flip(ec.stop_noise())
        ec.stop()
        ec.start_stimulus(start_of_trial=False)
        ec.get_mouse_position()
        ec.listen_clicks()
        ec.get_clicks()
        ec.toggle_cursor(False)
        ec.toggle_cursor(True, True)
        ec.wait_secs(0.001)
        print(ec.id_types)
        print(ec.stim_db)
        print(ec.noise_db)
        print(ec.on_next_flip_functions)
        print(ec.on_every_flip_functions)
        print(ec.window)
        data = ec.screenshot()
        assert_allclose(data.shape[:2], std_kwargs['window_size'])
        print(ec.fs)  # test fs support
        wait_secs(0.01)
        test_pix = (11.3, 0.5, 110003)
        print(test_pix)
        # test __repr__
        assert all([x in repr(ec) for x in ['foo', '"test"', '01']])
    del ec
def test_ec(ac, hide_window):
    """Test EC methods."""
    if ac == 'tdt':
        rd, tc, fs = 'tdt', 'tdt', get_tdt_rates()['25k']
        pytest.raises(ValueError, ExperimentController, *std_args,
                      audio_controller=dict(TYPE=ac, TDT_MODEL='foo'),
                      **std_kwargs)
    else:
        _check_skip_backend(ac)
        rd, tc, fs = 'keyboard', 'dummy', 44100
    for suppress in (True, False):
        with pytest.warns(None) as w:
            with ExperimentController(
                    *std_args, audio_controller=ac, response_device=rd,
                    trigger_controller=tc, stim_fs=100.,
                    suppress_resamp=suppress, **std_kwargs) as ec:
                pass
        assert len(w) == (1 if ac == 'tdt' else 0)
    with ExperimentController(
            *std_args, audio_controller=ac, response_device=rd,
            trigger_controller=tc, stim_fs=fs, **std_kwargs) as ec:
        assert (ec.participant == std_kwargs['participant'])
        assert (ec.session == std_kwargs['session'])
        assert (ec.exp_name == std_args[0])
        stamp = ec.current_time
        ec.write_data_line('hello')
        ec.wait_until(stamp + 0.02)
        ec.screen_prompt('test', 0.01, 0, None)
        ec.screen_prompt('test', 0.01, 0, ['1'])
        ec.screen_prompt(['test', 'ing'], 0.01, 0, ['1'])
        ec.screen_prompt('test', 1e-3, click=True)
        pytest.raises(ValueError, ec.screen_prompt, 'foo', np.inf, 0, [])
        pytest.raises(TypeError, ec.screen_prompt, 3, 0.01, 0, None)
        assert_equal(ec.wait_one_press(0.01), (None, None))
        assert (ec.wait_one_press(0.01, timestamp=False) is None)
        assert_equal(ec.wait_for_presses(0.01), [])
        assert_equal(ec.wait_for_presses(0.01, timestamp=False), [])
        pytest.raises(ValueError, ec.get_presses)
        ec.listen_presses()
        assert_equal(ec.get_presses(), [])
        assert_equal(ec.get_presses(kind='presses'), [])
        pytest.raises(ValueError, ec.get_presses, kind='foo')
        if rd == 'tdt':
            # TDT does not have key release events, so should raise an
            # exception if asked for them:
            pytest.raises(RuntimeError, ec.get_presses, kind='releases')
            pytest.raises(RuntimeError, ec.get_presses, kind='both')
        else:
            assert_equal(ec.get_presses(kind='both'), [])
            assert_equal(ec.get_presses(kind='releases'), [])
        ec.set_noise_db(0)
        ec.set_stim_db(20)
        # test buffer data handling
        ec.set_rms_checking(None)
        ec.load_buffer([0, 0, 0, 0, 0, 0])
        pytest.raises(ValueError, ec.load_buffer, [0, 2, 0, 0, 0, 0])
        ec.load_buffer(np.zeros((100,)))
        ec.load_buffer(np.zeros((100, 1)))
        ec.load_buffer(np.zeros((100, 2)))
        ec.load_buffer(np.zeros((1, 100)))
        ec.load_buffer(np.zeros((2, 100)))
        data = np.zeros(int(5e6), np.float32)  # too long for TDT
        if fs == get_tdt_rates()['25k']:
            pytest.raises(RuntimeError, ec.load_buffer, data)
        else:
            ec.load_buffer(data)
        ec.load_buffer(np.zeros(2))
        del data
        pytest.raises(ValueError, ec.stamp_triggers, 'foo')
        pytest.raises(ValueError, ec.stamp_triggers, 0)
        pytest.raises(ValueError, ec.stamp_triggers, 3)
        pytest.raises(ValueError, ec.stamp_triggers, 1, check='foo')
        print(ec._tc)  # test __repr__
        if tc == 'dummy':
            assert_equal(ec._tc._trigger_list, [])
        ec.stamp_triggers(3, check='int4')
        ec.stamp_triggers(2)
        ec.stamp_triggers([2, 4, 8])
        if tc == 'dummy':
            assert_equal(ec._tc._trigger_list, [3, 2, 2, 4, 8])
            ec._tc._trigger_list = list()
        pytest.raises(ValueError, ec.load_buffer, np.zeros((100, 3)))
        pytest.raises(ValueError, ec.load_buffer, np.zeros((3, 100)))
        pytest.raises(ValueError, ec.load_buffer, np.zeros((1, 1, 1)))

        # test RMS checking
        pytest.raises(ValueError, ec.set_rms_checking, 'foo')
        # click: RMS 0.0135, should pass 'fullfile' and fail 'windowed'
        click = np.zeros((int(ec.fs / 4),))  # 250 ms
        click[len(click) // 2] = 1.
        click[len(click) // 2 + 1] = -1.
        # noise: RMS 0.03, should fail both 'fullfile' and 'windowed'
        noise = np.random.normal(scale=0.03, size=(int(ec.fs / 4),))
        ec.set_rms_checking(None)
        ec.load_buffer(click)  # should go unchecked
        ec.load_buffer(noise)  # should go unchecked
        ec.set_rms_checking('wholefile')
        ec.load_buffer(click)  # should pass
        with pytest.warns(UserWarning, match='exceeds stated'):
            ec.load_buffer(noise)
        ec.set_rms_checking('windowed')
        with pytest.warns(UserWarning, match='exceeds stated'):
            ec.load_buffer(click)
        with pytest.warns(UserWarning, match='exceeds stated'):
            ec.load_buffer(noise)

        ec.stop()
        ec.set_visible()
        ec.set_visible(False)
        ec.call_on_every_flip(partial(dummy_print, 'called start stimuli'))

        # Note: we put some wait_secs in here because otherwise the delay in
        # play start (e.g. for trigdel and onsetdel) can
        # mess things up! So we probably eventually should add
        # some safeguard against stopping too quickly after starting...

        #
        # First: identify_trial
        #
        noise = np.random.normal(scale=0.01, size=(int(ec.fs),))
        ec.load_buffer(noise)
        pytest.raises(RuntimeError, ec.start_stimulus)  # order violation
        assert (ec._playing is False)
        if tc == 'dummy':
            assert_equal(ec._tc._trigger_list, [])
        ec.start_stimulus(start_of_trial=False)         # should work
        if tc == 'dummy':
            assert_equal(ec._tc._trigger_list, [1])
        ec.wait_secs(0.05)
        assert (ec._playing is True)
        pytest.raises(RuntimeError, ec.trial_ok)        # order violation
        ec.stop()
        assert (ec._playing is False)
        # only binary for TTL
        pytest.raises(KeyError, ec.identify_trial, ec_id='foo')  # need ttl_id
        pytest.raises(TypeError, ec.identify_trial, ec_id='foo', ttl_id='bar')
        pytest.raises(ValueError, ec.identify_trial, ec_id='foo', ttl_id=[2])
        assert (ec._playing is False)
        if tc == 'dummy':
            ec._tc._trigger_list = list()
        ec.identify_trial(ec_id='foo', ttl_id=[0, 1])
        assert (ec._playing is False)
        #
        # Second: start_stimuli
        #
        pytest.raises(RuntimeError, ec.identify_trial, ec_id='foo', ttl_id=[0])
        assert (ec._playing is False)
        pytest.raises(RuntimeError, ec.trial_ok)        # order violation
        assert (ec._playing is False)
        ec.start_stimulus(flip=False, when=-1)
        if tc == 'dummy':
            assert_equal(ec._tc._trigger_list, [4, 8, 1])
        if ac != 'tdt':
            # dummy TDT version won't do this check properly, as
            # ec._ac._playing -> GetTagVal('playing') always gives False
            pytest.raises(RuntimeError, ec.play)  # already played, must stop
        ec.wait_secs(0.05)
        ec.stop()
        assert (ec._playing is False)
        #
        # Third: trial_ok
        #
        pytest.raises(RuntimeError, ec.start_stimulus)  # order violation
        pytest.raises(RuntimeError, ec.identify_trial)  # order violation
        ec.trial_ok()
        # double-check
        pytest.raises(RuntimeError, ec.start_stimulus)  # order violation
        ec.start_stimulus(start_of_trial=False)         # should work
        pytest.raises(RuntimeError, ec.trial_ok)        # order violation
        ec.wait_secs(0.05)
        ec.stop()
        assert (ec._playing is False)

        ec.flip(-np.inf)
        assert (ec._playing is False)
        ec.estimate_screen_fs()
        assert (ec._playing is False)
        ec.play()
        ec.wait_secs(0.05)
        assert (ec._playing is True)
        ec.call_on_every_flip(None)
        ec.call_on_next_flip(ec.start_noise())
        ec.wait_secs(0.05)
        ec.stop()
        assert (ec._playing is False)
        ec.start_stimulus(start_of_trial=False)
        ec.call_on_next_flip(ec.stop_noise)
        ec.stop()
        ec.start_stimulus(start_of_trial=False)
        ec.get_mouse_position()
        ec.listen_clicks()
        ec.get_clicks()
        ec.toggle_cursor(False)
        ec.toggle_cursor(True, True)
        ec.wait_secs(0.001)
        print(ec.id_types)
        print(ec.stim_db)
        print(ec.noise_db)
        print(ec.on_next_flip_functions)
        print(ec.on_every_flip_functions)
        print(ec.window)
        data = ec.screenshot()
        assert_allclose(data.shape[:2], std_kwargs['window_size'])
        print(ec.fs)  # test fs support
        wait_secs(0.01)
        test_pix = (11.3, 0.5, 110003)
        print(test_pix)
        # test __repr__
        assert all([x in repr(ec) for x in ['foo', '"test"', '01']])
        ec.refocus()  # smoke test for refocusing
    del ec