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_datasets_dbpedia.py 4.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # Copyright 2021 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.dataset as ds
  16. DATA_DIR = '../data/dataset/testDBpedia/'
  17. def test_dbpedia_dataset_basic():
  18. """
  19. Feature: DBpediaDataset.
  20. Description: read data from train file.
  21. Expectation: the data is processed successfully.
  22. """
  23. buffer = []
  24. data = ds.DBpediaDataset(DATA_DIR, usage="train", shuffle=False)
  25. data = data.repeat(2)
  26. data = data.skip(3)
  27. for d in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  28. buffer.append(d)
  29. assert len(buffer) == 3
  30. def test_dbpedia_dataset_quoted():
  31. """
  32. Feature: DBpediaDataset.
  33. Description: read the data and compare it to expectations.
  34. Expectation: the data is processed successfully.
  35. """
  36. data = ds.DBpediaDataset(DATA_DIR, usage="test", shuffle=False)
  37. buffer = []
  38. for d in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  39. buffer.extend([d['class'].item().decode("utf8"),
  40. d['title'].item().decode("utf8"),
  41. d['content'].item().decode("utf8")])
  42. assert buffer == ["5", "My Bedroom", "Look at this room. It's my bedroom.",
  43. "8", "My English teacher", "She has two big eyes and a small mouth.",
  44. "6", "My Holiday", "I have a lot of fun every day."]
  45. def test_dbpedia_dataset_usage():
  46. """
  47. Feature: DBpediaDataset.
  48. Description: read all files with usage all.
  49. Expectation: the data is processed successfully.
  50. """
  51. buffer = []
  52. data = ds.DBpediaDataset(DATA_DIR, usage="all", shuffle=False)
  53. for d in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  54. buffer.append(d)
  55. assert len(buffer) == 6
  56. def test_dbpedia_dataset_get_datasetsize():
  57. """
  58. Feature: DBpediaDataset.
  59. Description: test get_dataset_size function.
  60. Expectation: the data is processed successfully.
  61. """
  62. data = ds.DBpediaDataset(DATA_DIR, usage="test", shuffle=False)
  63. size = data.get_dataset_size()
  64. assert size == 3
  65. def test_dbpedia_dataset_distribution():
  66. """
  67. Feature: DBpediaDataset.
  68. Description: test in a distributed state.
  69. Expectation: the data is processed successfully.
  70. """
  71. data = ds.DBpediaDataset(DATA_DIR, usage="test", shuffle=False, num_shards=2, shard_id=0)
  72. count = 0
  73. for _ in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  74. count += 1
  75. assert count == 2
  76. def test_dbpedia_dataset_num_samples():
  77. """
  78. Feature: DBpediaDataset.
  79. Description: test num_samples parameter.
  80. Expectation: the data is processed successfully.
  81. """
  82. data = ds.DBpediaDataset(DATA_DIR, usage="test", shuffle=False, num_samples=2)
  83. count = 0
  84. for _ in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  85. count += 1
  86. assert count == 2
  87. def test_dbpedia_dataset_exception():
  88. """
  89. Feature: DBpediaDataset.
  90. Description: test the wrong input.
  91. Expectation: Unable to read data properly.
  92. """
  93. def exception_func(item):
  94. raise Exception("Error occur!")
  95. try:
  96. data = ds.DBpediaDataset(DATA_DIR, usage="test", shuffle=False)
  97. data = data.map(operations=exception_func, input_columns=["class"], num_parallel_workers=1)
  98. for _ in data.create_dict_iterator():
  99. pass
  100. assert False
  101. except RuntimeError as e:
  102. assert "map operation: [PyFunc] failed. The corresponding data files" in str(e)
  103. try:
  104. data = ds.DBpediaDataset(DATA_DIR, usage="test", shuffle=False)
  105. data = data.map(operations=exception_func, input_columns=["content"], num_parallel_workers=1)
  106. for _ in data.create_dict_iterator():
  107. pass
  108. assert False
  109. except RuntimeError as e:
  110. assert "map operation: [PyFunc] failed. The corresponding data files" in str(e)
  111. if __name__ == "__main__":
  112. test_dbpedia_dataset_basic()
  113. test_dbpedia_dataset_quoted()
  114. test_dbpedia_dataset_usage()
  115. test_dbpedia_dataset_get_datasetsize()
  116. test_dbpedia_dataset_distribution()
  117. test_dbpedia_dataset_num_samples()
  118. test_dbpedia_dataset_exception()