Ejemplo n.º 1
0
def test_magic_setitem_wrong_n_bins():
    """Test the setitem for FrequencyData with wrong number of bins."""

    freq_a = FrequencyData([1, 0, -1], [0, .1, .3])
    freq_b = FrequencyData([2, 0, -2, 0], [0, .1, .3, .7])

    with pytest.raises(ValueError):
        freq_a[0] = freq_b
Ejemplo n.º 2
0
def test_data_frequency_setter_freq():
    """Test the setter for the frequency data."""
    data_a = [1, 0, -1]
    data_b = [2, 0, -2]
    freqs = [0, .1, .3]

    freq = FrequencyData(data_a, freqs)
    freq.freq = data_b
    npt.assert_allclose(freq.freq, np.atleast_2d(np.asarray(data_b)))
Ejemplo n.º 3
0
def test_magic_setitem_wrong_fft_norm():
    """Test the setitem for FrequencyData with wrong FFT norm."""
    freqs = [0, .1, .3]

    freq_a = FrequencyData([1, 0, -1], freqs)
    freq_b = FrequencyData([2, 0, -2], freqs, fft_norm='psd')

    with pytest.raises(ValueError):
        freq_a[0] = freq_b
Ejemplo n.º 4
0
def test_magic_setitem():
    """Test the setitem for FrequencyData."""
    freqs = [0, .1, .3]

    freq_a = FrequencyData([[1, 0, -1], [1, 0, -1]], freqs)
    freq_b = FrequencyData([2, 0, -2], freqs)
    freq_a[0] = freq_b

    npt.assert_allclose(freq_a.freq, np.asarray([[2, 0, -2], [1, 0, -1]]))
Ejemplo n.º 5
0
def test___eq___notEqual():
    """Check if FrequencyData is equal."""
    frequency_data = FrequencyData([1, 2, 3], [1, 2, 3])
    actual = FrequencyData([2, 3, 4], [1, 2, 3])
    assert not frequency_data == actual
    actual = FrequencyData([1, 2, 3], [2, 3, 4])
    assert not frequency_data == actual
    comment = f'{frequency_data.comment} A completely different thing'
    actual = FrequencyData([1, 2, 3], [1, 2, 3], comment=comment)
    assert not frequency_data == actual
Ejemplo n.º 6
0
def test_reshape_exceptions():
    data_in = FrequencyData(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))
Ejemplo n.º 7
0
def test_separation_from_signal():
    """Check if attributes from Signal are really not available."""
    data = [1, 0, -1]
    freqs = [0, .1, .3]
    freq = FrequencyData(data, freqs)

    with pytest.raises(AttributeError):
        freq.sampling_rate
    with pytest.raises(AttributeError):
        freq.domain = 'freq'
    with pytest.raises(AttributeError):
        freq.fft_norm = 'amplitude'
Ejemplo n.º 8
0
def test_data_frequency_find_nearest():
    """Test the find nearest function for a single number and list entry."""
    data = [1, 0, -1]
    freqs = [0, .1, .3]
    freq = FrequencyData(data, freqs)

    # test for a single number
    idx = freq.find_nearest_frequency(.15)
    assert idx == 1

    # test for a list
    idx = freq.find_nearest_frequency([.15, .4])
    npt.assert_allclose(idx, np.asarray([1, 2]))
Ejemplo n.º 9
0
def test_data_frequency_with_non_monotonously_increasing_frequencies():
    """Test if non monotnously increasing frequencies raises an assertion."""
    data = [1, 0, -1]
    freqs = [0, .2, .1]

    with pytest.raises(ValueError):
        FrequencyData(data, freqs)
Ejemplo n.º 10
0
def test_data_frequency_init_wrong_number_of_freqs():
    """Test if entering a wrong number of frequencies raises an assertion."""
    data = [1, 0, -1]
    freqs = [0, .1]

    with pytest.raises(ValueError):
        FrequencyData(data, freqs)
