Skip to content

Commit

Permalink
[Graph] Better string repr (dmlc#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-haibin-lin authored and jermainewang committed Dec 16, 2018
1 parent 3010740 commit e292654
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions python/dgl/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -2907,3 +2907,9 @@ def filter_edges(self, predicate, edges=ALL):
else:
edges = F.tensor(edges)
return edges[e_mask]

def __repr__(self):
s = 'DGLGraph with {node} nodes and {edge} edges.\nNode data: {ndata}\nEdge data: {edata}'
return s.format(node=self.number_of_nodes(), edge=self.number_of_edges(),
ndata=str(self.node_attr_schemes()),
edata=str(self.edge_attr_schemes()))
11 changes: 11 additions & 0 deletions tests/mxnet/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,16 @@ def test_pull_0deg():
check_pull_0deg(True)
check_pull_0deg(False)

def test_repr():
G = dgl.DGLGraph()
G.add_nodes(10)
G.add_edge(0, 1)
repr_string = G.__repr__()
G.ndata['x'] = mx.nd.zeros((10, 5))
G.add_edges([0, 1], 2)
G.edata['y'] = mx.nd.zeros((3, 4))
repr_string = G.__repr__()

if __name__ == '__main__':
test_batch_setter_getter()
test_batch_setter_autograd()
Expand All @@ -451,3 +461,4 @@ def test_pull_0deg():
test_recv_0deg_newfld()
test_update_all_0deg()
test_pull_0deg()
test_repr()
12 changes: 12 additions & 0 deletions tests/pytorch/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,17 @@ def test_dynamic_addition():
assert g.edata['h1'].shape[0] == g.edata['h2'].shape[0] == 5


def test_repr():
G = dgl.DGLGraph()
G.add_nodes(10)
G.add_edge(0, 1)
repr_string = G.__repr__()
G.ndata['x'] = th.zeros((10, 5))
G.add_edges([0, 1], 2)
G.edata['y'] = th.zeros((3, 4))
repr_string = G.__repr__()


if __name__ == '__main__':
test_nx_conversion()
test_batch_setter_getter()
Expand All @@ -630,3 +641,4 @@ def test_dynamic_addition():
test_pull_0deg()
test_send_multigraph()
test_dynamic_addition()
test_repr()

0 comments on commit e292654

Please sign in to comment.