• 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# ==============================================================================
15"""
16Testing LowpassBiquad op in DE
17"""
18
19import numpy as np
20import pytest
21
22import mindspore.dataset as ds
23import mindspore.dataset.audio.transforms as audio
24from mindspore import log as logger
25
26
27def count_unequal_element(data_expected, data_me, rtol, atol):
28    assert data_expected.shape == data_me.shape
29    total_count = len(data_expected.flatten())
30    error = np.abs(data_expected - data_me)
31    greater = np.greater(error, atol + np.abs(data_expected) * rtol)
32    loss_count = np.count_nonzero(greater)
33    assert (loss_count / total_count) < rtol, "\ndata_expected_std:{0}\ndata_me_error:{1}\nloss:{2}".format(
34        data_expected[greater], data_me[greater], error[greater])
35
36
37def test_lowpass_biquad_eager():
38    """ mindspore eager mode normal testcase:lowpass_biquad op"""
39    # Original waveform
40    waveform = np.array([[0.8236, 0.2049, 0.3335], [0.5933, 0.9911, 0.2482],
41                         [0.3007, 0.9054, 0.7598], [0.5394, 0.2842, 0.5634], [0.6363, 0.2226, 0.2288]])
42    # Expect waveform
43    expect_waveform = np.array([[0.2745, 0.6174, 0.4308], [0.1978, 0.7259, 0.8753],
44                                [0.1002, 0.5023, 0.9237], [0.1798, 0.4543, 0.4971], [0.2121, 0.4984, 0.3661]])
45    lowpass_biquad_op = audio.LowpassBiquad(4000, 1000.0, 1)
46    # Filtered waveform by lowpass_biquad
47    output = lowpass_biquad_op(waveform)
48    count_unequal_element(expect_waveform, output, 0.0001, 0.0001)
49
50
51def test_lowpass_biquad_pipeline():
52    """ mindspore pipeline mode normal testcase:lowpass_biquad op"""
53    # Original waveform
54    waveform = np.array([[3.5, 3.2, 2.5, 7.1], [5.5, 0.3, 4.9, 5.0],
55                         [1.3, 7.4, 7.1, 3.8], [3.4, 3.3, 3.7, 1.1]])
56    # Expect waveform
57    expect_waveform = np.array([[0.0481, 0.2029, 0.4180, 0.6830], [0.0755, 0.2538, 0.4555, 0.7107],
58                                [0.0178, 0.1606, 0.5220, 0.9729], [0.0467, 0.1997, 0.4322, 0.6546]])
59    dataset = ds.NumpySlicesDataset(waveform, ["col1"], shuffle=False)
60    lowpass_biquad_op = audio.LowpassBiquad(44100, 2000.0, 0.3)
61    # Filtered waveform by lowpass_biquad
62    dataset = dataset.map(
63        input_columns=["col1"], operations=lowpass_biquad_op, num_parallel_workers=4)
64    i = 0
65    for _ in dataset.create_dict_iterator(output_numpy=True):
66        count_unequal_element(expect_waveform[i, :],
67                              _["col1"], 0.0001, 0.0001)
68        i += 1
69
70
71def test_lowpass_biquad_invalid_input():
72    """
73    Test invalid input of LowpassBiquad
74    """
75    def test_invalid_input(test_name, sample_rate, cutoff_freq, Q, error, error_msg):
76        logger.info("Test LowpassBiquad with bad input: {0}".format(test_name))
77        with pytest.raises(error) as error_info:
78            audio.LowpassBiquad(sample_rate, cutoff_freq, Q)
79        assert error_msg in str(error_info.value)
80    test_invalid_input("invalid sample_rate parameter type as a float", 44100.5, 1000, 0.707, TypeError,
81                       "Argument sample_rate with value 44100.5 is not of type [<class 'int'>],"
82                       " but got <class 'float'>.")
83    test_invalid_input("invalid sample_rate parameter type as a String", "44100", 1000, 0.707, TypeError,
84                       "Argument sample_rate with value 44100 is not of type [<class 'int'>],"
85                       " but got <class 'str'>.")
86    test_invalid_input("invalid cutoff_freq parameter type as a String", 44100, "1000", 0.707, TypeError,
87                       "Argument cutoff_freq with value 1000 is not of type [<class 'float'>, <class 'int'>],"
88                       " but got <class 'str'>.")
89    test_invalid_input("invalid Q parameter type as a String", 44100, 1000, "0.707", TypeError,
90                       "Argument Q with value 0.707 is not of type [<class 'float'>, <class 'int'>],"
91                       " but got <class 'str'>.")
92
93    test_invalid_input("invalid sample_rate parameter value", 441324343243242342345300, 1000, 0.707, ValueError,
94                       "Input sample_rate is not within the required interval of [-2147483648, 0) and (0, 2147483647].")
95    test_invalid_input("invalid cutoff_freq parameter value", 44100, 32434324324234321, 0.707, ValueError,
96                       "Input cutoff_freq is not within the required interval of [-16777216, 16777216].")
97
98    test_invalid_input("invalid sample_rate parameter value", None, 1000, 0.707, TypeError,
99                       "Argument sample_rate with value None is not of type [<class 'int'>], "
100                       "but got <class 'NoneType'>.")
101    test_invalid_input("invalid cutoff_rate parameter value", 44100, None, 0.707, TypeError,
102                       "Argument cutoff_freq with value None is not of type [<class 'float'>, <class 'int'>],"
103                       " but got <class 'NoneType'>.")
104
105    test_invalid_input("invalid Q parameter value", 44100, 1000, 0, ValueError,
106                       "Input Q is not within the required interval of (0, 1].")
107
108
109if __name__ == "__main__":
110    test_lowpass_biquad_eager()
111    test_lowpass_biquad_pipeline()
112    test_lowpass_biquad_invalid_input()
113