MCPcopy Create free account
hub / github.com/alibaba/graph-learn / BatchGraph

Class BatchGraph

graphlearn/python/nn/tf/data/batchgraph.py:33–326  ·  view source on GitHub ↗

A BatchGraph object, which represents a batch of `SubGraph`s. Nodes, edges in subgraphs are concatenated together and their offsets are recorded with `graph_node_offsets` and `graph_edge_offsets`. The `edge_index` of subgraph is remapped according to the order offset of each subgrpah and t

Source from the content-addressed store, hash-verified

31
32
33class BatchGraph(SubGraph):
34 """A BatchGraph object, which represents a batch of `SubGraph`s.
35 Nodes, edges in subgraphs are concatenated together and their offsets
36 are recorded with `graph_node_offsets` and `graph_edge_offsets`. The
37 `edge_index` of subgraph is remapped according to the order offset of
38 each subgrpah and then form as a new `edge_index`.
39
40 Args:
41 edge_index: concatenated edge_index of `SubGraph`s.
42 nodes: A `Data`/Tensor object denoting concatenated nodes of `SubGraph`s
43 with shape [batch_size, attr_num].
44 node_schema: A (name, Decoder) tuple used to describe the nodes' feature.
45 graph_node_offsets: indicates the nodes offset of each `SubGraph`.
46 edges: A `Data`/Tensor object denoting concatenated edges of `SubGraph`s.
47 edge_schema: A (name, Decoder) tuple used to describe the edges' feature.
48 graph_edge_offsets: indicates the edges offset of each `SuGraph`.
49 additional_keys: A list of keys used to indicate the additional data. Note
50 that these keys must not contain the above args.
51 Note that we require this argument in order to keep the correct order of
52 the additional data when generating Tensor format of `BatchGraph`.
53 """
54 def __init__(self, edge_index, nodes, node_schema, graph_node_offsets,
55 edges=None, edge_schema=None, graph_edge_offsets=None,
56 additional_keys=[], **kwargs):
57 super(BatchGraph, self).__init__(edge_index, nodes)
58 self._edge_index = edge_index
59 self._nodes = nodes
60 self._node_schema = node_schema
61 self._edges = edges
62 self._edge_schema = edge_schema
63 self._graph_node_offsets = graph_node_offsets
64 self._graph_edge_offsets = graph_edge_offsets
65 self._additional_keys = additional_keys
66 for key, item in kwargs.items():
67 self[key] = item
68
69 @property
70 def num_nodes(self):
71 if isinstance(self._nodes.ids, np.ndarray):
72 return self._nodes.ids.size
73 else:
74 return self._nodes.ids.shape.as_list()[0]
75
76 @property
77 def num_edges(self):
78 if isinstance(self._edge_index, np.ndarray):
79 return self._edge_index.shape[1]
80 else:
81 return self._edge_index.shape.as_list()[1]
82
83 @property
84 def num_graphs(self):
85 """number of SubGraphs.
86 """
87 if isinstance(self.graph_node_offsets, np.ndarray):
88 return np.amax(self.graph_node_offsets) + 1
89 else:
90 return tf.reduce_max(self.graph_node_offsets) + 1

Callers 3

transformMethod · 0.85
from_graphsMethod · 0.85
from_tensorsMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected