• 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
17import mindspore.dataset as ds
18import mindspore.dataset.audio.transforms as audio
19from mindspore import log as logger
20
21
22def count_unequal_element(data_expected, data_me, rtol, atol):
23    assert data_expected.shape == data_me.shape
24    total_count = len(data_expected.flatten())
25    error = np.abs(data_expected - data_me)
26    greater = np.greater(error, atol + np.abs(data_expected) * rtol)
27    loss_count = np.count_nonzero(greater)
28    assert (loss_count / total_count) < rtol, "\ndata_expected_std:{0}\ndata_me_error:{1}\nloss:{2}".format(
29        data_expected[greater], data_me[greater], error[greater])
30
31
32def test_func_bandpass_biquad_eager():
33    """ mindspore eager mode normal testcase:bandpass_biquad op"""
34
35    # Original waveform
36    waveform = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64)
37    # Expect waveform
38    expect_waveform = np.array([[0.01979545, 0.07838227, 0.17417782],
39                                [0.07918181, 0.25414270, 0.46156447]], dtype=np.float64)
40    bandpass_biquad_op = audio.BandpassBiquad(44000, 200.0, 0.707, False)
41    # Filtered waveform by bandpassbiquad
42    output = bandpass_biquad_op(waveform)
43    count_unequal_element(expect_waveform, output, 0.0001, 0.0001)
44
45
46def test_func_bandpass_biquad_pipeline():
47    """ mindspore pipeline mode normal testcase:bandpass_biquad op"""
48
49    # Original waveform
50    waveform = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64)
51    # Expect waveform
52    expect_waveform = np.array([[0.01979545, 0.07838227, 0.17417782],
53                                [0.07918181, 0.25414270, 0.46156447]], dtype=np.float64)
54    label = np.random.sample((2, 1))
55    data = (waveform, label)
56    dataset = ds.NumpySlicesDataset(data, ["channel", "sample"], shuffle=False)
57    bandpass_biquad_op = audio.BandpassBiquad(44000, 200.0)
58    # Filtered waveform by bandpassbiquad
59    dataset = dataset.map(input_columns=["channel"], operations=bandpass_biquad_op, num_parallel_workers=8)
60    i = 0
61    for item in dataset.create_dict_iterator(output_numpy=True):
62        count_unequal_element(expect_waveform[i, :], item['channel'], 0.0001, 0.0001)
63        i += 1
64
65
66def test_bandpass_biquad_invalid_input():
67    def test_invalid_input(test_name, sample_rate, central_freq, Q, const_skirt_gain, error, error_msg):
68        logger.info(
69            "Test BandpassBiquad with bad input: {0}".format(test_name))
70        with pytest.raises(error) as error_info:
71            audio.BandpassBiquad(sample_rate, central_freq, Q, const_skirt_gain)
72        assert error_msg in str(error_info.value)
73
74    test_invalid_input("invalid sample_rate parameter type as a float", 44100.5, 200, 0.707, True, TypeError,
75                       "Argument sample_rate with value 44100.5 is not of type [<class 'int'>],"
76                       " but got <class 'float'>.")
77    test_invalid_input("invalid sample_rate parameter type as a String", "44100", 200, 0.707, True, TypeError,
78                       "Argument sample_rate with value 44100 is not of type [<class 'int'>], but got <class 'str'>.")
79    test_invalid_input("invalid contral_freq parameter type as a String", 44100, "200", 0.707, True, TypeError,
80                       "Argument central_freq with value 200 is not of type [<class 'float'>, <class 'int'>],"
81                       " but got <class 'str'>.")
82    test_invalid_input("invalid sample_rate parameter value", 0, 200, 0.707, True, ValueError,
83                       "Input sample_rate is not within the required interval of [-2147483648, 0) and (0, 2147483647].")
84    test_invalid_input("invalid contral_freq parameter value", 44100, 32434324324234321, 0.707, True, ValueError,
85                       "Input central_freq is not within the required interval of [-16777216, 16777216].")
86    test_invalid_input("invalid Q parameter type as a String", 44100, 200, "0.707", True, TypeError,
87                       "Argument Q with value 0.707 is not of type [<class 'float'>, <class 'int'>],"
88                       " but got <class 'str'>.")
89    test_invalid_input("invalid Q parameter value", 44100, 200, 1.707, True, ValueError,
90                       "Input Q is not within the required interval of (0, 1].")
91    test_invalid_input("invalid Q parameter value", 44100, 200, 0, True, ValueError,
92                       "Input Q is not within the required interval of (0, 1].")
93    test_invalid_input("invalid sample_rate parameter value", 441324343243242342345300, 200, 0.707, True, ValueError,
94                       "Input sample_rate is not within the required interval of [-2147483648, 0) and (0, 2147483647].")
95    test_invalid_input("invalid sample_rate parameter value", None, 200, 0.707, True, TypeError,
96                       "Argument sample_rate with value None is not of type [<class 'int'>],"
97                       " but got <class 'NoneType'>.")
98    test_invalid_input("invalid central_rate parameter value", 44100, None, 0.707, True, TypeError,
99                       "Argument central_freq with value None is not of type [<class 'float'>, <class 'int'>],"
100                       " but got <class 'NoneType'>.")
101    test_invalid_input("invalid const_skirt_gain parameter type as a String", 44100, 200, 0.707, "False", TypeError,
102                       "Argument const_skirt_gain with value False is not of type [<class 'bool'>], " +
103                       "but got <class 'str'>.")
104
105
106if __name__ == "__main__":
107    test_func_bandpass_biquad_eager()
108    test_func_bandpass_biquad_pipeline()
109    test_bandpass_biquad_invalid_input()
110