예제 #1
0
def test_result():
    "Test boosting results"
    ds = datasets._get_continuous()

    # 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)

    # 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)
예제 #2
0
def test_boosting():
    "Test boosting NDVars"
    ds = datasets._get_continuous()

    # test boosting results
    yield run_boosting, ds, True
    yield run_boosting, ds, False
예제 #3
0
def test_cwt():
    ds = datasets._get_continuous()
    # 1d
    y = cwt_morlet(ds['x1'], [4, 6, 8])
    assert y.ndim == 2
    # 2d
    y = cwt_morlet(ds['x2'], [4, 6, 8])
    assert y.ndim == 3
예제 #4
0
def test_cwt():
    ds = datasets._get_continuous()
    # 1d
    y = cwt_morlet(ds['x1'], [4, 6, 8])
    assert y.ndim == 2
    # 2d
    y = cwt_morlet(ds['x2'], [4, 6, 8])
    assert y.ndim == 3
예제 #5
0
def test_boosting():
    "Test boosting NDVars"
    ds = datasets._get_continuous()

    # test boosting results
    configure(n_workers=0)
    yield run_boosting, ds
    configure(n_workers=True)
    yield run_boosting, ds
예제 #6
0
def test_cross_correlation():
    ds = datasets._get_continuous()
    x = ds['x1']

    assert cross_correlation(x, x).argmax() == 0
    assert cross_correlation(x[2:], x).argmax() == 0
    assert cross_correlation(x[:9], x).argmax() == 0
    assert cross_correlation(x, x[1:]).argmax() == 0
    assert cross_correlation(x, x[:8]).argmax() == 0
    assert cross_correlation(x[2:], x[:8]).argmax() == 0
예제 #7
0
def test_cross_correlation():
    ds = datasets._get_continuous()
    x = ds['x1']

    eq_(cross_correlation(x, x).argmax(), 0)
    eq_(cross_correlation(x[2:], x).argmax(), 0)
    eq_(cross_correlation(x[:9], x).argmax(), 0)
    eq_(cross_correlation(x, x[1:]).argmax(), 0)
    eq_(cross_correlation(x, x[:8]).argmax(), 0)
    eq_(cross_correlation(x[2:], x[:8]).argmax(), 0)
예제 #8
0
def test_cross_correlation():
    ds = datasets._get_continuous()
    x = ds['x1']

    eq_(cross_correlation(x, x).argmax(), 0)
    eq_(cross_correlation(x[2:], x).argmax(), 0)
    eq_(cross_correlation(x[:9], x).argmax(), 0)
    eq_(cross_correlation(x, x[1:]).argmax(), 0)
    eq_(cross_correlation(x, x[:8]).argmax(), 0)
    eq_(cross_correlation(x[2:], x[:8]).argmax(), 0)
예제 #9
0
def test_boosting(n_workers):
    "Test boosting NDVars"
    ds = datasets._get_continuous()
    configure(n_workers=n_workers)

    y = ds['y']
    x1 = ds['x1']
    x2 = ds['x2']
    y_mean = y.mean()
    x2_mean = x2.mean()

    # test values from running function, not verified independently
    res = boosting(y, x1 * 2000, 0, 1, scale_data=False, mindelta=0.0025)
    assert repr(
        res) == '<boosting y ~ x1, 0 - 1, scale_data=False, mindelta=0.0025>'
    assert res.r == approx(0.75, abs=0.001)
    assert res.y_mean is None
    assert res.h.info['unit'] == 'V'
    assert res.h_scaled.info['unit'] == 'V'

    res = boosting(y, x1, 0, 1)
    assert repr(res) == '<boosting y ~ x1, 0 - 1>'
    assert res.r == approx(0.83, abs=0.001)
    assert res.y_mean == y_mean
    assert res.y_scale == y.std()
    assert res.x_mean == x1.mean()
    assert res.x_scale == x1.std()
    assert res.h.info['unit'] == 'normalized'
    assert res.h_scaled.info['unit'] == 'V'
    # inplace
    res_ip = boosting(y.copy(), x1.copy(), 0, 1, 'inplace')
    assert_res_equal(res_ip, res)
    # persistence
    res_p = pickle.loads(pickle.dumps(res, pickle.HIGHEST_PROTOCOL))
    assert_res_equal(res_p, res)

    res = boosting(y, x2, 0, 1)
    assert res.r == approx(0.601, abs=0.001)

    res = boosting(y, x2, 0, 1, error='l1')
    assert res.r == approx(0.553, abs=0.001)
    assert res.y_mean == y.mean()
    assert res.y_scale == (y - y_mean).abs().mean()
    assert res.x_mean == x2_mean
    assert res.x_scale == (x2 - x2_mean).abs().mean()

    # 2 predictors
    res = boosting(y, [x1, x2], 0, 1)
    assert res.r == approx(0.947, abs=0.001)
    # selective stopping
    res = boosting(y, [x1, x2], 0, 1, selective_stopping=1)
    assert res.r == approx(0.967, abs=0.001)
    res = boosting(y, [x1, x2], 0, 1, selective_stopping=2)
    assert res.r == approx(0.992, abs=0.001)
