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 HighpassBiquad 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, \ 34 "\ndata_expected_std:{0}\ndata_me_error:{1}\nloss:{2}". \ 35 format(data_expected[greater], data_me[greater], error[greater]) 36 37 38def test_highpass_biquad_eager(): 39 """ mindspore eager mode normal testcase:highpass_biquad op""" 40 # Original waveform 41 waveform = np.array([[0.8236, 0.2049, 0.3335], [0.5933, 0.9911, 0.2482], 42 [0.3007, 0.9054, 0.7598], [0.5394, 0.2842, 0.5634], [0.6363, 0.2226, 0.2288]]) 43 # Expect waveform 44 expect_waveform = np.array([[0.2745, -0.4808, 0.1576], [0.1978, -0.0652, -0.4462], 45 [0.1002, 0.1013, -0.2835], [0.1798, -0.2649, 0.1182], [0.2121, -0.3500, 0.0693]]) 46 highpass_biquad_op = audio.HighpassBiquad(4000, 1000.0, 1) 47 # Filtered waveform by highpass_biquad 48 output = highpass_biquad_op(waveform) 49 count_unequal_element(expect_waveform, output, 0.0001, 0.0001) 50 51 52def test_highpass_biquad_pipeline(): 53 """ mindspore pipeline mode normal testcase:highpass_biquad op""" 54 # Original waveform 55 waveform = np.array([[0.4063, 0.7729, 0.2325], [0.2687, 0.1426, 0.8987], 56 [0.6914, 0.6681, 0.1783], [0.2704, 0.2680, 0.7975], [0.5880, 0.1776, 0.6323]]) 57 # Expect waveform 58 expect_waveform = np.array([[0.1354, -0.0133, -0.3474], [0.0896, -0.1316, 0.2642], 59 [0.2305, -0.2382, -0.2323], [0.0901, -0.0909, 0.1473], [0.1960, -0.3328, 0.2230]]) 60 dataset = ds.NumpySlicesDataset(waveform, ["col1"], shuffle=False) 61 highpass_biquad_op = audio.HighpassBiquad(4000, 1000.0, 1) 62 # Filtered waveform by highpass_biquad 63 dataset = dataset.map( 64 input_columns=["col1"], operations=highpass_biquad_op, num_parallel_workers=4) 65 i = 0 66 for item in dataset.create_dict_iterator(output_numpy=True): 67 count_unequal_element(expect_waveform[i, :], 68 item["col1"], 0.0001, 0.0001) 69 i += 1 70 71 72def test_highpass_biquad_invalid_input(): 73 """ 74 Test invalid input of HighpassBiquad 75 """ 76 def test_invalid_input(test_name, sample_rate, cutoff_freq, Q, error, error_msg): 77 logger.info("Test HighpassBiquad with bad input: {0}".format(test_name)) 78 with pytest.raises(error) as error_info: 79 audio.HighpassBiquad(sample_rate, cutoff_freq, Q) 80 assert error_msg in str(error_info.value) 81 82 test_invalid_input("invalid sample_rate parameter type as a float", 44100.5, 1000, 0.707, TypeError, 83 "Argument sample_rate with value 44100.5 is not of type [<class 'int'>]," 84 " but got <class 'float'>.") 85 test_invalid_input("invalid sample_rate parameter type as a String", "44100", 1000, 0.707, TypeError, 86 "Argument sample_rate with value 44100 is not of type [<class 'int'>]," 87 " but got <class 'str'>.") 88 test_invalid_input("invalid cutoff_freq parameter type as a String", 44100, "1000", 0.707, TypeError, 89 "Argument cutoff_freq with value 1000 is not of type [<class 'float'>, <class 'int'>]," 90 " but got <class 'str'>.") 91 test_invalid_input("invalid Q parameter type as a String", 44100, 1000, "0.707", TypeError, 92 "Argument Q with value 0.707 is not of type [<class 'float'>, <class 'int'>]," 93 " but got <class 'str'>.") 94 95 test_invalid_input("invalid sample_rate parameter value", 441324343243242342345300, 1000, 0.707, ValueError, 96 "Input sample_rate is not within the required interval of [-2147483648, 0) and (0, 2147483647].") 97 test_invalid_input("invalid cutoff_freq parameter value", 44100, 32434324324234321, 0.707, ValueError, 98 "Input cutoff_freq is not within the required interval of [-16777216, 16777216].") 99 100 test_invalid_input("invalid sample_rate parameter value", None, 1000, 0.707, TypeError, 101 "Argument sample_rate with value None is not of type [<class 'int'>], " 102 "but got <class 'NoneType'>.") 103 test_invalid_input("invalid cutoff_rate parameter value", 44100, None, 0.707, TypeError, 104 "Argument cutoff_freq with value None is not of type [<class 'float'>, <class 'int'>]," 105 " but got <class 'NoneType'>.") 106 107 test_invalid_input("invalid sample_rate parameter value", 0, 1000, 0.707, ValueError, 108 "Input sample_rate is not within the required interval of [-2147483648, 0) and (0, 2147483647].") 109 test_invalid_input("invalid Q parameter value", 44100, 1000, 0, ValueError, 110 "Input Q is not within the required interval of (0, 1].") 111 112 113if __name__ == "__main__": 114 test_highpass_biquad_eager() 115 test_highpass_biquad_pipeline() 116 test_highpass_biquad_invalid_input() 117