<commit_before>import numpy as np
import matplotlib
matplotlib.use('Agg')  # noqa
from expyfun.stimuli import TrackerUD, TrackerBinom, TrackerDealer
from expyfun import ExperimentController
from nose.tools import assert_raises, assert_equal, assert_true
from expyfun._utils import _hide_window, requires_opengl21
import warnings


def callback(event_type, value=None, timestamp=None):
    print(event_type, value, timestamp)


std_kwargs = dict(output_dir=None, full_screen=False, window_size=(1, 1),
                  participant='foo', session='01', stim_db=0.0, noise_db=0.0,
                  trigger_controller='dummy', response_device='keyboard',
                  audio_controller='pyglet',
                  verbose=True, version='dev')


@_hide_window
@requires_opengl21
def test_tracker_ud():
    """Test TrackerUD"""
    import matplotlib.pyplot as plt
    tr = TrackerUD(callback, 3, 1, 1, 1, np.inf, 10, 1)
    with ExperimentController('test', **std_kwargs) as ec:
        tr = TrackerUD(ec, 3, 1, 1, 1, np.inf, 10, 1)
    tr = TrackerUD(None, 3, 1, 1, 1, 10, np.inf, 1)
    rand = np.random.RandomState(0)
    while not tr.stopped:
        tr.respond(rand.rand() < tr.x_current)
    assert_true(tr.n_reversals == tr.stop_reversals)

    tr = TrackerUD(None, 3, 1, 1, 1, np.inf, 10, 1)
    tr.threshold()
    rand = np.random.RandomState(0)
    while not tr.stopped:
        tr.respond(rand.rand() < tr.x_current)
    # test responding after stopped
    assert_raises(RuntimeError, tr.respond, 0)

    # all the properties better work
    tr.up
    tr.down
    tr.step_size_up
    tr.step_size_down
    tr.stop_reversals
    tr.stop_trials
    tr.start_value
    tr.x_min
    tr.x_max
    tr.stopped
    tr.x
    tr.responses
    tr.n_trials
    tr.n_reversals
    tr.reversals
    tr.reversal_inds
    fig, ax, lines = tr.plot()
    tr.plot_thresh(ax=ax)
    tr.plot_thresh()
    plt.close(fig)
    ax = plt.axes()
    fig, ax, lines = tr.plot(ax)
    plt.close(fig)
    tr.threshold()
    tr.check_valid(2)

    # bad callback type
    assert_raises(TypeError, TrackerUD, 'foo', 3, 1, 1, 1, 10, np.inf, 1)

    # test dynamic step size and error conditions
    tr = TrackerUD(None, 3, 1, [1, 0.5], [1, 0.5], 10, np.inf, 1,
                   change_indices=[2])
    tr.respond(True)

    with warnings.catch_warnings(record=True) as w:
        tr = TrackerUD(None, 1, 1, 0.75, 0.75, np.inf, 9, 1,
                       x_min=0, x_max=2)
        responses = [True, True, True, False, False, False, False, True, False]
        for r in responses:  # run long enough to encounter change_indices
            tr.respond(r)
        assert_equal(len(w), 1)
    assert(tr.check_valid(1))  # make sure checking validity is good
    assert(not tr.check_valid(3))
    assert_raises(ValueError, tr.threshold, 1)
    tr.threshold(3)
    assert_true(tr.n_trials == tr.stop_trials)

    # run tests with ignore too--should generate warnings, but no error
    with warnings.catch_warnings(record=True) as w:
        tr = TrackerUD(None, 1, 1, 0.75, 0.25, np.inf, 8, 1,
                       x_min=0, x_max=2, repeat_limit='ignore')
        responses = [False, True, False, False, True, True, False, True]
        for r in responses:  # run long enough to encounter change_indices<commit_msg>assert equal<commit_after>import numpy as np
