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_fill_op.py 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 fill op
  17. """
  18. import numpy as np
  19. import pytest
  20. import mindspore.dataset as ds
  21. import mindspore.dataset.transforms.c_transforms as data_trans
  22. def test_fillop_basic():
  23. def gen():
  24. yield (np.array([4, 5, 6, 7], dtype=np.uint8),)
  25. data = ds.GeneratorDataset(gen, column_names=["col"])
  26. fill_op = data_trans.Fill(3)
  27. data = data.map(operations=fill_op, input_columns=["col"])
  28. expected = np.array([3, 3, 3, 3], dtype=np.uint8)
  29. for data_row in data:
  30. np.testing.assert_array_equal(data_row[0].asnumpy(), expected)
  31. def test_fillop_down_type_cast():
  32. def gen():
  33. yield (np.array([4, 5, 6, 7], dtype=np.uint8),)
  34. data = ds.GeneratorDataset(gen, column_names=["col"])
  35. fill_op = data_trans.Fill(-3)
  36. data = data.map(operations=fill_op, input_columns=["col"])
  37. expected = np.array([253, 253, 253, 253], dtype=np.uint8)
  38. for data_row in data:
  39. np.testing.assert_array_equal(data_row[0].asnumpy(), expected)
  40. def test_fillop_up_type_cast():
  41. def gen():
  42. yield (np.array([4, 5, 6, 7], dtype=np.float),)
  43. data = ds.GeneratorDataset(gen, column_names=["col"])
  44. fill_op = data_trans.Fill(3)
  45. data = data.map(operations=fill_op, input_columns=["col"])
  46. expected = np.array([3., 3., 3., 3.], dtype=np.float)
  47. for data_row in data:
  48. np.testing.assert_array_equal(data_row[0].asnumpy(), expected)
  49. def test_fillop_string():
  50. def gen():
  51. yield (np.array(["45555", "45555"], dtype='S'),)
  52. data = ds.GeneratorDataset(gen, column_names=["col"])
  53. fill_op = data_trans.Fill("error")
  54. data = data.map(operations=fill_op, input_columns=["col"])
  55. expected = np.array(['error', 'error'], dtype='S')
  56. for data_row in data.create_tuple_iterator(output_numpy=True):
  57. np.testing.assert_array_equal(data_row[0], expected)
  58. def test_fillop_bytes():
  59. def gen():
  60. yield (np.array(["A", "B", "C"], dtype='S'),)
  61. data = ds.GeneratorDataset(gen, column_names=["col"])
  62. fill_op = data_trans.Fill(b'abc')
  63. data = data.map(operations=fill_op, input_columns=["col"])
  64. expected = np.array([b'abc', b'abc', b'abc'], dtype='S')
  65. for data_row in data.create_tuple_iterator(output_numpy=True):
  66. np.testing.assert_array_equal(data_row[0], expected)
  67. def test_fillop_error_handling():
  68. def gen():
  69. yield (np.array([4, 4, 4, 4]),)
  70. data = ds.GeneratorDataset(gen, column_names=["col"])
  71. fill_op = data_trans.Fill("words")
  72. data = data.map(operations=fill_op, input_columns=["col"])
  73. with pytest.raises(RuntimeError) as error_info:
  74. for _ in data:
  75. pass
  76. assert "fill datatype does not match the input datatype" in str(error_info.value)
  77. if __name__ == "__main__":
  78. test_fillop_basic()
  79. test_fillop_up_type_cast()
  80. test_fillop_down_type_cast()
  81. test_fillop_string()
  82. test_fillop_bytes()
  83. test_fillop_error_handling()