• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15import numpy as np
16import pytest
17
18import mindspore.dataset as ds
19import mindspore.dataset.audio.transforms as audio
20from mindspore import log as logger
21
22
23def count_unequal_element(data_expected, data_me, rtol, atol):
24    assert data_expected.shape == data_me.shape
25    total_count = len(data_expected.flatten())
26    error = np.abs(data_expected - data_me)
27    greater = np.greater(error, atol + np.abs(data_expected) * rtol)
28    loss_count = np.count_nonzero(greater)
29    assert (loss_count / total_count) < rtol, \
30        "\ndata_expected_std:{0}\ndata_me_error:{1}\nloss:{2}". \
31        format(data_expected[greater], data_me[greater], error[greater])
32
33
34def test_func_biquad_eager():
35    """ mindspore eager mode normal testcase:biquad op"""
36    # Original waveform
37    waveform = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64)
38    # Expect waveform
39    expect_waveform = np.array([[0.0100, 0.0388, 0.1923],
40                                [0.0400, 0.1252, 0.6530]], dtype=np.float64)
41    biquad_op = audio.Biquad(0.01, 0.02, 0.13, 1, 0.12, 0.3)
42    # Filtered waveform by biquad
43    output = biquad_op(waveform)
44    count_unequal_element(expect_waveform, output, 0.0001, 0.0001)
45
46
47def test_func_biquad_pipeline():
48    """ mindspore pipeline mode normal testcase:biquad op"""
49    # Original waveform
50    waveform = np.array([[3.2, 2.1, 1.3], [6.2, 5.3, 6]], dtype=np.float64)
51    # Expect waveform
52    expect_waveform = np.array([[1.0000, 1.0000, 0.5844],
53                                [1.0000, 1.0000, 1.0000]], dtype=np.float64)
54    dataset = ds.NumpySlicesDataset(waveform, ["audio"], shuffle=False)
55    biquad_op = audio.Biquad(1, 0.02, 0.13, 1, 0.12, 0.3)
56    # Filtered waveform by biquad
57    dataset = dataset.map(input_columns=["audio"], operations=biquad_op, num_parallel_workers=8)
58    i = 0
59    for item in dataset.create_dict_iterator(output_numpy=True):
60        count_unequal_element(expect_waveform[i, :],
61                              item['audio'], 0.0001, 0.0001)
62        i += 1
63
64
65def test_biquad_invalid_input():
66    def test_invalid_input(test_name, b0, b1, b2, a0, a1, a2, error, error_msg):
67        logger.info("Test Biquad with bad input: {0}".format(test_name))
68        with pytest.raises(error) as error_info:
69            audio.Biquad(b0, b1, b2, a0, a1, a2)
70        assert error_msg in str(error_info.value)
71
72    test_invalid_input("invalid b0 parameter type as a String", "0.01", 0.02, 0.13, 1, 0.12, 0.3, TypeError,
73                       "Argument b0 with value 0.01 is not of type [<class 'float'>, <class 'int'>],"
74                       " but got <class 'str'>.")
75    test_invalid_input("invalid b0 parameter value", 441324343243242342345300, 0.02, 0.13, 1, 0.12, 0.3, ValueError,
76                       "Input b0 is not within the required interval of [-16777216, 16777216].")
77    test_invalid_input("invalid b1 parameter type as a String", 0.01, "0.02", 0.13, 0, 0.12, 0.3, TypeError,
78                       "Argument b1 with value 0.02 is not of type [<class 'float'>, <class 'int'>],"
79                       " but got <class 'str'>.")
80    test_invalid_input("invalid b1 parameter value", 0.01, 441324343243242342345300, 0.13, 1, 0.12, 0.3, ValueError,
81                       "Input b1 is not within the required interval of [-16777216, 16777216].")
82    test_invalid_input("invalid b2 parameter type as a String", 0.01, 0.02, "0.13", 0, 0.12, 0.3, TypeError,
83                       "Argument b2 with value 0.13 is not of type [<class 'float'>, <class 'int'>],"
84                       " but got <class 'str'>.")
85    test_invalid_input("invalid b2 parameter value", 0.01, 0.02, 441324343243242342345300, 1, 0.12, 0.3, ValueError,
86                       "Input b2 is not within the required interval of [-16777216, 16777216].")
87    test_invalid_input("invalid a0 parameter type as a String", 0.01, 0.02, 0.13, '1', 0.12, 0.3, TypeError,
88                       "Argument a0 with value 1 is not of type [<class 'float'>, <class 'int'>],"
89                       " but got <class 'str'>.")
90    test_invalid_input("invalid a0 parameter value", 0.01, 0.02, 0.13, 0, 0.12, 0.3, ValueError,
91                       "Input a0 is not within the required interval of [-16777216, 0) and (0, 16777216].")
92    test_invalid_input("invalid a0 parameter value", 0.01, 0.02, 0.13, 441324343243242342345300, 0.12, 0.3, ValueError,
93                       "Input a0 is not within the required interval of [-16777216, 0) and (0, 16777216].")
94    test_invalid_input("invalid a1 parameter type as a String", 0.01, 0.02, 0.13, 1, '0.12', 0.3, TypeError,
95                       "Argument a1 with value 0.12 is not of type [<class 'float'>, <class 'int'>],"
96                       " but got <class 'str'>.")
97    test_invalid_input("invalid a1 parameter value", 0.01, 0.02, 0.13, 1, 441324343243242342345300, 0.3, ValueError,
98                       "Input a1 is not within the required interval of [-16777216, 16777216].")
99    test_invalid_input("invalid a2 parameter type as a String", 0.01, 0.02, 0.13, 1, 0.12, '0.3', TypeError,
100                       "Argument a2 with value 0.3 is not of type [<class 'float'>, <class 'int'>],"
101                       " but got <class 'str'>.")
102    test_invalid_input("invalid a1 parameter value", 0.01, 0.02, 0.13, 1, 0.12, 441324343243242342345300, ValueError,
103                       "Input a2 is not within the required interval of [-16777216, 16777216].")
104
105
106if __name__ == '__main__':
107    test_func_biquad_eager()
108    test_func_biquad_pipeline()
109    test_biquad_invalid_input()
110