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