예제 #1
0
def test_magic_setitem_wrong_n_samples():
    """Test the setimtem for TimeData with wrong number of samples."""

    time_a = TimeData([1, 0, -1], [0, .1, .3])
    time_b = TimeData([2, 0, -2, 0], [0, .1, .3, .7])
    with pytest.raises(ValueError):
        time_a[0] = time_b
예제 #2
0
def test___eq___notEqual(timedata, sine):
    """Check if timedata signal is equal."""
    actual = TimeData(0.5 * timedata.time, timedata.times)
    assert not timedata == actual
    actual = TimeData(timedata.time, 0.5 * timedata.times)
    assert not timedata == actual
    actual = timedata.copy()
    actual.comment = f'{actual.comment} A completely different thing'
    assert not timedata == actual
예제 #3
0
def test_data_time_setter_time():
    """Test the setter for the time data."""
    data_a = [1, 0, -1]
    data_b = [2, 0, -2]
    times = [0, .1, .3]

    time = TimeData(data_a, times)
    time.time = data_b
    npt.assert_allclose(time.time, np.atleast_2d(np.asarray(data_b)))
예제 #4
0
def test_magic_setitem():
    """Test the setimtem for TimeData."""
    times = [0, .1, .3]

    time_a = TimeData([[1, 0, -1], [1, 0, -1]], times)
    time_b = TimeData([2, 0, -2], times)
    time_a[0] = time_b

    npt.assert_allclose(time_a.time, np.asarray([[2, 0, -2], [1, 0, -1]]))
예제 #5
0
def test_separation_from_signal():
    """Check if attributes from Signal are really not available."""
    data = [1, 0, -1]
    times = [0, .1, .3]
    time = TimeData(data, times)

    with pytest.raises(AttributeError):
        time.sampling_rate
    with pytest.raises(AttributeError):
        time.domain = 'time'
예제 #6
0
def test_reshape_exceptions():
    data_in = TimeData(np.random.rand(6, 256), range(256))
    data_out = data_in.reshape((3, 2))
    npt.assert_allclose(data_in._data.reshape(3, 2, -1), data_out._data)
    # test assertion for non-tuple input
    with pytest.raises(ValueError):
        data_out = data_in.reshape([3, 2])

    # test assertion for wrong dimension
    with pytest.raises(ValueError, match='Can not reshape signal of cshape'):
        data_out = data_in.reshape((3, 4))
예제 #7
0
def test_data_time_find_nearest():
    """Test the find nearest function for a single number and list entry."""
    data = [1, 0, -1]
    times = [0, .1, .3]
    time = TimeData(data, times)

    # test for a single number
    idx = time.find_nearest_time(.15)
    assert idx == 1

    # test for a list
    idx = time.find_nearest_time([.15, .4])
    npt.assert_allclose(idx, np.asarray([1, 2]))
예제 #8
0
def test_data_time_init_wrong_number_of_times():
    """Test if entering a wrong number of times raises an assertion."""
    data = [1, 0, -1]
    times = [0, .1]

    with pytest.raises(ValueError):
        TimeData(data, times)
예제 #9
0
def test_data_time_with_non_monotonously_increasing_time():
    """Test if non monotnously increasing of times raises an assertion."""
    data = [1, 0, -1]
    times = [0, .2, .1]

    with pytest.raises(ValueError):
        TimeData(data, times)
예제 #10
0
def test_overloaded_operators_time_data():
    x = TimeData([2, 1, 0], [0, 1, 2])
    y = TimeData([2, 2, 2], [0, 1, 2])

    # addition
    z = x + y
    npt.assert_allclose(z.time, np.array([4, 3, 2], ndmin=2), atol=1e-15)
    # subtraction
    z = x - y
    npt.assert_allclose(z.time, np.array([0, -1, -2], ndmin=2), atol=1e-15)
    # multiplication
    z = x * y
    npt.assert_allclose(z.time, np.array([4, 2, 0], ndmin=2), atol=1e-15)
    # division
    z = x / y
    npt.assert_allclose(z.time, np.array([1, .5, 0], ndmin=2), atol=1e-15)
    # power
    z = x**y
    npt.assert_allclose(z.time, np.array([4, 1, 0], ndmin=2), atol=1e-15)
