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.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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(input_columns=["col"], operations=fill_op)
  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], 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(input_columns=["col"], operations=fill_op)
  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], 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(input_columns=["col"], operations=fill_op)
  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], 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(input_columns=["col"], operations=fill_op)
  55. expected = np.array(['error', 'error'], dtype='S')
  56. for data_row in data:
  57. np.testing.assert_array_equal(data_row[0], expected)
  58. def test_fillop_error_handling():
  59. def gen():
  60. yield (np.array([4, 4, 4, 4]),)
  61. data = ds.GeneratorDataset(gen, column_names=["col"])
  62. fill_op = data_trans.Fill("words")
  63. data = data.map(input_columns=["col"], operations=fill_op)
  64. with pytest.raises(RuntimeError) as error_info:
  65. for _ in data:
  66. pass
  67. assert "Types do not match" in str(error_info.value)
  68. if __name__ == "__main__":
  69. test_fillop_basic()
  70. test_fillop_up_type_cast()
  71. test_fillop_down_type_cast()
  72. test_fillop_string()
  73. test_fillop_error_handling()