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 17 18import mindspore.dataset as ds 19import mindspore.dataset.audio.transforms as audio 20from mindspore import log as logger 21 22 23def count_unequal_element(data_expected, data_me, rtol, atol): 24 assert data_expected.shape == data_me.shape 25 total_count = len(data_expected.flatten()) 26 error = np.abs(data_expected - data_me) 27 greater = np.greater(error, atol + np.abs(data_expected) * rtol) 28 loss_count = np.count_nonzero(greater) 29 assert (loss_count / total_count) < rtol, \ 30 "\ndata_expected_std:{0}\ndata_me_error:{1}\nloss:{2}". \ 31 format(data_expected[greater], data_me[greater], error[greater]) 32 33 34def test_func_deemph_biquad_eager(): 35 """ mindspore eager mode normal testcase:deemph_biquad op""" 36 # Original waveform 37 waveform = np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float64) 38 # Expect waveform 39 expect_waveform = np.array([[0.04603508, 0.11216372, 0.19070681], 40 [0.18414031, 0.31054966, 0.42633607]], dtype=np.float64) 41 deemph_biquad_op = audio.DeemphBiquad(44100) 42 # Filtered waveform by deemphbiquad 43 output = deemph_biquad_op(waveform) 44 count_unequal_element(expect_waveform, output, 0.0001, 0.0001) 45 46 47def test_func_deemph_biquad_pipeline(): 48 """ mindspore pipeline mode normal testcase:deemph_biquad op""" 49 # Original waveform 50 waveform = np.array([[0.2, 0.2, 0.3], [0.4, 0.5, 0.7]], dtype=np.float64) 51 # Expect waveform 52 expect_waveform = np.array([[0.0895, 0.1279, 0.1972], 53 [0.1791, 0.3006, 0.4583]], dtype=np.float64) 54 dataset = ds.NumpySlicesDataset(waveform, ["audio"], shuffle=False) 55 deemph_biquad_op = audio.DeemphBiquad(48000) 56 # Filtered waveform by deemphbiquad 57 dataset = dataset.map(input_columns=["audio"], operations=deemph_biquad_op, num_parallel_workers=8) 58 i = 0 59 for data in dataset.create_dict_iterator(output_numpy=True): 60 count_unequal_element(expect_waveform[i, :], data['audio'], 0.0001, 0.0001) 61 i += 1 62 63 64def test_invalid_input_all(): 65 waveform = np.random.rand(2, 1000) 66 def test_invalid_input(test_name, sample_rate, error, error_msg): 67 logger.info("Test DeemphBiquad with bad input: {0}".format(test_name)) 68 with pytest.raises(error) as error_info: 69 audio.DeemphBiquad(sample_rate)(waveform) 70 assert error_msg in str(error_info.value) 71 72 test_invalid_input("invalid sample_rate parameter type as a float", 44100.5, TypeError, 73 "Argument sample_rate with value 44100.5 is not of type [<class 'int'>]," 74 + " but got <class 'float'>.") 75 test_invalid_input("invalid sample_rate parameter type as a String", "44100", TypeError, 76 "Argument sample_rate with value 44100 is not of type [<class 'int'>]," 77 + " but got <class 'str'>.") 78 test_invalid_input("invalid sample_rate parameter value", 45000, ValueError, 79 "Input sample_rate should be 44100 or 48000, but got 45000.") 80 81 82if __name__ == '__main__': 83 test_func_deemph_biquad_eager() 84 test_func_deemph_biquad_pipeline() 85 test_invalid_input_all() 86