예제 #11
0
def test_add_time_data_and_time_data():
    # generate and add signals
    x = TimeData([1, 0, 0], [0, .1, .5])
    y = signal.add((x, x), 'time')

    # check if old signal did not change
    npt.assert_allclose(x.time, np.atleast_2d([1, 0, 0]), atol=1e-15)
    npt.assert_allclose(x.times, np.atleast_1d([0, .1, .5]), atol=1e-15)

    # check result
    assert isinstance(y, TimeData)
    npt.assert_allclose(y.time, np.atleast_2d([2, 0, 0]), atol=1e-15)
    npt.assert_allclose(y.times, np.atleast_1d([0, .1, .5]), atol=1e-15)
예제 #12
0
def test_separation_from_data_frequency():
    """Check if attributes from DataFrequency are really not available."""
    data = [1, 0, -1]
    times = [0, .1, .3]
    time = TimeData(data, times)

    with pytest.raises(AttributeError):
        time.freq
    with pytest.raises(AttributeError):
        time.frequencies
    with pytest.raises(AttributeError):
        time.n_bins
    with pytest.raises(AttributeError):
        time.find_nearest_frequency
예제 #13
0
def test_data_time_init_with_defaults():
    """
    Test to init without optional parameters.
    Test getter for domain, time, times, length, and n_samples.
    """
    data = [1, 0, -1]
    times = [0, .1, .3]

    time = TimeData(data, times)
    assert isinstance(time, TimeData)
    npt.assert_allclose(time.time, np.atleast_2d(np.asarray(data)))
    npt.assert_allclose(time.times, np.atleast_1d(np.asarray(times)))
    assert time.signal_length == .3
    assert time.n_samples == 3
    assert time.domain == 'time'
예제 #14
0
def test_reshape():

    # test reshape with tuple
    data_in = TimeData(np.random.rand(6, 256), range(256))
    data_out = data_in.reshape((3, 2))
    npt.assert_allclose(data_in._data.reshape(3, 2, -1), data_out._data)
    assert id(data_in) != id(data_out)

    data_out = data_in.reshape((3, -1))
    npt.assert_allclose(data_in._data.reshape(3, 2, -1), data_out._data)
    assert id(data_in) != id(data_out)

    # test reshape with int
    data_in = TimeData(np.random.rand(3, 2, 256), range(256))
    data_out = data_in.reshape(6)
    npt.assert_allclose(data_in._data.reshape(6, -1), data_out._data)
    assert id(data_in) != id(data_out)
예제 #15
0
def test_flatten():

    # test 2D signal (flatten should not change anything)
    x = np.random.rand(2, 256)
    data_in = TimeData(x, range(256))
    data_out = data_in.flatten()

    npt.assert_allclose(data_in._data, data_out._data)
    assert id(data_in) != id(data_out)

    # test 3D signal
    x = np.random.rand(3, 2, 256)
    data_in = TimeData(x, range(256))
    data_out = data_in.flatten()

    npt.assert_allclose(data_in._data.reshape((6, -1)), data_out._data)
    assert id(data_in) != id(data_out)
예제 #16
0
def test_add_time_data_and_number_wrong_domain():
    # generate and add signals
    x = TimeData([1, 0, 0], [0, .1, .5])
    with raises(ValueError):
        signal.add((x, 1), 'freq')
예제 #17
0
def test_assert_match_for_arithmetic_data_different_audio_classes():
    with raises(ValueError):
        signal._assert_match_for_arithmetic(
            (Signal(1, 1), TimeData(1, 1)), 'time')
예제 #18
0
def test_magic_getitem_slice():
    """Test slicing operations by the magic function __getitem__."""
    data = np.array([[1, 0, -1], [2, 0, -2]])
    times = [0, .1, .3]
    time = TimeData(data, times)
    npt.assert_allclose(TimeData(data[0], times)._data, time[0]._data)
예제 #19
0
def timedata():
    data = [1, 0, -1]
    times = [0, .1, .3]
    return TimeData(data, times)
예제 #20
0
def test_add_time_data_and_number_wrong_times():
    # generate and add signals
    x = TimeData([1, 0, 0], [0, .1, .5])
    y = TimeData([1, 0, 0], [0, .1, .4])
    with raises(ValueError):
        signal.add((x, y), 'time')