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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 "utils/ms_utils.h"
  21. #include "minddata/dataset/core/client.h"
  22. #include "minddata/dataset/engine/jagged_connector.h"
  23. #include "gtest/gtest.h"
  24. #include "utils/log_adapter.h"
  25. namespace common = mindspore::common;
  26. using namespace mindspore::dataset;
  27. using mindspore::LogStream;
  28. using mindspore::ExceptionType::NoExceptionType;
  29. using mindspore::MsLogLevel::INFO;
  30. class MindDataTestProjectOp : public UT::DatasetOpTesting {};
  31. TEST_F(MindDataTestProjectOp, TestProjectProject) {
  32. // Start with an empty execution tree
  33. auto my_tree = std::make_shared<ExecutionTree>();
  34. Status rc;
  35. std::string dataset_path;
  36. dataset_path = datasets_root_path_ + "/testTFTestAllTypes/test.data";
  37. std::shared_ptr<ConfigManager> config_manager = GlobalContext::config_manager();
  38. auto op_connector_size = config_manager->op_connector_size();
  39. auto num_workers = 1; // one file, one worker
  40. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  41. std::vector<std::string> columns_to_load = {};
  42. std::vector<std::string> files = {dataset_path};
  43. schema->LoadSchemaFile(datasets_root_path_ + "/testTFTestAllTypes/datasetSchema.json", {});
  44. std::shared_ptr<TFReaderOp> my_tfreader_op = std::make_shared<TFReaderOp>(
  45. num_workers, 16, 0, files, std::move(schema), op_connector_size, columns_to_load, false, 1, 0, false);
  46. rc = my_tfreader_op->Init();
  47. ASSERT_TRUE(rc.IsOk());
  48. rc = my_tree->AssociateNode(my_tfreader_op);
  49. ASSERT_TRUE(rc.IsOk());
  50. // ProjectOp
  51. std::vector<std::string> columns_to_project = {"col_sint16", "col_float", "col_2d"};
  52. std::shared_ptr<ProjectOp> my_project_op = std::make_shared<ProjectOp>(columns_to_project);
  53. rc = my_tree->AssociateNode(my_project_op);
  54. ASSERT_TRUE(rc.IsOk());
  55. // Set children/root layout.
  56. rc = my_project_op->AddChild(my_tfreader_op);
  57. ASSERT_TRUE(rc.IsOk());
  58. rc = my_tree->AssignRoot(my_project_op);
  59. ASSERT_TRUE(rc.IsOk());
  60. MS_LOG(INFO) << "Launching tree and begin iteration.";
  61. rc = my_tree->Prepare();
  62. ASSERT_TRUE(rc.IsOk());
  63. rc = my_tree->Launch();
  64. ASSERT_TRUE(rc.IsOk());
  65. // Start the loop of reading tensors from our pipeline
  66. DatasetIterator di(my_tree);
  67. TensorRow tensor_list;
  68. rc = di.FetchNextTensorRow(&tensor_list);
  69. ASSERT_TRUE(rc.IsOk());
  70. int row_count = 0;
  71. while (!tensor_list.empty()) {
  72. MS_LOG(INFO) << "Row display for row #: " << row_count << ".";
  73. ASSERT_EQ(tensor_list.size(), columns_to_project.size());
  74. // Display the tensor by calling the printer on it
  75. for (int i = 0; i < tensor_list.size(); i++) {
  76. std::ostringstream ss;
  77. ss << "(" << tensor_list[i] << "): " << *tensor_list[i] << std::endl;
  78. MS_LOG(INFO) << "Tensor print: " << ss.str() << ".";
  79. }
  80. rc = di.FetchNextTensorRow(&tensor_list);
  81. ASSERT_TRUE(rc.IsOk());
  82. row_count++;
  83. }
  84. ASSERT_EQ(row_count, 12);
  85. }