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_tensor_string.py 1.8 kB

6 years ago
6 years ago
6 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright 2019 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. import mindspore._c_dataengine as cde
  16. import numpy as np
  17. import pytest
  18. import mindspore.dataset as ds
  19. def test_basic():
  20. x = np.array([["ab", "cde", "121"], ["x", "km", "789"]], dtype='S')
  21. # x = np.array(["ab", "cde"], dtype='S')
  22. n = cde.Tensor(x)
  23. arr = n.as_array()
  24. y = np.array([1, 2])
  25. assert all(y == y)
  26. # assert np.testing.assert_array_equal(y,y)
  27. def compare(strings):
  28. arr = np.array(strings, dtype='S')
  29. def gen():
  30. yield arr,
  31. data = ds.GeneratorDataset(gen, column_names=["col"])
  32. for d in data:
  33. np.testing.assert_array_equal(d[0], arr)
  34. def test_generator():
  35. compare(["ab"])
  36. compare(["ab", "cde", "121"])
  37. compare([["ab", "cde", "121"], ["x", "km", "789"]])
  38. def test_batching_strings():
  39. def gen():
  40. yield np.array(["ab", "cde", "121"], dtype='S'),
  41. data = ds.GeneratorDataset(gen, column_names=["col"]).batch(10)
  42. with pytest.raises(RuntimeError) as info:
  43. for _ in data:
  44. pass
  45. assert "[Batch ERROR] Batch does not support" in str(info)
  46. if __name__ == '__main__':
  47. test_generator()
  48. test_basic()
  49. test_batching_strings()