예제 #10
0
def test_boosting(n_workers):
    "Test boosting NDVars"
    ds = datasets._get_continuous()
    configure(n_workers=n_workers)

    y = ds['y']
    x1 = ds['x1']
    x2 = ds['x2']
    y_mean = y.mean()
    x2_mean = x2.mean()

    # test values from running function, not verified independently
    res = boosting(y, x1 * 2000, 0, 1, scale_data=False, mindelta=0.0025)
    assert repr(res) == '<boosting y ~ x1, 0 - 1, scale_data=False, mindelta=0.0025>'
    assert res.r == approx(0.75, abs=0.001)
    assert res.y_mean is None
    assert res.h.info['unit'] == 'V'
    assert res.h_scaled.info['unit'] == 'V'

    res = boosting(y, x1, 0, 1)
    assert repr(res) == '<boosting y ~ x1, 0 - 1>'
    assert res.r == approx(0.83, abs=0.001)
    assert res.y_mean == y_mean
    assert res.y_scale == y.std()
    assert res.x_mean == x1.mean()
    assert res.x_scale == x1.std()
    assert res.h.info['unit'] == 'normalized'
    assert res.h_scaled.info['unit'] == 'V'
    # inplace
    res_ip = boosting(y.copy(), x1.copy(), 0, 1, 'inplace')
    assert_res_equal(res_ip, res)
    # persistence
    res_p = pickle.loads(pickle.dumps(res, pickle.HIGHEST_PROTOCOL))
    assert_res_equal(res_p, res)

    res = boosting(y, x2, 0, 1)
    assert res.r == approx(0.601, abs=0.001)

    res = boosting(y, x2, 0, 1, error='l1')
    assert res.r == approx(0.553, abs=0.001)
    assert res.y_mean == y.mean()
    assert res.y_scale == (y - y_mean).abs().mean()
    assert res.x_mean == x2_mean
    assert res.x_scale == (x2 - x2_mean).abs().mean()

    # 2 predictors
    res = boosting(y, [x1, x2], 0, 1)
    assert res.r == approx(0.947, abs=0.001)
    # selective stopping
    res = boosting(y, [x1, x2], 0, 1, selective_stopping=1)
    assert res.r == approx(0.967, abs=0.001)
    res = boosting(y, [x1, x2], 0, 1, selective_stopping=2)
    assert res.r == approx(0.992, abs=0.001)
예제 #11
0
def test_convolve():
    ds = datasets._get_continuous()

    h1 = ds['h1']
    h2 = ds['h2']
    x1 = ds['x1']

    xc = convolve(h1, x1)
    xc_np = np.convolve(h1.x, x1.x)
    assert_array_equal(xc.x, xc_np[:100])

    # add dimension through kernel
    xc = convolve(h2, x1)
    xc_np = np.vstack(
        (np.convolve(h2.x[0], x1.x)[:100], np.convolve(h2.x[1], x1.x)[:100]))
    assert_array_equal(xc.x, xc_np)
