Exemplo n.º 1
0
def test_continuous_inplace_mutation():
    from rockpool import TSContinuous

    ts1 = TSContinuous([0, 1, 2], [0, 1, 2])

    # - Delay
    ts1.delay(1, inplace=True)
    assert ts1.t_start == 1

    # - Resample
    ts1.resample([0.125, 1.1, 1.9], inplace=True)
    assert ts1.t_start == 0.125

    # - Merge
    ts1 = TSContinuous([0, 1, 2], [0, 1, 2])
    ts2 = TSContinuous([0, 1, 2], [1, 2, 3])
    ts1.merge(ts2, remove_duplicates=True, inplace=True)
    assert np.size(ts1.samples) == 3

    ts3 = ts1.merge(ts2, remove_duplicates=False, inplace=True)
    assert np.size(ts3.samples) == 6

    # - Append
    ts1 = TSContinuous([0, 1, 2], [0, 1, 2])
    ts2 = TSContinuous([0, 1, 2], [1, 2, 3])
    ts1.append_t(ts2, inplace=True)
    assert np.size(ts1.times) == 6

    ts1 = TSContinuous([0, 1, 2], [0, 1, 2])
    ts2 = TSContinuous([0, 1, 2], [1, 2, 3])
    ts1.append_c(ts2, inplace=True)
    assert ts1.num_channels == 2

    # - clip
    ts1.clip(0.5, 1.5, inplace=True)
    assert ts1.t_start == 0.5

    # - Start at 0
    ts1.start_at_zero(inplace=True)
    assert ts1.t_start == 0
Exemplo n.º 2
0
def test_continuous_clip():
    from rockpool import TSContinuous

    # - Generate series
    times = np.arange(1, 6) * 0.1
    samples = np.arange(5).reshape(-1, 1) + np.arange(2) * 2
    ts = TSContinuous(times, samples)
    ts_empty = TSContinuous()

    # - Clip ts in time
    assert (ts.clip(0.2, 0.4, include_stop=True).times == times[1:4]).all()
    assert (ts.clip(0.2, 0.4, include_stop=True).samples == samples[1:4]).all()
    assert (ts.clip(0.2, 0.4, include_stop=False).times == times[1:3]).all()
    assert (ts.clip(0.2, 0.4,
                    include_stop=False).samples == samples[1:3]).all()
    ts_limits = ts.clip(0.2, 0.35, include_stop=True, sample_limits=True)
    assert np.allclose(ts_limits.times, np.array([0.2, 0.3, 0.35]))
    expected_samples = np.vstack((samples[1:3], [2.5, 4.5]))
    assert np.allclose(ts_limits.samples, expected_samples)
    ts_beyond = ts.clip(0.4, 0.6, sample_limits=False)
    assert (ts_beyond.times == times[-2:]).all()
    assert (ts_beyond.samples == samples[-2:]).all()
    # - Clip ts channels
    assert (ts.clip(channels=1).times == times).all()
    assert (ts.clip(channels=1).samples == samples[:, [1]]).all()
    assert (ts.clip(channels=[1, 0]).times == times).all()
    assert (ts.clip(channels=[1, 0]).samples == samples[:, [1, 0]]).all()
    # - Clip ts channels and time
    ts_ch_t = ts.clip(0.2, 0.4, channels=1, include_stop=True)
    assert (ts_ch_t.times == times[1:4]).all()
    assert (ts_ch_t.samples == samples[1:4, [1]]).all()

    # - Clip empty
    ts_et = ts_empty.clip(0.2, 0.4, sample_limits=False)
    assert ts_et.isempty()
    assert ts_et.t_start == 0.2 and ts_et.t_stop == 0.4
    with pytest.raises(IndexError):
        ts_empty.clip(channels=0)
        ts_empty.clip(2, 4, channels=0)
Exemplo n.º 3
0
def test_continuous_methods():
    from rockpool import TSContinuous

    ts1 = TSContinuous([0, 1, 2], [0, 1, 2])

    # - Interpolation
    assert ts1(0) == 0
    assert ts1(2) == 2
    assert ts1(1.5) == 1.5

    assert ts1._interpolate(0) == 0
    assert ts1._interpolate(2) == 2
    assert ts1._interpolate(1.5) == 1.5

    # - Delay
    ts2 = ts1.delay(1)
    assert ts1.t_start == 0
    assert ts2.t_start == 1

    ts20 = ts1.start_at_zero()
    assert ts20.t_start == 0

    # - Contains
    assert ts1.contains(0)
    assert ~ts1.contains(-1)
    assert ts1.contains([0, 1, 2])
    assert ~ts1.contains([0, 1, 2, 3])

    # - Resample
    ts2 = ts1.resample([0.1, 1.1, 1.9])

    # - Merge
    ts1 = TSContinuous([0, 1, 2], [0, 1, 2])
    ts2 = TSContinuous([0, 1, 2], [1, 2, 3])
    ts3 = ts1.merge(ts2, remove_duplicates=True)
    assert np.size(ts3.samples) == 3
    assert np.size(ts1.samples) == 3
    assert np.size(ts2.samples) == 3

    ts3 = ts1.merge(ts2, remove_duplicates=False)
    assert np.size(ts3.samples) == 6

    # - Append
    ts1 = TSContinuous([0, 1, 2], [0, 1, 2])
    ts2 = TSContinuous([0, 1, 2], [1, 2, 3])
    ts3 = ts1.append_t(ts2)
    assert np.size(ts3.times) == 6

    ts3 = ts1.append_c(ts2)
    assert ts3.num_channels == 2

    # - isempty
    assert ~ts1.isempty()
    assert TSContinuous().isempty()

    # - clip
    ts2 = ts1.clip(0.5, 1.5)

    # - Min / Max
    ts1 = TSContinuous([0, 1, 2], [0, 1, 2])
    assert ts1.min == 0
    assert ts1.max == 2