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_allpass_biquad_eager(): 33 """ mindspore eager mode normal testcase:allpass_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.96049707, 1.0, 1.0], [1.0, 1.0, 1.0]], dtype=np.float64) 39 allpass_biquad_op = audio.AllpassBiquad(44100, 200.0, 0.707) 40 # Filtered waveform by allpassbiquad 41 output = allpass_biquad_op(waveform) 42 count_unequal_element(expect_waveform, output, 0.0001, 0.0001) 43 44 45def test_func_allpass_biquad_pipeline(): 46 """ mindspore pipeline mode normal testcase:allpass_biquad op""" 47 48 # Original waveform 49 waveform = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64) 50 # Expect waveform 51 expect_waveform = np.array([[0.96049707, 1.0, 1.0], [1.0, 1.0, 1.0]], dtype=np.float64) 52 label = np.random.sample((2, 1)) 53 data = (waveform, label) 54 dataset = ds.NumpySlicesDataset(data, ["channel", "sample"], shuffle=False) 55 allpass_biquad_op = audio.AllpassBiquad(44100, 200.0) 56 # Filtered waveform by allpassbiquad 57 dataset = dataset.map(input_columns=["channel"], operations=allpass_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, :], item['channel'], 0.0001, 0.0001) 61 i += 1 62 63 64def test_invalid_input_all(): 65 waveform = np.random.rand(2, 1000) 66 67 def test_invalid_input(test_name, sample_rate, central_freq, Q, error, error_msg): 68 logger.info("Test Allpassallpassiquad with bad input: {0}".format(test_name)) 69 with pytest.raises(error) as error_info: 70 audio.AllpassBiquad(sample_rate, central_freq, Q)(waveform) 71 assert error_msg in str(error_info.value) 72 73 test_invalid_input("invalid sample_rate parameter type as a float", 44100.5, 200, 0.707, TypeError, 74 "Argument sample_rate with value 44100.5 is not of type [<class 'int'>]," 75 + " but got <class 'float'>.") 76 test_invalid_input("invalid sample_rate parameter type as a String", "44100", 200, 0.707, TypeError, 77 "Argument sample_rate with value 44100 is not of type [<class 'int'>]," + 78 " but got <class 'str'>.") 79 test_invalid_input("invalid contral_freq parameter type as a String", 44100, "200", 0.707, 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 Q parameter type as a String", 44100, 200, "0.707", TypeError, 83 "Argument Q with value 0.707 is not of type [<class 'float'>, <class 'int'>]," 84 + " but got <class 'str'>.") 85 test_invalid_input("invalid sample_rate parameter value", 441324343243242342345300, 200, 0.707, ValueError, 86 "Input sample_rate is not within the required interval of [-2147483648, 0) and (0, 2147483647].") 87 test_invalid_input("invalid contral_freq parameter value", 44100, 32434324324234321, 0.707, ValueError, 88 "Input central_freq is not within the required interval of [-16777216, 16777216].") 89 test_invalid_input("invalid sample_rate parameter value", None, 200, 0.707, TypeError, 90 "Argument sample_rate with value None is not of type [<class 'int'>]," 91 + " but got <class 'NoneType'>.") 92 test_invalid_input("invalid central_rate parameter value", 44100, None, 0.707, TypeError, 93 "Argument central_freq with value None is not of type [<class 'float'>, <class 'int'>]," 94 + " but got <class 'NoneType'>.") 95 test_invalid_input("invalid sample_rate parameter value", 0, 200, 0.707, ValueError, 96 "Input sample_rate is not within the required interval of [-2147483648, 0) and (0, 2147483647].") 97 test_invalid_input("invalid Q parameter value", 44100, 200, 1.707, ValueError, 98 "Input Q is not within the required interval of (0, 1].") 99 100 101if __name__ == '__main__': 102 test_func_allpass_biquad_eager() 103 test_func_allpass_biquad_pipeline() 104 test_invalid_input_all() 105