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_mask_op.py 5.3 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 Mask op in DE
  17. """
  18. import numpy as np
  19. import pytest
  20. import mindspore.common.dtype as mstype
  21. import mindspore.dataset as ds
  22. import mindspore.dataset.transforms.c_transforms as ops
  23. mstype_to_np_type = {
  24. mstype.bool_: np.bool,
  25. mstype.int8: np.int8,
  26. mstype.uint8: np.uint8,
  27. mstype.int16: np.int16,
  28. mstype.uint16: np.uint16,
  29. mstype.int32: np.int32,
  30. mstype.uint32: np.uint32,
  31. mstype.int64: np.int64,
  32. mstype.uint64: np.uint64,
  33. mstype.float16: np.float16,
  34. mstype.float32: np.float32,
  35. mstype.float64: np.float64,
  36. mstype.string: np.str
  37. }
  38. def mask_compare(array, op, constant, dtype=mstype.bool_):
  39. data = ds.NumpySlicesDataset([array])
  40. array = np.array(array)
  41. data = data.map(operations=ops.Mask(op, constant, dtype))
  42. for d in data:
  43. if op == ops.Relational.EQ:
  44. array = array == np.array(constant, dtype=array.dtype)
  45. elif op == ops.Relational.NE:
  46. array = array != np.array(constant, dtype=array.dtype)
  47. elif op == ops.Relational.GT:
  48. array = array > np.array(constant, dtype=array.dtype)
  49. elif op == ops.Relational.GE:
  50. array = array >= np.array(constant, dtype=array.dtype)
  51. elif op == ops.Relational.LT:
  52. array = array < np.array(constant, dtype=array.dtype)
  53. elif op == ops.Relational.LE:
  54. array = array <= np.array(constant, dtype=array.dtype)
  55. array = array.astype(dtype=mstype_to_np_type[dtype])
  56. np.testing.assert_array_equal(array, d[0])
  57. def test_mask_int_comparison():
  58. for k in mstype_to_np_type:
  59. if k == mstype.string:
  60. continue
  61. mask_compare([1, 2, 3, 4, 5], ops.Relational.EQ, 3, k)
  62. mask_compare([1, 2, 3, 4, 5], ops.Relational.NE, 3, k)
  63. mask_compare([1, 2, 3, 4, 5], ops.Relational.LT, 3, k)
  64. mask_compare([1, 2, 3, 4, 5], ops.Relational.LE, 3, k)
  65. mask_compare([1, 2, 3, 4, 5], ops.Relational.GT, 3, k)
  66. mask_compare([1, 2, 3, 4, 5], ops.Relational.GE, 3, k)
  67. def test_mask_float_comparison():
  68. for k in mstype_to_np_type:
  69. if k == mstype.string:
  70. continue
  71. mask_compare([1.5, 2.5, 3., 4.5, 5.5], ops.Relational.EQ, 3, k)
  72. mask_compare([1.5, 2.5, 3., 4.5, 5.5], ops.Relational.NE, 3, k)
  73. mask_compare([1.5, 2.5, 3., 4.5, 5.5], ops.Relational.LT, 3, k)
  74. mask_compare([1.5, 2.5, 3., 4.5, 5.5], ops.Relational.LE, 3, k)
  75. mask_compare([1.5, 2.5, 3., 4.5, 5.5], ops.Relational.GT, 3, k)
  76. mask_compare([1.5, 2.5, 3., 4.5, 5.5], ops.Relational.GE, 3, k)
  77. def test_mask_float_comparison2():
  78. for k in mstype_to_np_type:
  79. if k == mstype.string:
  80. continue
  81. mask_compare([1, 2, 3, 4, 5], ops.Relational.EQ, 3.5, k)
  82. mask_compare([1, 2, 3, 4, 5], ops.Relational.NE, 3.5, k)
  83. mask_compare([1, 2, 3, 4, 5], ops.Relational.LT, 3.5, k)
  84. mask_compare([1, 2, 3, 4, 5], ops.Relational.LE, 3.5, k)
  85. mask_compare([1, 2, 3, 4, 5], ops.Relational.GT, 3.5, k)
  86. mask_compare([1, 2, 3, 4, 5], ops.Relational.GE, 3.5, k)
  87. def test_mask_string_comparison():
  88. for k in mstype_to_np_type:
  89. if k == mstype.string:
  90. continue
  91. mask_compare(["1.5", "2.5", "3.", "4.5", "5.5"], ops.Relational.EQ, "3.", k)
  92. mask_compare(["1.5", "2.5", "3.", "4.5", "5.5"], ops.Relational.NE, "3.", k)
  93. mask_compare(["1.5", "2.5", "3.", "4.5", "5.5"], ops.Relational.LT, "3.", k)
  94. mask_compare(["1.5", "2.5", "3.", "4.5", "5.5"], ops.Relational.LE, "3.", k)
  95. mask_compare(["1.5", "2.5", "3.", "4.5", "5.5"], ops.Relational.GT, "3.", k)
  96. mask_compare(["1.5", "2.5", "3.", "4.5", "5.5"], ops.Relational.GE, "3.", k)
  97. def test_mask_exceptions_str():
  98. with pytest.raises(RuntimeError) as info:
  99. mask_compare([1, 2, 3, 4, 5], ops.Relational.EQ, "3.5")
  100. assert "Cannot convert constant value to the type of the input tensor." in str(info.value)
  101. with pytest.raises(RuntimeError) as info:
  102. mask_compare(["1", "2", "3", "4", "5"], ops.Relational.EQ, 3.5)
  103. assert "Cannot convert constant value to the type of the input tensor." in str(info.value)
  104. with pytest.raises(RuntimeError) as info:
  105. mask_compare(["1", "2", "3", "4", "5"], ops.Relational.EQ, "3.5", mstype.string)
  106. assert "Cannot generate a string mask. Type should be numeric." in str(info.value)
  107. if __name__ == "__main__":
  108. test_mask_int_comparison()
  109. test_mask_float_comparison()
  110. test_mask_float_comparison2()
  111. test_mask_string_comparison()
  112. test_mask_exceptions_str()