1# Copyright 2020 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 mindspore.common.dtype as mstype 17import mindspore.dataset as ds 18import mindspore.dataset.transforms.c_transforms as ops 19 20 21def test_random_apply(): 22 ds.config.set_seed(0) 23 24 def test_config(arr, op_list, prob=0.5): 25 try: 26 data = ds.NumpySlicesDataset(arr, column_names="col", shuffle=False) 27 data = data.map(operations=ops.RandomApply(op_list, prob), input_columns=["col"]) 28 res = [] 29 for i in data.create_dict_iterator(num_epochs=1, output_numpy=True): 30 res.append(i["col"].tolist()) 31 return res 32 except (TypeError, ValueError) as e: 33 return str(e) 34 35 res1 = test_config([[0, 1]], [ops.Duplicate(), ops.Concatenate()]) 36 assert res1 in [[[0, 1]], [[0, 1, 0, 1]]] 37 # test single nested compose 38 assert test_config([[0, 1, 2]], [ops.Compose([ops.Duplicate(), ops.Concatenate(), ops.Slice([0, 1, 2])])]) == [ 39 [0, 1, 2]] 40 # test exception 41 assert "is not of type [<class 'list'>]" in test_config([1, 0], ops.TypeCast(mstype.int32)) 42 assert "Input prob is not within the required interval" in test_config([0, 1], [ops.Slice([0, 1])], 1.1) 43 assert "is not of type [<class 'float'>, <class 'int'>]" in test_config([1, 0], [ops.TypeCast(mstype.int32)], None) 44 assert "op_list with value None is not of type [<class 'list'>]" in test_config([1, 0], None) 45 46 47if __name__ == "__main__": 48 test_random_apply() 49