<commit_before>from nose.tools import assert_raises, assert_equal, assert_true
from numpy.testing import assert_allclose
try:
    from scipy.special import logit as splogit
except ImportError:
    splogit = None
import numpy as np
import warnings

import expyfun.analyze as ea

warnings.simplefilter('always')


def test_dprime():
    """Test dprime and dprime_2afc accuracy
    """
    assert_raises(TypeError, ea.dprime, 'foo', 0, 0, 0)
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        ea.dprime((1.1, 0, 0, 0))
    assert_equal(len(w), 1)
    assert_equal(0, ea.dprime((1, 0, 1, 0)))
    assert_equal(np.inf, ea.dprime((1, 0, 2, 1), False))
    assert_equal(ea.dprime((5, 0, 1, 0)), ea.dprime_2afc((5, 1)))
    assert_raises(ValueError, ea.dprime, np.ones((5, 4, 3)))
    assert_raises(ValueError, ea.dprime, (1, 2, 3))
    assert_raises(ValueError, ea.dprime_2afc, (1, 2, 3))
    assert_equal(np.sum(ea.dprime_2afc([[5, 1], [1, 5]])), 0)


def test_logit():
    """Test logit calculations
    """
    assert_raises(ValueError, ea.logit, 2)
    # On some versions, this throws warnings about divide-by-zero
    with warnings.catch_warnings(record=True):
        warnings.simplefilter('always')
        assert_equal(ea.logit(0), -np.inf)
        assert_equal(ea.logit(1), np.inf)
    assert_true(ea.logit(1, max_events=1) < np.inf)
    assert_equal(ea.logit(0.5), 0)
    if splogit is not None:
        # Travis doesn't support scipy.special.logit, but this passes locally:
        foo = np.random.rand(5)
        assert_allclose(ea.logit(foo), splogit(foo))
    foo = np.array([[0, 0.5, 1], [1, 0.5, 0]])
    bar = np.ones_like(foo).astype(int)
    assert_true(np.all(np.equal(ea.logit(foo, 1), np.zeros_like(foo))))
    assert_true(np.all(np.equal(ea.logit(foo, [1, 1, 1]), np.zeros_like(foo))))
    assert_true(np.all(np.equal(ea.logit(foo, bar), np.zeros_like(foo))))
    assert_raises(ValueError, ea.logit, foo, [1, 1])  # can't broadcast


def test_sigmoid():
    """Test sigmoidal fitting and generation
    """
    n_pts = 1000
    x = np.random.randn(n_pts)
    p0 = (0., 1., 0., 1.)
    y = ea.sigmoid(x, *p0)
    assert_true(np.all(np.logical_and(y <= 1, y >= 0)))
    p = ea.fit_sigmoid(x, y)
    assert_allclose(p, p0, atol=1e-4, rtol=1e-4)

    y += np.random.rand(n_pts) * 0.01
    p = ea.fit_sigmoid(x, y)
    assert_allclose(p, p0, atol=0.1, rtol=0.1)<commit_msg>test for rt-chisq<commit_after>from nose.tools import assert_raises, assert_equal, assert_true
from numpy.testing import assert_allclose
try:
    from scipy.special import logit as splogit
except ImportError:
    splogit = None
import numpy as np
import warnings

import expyfun.analyze as ea

warnings.simplefilter('always')


def test_dprime():
    """Test dprime and dprime_2afc accuracy
    """
    assert_raises(TypeError, ea.dprime, 'foo', 0, 0, 0)
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        ea.dprime((1.1, 0, 0, 0))
    assert_equal(len(w), 1)
    assert_equal(0, ea.dprime((1, 0, 1, 0)))
    assert_equal(np.inf, ea.dprime((1, 0, 2, 1), False))
    assert_equal(ea.dprime((5, 0, 1, 0)), ea.dprime_2afc((5, 1)))
    assert_raises(ValueError, ea.dprime, np.ones((5, 4, 3)))
    assert_raises(ValueError, ea.dprime, (1, 2, 3))
    assert_raises(ValueError, ea.dprime_2afc, (1, 2, 3))
    assert_equal(np.sum(ea.dprime_2afc([[5, 1], [1, 5]])), 0)


def test_logit():
    """Test logit calculations
    """
    assert_raises(ValueError, ea.logit, 2)
    # On some versions, this throws warnings about divide-by-zero
    with warnings.catch_warnings(record=True):
        warnings.simplefilter('always')
        assert_equal(ea.logit(0), -np.inf)
        assert_equal(ea.logit(1), np.inf)
    assert_true(ea.logit(1, max_events=1) < np.inf)
    assert_equal(ea.logit(0.5), 0)
    if splogit is not None:
        # Travis doesn't support scipy.special.logit, but this passes locally:
        foo = np.random.rand(5)
        assert_allclose(ea.logit(foo), splogit(foo))
    foo = np.array([[0, 0.5, 1], [1, 0.5, 0]])
    bar = np.ones_like(foo).astype(int)
    assert_true(np.all(np.equal(ea.logit(foo, 1), np.zeros_like(foo))))
    assert_true(np.all(np.equal(ea.logit(foo, [1, 1, 1]), np.zeros_like(foo))))
    assert_true(np.all(np.equal(ea.logit(foo, bar), np.zeros_like(foo))))
    assert_raises(ValueError, ea.logit, foo, [1, 1])  # can't broadcast


def test_sigmoid():
    """Test sigmoidal fitting and generation
    """
    n_pts = 1000
    x = np.random.randn(n_pts)
    p0 = (0., 1., 0., 1.)
    y = ea.sigmoid(x, *p0)
    assert_true(np.all(np.logical_and(y <= 1, y >= 0)))
    p = ea.fit_sigmoid(x, y)
    assert_allclose(p, p0, atol=1e-4, rtol=1e-4)

    y += np.random.rand(n_pts) * 0.01
    p = ea.fit_sigmoid(x, y)
    assert_allclose(p, p0, atol=0.1, rtol=0.1)


def test_rt_chisq():
    """Test reaction time chi-square fitting
    """
    # 1D should return single float
    foo = np.random.rand(30)
    assert_equal(np.array(ea.rt_chisq(foo)).shape, ())
    # 2D should return as many values as length of axis passed in
    foo = np.random.rand(30).reshape((2, 3, 5))
    for axis in range(foo.ndim):
        bar = ea.rt_chisq(foo, axis=axis)
        assert_equal(foo.shape[axis], len(bar))

{'fraction_non_alphanumeric': 0.16025641025641027, 'fraction_numerical': 0.035633484162895926, 'mean_word_length': 3.480574324324324, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '3426363', 'n_tokens_mistral': 2242, 'n_tokens_neox': 2129, 'n_words': 451}