示例#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_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)))
示例#3
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]]))
示例#4
0
def test___eq___notEqual():
    """Check if TimeData object is equal."""
    time_data = TimeData([1, 2, 3], [0.1, 0.2, 0.3])
    actual = TimeData([2, 3, 4], [0.1, 0.2, 0.3])
    assert not time_data == actual
    actual = TimeData([1, 2, 3], [0.2, 0.3, 0.4])
    assert not time_data == actual
    comment = f'{time_data.comment} A completely different thing'
    actual = TimeData([1, 2, 3], [0.1, 0.2, 0.3], comment=comment)
    assert not time_data == actual
示例#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 audio object'):
        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 time_data_stub(time, times):
    """Function to generate stub of pyfar TimeData class based on MagicMock.
    The properties of the signal are set without any further check.

    Parameters
    ----------
    time : ndarray
        Time data
    times : ndarray
        Times of time in second

    Returns
    -------
    time_data
        stub of pyfar TimeData class
    """

    # Use MagicMock and side_effect to mock __getitem__
    # See "Mocking a dictionary with MagicMock",
    # https://het.as.utexas.edu/HET/Software/mock/examples.html
    def getitem(slice):
        time = np.atleast_2d(time_data.time[slice])
        item = time_data_stub(time, time_data.times)
        return item

    time_data = mock.MagicMock(spec_set=TimeData(time, times))
    time_data.time = np.atleast_2d(time)
    time_data.times = np.atleast_1d(times)
    time_data.domain = 'time'
    time_data.n_samples = time_data.time.shape[-1]
    time_data.cshape = time_data.time.shape[:-1]
    time_data.__getitem__.side_effect = getitem

    return time_data
示例#11
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)
示例#12
0
def time_data():
    """
    TimeData object with three data points.

    Returns
    -------
    time_data TimeData
        Data
    """
    time_data = TimeData([1, 0, -1], [0, .1, .4])
    return time_data
示例#13
0
def test_time_data_stub_properties():
    """ Test comparing properties of TimeData stub
    with actual TimeData implementation.
    """
    time = [1, 0, -1]
    times = [0, .1, .4]

    time_data_stub = stub_utils.time_data_stub(time, times)
    stub_dir = dir(time_data_stub)
    time_data_dir = dir(TimeData(time, times))

    assert stub_dir.sort() == time_data_dir.sort()
示例#14
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)
示例#15
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
示例#16
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'
示例#17
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)
示例#18
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)
示例#19
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')
示例#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')
示例#21
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')
示例#22
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)
示例#23
0
def test___eq___equal():
    """Check if copied TimeData is equal."""
    time_data = TimeData([1, 2, 3], [0.1, 0.2, 0.3])
    actual = time_data.copy()
    assert time_data == actual