You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_c_random_apply.py 2.1 kB

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