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_yahoo_answers.py 5.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/testYahooAnswers/'
  17. def test_yahoo_answers_dataset_basic():
  18. """
  19. Feature: YahooAnswersDataset.
  20. Description: read data from train file.
  21. Expectation: the data is processed successfully.
  22. """
  23. buffer = []
  24. data = ds.YahooAnswersDataset(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) == 5
  30. def test_yahoo_answers_dataset_quoted():
  31. """
  32. Feature: YahooAnswersDataset.
  33. Description: read the data and compare it to expectations.
  34. Expectation: the data is processed successfully.
  35. """
  36. data = ds.YahooAnswersDataset(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. d['answer'].item().decode("utf8")])
  43. assert buffer == ["4", "My pet", "My pet is a toy bear.", "He is white.",
  44. "1", "My favourite seasion", "My favorite season is summer.",
  45. "In summer it is often sunny and hot."]
  46. def test_yahoo_answers_dataset_usage():
  47. """
  48. Feature: YahooAnswersDataset.
  49. Description: read all files with usage all.
  50. Expectation: the data is processed successfully.
  51. """
  52. buffer = []
  53. data = ds.YahooAnswersDataset(DATA_DIR, usage="all", shuffle=False)
  54. for d in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  55. buffer.append(d)
  56. assert len(buffer) == 6
  57. def test_yahoo_answers_dataset_get_datasetsize():
  58. """
  59. Feature: YahooAnswersDataset.
  60. Description: test get_dataset_size function.
  61. Expectation: the data is processed successfully.
  62. """
  63. data = ds.YahooAnswersDataset(DATA_DIR, usage="test", shuffle=False)
  64. size = data.get_dataset_size()
  65. assert size == 2
  66. def test_yahoo_answers_dataset_distribution():
  67. """
  68. Feature: YahooAnswersDataset.
  69. Description: test in a distributed state.
  70. Expectation: the data is processed successfully.
  71. """
  72. data = ds.YahooAnswersDataset(DATA_DIR, usage="test", shuffle=False, num_shards=2, shard_id=0)
  73. count = 0
  74. for _ in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  75. count += 1
  76. assert count == 1
  77. def test_yahoo_answers_dataset_num_samples():
  78. """
  79. Feature: YahooAnswersDataset.
  80. Description: test num_samples parameter.
  81. Expectation: the data is processed successfully.
  82. """
  83. data = ds.YahooAnswersDataset(DATA_DIR, usage="test", shuffle=False, num_samples=2)
  84. count = 0
  85. for _ in data.create_dict_iterator(num_epochs=1, output_numpy=True):
  86. count += 1
  87. assert count == 2
  88. def test_yahoo_answers_dataset_exception():
  89. """
  90. Feature: YahooAnswersDataset.
  91. Description: test the wrong input.
  92. Expectation: Unable to read data properly.
  93. """
  94. def exception_func(item):
  95. raise Exception("Error occur!")
  96. try:
  97. data = ds.YahooAnswersDataset(DATA_DIR, usage="test", shuffle=False)
  98. data = data.map(operations=exception_func, input_columns=["class"], num_parallel_workers=1)
  99. for _ in data.create_dict_iterator():
  100. pass
  101. assert False
  102. except RuntimeError as e:
  103. assert "map operation: [PyFunc] failed. The corresponding data files" in str(e)
  104. try:
  105. data = ds.YahooAnswersDataset(DATA_DIR, usage="test", shuffle=False)
  106. data = data.map(operations=exception_func, input_columns=["content"], num_parallel_workers=1)
  107. for _ in data.create_dict_iterator():
  108. pass
  109. assert False
  110. except RuntimeError as e:
  111. assert "map operation: [PyFunc] failed. The corresponding data files" in str(e)
  112. if __name__ == "__main__":
  113. test_yahoo_answers_dataset_basic()
  114. test_yahoo_answers_dataset_quoted()
  115. test_yahoo_answers_dataset_usage()
  116. test_yahoo_answers_dataset_get_datasetsize()
  117. test_yahoo_answers_dataset_distribution()
  118. test_yahoo_answers_dataset_num_samples()
  119. test_yahoo_answers_dataset_exception()