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_pad_end_op.py 2.6 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. """
  16. Testing PadEnd op in DE
  17. """
  18. import numpy as np
  19. import pytest
  20. import mindspore.dataset as ds
  21. import mindspore.dataset.transforms.c_transforms as ops
  22. # Extensive testing of PadEnd is already done in batch with Pad test cases
  23. def pad_compare(array, pad_shape, pad_value, res):
  24. data = ds.NumpySlicesDataset([array])
  25. if pad_value is not None:
  26. data = data.map(operations=ops.PadEnd(pad_shape, pad_value))
  27. else:
  28. data = data.map(operations=ops.PadEnd(pad_shape))
  29. for d in data.create_tuple_iterator(output_numpy=True):
  30. np.testing.assert_array_equal(res, d[0])
  31. def test_pad_end_basics():
  32. pad_compare([1, 2], [3], -1, [1, 2, -1])
  33. pad_compare([1, 2, 3], [3], -1, [1, 2, 3])
  34. pad_compare([1, 2, 3], [2], -1, [1, 2])
  35. pad_compare([1, 2, 3], [5], None, [1, 2, 3, 0, 0])
  36. def test_pad_end_str():
  37. pad_compare([b"1", b"2"], [3], b"-1", [b"1", b"2", b"-1"])
  38. pad_compare([b"1", b"2", b"3"], [3], b"-1", [b"1", b"2", b"3"])
  39. pad_compare([b"1", b"2", b"3"], [2], b"-1", [b"1", b"2"])
  40. pad_compare([b"1", b"2", b"3"], [5], None, [b"1", b"2", b"3", b"", b""])
  41. def test_pad_end_exceptions():
  42. with pytest.raises(RuntimeError) as info:
  43. pad_compare([1, 2], [3], "-1", [])
  44. assert "pad_value and item of dataset are not of the same type" in str(info.value)
  45. with pytest.raises(RuntimeError) as info:
  46. pad_compare([b"1", b"2", b"3", b"4", b"5"], [2], 1, [])
  47. assert "pad_value and item of dataset are not of the same type" in str(info.value)
  48. with pytest.raises(TypeError) as info:
  49. pad_compare([3, 4, 5], ["2"], 1, [])
  50. assert "a value in the list is not an integer." in str(info.value)
  51. with pytest.raises(TypeError) as info:
  52. pad_compare([1, 2], 3, -1, [1, 2, -1])
  53. assert "Argument pad_shape with value 3 is not of type [<class 'list'>]" in str(info.value)
  54. if __name__ == "__main__":
  55. test_pad_end_basics()
  56. test_pad_end_str()
  57. test_pad_end_exceptions()