예제 #12
0
def test_plot_array():
    "Test plot.Array"
    ds = datasets.get_uts(utsnd=True)
    p = plot.Array('utsnd', ds=ds)
    p.close()
    p = plot.Array('utsnd', 'A%B', ds=ds)
    assert p._layout.nax == 4
    p.close()
    p = plot.Array('utsnd', 'A', sub='B=="b1"', ds=ds)
    assert p._layout.nax == 2
    p.close()

    # Categorial dimension
    ds = datasets._get_continuous()
    p = plot.Array(ds['x2'], interpolation='none')
    assert len(p.figure.axes[0].get_yticks()) == 2
예제 #13
0
def test_plot_array():
    "Test plot.Array"
    ds = datasets.get_uts(utsnd=True)
    p = plot.Array('utsnd', ds=ds, show=False)
    p.close()
    p = plot.Array('utsnd', 'A%B', ds=ds, show=False)
    eq_(p._layout.nax, 4)
    p.close()
    p = plot.Array('utsnd', 'A', sub='B=="b1"', ds=ds, show=False)
    eq_(p._layout.nax, 2)
    p.close()

    # Categorial dimension
    ds = datasets._get_continuous()
    p = plot.Array(ds['x2'], interpolation='none', show=False)
    eq_(len(p.figure.axes[0].get_yticks()), 2)
예제 #14
0
def test_convolve():
    # convolve is also tested in test_boosting.py
    ds = datasets._get_continuous()

    h1 = ds['h1']
    h2 = ds['h2']
    x1 = ds['x1']

    xc = convolve(h1, x1)
    xc_np = np.convolve(h1.x, x1.x)
    assert_array_equal(xc.x, xc_np[:100])

    # add dimension through kernel
    xc = convolve(h2, x1)
    xc_np = np.vstack((np.convolve(h2.x[0], x1.x)[:100],
                       np.convolve(h2.x[1], x1.x)[:100]))
    assert_array_equal(xc.x, xc_np)
예제 #15
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']])
    assert_dataobj_equal(y, ds['y'], name=False)

    # 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)
    # reconstruction
    res = boosting(x1, y, -1, 0, debug=True)
    x1r = convolve(res.h_scaled, y)
    assert correlation_coefficient(res.y_pred, x1r) > .999
    assert correlation_coefficient(x1r[0.9:], x1[0.9:]) == approx(res.r,
                                                                  abs=1e-3)

    # test NaN checks  (modifies data)
    ds['x2'].x[1, 50] = np.nan
    with pytest.raises(ValueError):
        boosting(ds['y'], ds['x2'], 0, .5)
    with pytest.raises(ValueError):
        boosting(ds['y'], ds['x2'], 0, .5, False)
    ds['x2'].x[1, :] = 1
    with catch_warnings():
        filterwarnings('ignore', category=RuntimeWarning)
        with pytest.raises(ValueError):
            boosting(ds['y'], ds['x2'], 0, .5)
        ds['y'].x[50] = np.nan
        with pytest.raises(ValueError):
            boosting(ds['y'], ds['x1'], 0, .5)
        with pytest.raises(ValueError):
            boosting(ds['y'], ds['x1'], 0, .5, False)
예제 #16
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']])
    assert_dataobj_equal(y, ds['y'], name=False)

    # 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)
    # reconstruction
    res = boosting(x1, y, -1, 0, debug=True)
    x1r = convolve(res.h_scaled, y)
    assert correlation_coefficient(res.y_pred, x1r) > .999
    assert correlation_coefficient(x1r[0.9:], x1[0.9:]) == approx(res.r, abs=1e-3)

    # test NaN checks  (modifies data)
    ds['x2'].x[1, 50] = np.nan
    with pytest.raises(ValueError):
        boosting(ds['y'], ds['x2'], 0, .5)
    with pytest.raises(ValueError):
        boosting(ds['y'], ds['x2'], 0, .5, False)
    ds['x2'].x[1, :] = 1
    with catch_warnings():
        filterwarnings('ignore', category=RuntimeWarning)
        with pytest.raises(ValueError):
            boosting(ds['y'], ds['x2'], 0, .5)
        ds['y'].x[50] = np.nan
        with pytest.raises(ValueError):
            boosting(ds['y'], ds['x1'], 0, .5)
        with pytest.raises(ValueError):
            boosting(ds['y'], ds['x1'], 0, .5, False)
