• 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_bandreject_biquad_eager():
33    """ mindspore eager mode normal testcase:bandreject_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([[9.802485108375549316e-01, 1.000000000000000000e+00, 1.000000000000000000e+00],
39                                [1.000000000000000000e+00, 1.000000000000000000e+00, 1.000000000000000000e+00]],
40                               dtype=np.float64)
41    bandreject_biquad_op = audio.BandrejectBiquad(44100, 200.0, 0.707)
42    # Filtered waveform by bandrejectbiquad
43    output = bandreject_biquad_op(waveform)
44    count_unequal_element(expect_waveform, output, 0.0001, 0.0001)
45
46
47def test_func_bandreject_biquad_pipeline():
48    """ mindspore pipeline mode normal testcase:bandreject_biquad op"""
49
50    # Original waveform
51    waveform = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64)
52    # Expect waveform
53    expect_waveform = np.array([[9.802485108375549316e-01, 1.000000000000000000e+00, 1.000000000000000000e+00],
54                                [1.000000000000000000e+00, 1.000000000000000000e+00, 1.000000000000000000e+00]],
55                               dtype=np.float64)
56    label = np.random.sample((2, 1))
57    data = (waveform, label)
58    dataset = ds.NumpySlicesDataset(data, ["channel", "sample"], shuffle=False)
59    bandreject_biquad_op = audio.BandrejectBiquad(44100, 200.0)
60    # Filtered waveform by bandrejectbiquad
61    dataset = dataset.map(
62        input_columns=["channel"], operations=bandreject_biquad_op, num_parallel_workers=8)
63    i = 0
64    for item in dataset.create_dict_iterator(output_numpy=True):
65        count_unequal_element(expect_waveform[i, :],
66                              item['channel'], 0.0001, 0.0001)
67        i += 1
68
69
70def test_bandreject_biquad_invalid_input():
71    def test_invalid_input(test_name, sample_rate, central_freq, Q, error, error_msg):
72        logger.info(
73            "Test BandrejectBiquad with bad input: {0}".format(test_name))
74        with pytest.raises(error) as error_info:
75            audio.BandrejectBiquad(sample_rate, central_freq, Q)
76        assert error_msg in str(error_info.value)
77
78    test_invalid_input("invalid sample_rate parameter type as a float", 44100.5, 200, 0.707, TypeError,
79                       "Argument sample_rate with value 44100.5 is not of type [<class 'int'>],"
80                       " but got <class 'float'>.")
81    test_invalid_input("invalid sample_rate parameter type as a String", "44100", 200, 0.707, TypeError,
82                       "Argument sample_rate with value 44100 is not of type [<class 'int'>], but got <class 'str'>.")
83    test_invalid_input("invalid contral_freq parameter type as a String", 44100, "200", 0.707, TypeError,
84                       "Argument central_freq with value 200 is not of type [<class 'float'>, <class 'int'>],"
85                       " but got <class 'str'>.")
86    test_invalid_input("invalid sample_rate parameter value", 0, 200, 0.707, ValueError,
87                       "Input sample_rate is not within the required interval of [-2147483648, 0) and (0, 2147483647].")
88    test_invalid_input("invalid contral_freq parameter value", 44100, 32434324324234321, 0.707, ValueError,
89                       "Input central_freq is not within the required interval of [-16777216, 16777216].")
90    test_invalid_input("invalid Q parameter type as a String", 44100, 200, "0.707", TypeError,
91                       "Argument Q with value 0.707 is not of type [<class 'float'>, <class 'int'>],"
92                       " but got <class 'str'>.")
93    test_invalid_input("invalid Q parameter value", 44100, 200, 1.707, ValueError,
94                       "Input Q is not within the required interval of (0, 1].")
95    test_invalid_input("invalid Q parameter value", 44100, 200, 0, ValueError,
96                       "Input Q is not within the required interval of (0, 1].")
97    test_invalid_input("invalid sample_rate parameter value", 441324343243242342345300, 200, 0.707, ValueError,
98                       "Input sample_rate is not within the required interval of [-2147483648, 0) and (0, 2147483647].")
99    test_invalid_input("invalid sample_rate parameter value", None, 200, 0.707, TypeError,
100                       "Argument sample_rate with value None is not of type [<class 'int'>],"
101                       " but got <class 'NoneType'>.")
102    test_invalid_input("invalid central_rate parameter value", 44100, None, 0.707, TypeError,
103                       "Argument central_freq with value None is not of type [<class 'float'>, <class 'int'>],"
104                       " but got <class 'NoneType'>.")
105
106
107if __name__ == "__main__":
108    test_func_bandreject_biquad_eager()
109    test_func_bandreject_biquad_pipeline()
110    test_bandreject_biquad_invalid_input()
111