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.

project_op_test.cc 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <iostream>
  17. #include <memory>
  18. #include <vector>
  19. #include "common/common.h"
  20. #include "common/utils.h"
  21. #include "minddata/dataset/core/client.h"
  22. #include "gtest/gtest.h"
  23. #include "utils/log_adapter.h"
  24. namespace common = mindspore::common;
  25. using namespace mindspore::dataset;
  26. using mindspore::MsLogLevel::INFO;
  27. using mindspore::ExceptionType::NoExceptionType;
  28. using mindspore::LogStream;
  29. class MindDataTestProjectOp : public UT::DatasetOpTesting {};
  30. TEST_F(MindDataTestProjectOp, TestProjectProject) {
  31. // Start with an empty execution tree
  32. auto my_tree = std::make_shared<ExecutionTree>();
  33. std::string dataset_path;
  34. dataset_path = datasets_root_path_ + "/testTFTestAllTypes/test.data";
  35. std::shared_ptr<TFReaderOp> my_tfreader_op;
  36. TFReaderOp::Builder builder;
  37. builder.SetDatasetFilesList({dataset_path})
  38. .SetRowsPerBuffer(16)
  39. .SetWorkerConnectorSize(16)
  40. .SetNumWorkers(16);
  41. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  42. schema->LoadSchemaFile(datasets_root_path_ + "/testTFTestAllTypes/datasetSchema.json", {});
  43. builder.SetDataSchema(std::move(schema));
  44. Status rc = builder.Build(&my_tfreader_op); ASSERT_TRUE(rc.IsOk());
  45. rc = my_tree->AssociateNode(my_tfreader_op);
  46. ASSERT_TRUE(rc.IsOk());
  47. // ProjectOp
  48. std::vector<std::string> columns_to_project = {"col_sint16", "col_float", "col_2d"};
  49. std::shared_ptr<ProjectOp> my_project_op = std::make_shared<ProjectOp>(columns_to_project);
  50. rc = my_tree->AssociateNode(my_project_op);
  51. ASSERT_TRUE(rc.IsOk());
  52. // Set children/root layout.
  53. rc = my_project_op->AddChild(my_tfreader_op);
  54. ASSERT_TRUE(rc.IsOk());
  55. rc = my_tree->AssignRoot(my_project_op);
  56. ASSERT_TRUE(rc.IsOk());
  57. MS_LOG(INFO) << "Launching tree and begin iteration.";
  58. rc = my_tree->Prepare();
  59. ASSERT_TRUE(rc.IsOk());
  60. rc = my_tree->Launch();
  61. ASSERT_TRUE(rc.IsOk());
  62. // Start the loop of reading tensors from our pipeline
  63. DatasetIterator di(my_tree);
  64. TensorRow tensor_list;
  65. rc = di.FetchNextTensorRow(&tensor_list);
  66. ASSERT_TRUE(rc.IsOk());
  67. int row_count = 0;
  68. while (!tensor_list.empty()) {
  69. MS_LOG(INFO) << "Row display for row #: " << row_count << ".";
  70. ASSERT_EQ(tensor_list.size(), columns_to_project.size());
  71. // Display the tensor by calling the printer on it
  72. for (int i = 0; i < tensor_list.size(); i++) {
  73. std::ostringstream ss;
  74. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  75. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  76. }
  77. rc = di.FetchNextTensorRow(&tensor_list);
  78. ASSERT_TRUE(rc.IsOk());
  79. row_count++;
  80. }
  81. ASSERT_EQ(row_count, 12);
  82. }