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_slice_op.py 8.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 Slice 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. def slice_compare(array, indexing):
  23. data = ds.NumpySlicesDataset([array])
  24. array = np.array(array)
  25. data = data.map(operations=ops.Slice(indexing))
  26. for d in data:
  27. if indexing is None:
  28. array = array[:]
  29. else:
  30. array = array[indexing]
  31. np.testing.assert_array_equal(array, d[0])
  32. def test_slice_all():
  33. slice_compare([1, 2, 3, 4, 5], None)
  34. slice_compare([1, 2, 3, 4, 5], ...)
  35. def test_slice_single_index():
  36. slice_compare([1, 2, 3, 4, 5], 0)
  37. slice_compare([1, 2, 3, 4, 5], 4)
  38. slice_compare([1, 2, 3, 4, 5], 2)
  39. slice_compare([1, 2, 3, 4, 5], -1)
  40. slice_compare([1, 2, 3, 4, 5], -5)
  41. slice_compare([1, 2, 3, 4, 5], -3)
  42. def test_slice_list_index():
  43. slice_compare([1, 2, 3, 4, 5], [0, 1, 4])
  44. slice_compare([1, 2, 3, 4, 5], [4, 1, 0])
  45. slice_compare([1, 2, 3, 4, 5], [-1, 1, 0])
  46. slice_compare([1, 2, 3, 4, 5], [-1, -4, -2])
  47. slice_compare([1, 2, 3, 4, 5], [3, 3, 3])
  48. slice_compare([1, 2, 3, 4, 5], [1, 1, 1, 1, 1])
  49. def test_slice_slice_obj_2s():
  50. slice_compare([1, 2, 3, 4, 5], slice(0, 2))
  51. slice_compare([1, 2, 3, 4, 5], slice(2, 4))
  52. slice_compare([1, 2, 3, 4, 5], slice(4, 10))
  53. def test_slice_slice_obj_1s():
  54. slice_compare([1, 2, 3, 4, 5], slice(1))
  55. slice_compare([1, 2, 3, 4, 5], slice(4))
  56. slice_compare([1, 2, 3, 4, 5], slice(10))
  57. def test_slice_slice_obj_3s():
  58. slice_compare([1, 2, 3, 4, 5], slice(0, 2, 1))
  59. slice_compare([1, 2, 3, 4, 5], slice(0, 4, 1))
  60. slice_compare([1, 2, 3, 4, 5], slice(0, 10, 1))
  61. slice_compare([1, 2, 3, 4, 5], slice(0, 5, 2))
  62. slice_compare([1, 2, 3, 4, 5], slice(0, 2, 2))
  63. slice_compare([1, 2, 3, 4, 5], slice(0, 1, 2))
  64. slice_compare([1, 2, 3, 4, 5], slice(4, 5, 1))
  65. slice_compare([1, 2, 3, 4, 5], slice(2, 5, 3))
  66. def test_slice_multiple_rows():
  67. dataset = [[1, 2], [3, 4, 5], [1], [1, 2, 3, 4, 5, 6, 7]]
  68. def gen():
  69. for row in dataset:
  70. yield (np.array(row),)
  71. data = ds.GeneratorDataset(gen, column_names=["col"])
  72. indexing = slice(0, 4)
  73. data = data.map(operations=ops.Slice(indexing))
  74. for i, d in enumerate(data):
  75. array = np.array(dataset[i])
  76. array = array[indexing]
  77. np.testing.assert_array_equal(array, d[0])
  78. def test_slice_slice_obj_3s_double():
  79. slice_compare([1., 2., 3., 4., 5.], slice(0, 2, 1))
  80. slice_compare([1., 2., 3., 4., 5.], slice(0, 4, 1))
  81. slice_compare([1., 2., 3., 4., 5.], slice(0, 10, 1))
  82. slice_compare([1., 2., 3., 4., 5.], slice(0, 5, 2))
  83. slice_compare([1., 2., 3., 4., 5.], slice(0, 2, 2))
  84. slice_compare([1., 2., 3., 4., 5.], slice(0, 1, 2))
  85. slice_compare([1., 2., 3., 4., 5.], slice(4, 5, 1))
  86. slice_compare([1., 2., 3., 4., 5.], slice(2, 5, 3))
  87. def test_slice_slice_obj_neg():
  88. slice_compare([1, 2, 3, 4, 5], slice(-1, -5, -1))
  89. slice_compare([1, 2, 3, 4, 5], slice(-1))
  90. slice_compare([1, 2, 3, 4, 5], slice(-2))
  91. slice_compare([1, 2, 3, 4, 5], slice(-1, -5, -2))
  92. slice_compare([1, 2, 3, 4, 5], slice(-5, -1, 2))
  93. slice_compare([1, 2, 3, 4, 5], slice(-5, -1))
  94. def test_slice_exceptions():
  95. with pytest.raises(RuntimeError) as info:
  96. slice_compare([1, 2, 3, 4, 5], 5)
  97. assert "Index 5 is out of bounds [0,5)" in str(info.value)
  98. with pytest.raises(RuntimeError) as info:
  99. slice_compare([1, 2, 3, 4, 5], slice(0))
  100. assert "Indices are empty, generated tensor would be empty." in str(info.value)
  101. with pytest.raises(RuntimeError) as info:
  102. slice_compare([1, 2, 3, 4, 5], slice(3, 1, 1))
  103. assert "Indices are empty, generated tensor would be empty." in str(info.value)
  104. with pytest.raises(RuntimeError) as info:
  105. slice_compare([1, 2, 3, 4, 5], slice(5, 10, 1))
  106. assert "Indices are empty, generated tensor would be empty." in str(info.value)
  107. with pytest.raises(RuntimeError) as info:
  108. slice_compare([1, 2, 3, 4, 5], slice(-1, -5, 1))
  109. assert "Indices are empty, generated tensor would be empty." in str(info.value)
  110. def test_slice_all_str():
  111. slice_compare([b"1", b"2", b"3", b"4", b"5"], None)
  112. slice_compare([b"1", b"2", b"3", b"4", b"5"], ...)
  113. def test_slice_single_index_str():
  114. slice_compare([b"1", b"2", b"3", b"4", b"5"], 0)
  115. slice_compare([b"1", b"2", b"3", b"4", b"5"], 4)
  116. slice_compare([b"1", b"2", b"3", b"4", b"5"], 2)
  117. slice_compare([b"1", b"2", b"3", b"4", b"5"], -1)
  118. slice_compare([b"1", b"2", b"3", b"4", b"5"], -5)
  119. slice_compare([b"1", b"2", b"3", b"4", b"5"], -3)
  120. def test_slice_list_index_str():
  121. slice_compare([b"1", b"2", b"3", b"4", b"5"], [0, 1, 4])
  122. slice_compare([b"1", b"2", b"3", b"4", b"5"], [4, 1, 0])
  123. slice_compare([b"1", b"2", b"3", b"4", b"5"], [-1, 1, 0])
  124. slice_compare([b"1", b"2", b"3", b"4", b"5"], [-1, -4, -2])
  125. slice_compare([b"1", b"2", b"3", b"4", b"5"], [3, 3, 3])
  126. slice_compare([b"1", b"2", b"3", b"4", b"5"], [1, 1, 1, 1, 1])
  127. def test_slice_slice_obj_2s_str():
  128. slice_compare([b"1", b"2", b"3", b"4", b"5"], slice(0, 2))
  129. slice_compare([b"1", b"2", b"3", b"4", b"5"], slice(2, 4))
  130. slice_compare([b"1", b"2", b"3", b"4", b"5"], slice(4, 10))
  131. def test_slice_slice_obj_1s_str():
  132. slice_compare([b"1", b"2", b"3", b"4", b"5"], slice(1))
  133. slice_compare([b"1", b"2", b"3", b"4", b"5"], slice(4))
  134. slice_compare([b"1", b"2", b"3", b"4", b"5"], slice(10))
  135. def test_slice_slice_obj_3s_str():
  136. slice_compare([b"1", b"2", b"3", b"4", b"5"], slice(0, 2, 1))
  137. slice_compare([b"1", b"2", b"3", b"4", b"5"], slice(0, 4, 1))
  138. slice_compare([b"1", b"2", b"3", b"4", b"5"], slice(0, 10, 1))
  139. slice_compare([b"1", b"2", b"3", b"4", b"5"], slice(0, 5, 2))
  140. slice_compare([b"1", b"2", b"3", b"4", b"5"], slice(0, 2, 2))
  141. slice_compare([b"1", b"2", b"3", b"4", b"5"], slice(0, 1, 2))
  142. slice_compare([b"1", b"2", b"3", b"4", b"5"], slice(4, 5, 1))
  143. slice_compare([b"1", b"2", b"3", b"4", b"5"], slice(2, 5, 3))
  144. def test_slice_slice_obj_neg_str():
  145. slice_compare([b"1", b"2", b"3", b"4", b"5"], slice(-1, -5, -1))
  146. slice_compare([b"1", b"2", b"3", b"4", b"5"], slice(-1))
  147. slice_compare([b"1", b"2", b"3", b"4", b"5"], slice(-2))
  148. slice_compare([b"1", b"2", b"3", b"4", b"5"], slice(-1, -5, -2))
  149. slice_compare([b"1", b"2", b"3", b"4", b"5"], slice(-5, -1, 2))
  150. slice_compare([b"1", b"2", b"3", b"4", b"5"], slice(-5, -1))
  151. def test_slice_exceptions_str():
  152. with pytest.raises(RuntimeError) as info:
  153. slice_compare([b"1", b"2", b"3", b"4", b"5"], 5)
  154. assert "Index 5 is out of bounds [0,5)" in str(info.value)
  155. with pytest.raises(RuntimeError) as info:
  156. slice_compare([b"1", b"2", b"3", b"4", b"5"], slice(0))
  157. assert "Indices are empty, generated tensor would be empty." in str(info.value)
  158. with pytest.raises(RuntimeError) as info:
  159. slice_compare([b"1", b"2", b"3", b"4", b"5"], slice(3, 1, 1))
  160. assert "Indices are empty, generated tensor would be empty." in str(info.value)
  161. with pytest.raises(RuntimeError) as info:
  162. slice_compare([b"1", b"2", b"3", b"4", b"5"], slice(5, 10, 1))
  163. assert "Indices are empty, generated tensor would be empty." in str(info.value)
  164. with pytest.raises(RuntimeError) as info:
  165. slice_compare([b"1", b"2", b"3", b"4", b"5"], slice(-1, -5, 1))
  166. assert "Indices are empty, generated tensor would be empty." in str(info.value)
  167. if __name__ == "__main__":
  168. test_slice_all()
  169. test_slice_single_index()
  170. test_slice_list_index()
  171. test_slice_slice_obj_3s()
  172. test_slice_slice_obj_2s()
  173. test_slice_slice_obj_1s()
  174. test_slice_slice_obj_neg()
  175. test_slice_exceptions()
  176. test_slice_slice_obj_3s_double()
  177. test_slice_all_str()
  178. test_slice_single_index_str()
  179. test_slice_list_index_str()
  180. test_slice_slice_obj_3s_str()
  181. test_slice_slice_obj_2s_str()
  182. test_slice_slice_obj_1s_str()
  183. test_slice_slice_obj_neg_str()
  184. test_slice_exceptions_str()
  185. test_slice_multiple_rows()