Ejemplo n.º 11
0
def frequency_data_stub(freq, frequencies):
    """
    Function to generate stub of pyfar FrequencyData class based onMagicMock.
    The properties of the signal are set without any further check.

    Parameters
    ----------
    freq : ndarray
        Frequency data
    frequencies : ndarray
        Frequencies of freq in Hz

    Returns
    -------
    frequency_data
        stub of pyfar FrequencyData 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):
        freq = np.atleast_2d(frequency_data.freq[slice])
        item = frequency_data_stub(freq, frequency_data.frequencies)
        return item

    frequency_data = mock.MagicMock(spec_set=FrequencyData(freq, frequencies))
    frequency_data.freq = np.atleast_2d(freq)
    frequency_data.frequencies = np.atleast_1d(frequencies)
    frequency_data.domain = 'freq'
    frequency_data.n_bins = frequency_data.freq.shape[-1]
    frequency_data.cshape = frequency_data.freq.shape[:-1]
    frequency_data.__getitem__.side_effect = getitem

    return frequency_data
Ejemplo n.º 12
0
def test_overloaded_operators_frequency_data():
    x = FrequencyData([2, 1, 0], [0, 1, 2])
    y = FrequencyData([2, 2, 2], [0, 1, 2])

    # addition
    z = x + y
    npt.assert_allclose(z.freq, np.array([4, 3, 2], ndmin=2), atol=1e-15)
    # subtraction
    z = x - y
    npt.assert_allclose(z.freq, np.array([0, -1, -2], ndmin=2), atol=1e-15)
    # multiplication
    z = x * y
    npt.assert_allclose(z.freq, np.array([4, 2, 0], ndmin=2), atol=1e-15)
    # division
    z = x / y
    npt.assert_allclose(z.freq, np.array([1, .5, 0], ndmin=2), atol=1e-15)
    # power
    z = x**y
    npt.assert_allclose(z.freq, np.array([4, 1, 0], ndmin=2), atol=1e-15)
Ejemplo n.º 13
0
def frequency_data_one_point():
    """
    FrequencyData object with one data point.

    Returns
    -------
    frequency_data FrequencyData
        Data
    """
    frequency_data = FrequencyData([2], [0])
    return frequency_data
Ejemplo n.º 14
0
def frequency_data():
    """
    FrequencyData object with three data points.

    Returns
    -------
    frequency_data FrequencyData
        Data
    """
    frequency_data = FrequencyData([2, .25, .5], [100, 1000, 20000])
    return frequency_data
Ejemplo n.º 15
0
def test_frequency_data_stub_properties():
    """ Test comparing properties of FrequencyData stub
    with actual FrequencyData implementation.
    """
    freq = [1, 0, -1]
    frequencies = [0, .1, .4]

    frequency_data_stub = stub_utils.frequency_data_stub(freq, frequencies)
    stub_dir = dir(frequency_data_stub)
    frequency_data_dir = dir(FrequencyData(freq, frequencies))

    assert stub_dir.sort() == frequency_data_dir.sort()
Ejemplo n.º 16
0
def test_add_frequency_data_and_frequency_data():
    # generate and add signals
    x = FrequencyData([1, 0, 0], [0, .1, .5])
    y = signal.add((x, x), 'freq')

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

    # check result
    assert isinstance(y, FrequencyData)
    npt.assert_allclose(y.freq, np.atleast_2d([2, 0, 0]), atol=1e-15)
    npt.assert_allclose(y.frequencies, np.atleast_1d([0, .1, .5]), atol=1e-15)
Ejemplo n.º 17
0
def test_data_frequency_init_with_defaults():
    """
    Test to init without optional parameters.
    Test getter for domain, freq, frequencies, and n_bins.
    """
    data = [1, 0, -1]
    freqs = [0, .1, .3]

    freq = FrequencyData(data, freqs)
    assert isinstance(freq, FrequencyData)
    npt.assert_allclose(freq.freq, np.atleast_2d(np.asarray(data)))
    npt.assert_allclose(freq.frequencies, np.atleast_1d(np.asarray(freqs)))
    assert freq.n_bins == 3
    assert freq.domain == 'freq'
Ejemplo n.º 18
0
def test_separation_from_time_data():
    """Check if attributes from FrequencyData are really not available."""
    data = [1, 0, -1]
    freqs = [0, .1, .3]
    freq = FrequencyData(data, freqs)

    with pytest.raises(AttributeError):
        freq.time
    with pytest.raises(AttributeError):
        freq.times
    with pytest.raises(AttributeError):
        freq.n_samples
    with pytest.raises(AttributeError):
        freq.signal_length
    with pytest.raises(AttributeError):
        freq.find_nearest_time
Ejemplo n.º 19
0
def test_reshape():

    # test reshape with tuple
    data_in = FrequencyData(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 = FrequencyData(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)
Ejemplo n.º 20
0
def test_flatten():

    # test 2D signal (flatten should not change anything)
    x = np.random.rand(2, 256)
    data_in = FrequencyData(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 = FrequencyData(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)
Ejemplo n.º 21
0
def test_data_frequency_with_wrong_fft_norm():
    data = [1, 0, -1]
    freqs = [0, .1, .2]

    with pytest.raises(ValueError):
        FrequencyData(data, freqs, fft_norm='bull shit')
Ejemplo n.º 22
0
def test_magic_getitem_slice():
    """Test slicing operations by the magic function __getitem__."""
    data = np.array([[1, 0, -1], [2, 0, -2]])
    freqs = [0, .1, .3]
    freq = FrequencyData(data, freqs)
    npt.assert_allclose(FrequencyData(data[0], freqs)._data, freq[0]._data)
Ejemplo n.º 23
0
def test_add_frequency_data_and_number_wrong_fft_norm():
    # generate and add signals
    x = FrequencyData([1, 0, 0], [0, .1, .5])
    y = FrequencyData([1, 0, 0], [0, .1, .5], fft_norm='rms')
    with raises(ValueError):
        signal.add((x, y), 'freq')
Ejemplo n.º 24
0
def test_add_frequency_data_and_number_wrong_domain():
    # generate and add signals
    x = FrequencyData([1, 0, 0], [0, .1, .5])
    with raises(ValueError):
        signal.add((x, 1), 'time')
Ejemplo n.º 25
0
def test_getter_fft_norm():
    data = [1, 0, -1]
    freqs = [0, .1, .3]

    freq = FrequencyData(data, freqs, fft_norm='psd')
    assert freq.fft_norm == 'psd'
Ejemplo n.º 26
0
def test___eq___equal():
    """Check if copied FrequencyData is equal."""
    frequency_data = FrequencyData([1, 2, 3], [1, 2, 3])
    actual = frequency_data.copy()
    assert frequency_data == actual