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_bass_biquad_eager(): 33 """ mindspore eager mode normal testcase:bass_biquad op""" 34 35 # Original waveform 36 waveform = np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float64) 37 # Expect waveform 38 expect_waveform = np.array([[0.10409035359, 0.21652136269, 0.33761211292], 39 [0.41636141439, 0.55381438997, 0.70088436361]], dtype=np.float64) 40 bass_biquad_op = audio.BassBiquad(44100, 50.0, 100.0, 0.707) 41 # Filtered waveform by bassbiquad 42 output = bass_biquad_op(waveform) 43 count_unequal_element(expect_waveform, output, 0.0001, 0.0001) 44 45 46def test_func_bass_biquad_pipeline(): 47 """ mindspore pipeline mode normal testcase:bass_biquad op""" 48 49 # Original waveform 50 waveform = np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float64) 51 # Expect waveform 52 expect_waveform = np.array([[0.10409035359, 0.21652136269, 0.33761211292], 53 [0.41636141439, 0.55381438997, 0.70088436361]], dtype=np.float64) 54 label = np.random.sample((2, 1)) 55 data = (waveform, label) 56 dataset = ds.NumpySlicesDataset(data, ["channel", "sample"], shuffle=False) 57 bass_biquad_op = audio.BassBiquad(44100, 50, 100.0, 0.707) 58 # Filtered waveform by bassbiquad 59 dataset = dataset.map( 60 input_columns=["channel"], operations=bass_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_invalid_invalid_input(): 69 def test_invalid_input(test_name, sample_rate, gain, central_freq, Q, error, error_msg): 70 logger.info("Test BassBiquad with bad input: {0}".format(test_name)) 71 with pytest.raises(error) as error_info: 72 audio.BassBiquad(sample_rate, gain, central_freq, Q) 73 assert error_msg in str(error_info.value) 74 75 test_invalid_input("invalid sample_rate parameter type as a float", 44100.5, 50.0, 200, 0.707, 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", 50.0, 200, 0.707, TypeError, 79 "Argument sample_rate with value 44100 is not of type [<class 'int'>]," 80 " but got <class 'str'>.") 81 test_invalid_input("invalid gain parameter type as a String", 44100, "50.0", 200, 0.707, TypeError, 82 "Argument gain with value 50.0 is not of type [<class 'float'>, <class 'int'>]," 83 " but got <class 'str'>.") 84 test_invalid_input("invalid contral_freq parameter type as a String", 44100, 50.0, "200", 0.707, TypeError, 85 "Argument central_freq with value 200 is not of type [<class 'float'>, <class 'int'>]," 86 " but got <class 'str'>.") 87 test_invalid_input("invalid Q parameter type as a String", 44100, 50.0, 200, "0.707", TypeError, 88 "Argument Q with value 0.707 is not of type [<class 'float'>, <class 'int'>]," 89 " but got <class 'str'>.") 90 91 test_invalid_input("invalid sample_rate parameter value", 441324343243242342345300, 50.0, 200, 0.707, ValueError, 92 "Input sample_rate is not within the required interval of [-2147483648, 0) and (0, 2147483647].") 93 test_invalid_input("invalid gain parameter value", 44100, 32434324324234321, 200, 0.707, ValueError, 94 "Input gain is not within the required interval of [-16777216, 16777216].") 95 test_invalid_input("invalid contral_freq parameter value", 44100, 50, 32434324324234321, 0.707, ValueError, 96 "Input central_freq is not within the required interval of [-16777216, 16777216].") 97 98 test_invalid_input("invalid sample_rate parameter value", None, 50.0, 200, 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 gain parameter value", 44100, None, 200, 0.707, TypeError, 102 "Argument gain with value None is not of type [<class 'float'>, <class 'int'>], " 103 "but got <class 'NoneType'>.") 104 test_invalid_input("invalid central_rate parameter value", 44100, 50.0, None, 0.707, TypeError, 105 "Argument central_freq with value None is not of type [<class 'float'>, <class 'int'>]," 106 " but got <class 'NoneType'>.") 107 108 test_invalid_input("invalid sample_rate parameter value", 0, 50.0, 200, 0.707, ValueError, 109 "Input sample_rate is not within the required interval of [-2147483648, 0) and (0, 2147483647].") 110 test_invalid_input("invalid Q parameter value", 44100, 50.0, 200, 1.707, ValueError, 111 "Input Q is not within the required interval of (0, 1].") 112 113 114if __name__ == '__main__': 115 test_func_bass_biquad_eager() 116 test_func_bass_biquad_pipeline() 117 test_invalid_invalid_input() 118