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.

gnn_graph_test.cc 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * Copyright 2020 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 <string>
  17. #include <memory>
  18. #include "common/common.h"
  19. #include "gtest/gtest.h"
  20. #include "dataset/util/status.h"
  21. #include "dataset/engine/gnn/node.h"
  22. #include "dataset/engine/gnn/graph_loader.h"
  23. using namespace mindspore::dataset;
  24. using namespace mindspore::dataset::gnn;
  25. class MindDataTestGNNGraph : public UT::Common {
  26. protected:
  27. MindDataTestGNNGraph() = default;
  28. };
  29. TEST_F(MindDataTestGNNGraph, TestGraphLoader) {
  30. std::string path = "data/mindrecord/testGraphData/testdata";
  31. GraphLoader gl(path, 4);
  32. EXPECT_TRUE(gl.InitAndLoad().IsOk());
  33. NodeIdMap n_id_map;
  34. EdgeIdMap e_id_map;
  35. NodeTypeMap n_type_map;
  36. EdgeTypeMap e_type_map;
  37. NodeFeatureMap n_feature_map;
  38. EdgeFeatureMap e_feature_map;
  39. DefaultFeatureMap default_feature_map;
  40. EXPECT_TRUE(gl.GetNodesAndEdges(&n_id_map, &e_id_map, &n_type_map, &e_type_map, &n_feature_map, &e_feature_map,
  41. &default_feature_map)
  42. .IsOk());
  43. EXPECT_EQ(n_id_map.size(), 20);
  44. EXPECT_EQ(e_id_map.size(), 20);
  45. EXPECT_EQ(n_type_map[2].size(), 10);
  46. EXPECT_EQ(n_type_map[1].size(), 10);
  47. }
  48. TEST_F(MindDataTestGNNGraph, TestGetAllNeighbors) {
  49. std::string path = "data/mindrecord/testGraphData/testdata";
  50. Graph graph(path, 1);
  51. Status s = graph.Init();
  52. EXPECT_TRUE(s.IsOk());
  53. std::vector<NodeMetaInfo> node_info;
  54. std::vector<EdgeMetaInfo> edge_info;
  55. s = graph.GetMetaInfo(&node_info, &edge_info);
  56. EXPECT_TRUE(s.IsOk());
  57. EXPECT_TRUE(node_info.size() == 2);
  58. std::shared_ptr<Tensor> nodes;
  59. s = graph.GetNodes(node_info[1].type, -1, &nodes);
  60. EXPECT_TRUE(s.IsOk());
  61. std::vector<NodeIdType> node_list;
  62. for (auto itr = nodes->begin<NodeIdType>(); itr != nodes->end<NodeIdType>(); ++itr) {
  63. node_list.push_back(*itr);
  64. if (node_list.size() >= 10) {
  65. break;
  66. }
  67. }
  68. std::shared_ptr<Tensor> neighbors;
  69. s = graph.GetAllNeighbors(node_list, node_info[0].type, &neighbors);
  70. EXPECT_TRUE(s.IsOk());
  71. EXPECT_TRUE(neighbors->shape().ToString() == "<10,6>");
  72. TensorRow features;
  73. s = graph.GetNodeFeature(nodes, node_info[1].feature_type, &features);
  74. EXPECT_TRUE(s.IsOk());
  75. EXPECT_TRUE(features.size() == 3);
  76. EXPECT_TRUE(features[0]->shape().ToString() == "<10,5>");
  77. EXPECT_TRUE(features[0]->ToString() ==
  78. "Tensor (shape: <10,5>, Type: int32)\n"
  79. "[[0,1,0,0,0],[1,0,0,0,1],[0,0,1,1,0],[0,0,0,0,0],[1,1,0,1,0],[0,0,0,0,1],[0,1,0,0,0],[0,0,0,1,1],[0,1,1,"
  80. "0,0],[0,1,0,1,0]]");
  81. EXPECT_TRUE(features[1]->shape().ToString() == "<10>");
  82. EXPECT_TRUE(features[1]->ToString() ==
  83. "Tensor (shape: <10>, Type: float32)\n[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1]");
  84. EXPECT_TRUE(features[2]->shape().ToString() == "<10>");
  85. EXPECT_TRUE(features[2]->ToString() == "Tensor (shape: <10>, Type: int32)\n[1,2,3,1,4,3,5,3,5,4]");
  86. }