Beispiel #1
0
def test_corr():
    "Test testnd.corr()"
    ds = datasets.get_uts(True)

    # add correlation
    Y = ds['Y']
    utsnd = ds['utsnd']
    utsnd.x[:, 3:5, 50:65] += Y.x[:, None, None]

    res = testnd.corr('utsnd', 'Y', ds=ds)
    repr(res)
    for s, t in product('01234', (0.1, 0.2, 0.35)):
        target = test.Correlation(utsnd.sub(sensor=s, time=t), Y).r
        assert_almost_equal(res.r.sub(sensor=s, time=t), target, 10)
    res = testnd.corr('utsnd', 'Y', 'rm', ds=ds)
    repr(res)
    res = testnd.corr('utsnd', 'Y', ds=ds, samples=10, pmin=0.05)
    repr(res)
    res = testnd.corr('utsnd', 'Y', ds=ds, samples=10, tfce=True)
    repr(res)

    # persistence
    string = pickle.dumps(res, protocol=pickle.HIGHEST_PROTOCOL)
    res_ = pickle.loads(string)
    assert_equal(repr(res_), repr(res))
    assert_dataobj_equal(res.p_uncorrected, res_.p_uncorrected)
    assert_dataobj_equal(res.p, res_.p)
Beispiel #2
0
def test_result():
    "Test boosting results"
    ds = datasets._get_continuous()
    x1 = ds['x1']

    # convolve function
    y = convolve([ds['h1'], ds['h2']], [ds['x1'], ds['x2']])
    y.name = 'y'
    assert_dataobj_equal(y, ds['y'])

    # test prediction with res.h and res.h_scaled
    res = boosting(ds['y'], ds['x1'], 0, 1)
    y1 = convolve(res.h_scaled, ds['x1'])
    x_scaled = ds['x1'] / res.x_scale
    y2 = convolve(res.h, x_scaled)
    y2 *= res.y_scale
    y2 += y1.mean() - y2.mean()  # mean can't be reconstructed
    assert_dataobj_equal(y1, y2, decimal=12)
    # reconstriction
    res = boosting(x1, y, -1, 0)
    x1r = convolve(res.h_scaled, y[:9.1])
    assert_almost_equal(test.Correlation(x1r, x1[:9.1]).r, res.r, 2)

    # test NaN checks  (modifies data)
    ds['x2'].x[1, 50] = np.nan
    assert_raises(ValueError, boosting, ds['y'], ds['x2'], 0, .5)
    assert_raises(ValueError, boosting, ds['y'], ds['x2'], 0, .5, False)
    ds['x2'].x[1, :] = 1
    assert_raises(ValueError, boosting, ds['y'], ds['x2'], 0, .5)
    ds['y'].x[50] = np.nan
    assert_raises(ValueError, boosting, ds['y'], ds['x1'], 0, .5)
    assert_raises(ValueError, boosting, ds['y'], ds['x1'], 0, .5, False)
Beispiel #3
0
def test_correlations():
    "Test test.correlations()"
    ds = datasets.get_uv()

    res = test.correlations('fltvar', 'fltvar2', ds=ds)
    print(res)
    assert str(res[2][0]).strip() == '.398'
    res = test.correlations('fltvar', 'fltvar2', ds=ds, asds=True)
    assert res[0, 'r'] == pytest.approx(.398, abs=1e-3)
    res = test.Correlation('fltvar', 'fltvar2', ds=ds)
    assert res.r == pytest.approx(.398, abs=1e-3)

    res = test.correlations('fltvar', 'fltvar2', 'A', ds=ds)
    print(res)
    assert str(res[2][0]).strip() == 'a1'
    assert str(res[2][1]).strip() == '-.149'
    assert str(res[3][1]).strip() == '.740'
    res = test.correlations('fltvar', 'fltvar2', 'A', ds=ds, asds=True)
    assert res[0, 'A'] == 'a1'
    assert res[0, 'r'] == pytest.approx(-0.149, abs=1e-3)
    assert res[1, 'r'] == pytest.approx(.740, abs=1e-3)
    res = test.Correlation('fltvar', 'fltvar2', "A == 'a1'", ds)
    assert res.r == pytest.approx(-0.149, abs=1e-3)

    res = test.correlations('fltvar', 'fltvar2', 'A%B', ds=ds)
    print(res)
    assert str(res[2][2]).strip() == '-.276'
    res = test.correlations('fltvar', 'fltvar2', 'A%B', ds=ds, asds=True)
    assert res[0, 'r'] == pytest.approx(-0.276, abs=1e-3)

    res = test.correlations('fltvar', ('fltvar2', 'intvar'), 'A%B', ds=ds)
    print(res)
    assert str(res[2][1]).strip() == 'a1'
    assert str(res[2][2]).strip() == 'b1'
    assert str(res[2][3]).strip() == '-.276'
    res = test.correlations('fltvar', ('fltvar2', 'intvar'),
                            'A%B',
                            ds=ds,
                            asds=True)
    assert res[0, 'r'] == pytest.approx(-0.276, abs=1e-3)
    res = test.Correlation('fltvar', 'intvar', "(A=='a1')&(B=='b1')", ds)
    assert res.r == pytest.approx(0.315, abs=1e-3)
Beispiel #4
0
def test_correlations():
    "Test test.correlations()"
    ds = datasets.get_uv()

    res = test.correlations('fltvar', 'fltvar2', ds=ds)
    print(res)
    eq_(str(res[2][0]).strip(), '.398')
    res = test.correlations('fltvar', 'fltvar2', ds=ds, asds=True)
    assert_almost_equal(res[0, 'r'], .398, 3)
    res = test.Correlation('fltvar', 'fltvar2', ds=ds)
    assert_almost_equal(res.r, .398, 3)

    res = test.correlations('fltvar', 'fltvar2', 'A', ds=ds)
    print(res)
    eq_(str(res[2][0]).strip(), 'a1')
    eq_(str(res[2][1]).strip(), '-.149')
    eq_(str(res[3][1]).strip(), '.740')
    res = test.correlations('fltvar', 'fltvar2', 'A', ds=ds, asds=True)
    eq_(res[0, 'A'], 'a1')
    assert_almost_equal(res[0, 'r'], -0.149, 3)
    assert_almost_equal(res[1, 'r'], .740, 3)
    res = test.Correlation('fltvar', 'fltvar2', "A == 'a1'", ds)
    assert_almost_equal(res.r, -0.149, 3)

    res = test.correlations('fltvar', 'fltvar2', 'A%B', ds=ds)
    print(res)
    eq_(str(res[2][2]).strip(), '-.276')
    res = test.correlations('fltvar', 'fltvar2', 'A%B', ds=ds, asds=True)
    assert_almost_equal(res[0, 'r'], -0.276, 3)

    res = test.correlations('fltvar', ('fltvar2', 'intvar'), 'A%B', ds=ds)
    print(res)
    eq_(str(res[2][1]).strip(), 'a1')
    eq_(str(res[2][2]).strip(), 'b1')
    eq_(str(res[2][3]).strip(), '-.276')
    res = test.correlations('fltvar', ('fltvar2', 'intvar'), 'A%B', ds=ds, asds=True)
    assert_almost_equal(res[0, 'r'], -0.276, 3)
    res = test.Correlation('fltvar', 'intvar', "(A=='a1')&(B=='b1')", ds)
    assert_almost_equal(res.r, 0.315, 3)