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_sliding_window.py 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 SlidingWindow in mindspore.dataset
  17. """
  18. import numpy as np
  19. import mindspore.dataset as ds
  20. import mindspore.dataset.text as text
  21. def test_sliding_window_string():
  22. """ test sliding_window with string type"""
  23. inputs = [["大", "家", "早", "上", "好"]]
  24. expect = np.array([['大', '家'], ['家', '早'], ['早', '上'], ['上', '好']])
  25. dataset = ds.NumpySlicesDataset(inputs, column_names=["text"], shuffle=False)
  26. dataset = dataset.map(input_columns=["text"], operations=text.SlidingWindow(2, 0))
  27. result = []
  28. for data in dataset.create_dict_iterator():
  29. for i in range(data['text'].shape[0]):
  30. result.append([])
  31. for j in range(data['text'].shape[1]):
  32. result[i].append(data['text'][i][j].decode('utf8'))
  33. result = np.array(result)
  34. np.testing.assert_array_equal(result, expect)
  35. def test_sliding_window_number():
  36. inputs = [1]
  37. expect = np.array([[1]])
  38. def gen(nums):
  39. yield (np.array(nums),)
  40. dataset = ds.GeneratorDataset(gen(inputs), column_names=["number"])
  41. dataset = dataset.map(input_columns=["number"], operations=text.SlidingWindow(1, -1))
  42. for data in dataset.create_dict_iterator():
  43. np.testing.assert_array_equal(data['number'], expect)
  44. def test_sliding_window_big_width():
  45. inputs = [[1, 2, 3, 4, 5]]
  46. expect = np.array([])
  47. dataset = ds.NumpySlicesDataset(inputs, column_names=["number"], shuffle=False)
  48. dataset = dataset.map(input_columns=["number"], operations=text.SlidingWindow(30, 0))
  49. for data in dataset.create_dict_iterator():
  50. np.testing.assert_array_equal(data['number'], expect)
  51. def test_sliding_window_exception():
  52. try:
  53. _ = text.SlidingWindow(0, 0)
  54. assert False
  55. except ValueError:
  56. pass
  57. try:
  58. _ = text.SlidingWindow("1", 0)
  59. assert False
  60. except TypeError:
  61. pass
  62. try:
  63. _ = text.SlidingWindow(1, "0")
  64. assert False
  65. except TypeError:
  66. pass
  67. try:
  68. inputs = [[1, 2, 3, 4, 5]]
  69. dataset = ds.NumpySlicesDataset(inputs, column_names=["text"], shuffle=False)
  70. dataset = dataset.map(input_columns=["text"], operations=text.SlidingWindow(3, -100))
  71. for _ in dataset.create_dict_iterator():
  72. pass
  73. assert False
  74. except RuntimeError as e:
  75. assert "axis supports 0 or -1 only for now." in str(e)
  76. try:
  77. inputs = ["aa", "bb", "cc"]
  78. dataset = ds.NumpySlicesDataset(inputs, column_names=["text"], shuffle=False)
  79. dataset = dataset.map(input_columns=["text"], operations=text.SlidingWindow(2, 0))
  80. for _ in dataset.create_dict_iterator():
  81. pass
  82. assert False
  83. except RuntimeError as e:
  84. assert "SlidingWindosOp supports 1D Tensors only for now." in str(e)
  85. if __name__ == '__main__':
  86. test_sliding_window_string()
  87. test_sliding_window_number()
  88. test_sliding_window_big_width()
  89. test_sliding_window_exception()