コード例 #1
0
def test_detrend(show=False):
    """Test detrending."""
    # basic
    x = np.arange(100)[:, None]
    x = x + np.random.randn(*x.shape)
    y, _, _ = detrend(x, 1)

    assert y.shape == x.shape

    # detrend biased random walk
    x = np.cumsum(np.random.randn(1000, 1) + 0.1)
    y, _, _ = detrend(x, 3)

    assert y.shape == x.shape

    # weights
    trend = np.linspace(0, 100, 1000)[:, None]
    data = 3 * np.random.randn(*trend.shape)
    x = trend + data
    x[:100, :] = 100
    w = np.ones(x.shape)
    w[:100, :] = 0
    y, _, _ = detrend(x, 3, None, show=show)
    yy, _, _ = detrend(x, 3, w, show=show)

    assert y.shape == x.shape
    assert yy.shape == x.shape
コード例 #2
0
def test_detrend(show=False):
    """Test detrending."""
    # basic
    x = np.arange(100)[:, None]  # trend
    source = np.random.randn(*x.shape)
    x = x + source
    y, _, _ = detrend(x, 1)

    assert y.shape == x.shape

    # detrend biased random walk
    x = np.cumsum(np.random.randn(1000, 1) + 0.1)
    y, _, _ = detrend(x, 3)

    assert y.shape == x.shape

    # test weights
    trend = np.linspace(0, 100, 1000)[:, None]
    data = 3 * np.random.randn(*trend.shape)
    data[:100, :] = 100
    x = trend + data
    w = np.ones(x.shape)
    w[:100, :] = 0

    # Without weights – detrending fails
    y, _, _ = detrend(x, 3, None, show=show)

    # With weights – detrending works
    yy, _, _ = detrend(x, 3, w, show=show)

    assert y.shape == x.shape
    assert yy.shape == x.shape
    assert np.all(np.abs(yy[100:] - data[100:]) < 1.)

    # detrend higher-dimensional data
    x = np.cumsum(np.random.randn(1000, 16) + 0.1, axis=0)
    y, _, _ = detrend(x, 1, show=False)

    # detrend higher-dimensional data with order 3 polynomial
    x = np.cumsum(np.random.randn(1000, 16) + 0.1, axis=0)
    y, _, _ = detrend(x, 3, basis='polynomials', show=True)

    # detrend with sinusoids
    x = np.random.randn(1000, 2)
    x += 2 * np.sin(2 * np.pi * np.arange(1000) / 200)[:, None]
    y, _, _ = detrend(x, 5, basis='sinusoids', show=True)
コード例 #3
0
plt.figure(4)
plt.plot(x, label='data', color='C0')
plt.plot(y, ls=':', label='fit', color='C1')
plt.title('Channel-wise regression')
plt.legend()

###############################################################################
# Detrending
# =============================================================================

###############################################################################
# Basic example with a linear trend
# -----------------------------------------------------------------------------
x = np.arange(100)[:, None]
x = x + np.random.randn(*x.shape)
y, _, _ = detrend(x, 1)

plt.figure(5)
plt.plot(x, label='original')
plt.plot(y, label='detrended')
plt.legend()

###############################################################################
# Detrend biased random walk with a third-order polynomial
# -----------------------------------------------------------------------------
x = np.cumsum(np.random.randn(1000, 1) + 0.1)
y, _, _ = detrend(x, 3)

plt.figure(6)
plt.plot(x, label='original')
plt.plot(y, label='detrended')