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 16import numpy as np 17import pytest 18 19import mindspore.dataset as ds 20import mindspore.dataset.audio.transforms as a_c_trans 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, "\ndata_expected_std:{0}\ndata_me_error:{1}\nloss:{2}".format( 30 data_expected[greater], data_me[greater], error[greater]) 31 32 33def test_func_angle_001(): 34 """ 35 Eager Test 36 """ 37 arr = np.array([[73.04, -13.00], [57.49, 13.20], [-57.64, 6.51], [-52.25, 30.67], [-30.11, -18.34], 38 [-63.32, 99.33], [95.82, -24.76]], dtype=np.double) 39 expected = np.array([-0.17614017, 0.22569334, 3.02912684, 2.6107975, -2.59450886, 2.13831337, -0.25286988], 40 dtype=np.double) 41 angle_op = a_c_trans.Angle() 42 output = angle_op(arr) 43 count_unequal_element(expected, output, 0.0001, 0.0001) 44 45 46def test_func_angle_002(): 47 """ 48 Pipeline Test 49 """ 50 np.random.seed(6) 51 arr = np.array([[[84.25, -85.92], [-92.23, 23.06], [-7.33, -44.17], [-62.95, -14.73]], 52 [[93.09, 38.18], [-81.94, 71.34], [71.33, -39.00], [95.25, -32.94]]], dtype=np.double) 53 expected = np.array([[-0.79521156, 2.89658848, -1.73524737, -2.91173309], 54 [0.3892177, 2.42523905, -0.50034807, -0.33295219]], dtype=np.double) 55 label = np.random.sample((2, 4, 1)) 56 data = (arr, label) 57 dataset = ds.NumpySlicesDataset(data, column_names=["col1", "col2"], shuffle=False) 58 angle_op = a_c_trans.Angle() 59 dataset = dataset.map(operations=angle_op, input_columns=["col1"]) 60 for item1, item2 in zip(dataset.create_dict_iterator(output_numpy=True), expected): 61 count_unequal_element(item2, item1['col1'], 0.0001, 0.0001) 62 63 64def test_func_angle_003(): 65 """ 66 Pipeline Error Test 67 """ 68 np.random.seed(78) 69 arr = np.array([["11", "22"], ["33", "44"], ["55", "66"], ["77", "88"]]) 70 label = np.random.sample((4, 1)) 71 data = (arr, label) 72 dataset = ds.NumpySlicesDataset(data, column_names=["col1", 'col2'], shuffle=False) 73 angle_op = a_c_trans.Angle() 74 dataset = dataset.map(operations=angle_op, input_columns=["col1"]) 75 num_itr = 0 76 with pytest.raises(RuntimeError, match="input tensor type should be int, float or double"): 77 for _ in dataset.create_dict_iterator(output_numpy=True): 78 num_itr += 1 79 80 81if __name__ == "__main__": 82 test_func_angle_001() 83 test_func_angle_002() 84 test_func_angle_003() 85