GCNGraph.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \file GCNGraph.cxx
3 /// \brief GCNGraph for GCN
4 /// \author Leigh H. Whitehead - leigh.howard.whitehead@cern.ch
5 ///////////////////////////////////////////////////////////////////////
6 
7 #include <cassert>
8 #include <iostream>
9 #include <ostream>
12 
13 namespace cvn
14 {
15 
17  {}
18 
19  GCNGraph::GCNGraph(std::vector<GCNGraphNode> nodes):
20  fNodes(nodes)
21  {
22 
23  }
24 
25  GCNGraph::GCNGraph(std::vector<std::vector<float>> positions,std::vector<std::vector<float>> features)
26  {
27  if(positions.size() != features.size()){
28  std::cerr << "The number of nodes must be the same for the position and feature vectors" << std::endl;
29  assert(0);
30  }
31  for(unsigned int n = 0; n < positions.size(); ++n){
32  this->AddNode(positions.at(n),features.at(n));
33  }
34  }
35 
36  // Add a new node
37  void GCNGraph::AddNode(std::vector<float> position, std::vector<float> features){
38  GCNGraphNode newNode(position,features);
39  AddNode(newNode);
40  }
41 
42  // Add a new node
43  void GCNGraph::AddNode(std::vector<float> position, std::vector<float> features,
44  std::vector<float> groundTruth){
45  GCNGraphNode newNode(position,features,groundTruth);
46  AddNode(newNode);
47  }
48 
50  fNodes.push_back(node);
51  }
52 
53  // Get the number of nodes
54  const unsigned int GCNGraph::GetNumberOfNodes() const{
55  return fNodes.size();
56  }
57 
58  // Access nodes
59  const GCNGraphNode& GCNGraph::GetNode(const unsigned int index) const{
60  if(this->GetNumberOfNodes() == 0){
61  std::cerr << "GCNGraph::GetNode(): Can't access node with index " << index << std::endl;
62  assert(0);
63  }
64 
65  return fNodes.at(index);
66  }
67 
69  if(this->GetNumberOfNodes() == 0){
70  std::cerr << "GCNGraph::GetNode(): Can't access node with index " << index << std::endl;
71  assert(0);
72  }
73 
74  return fNodes.at(index);
75  }
76 
77  // Return minimum and maximum coordinate values
78  const std::vector<std::pair<float,float>> GCNGraph::GetMinMaxPositions() const{
79 
80  std::pair<float,float> dummyPair = std::make_pair(1.e6,-1.e6);
81  std::vector<std::pair<float,float>> minMaxVals;
82 
83  if(fNodes.size() == 0){
84  std::cerr << "No nodes found in the graph, returning empty vector" << std::endl;
85  return minMaxVals;
86  }
87 
88  // Initialise the vector of pairs for the number of coordinates
89  for(unsigned int i = 0; i < GetNumberOfNodeCoordinates(); ++i){
90  minMaxVals.push_back(dummyPair);
91  }
92 
93  for(GCNGraphNode node : fNodes){
94  std::vector<float> nodePos = node.GetPosition();
95  for(unsigned int p = 0; p < nodePos.size(); ++p){
96  if(nodePos[p] < minMaxVals[p].first) minMaxVals[p].first = nodePos[p];
97  if(nodePos[p] > minMaxVals[p].second) minMaxVals[p].second = nodePos[p];
98  }
99  }
100 
101  return minMaxVals;
102 
103  }
104 
105  const std::pair<float,float> GCNGraph::GetCoordinateMinMax(unsigned int coord) const{
106  if(coord > fNodes.size()){
107  std::cerr << "Node index is out of bounds" << std::endl;
108  assert(0);
109  }
110  return this->GetMinMaxPositions()[coord];
111  }
112 
113  // Return spacial extent of the graph in all coordinates
114  const std::vector<float> GCNGraph::GetSpacialExtent() const{
115 
116  std::vector<std::pair<float,float>> minMaxVals = this->GetMinMaxPositions();
117 
118  std::vector<float> extent;
119  for(std::pair<float,float> pair : minMaxVals){
120  extent.push_back(pair.second - pair.first);
121  }
122 
123  return extent;
124  }
125 
126  const float GCNGraph::GetCoordinateSpacialExtent(unsigned int coord) const{
127  if(coord > fNodes.size()){
128  std::cerr << "Node index is out of bounds" << std::endl;
129  assert(0);
130  }
131  return this->GetSpacialExtent()[coord];
132  }
133 
134  // This function returns a vector of the format for a graph
135  // with N nodes, P positions and M features per node
136  // (node0_pos0, node0_pos1, ... , node0_posP, node0_feature0, ... ,node0_featureM,...
137  // (nodeN_pos0, nodeN_pos1, ... , nodeN_posP, nodeN_posz, nodeN_feature0, ... ,nodeN_featureM)
138  const std::vector<float> GCNGraph::ConvertGraphToVector() const{
139 
140  std::vector<float> nodeVector;
141 
142  for(const GCNGraphNode &node : fNodes){
143  // First add the position components
144  for(const float pos : node.GetPosition()){
145  nodeVector.push_back(pos);
146  }
147  // Now add the features
148  for(const float feat : node.GetFeatures()){
149  nodeVector.push_back(feat);
150  }
151  // Now add the ground truth
152  for (const float truth : node.GetGroundTruth()) {
153  nodeVector.push_back(truth);
154  }
155  }
156 
157  return nodeVector;
158  }
159 
160  // Return the number of coordinates for each node
161  const unsigned int GCNGraph::GetNumberOfNodeCoordinates() const{
162  if(fNodes.size() == 0){
163  std::cerr << "Graph has no nodes, returning 0" << std::endl;
164  return 0;
165  }
166  else return fNodes[0].GetNumberOfCoordinates();
167  }
168 
169  // Return the number of features for each node
170  const unsigned int GCNGraph::GetNumberOfNodeFeatures() const{
171  if(fNodes.size() == 0){
172  std::cerr << "Graph has no nodes, returning 0" << std::endl;
173  return 0;
174  }
175  else return fNodes[0].GetNumberOfFeatures();
176  }
177 
178  std::ostream& operator<<(std::ostream& os, const GCNGraph& m)
179  {
180  os << "GCNGraph with " << m.GetNumberOfNodes() << " nodes, ";
181  return os;
182  }
183 }
const unsigned int GetNumberOfNodeCoordinates() const
Return the number of coordinates for each node.
Definition: GCNGraph.cxx:161
Node for GCN.
auto coord(Vector &v, unsigned int n) noexcept
Returns an object to manage the coordinate n of a vector.
GCNGraph, basic input for the GCN.
Definition: GCNGraph.h:18
std::ostream & operator<<(std::ostream &os, const PixelMapProducer &p)
struct vector vector
void AddNode(std::vector< float > position, std::vector< float > features)
Add a new node.
Definition: GCNGraph.cxx:37
const std::vector< float > GetSpacialExtent() const
Get the extent in each dimension.
Definition: GCNGraph.cxx:114
Utility class for truth labels.
const char features[]
Definition: feature_tests.c:2
GCNGraph()
Default constructor.
Definition: GCNGraph.cxx:16
const std::vector< std::pair< float, float > > GetMinMaxPositions() const
Return minimum and maximum position coordinate values.
Definition: GCNGraph.cxx:78
#define nodes
std::void_t< T > n
p
Definition: test.py:223
GCNGraphNode & GetNodeEditable(const unsigned int index)
Definition: GCNGraph.cxx:68
const float GetCoordinateSpacialExtent(unsigned int index) const
Definition: GCNGraph.cxx:126
const std::pair< float, float > GetCoordinateMinMax(unsigned int index) const
Definition: GCNGraph.cxx:105
const GCNGraphNode & GetNode(const unsigned int index) const
Access nodes.
Definition: GCNGraph.cxx:59
std::vector< GCNGraphNode > fNodes
Store the nodes.
Definition: GCNGraph.h:64
GCNGraph for GCN.
const unsigned int GetNumberOfNodeFeatures() const
Return the number of features for each node.
Definition: GCNGraph.cxx:170
const std::vector< float > ConvertGraphToVector() const
Function to linearise the graph to a vector for zlib file creation.
Definition: GCNGraph.cxx:138
const unsigned int GetNumberOfNodes() const
Get the number of nodes.
Definition: GCNGraph.cxx:54
second_as<> second
Type of time stored in seconds, in double precision.
Definition: spacetime.h:85
QTextStream & endl(QTextStream &s)