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 19import mindspore.dataset.transforms.py_transforms as py_ops 20 21 22def test_compose(): 23 ds.config.set_seed(0) 24 25 def test_config(arr, op_list): 26 try: 27 data = ds.NumpySlicesDataset(arr, column_names="col", shuffle=False) 28 data = data.map(operations=ops.Compose(op_list), input_columns=["col"]) 29 res = [] 30 for i in data.create_dict_iterator(num_epochs=1, output_numpy=True): 31 res.append(i["col"].tolist()) 32 return res 33 except (TypeError, ValueError) as e: 34 return str(e) 35 36 # test simple compose with only 1 op, this would generate a warning 37 assert test_config([[1, 0], [3, 4]], [ops.Fill(2)]) == [[2, 2], [2, 2]] 38 # test 1 column -> 2columns -> 1 -> 2 -> 1 39 assert test_config([[1, 0]], [ops.Duplicate(), ops.Concatenate(), ops.Duplicate(), ops.Concatenate()]) == [ 40 [1, 0] * 4] 41 # test one python transform followed by a C transform. type after oneHot is float (mixed use-case) 42 assert test_config([1, 0], [py_ops.OneHotOp(2), ops.TypeCast(mstype.int32)]) == [[0, 1], [1, 0]] 43 # test exceptions. compose, randomApply randomChoice use the same validator 44 assert "op_list[0] is neither a c_transform op" in test_config([1, 0], [1, ops.TypeCast(mstype.int32)]) 45 # test empty op list 46 assert "op_list can not be empty." in test_config([1, 0], []) 47 48 49if __name__ == "__main__": 50 test_compose() 51