예제 #17
0
def test_boosting(n_workers):
    "Test boosting NDVars"
    ds = datasets._get_continuous(ynd=True)
    configure(n_workers=n_workers)

    y = ds['y']
    ynd = ds['ynd']
    x1 = ds['x1']
    x2 = ds['x2']
    y_mean = y.mean()
    x2_mean = x2.mean('time')

    # test values from running function, not verified independently
    res = boosting(y, x1 * 2000, 0, 1, scale_data=False, mindelta=0.0025)
    assert repr(
        res) == '<boosting y ~ x1, 0 - 1, scale_data=False, mindelta=0.0025>'
    assert res.r == approx(0.75, abs=0.001)
    assert res.y_mean is None
    assert res.h.info['unit'] == 'V'
    assert res.h_scaled.info['unit'] == 'V'
    with pytest.raises(NotImplementedError):
        res.proportion_explained

    res = boosting(y, x1, 0, 1)
    assert repr(res) == '<boosting y ~ x1, 0 - 1>'
    assert res.r == approx(0.83, abs=0.001)
    assert res.y_mean == y_mean
    assert res.y_scale == y.std()
    assert res.x_mean == x1.mean()
    assert res.x_scale == x1.std()
    assert res.h.name == 'x1'
    assert res.h.info['unit'] == 'normalized'
    assert res.h_scaled.name == 'x1'
    assert res.h_scaled.info['unit'] == 'V'
    assert res.proportion_explained == approx(0.506, abs=0.001)
    # inplace
    res_ip = boosting(y.copy(), x1.copy(), 0, 1, 'inplace')
    assert_res_equal(res_ip, res)
    # persistence
    res_p = pickle.loads(pickle.dumps(res, pickle.HIGHEST_PROTOCOL))
    assert_res_equal(res_p, res)

    res = boosting(y, x2, 0, 1)
    assert res.r == approx(0.601, abs=0.001)
    assert res.proportion_explained == approx(0.273, abs=0.001)

    res = boosting(y, x2, 0, 1, error='l1')
    assert res.r == approx(0.553, abs=0.001)
    assert res.y_mean == y.mean()
    assert res.y_scale == (y - y_mean).abs().mean()
    assert_array_equal(res.x_mean.x, x2_mean)
    assert_array_equal(res.x_scale, (x2 - x2_mean).abs().mean('time'))
    assert res.proportion_explained == approx(0.123, abs=0.001)

    # 2 predictors
    res = boosting(y, [x1, x2], 0, 1)
    assert res.r == approx(0.947, abs=0.001)
    # selective stopping
    res = boosting(y, [x1, x2], 0, 1, selective_stopping=1)
    assert res.r == approx(0.967, abs=0.001)
    res = boosting(y, [x1, x2], 0, 1, selective_stopping=2)
    assert res.r == approx(0.992, abs=0.001)

    # prefit
    res_full = boosting(y, [x1, x2], 0, 1)
    prefit = boosting(y, x1, 0, 1)
    res = boosting(y, [x1, x2], 0, 1, prefit=prefit)
    assert correlation_coefficient(res.h, res_full.h[1]) == approx(0.984, 1e-3)
    prefit = boosting(y, x2, 0, 1)
    res = boosting(y, [x1, x2], 0, 1, prefit=prefit)
    assert correlation_coefficient(res.h, res_full.h[0]) == approx(0.995, 1e-3)
    # ynd
    res_full = boosting(ynd, [x1, x2], 0, 1)
    prefit = boosting(ynd, x1, 0, 1)
    res = boosting(ynd, [x1, x2], 0, 1, prefit=prefit)
    assert correlation_coefficient(res.h, res_full.h[1]) == approx(0.978, 1e-3)
    prefit = boosting(ynd, x2, 0, 1)
    res = boosting(ynd, [x1, x2], 0, 1, prefit=prefit)
    assert correlation_coefficient(res.h, res_full.h[0]) == approx(0.997, 1e-3)