import matplotlib
matplotlib.use('Agg')  # noqa
from expyfun.stimuli import TrackerUD, TrackerBinom, TrackerDealer
from expyfun import ExperimentController
from nose.tools import assert_raises, assert_equal
from expyfun._utils import _hide_window, requires_opengl21
import warnings


def callback(event_type, value=None, timestamp=None):
    print(event_type, value, timestamp)


std_kwargs = dict(output_dir=None, full_screen=False, window_size=(1, 1),
                  participant='foo', session='01', stim_db=0.0, noise_db=0.0,
                  trigger_controller='dummy', response_device='keyboard',
                  audio_controller='pyglet',
                  verbose=True, version='dev')


@_hide_window
@requires_opengl21
def test_tracker_ud():
    """Test TrackerUD"""
    import matplotlib.pyplot as plt
    tr = TrackerUD(callback, 3, 1, 1, 1, np.inf, 10, 1)
    with ExperimentController('test', **std_kwargs) as ec:
        tr = TrackerUD(ec, 3, 1, 1, 1, np.inf, 10, 1)
    tr = TrackerUD(None, 3, 1, 1, 1, 10, np.inf, 1)
    rand = np.random.RandomState(0)
    while not tr.stopped:
        tr.respond(rand.rand() < tr.x_current)
    assert_equal(tr.n_reversals, tr.stop_reversals)

    tr = TrackerUD(None, 3, 1, 1, 1, np.inf, 10, 1)
    tr.threshold()
    rand = np.random.RandomState(0)
    while not tr.stopped:
        tr.respond(rand.rand() < tr.x_current)
    # test responding after stopped
    assert_raises(RuntimeError, tr.respond, 0)

    # all the properties better work
    tr.up
    tr.down
    tr.step_size_up
    tr.step_size_down
    tr.stop_reversals
    tr.stop_trials
    tr.start_value
    tr.x_min
    tr.x_max
    tr.stopped
    tr.x
    tr.responses
    tr.n_trials
    tr.n_reversals
    tr.reversals
    tr.reversal_inds
    fig, ax, lines = tr.plot()
    tr.plot_thresh(ax=ax)
    tr.plot_thresh()
    plt.close(fig)
    ax = plt.axes()
    fig, ax, lines = tr.plot(ax)
    plt.close(fig)
    tr.threshold()
    tr.check_valid(2)

    # bad callback type
    assert_raises(TypeError, TrackerUD, 'foo', 3, 1, 1, 1, 10, np.inf, 1)

    # test dynamic step size and error conditions
    tr = TrackerUD(None, 3, 1, [1, 0.5], [1, 0.5], 10, np.inf, 1,
                   change_indices=[2])
    tr.respond(True)

    with warnings.catch_warnings(record=True) as w:
        tr = TrackerUD(None, 1, 1, 0.75, 0.75, np.inf, 9, 1,
                       x_min=0, x_max=2)
        responses = [True, True, True, False, False, False, False, True, False]
        for r in responses:  # run long enough to encounter change_indices
            tr.respond(r)
        assert_equal(len(w), 1)
    assert(tr.check_valid(1))  # make sure checking validity is good
    assert(not tr.check_valid(3))
    assert_raises(ValueError, tr.threshold, 1)
    tr.threshold(3)
    assert_equal(tr.n_trials, tr.stop_trials)

    # run tests with ignore too--should generate warnings, but no error
    with warnings.catch_warnings(record=True) as w:
        tr = TrackerUD(None, 1, 1, 0.75, 0.25, np.inf, 8, 1,
                       x_min=0, x_max=2, repeat_limit='ignore')
        responses = [False, True, False, False, True, True, False, True]
        for r in responses:  # run long enough to encounter change_indices
{'fraction_non_alphanumeric': 0.11643835616438356, 'fraction_numerical': 0.028006088280060883, 'mean_word_length': 2.835960303561004, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '508872', 'n_tokens_mistral': 2462, 'n_tokens_neox': 2291, 'n_words': 589}