/
heterograph.py
2260 lines (1831 loc) · 73.5 KB
/
heterograph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Classes for heterogeneous graphs."""
# pylint: disable=unnecessary-pass
class DGLBaseHeteroGraph(object):
"""Base Heterogeneous graph class.
A Heterogeneous graph is defined as a graph with node types and edge
types.
If two edges share the same edge type, then their source nodes, as well
as their destination nodes, also have the same type (the source node
types don't have to be the same as the destination node types).
Parameters
----------
metagraph : NetworkX MultiGraph or compatible data structure
The set of node types and edge types, as well as the
source/destination node type of each edge type is specified in the
metagraph.
The edge types are specified as edge keys on the NetworkX MultiGraph.
The node types and edge types must be strings.
number_of_nodes_by_type : dict[str, int]
Number of nodes for each node type.
edge_connections_by_type : dict
Specifies how edges would connect nodes of the source type to nodes of
the destination type in the following form:
{edge_type: edge_specifier}
where ``edge_type`` is a triplet of
(source_node_type_name,
destination_node_type_name,
edge_type_name)
and ``edge_specifier`` can be either of the following:
* (source_node_id_tensor, destination_node_id_tensor)
* ``source_node_id_tensor`` and ``destination_node_id_tensor`` are
IDs within the source and destination node type respectively.
* source node id and destination node id are both in their own ID space.
That is, source nodes and destination nodes may have the same ID,
but they are different nodes if they belong to different node types.
* scipy.sparse.matrix
By default, the rows represent the destination of an edge, and the
column represents the source.
Examples
--------
Suppose that we want to construct the following heterogeneous graph:
.. graphviz::
digraph G {
Alice -> Bob [label=follows]
Bob -> Carol [label=follows]
Alice -> Tetris [label=plays]
Bob -> Tetris [label=plays]
Bob -> Minecraft [label=plays]
Carol -> Minecraft [label=plays]
Nintendo -> Tetris [label=develops]
Mojang -> Minecraft [label=develops]
{rank=source; Alice; Bob; Carol}
{rank=sink; Nintendo; Mojang}
}
One can analyze the graph and figure out the metagraph as follows:
.. graphviz::
digraph G {
User -> User [label=follows]
User -> Game [label=plays]
Developer -> Game [label=develops]
}
Suppose that one maps the users, games and developers to the following
IDs:
User name Alice Bob Carol
User ID 0 1 2
Game name Tetris Minecraft
Game ID 0 1
Developer name Nintendo Mojang
Developer ID 0 1
One can construct the graph as follows:
>>> import networkx as nx
>>> metagraph = nx.MultiGraph([
... ('user', 'user', 'follows'),
... ('user', 'game', 'plays'),
... ('developer', 'game', 'develops')])
>>> g = DGLBaseHeteroGraph(
... metagraph=metagraph,
... number_of_nodes_by_type={'user': 4, 'game': 2, 'developer': 2},
... edge_connections_by_type={
... # Alice follows Bob and Bob follows Carol
... ('user', 'user', 'follows'): ([0, 1], [1, 2]),
... # Alice and Bob play Tetris and Bob and Carol play Minecraft
... ('user', 'game', 'plays'): ([0, 1, 1, 2], [0, 0, 1, 1]),
... # Nintendo develops Tetris and Mojang develops Minecraft
... ('developer', 'game', 'develops'): ([0, 1], [0, 1])})
"""
# pylint: disable=unused-argument
def __init__(
self,
metagraph,
number_of_nodes_by_type,
edge_connections_by_type):
super(DGLBaseHeteroGraph, self).__init__()
def __getitem__(self, key):
"""Returns a view on the heterogeneous graph with given node/edge
type:
* If ``key`` is a str, it returns a heterogeneous subgraph induced
from nodes of type ``key``.
* If ``key`` is a pair of str (type_A, type_B), it returns a
heterogeneous subgraph induced from the union of both node types.
* If ``key`` is a triplet of str
(src_type_name, dst_type_name, edge_type_name)
It returns a heterogeneous subgraph induced from the edges with
source type name ``src_type_name``, destination type name
``dst_type_name``, and edge type name ``edge_type_name``.
The view would share the frames with the parent graph; any
modifications on one's frames would reflect on the other.
Note that the subgraph itself is not materialized until someone
queries the subgraph structure. This implies that calling computation
methods such as
g['user'].update_all(...)
would not create a subgraph of users.
Parameters
----------
key : str or tuple
See above
Returns
-------
DGLBaseHeteroGraphView
The induced subgraph view.
"""
pass
@property
def metagraph(self):
"""Return the metagraph as networkx.MultiDiGraph."""
pass
def number_of_nodes(self):
"""Return the number of nodes in the graph.
Returns
-------
int
The number of nodes
"""
pass
def __len__(self):
"""Return the number of nodes in the graph."""
pass
# TODO: REVIEW
def add_nodes(self, num, node_type, data=None):
"""Add multiple new nodes of the same node type
Parameters
----------
num : int
Number of nodes to be added.
node_type : str
Type of the added nodes. Must appear in the metagraph.
data : dict, optional
Feature data of the added nodes.
Examples
--------
The variable ``g`` is constructed from the example in
DGLBaseHeteroGraph.
>>> g['game'].number_of_nodes()
2
>>> g.add_nodes(3, 'game') # add 3 new games
>>> g['game'].number_of_nodes()
5
"""
pass
# TODO: REVIEW
def add_edge(self, u, v, utype, vtype, etype, data=None):
"""Add an edge of ``etype`` between u of type ``utype`` and v of type
``vtype``.
Parameters
----------
u : int
The source node ID of type ``utype``. Must exist in the graph.
v : int
The destination node ID of type ``vtype``. Must exist in the
graph.
utype : str
The source node type name. Must exist in the metagraph.
vtype : str
The destination node type name. Must exist in the metagraph.
etype : str
The edge type name. Must exist in the metagraph.
data : dict, optional
Feature data of the added edge.
Examples
--------
The variable ``g`` is constructed from the example in
DGLBaseHeteroGraph.
>>> g['user', 'game', 'plays'].number_of_edges()
4
>>> g.add_edge(2, 0, 'user', 'game', 'plays')
>>> g['user', 'game', 'plays'].number_of_edges()
5
"""
pass
def add_edges(self, u, v, utype, vtype, etype, data=None):
"""Add multiple edges of ``etype`` between list of source nodes ``u``
of type ``utype`` and list of destination nodes ``v`` of type
``vtype``. A single edge is added between every pair of ``u[i]`` and
``v[i]``.
Parameters
----------
u : list, tensor
The source node IDs of type ``utype``. Must exist in the graph.
v : list, tensor
The destination node IDs of type ``vtype``. Must exist in the
graph.
utype : str
The source node type name. Must exist in the metagraph.
vtype : str
The destination node type name. Must exist in the metagraph.
etype : str
The edge type name. Must exist in the metagraph.
data : dict, optional
Feature data of the added edge.
Examples
--------
The variable ``g`` is constructed from the example in
DGLBaseHeteroGraph.
>>> g['user', 'game', 'plays'].number_of_edges()
4
>>> g.add_edges([0, 2], [1, 0], 'user', 'game', 'plays')
>>> g['user', 'game', 'plays'].number_of_edges()
6
"""
pass
@property
def is_multigraph(self):
"""True if the graph is a multigraph, False otherwise.
"""
pass
@property
def is_readonly(self):
"""True if the graph is readonly, False otherwise.
"""
pass
def number_of_edges(self):
"""Return the number of edges in the graph.
Returns
-------
int
The number of edges
"""
pass
def has_node(self, vid):
"""Return True if the graph contains node `vid`.
Only works if the graph has one node type. For multiple types,
query with
.. code::
g['vtype'].has_node(vid)
Parameters
----------
vid : int
The node ID.
Returns
-------
bool
True if the node exists
Examples
--------
>>> g['user'].has_node(0)
True
>>> g['user'].has_node(4)
False
Equivalently,
>>> 0 in g['user']
True
See Also
--------
has_nodes
"""
pass
def __contains__(self, vid):
"""Return True if the graph contains node `vid`.
Only works if the graph has one node type. For multiple types,
query with
.. code::
vid in g['vtype']
Examples
--------
>>> 0 in g['user']
True
"""
pass
def has_nodes(self, vids):
"""Return a 0-1 array ``a`` given the node ID array ``vids``.
``a[i]`` is 1 if the graph contains node ``vids[i]``, 0 otherwise.
Only works if the graph has one node type. For multiple types,
query with
.. code::
g['vtype'].has_nodes(vids)
Parameters
----------
vid : list or tensor
The array of node IDs.
Returns
-------
a : tensor
0-1 array indicating existence
Examples
--------
The following example uses PyTorch backend.
>>> g['user'].has_nodes([0, 1, 2, 3, 4])
tensor([1, 1, 1, 0, 0])
See Also
--------
has_node
"""
pass
def has_edge_between(self, u, v):
"""Return True if the edge (u, v) is in the graph.
Only works if the graph has one edge type. For multiple types,
query with
.. code::
g['srctype', 'dsttype', 'edgetype'].has_edge_between(u, v)
Parameters
----------
u : int
The node ID of source type.
v : int
The node ID of destination type.
Returns
-------
bool
True if the edge is in the graph, False otherwise.
Examples
--------
Check whether Alice plays Tetris
>>> g['user', 'game', 'plays'].has_edge_between(0, 1)
True
And whether Alice plays Minecraft
>>> g['user', 'game', 'plays'].has_edge_between(0, 2)
False
See Also
--------
has_edges_between
"""
pass
def has_edges_between(self, u, v):
"""Return a 0-1 array `a` given the source node ID array `u` and
destination node ID array `v`.
`a[i]` is 1 if the graph contains edge `(u[i], v[i])`, 0 otherwise.
Only works if the graph has one edge type. For multiple types,
query with
.. code::
g['srctype', 'dsttype', 'edgetype'].has_edges_between(u, v)
Parameters
----------
u : list, tensor
The node ID array of source type.
v : list, tensor
The node ID array of destination type.
Returns
-------
a : tensor
0-1 array indicating existence.
Examples
--------
The following example uses PyTorch backend.
>>> g['user', 'game', 'plays'].has_edges_between([0, 0], [1, 2])
tensor([1, 0])
See Also
--------
has_edge_between
"""
pass
def predecessors(self, v):
"""Return the predecessors of node `v` in the graph with the same
edge type.
Node `u` is a predecessor of `v` if an edge `(u, v)` exist in the
graph.
Only works if the graph has one edge type. For multiple types,
query with
.. code::
g['srctype', 'dsttype', 'edgetype'].predecessors(v)
Parameters
----------
v : int
The node of destination type.
Returns
-------
tensor
Array of predecessor node IDs of source node type.
Examples
--------
The following example uses PyTorch backend.
Query who plays Tetris:
>>> g['user', 'game', 'plays'].predecessors(0)
tensor([0, 1])
This indicates User #0 (Alice) and User #1 (Bob).
See Also
--------
successors
"""
pass
def successors(self, v):
"""Return the successors of node `v` in the graph with the same edge
type.
Node `u` is a successor of `v` if an edge `(v, u)` exist in the
graph.
Only works if the graph has one edge type. For multiple types,
query with
.. code::
g['srctype', 'dsttype', 'edgetype'].successors(v)
Parameters
----------
v : int
The node of source type.
Returns
-------
tensor
Array of successor node IDs if destination node type.
Examples
--------
The following example uses PyTorch backend.
Asks which game Alice plays:
>>> g['user', 'game', 'plays'].successors(0)
tensor([0])
This indicates Game #0 (Tetris).
See Also
--------
predecessors
"""
pass
def edge_id(self, u, v, force_multi=False):
"""Return the edge ID, or an array of edge IDs, between source node
`u` and destination node `v`.
Only works if the graph has one edge type. For multiple types,
query with
.. code::
g['srctype', 'dsttype', 'edgetype'].edge_id(u, v)
Parameters
----------
u : int
The node ID of source type.
v : int
The node ID of destination type.
force_multi : bool
If False, will return a single edge ID if the graph is a simple graph.
If True, will always return an array.
Returns
-------
int or tensor
The edge ID if force_multi == True and the graph is a simple graph.
The edge ID array otherwise.
Examples
--------
The following example uses PyTorch backend.
Find the edge ID of "Bob plays Tetris"
>>> g['user', 'game', 'plays'].edge_id(1, 0)
1
See Also
--------
edge_ids
"""
pass
def edge_ids(self, u, v, force_multi=False):
"""Return all edge IDs between source node array `u` and destination
node array `v`.
Only works if the graph has one edge type. For multiple types,
query with
.. code::
g['srctype', 'dsttype', 'edgetype'].edge_ids(u, v)
Parameters
----------
u : list, tensor
The node ID array of source type.
v : list, tensor
The node ID array of destination type.
force_multi : bool
Whether to always treat the graph as a multigraph.
Returns
-------
tensor, or (tensor, tensor, tensor)
If the graph is a simple graph and `force_multi` is False, return
a single edge ID array `e`. `e[i]` is the edge ID between `u[i]`
and `v[i]`.
Otherwise, return three arrays `(eu, ev, e)`. `e[i]` is the ID
of an edge between `eu[i]` and `ev[i]`. All edges between `u[i]`
and `v[i]` are returned.
Notes
-----
If the graph is a simple graph, `force_multi` is False, and no edge
exist between some pairs of `u[i]` and `v[i]`, the result is undefined.
Examples
--------
The following example uses PyTorch backend.
Find the edge IDs of "Alice plays Tetris" and "Bob plays Minecraft".
>>> g['user', 'game', 'plays'].edge_ids([0, 1], [0, 1])
tensor([0, 2])
See Also
--------
edge_id
"""
pass
def find_edges(self, eid):
"""Given an edge ID array, return the source and destination node ID
array `s` and `d`. `s[i]` and `d[i]` are source and destination node
ID for edge `eid[i]`.
Only works if the graph has one edge type. For multiple types,
query with
.. code::
g['srctype', 'dsttype', 'edgetype'].edge_ids(u, v)
Parameters
----------
eid : list, tensor
The edge ID array.
Returns
-------
tensor
The source node ID array.
tensor
The destination node ID array.
Examples
--------
The following example uses PyTorch backend.
Find the user and game of gameplay #0 and #2:
>>> g['user', 'game', 'plays'].find_edges([0, 2])
(tensor([0, 1]), tensor([0, 1]))
"""
pass
def in_edges(self, v, form='uv'):
"""Return the inbound edges of the node(s).
Only works if the graph has one edge type. For multiple types,
query with
.. code::
g['srctype', 'dsttype', 'edgetype'].edge_ids(u, v)
Parameters
----------
v : int, list, tensor
The node(s) of destination type.
form : str, optional
The return form. Currently support:
- 'all' : a tuple (u, v, eid)
- 'uv' : a pair (u, v), default
- 'eid' : one eid tensor
Returns
-------
A tuple of Tensors ``(eu, ev, eid)`` if ``form == 'all'``.
``eid[i]`` is the ID of an inbound edge to ``ev[i]`` from ``eu[i]``.
All inbound edges to ``v`` are returned.
A pair of Tensors (eu, ev) if form == 'uv'
``eu[i]`` is the source node of an inbound edge to ``ev[i]``.
All inbound edges to ``v`` are returned.
One Tensor if form == 'eid'
``eid[i]`` is ID of an inbound edge to any of the nodes in ``v``.
Examples
--------
The following example uses PyTorch backend.
Find the gameplay IDs of game #0 (Tetris)
>>> g['user', 'game', 'plays'].in_edges(0, 'eid')
tensor([0, 1])
"""
pass
def out_edges(self, v, form='uv'):
"""Return the outbound edges of the node(s).
Only works if the graph has one edge type. For multiple types,
query with
.. code::
g['srctype', 'dsttype', 'edgetype'].edge_ids(u, v)
Parameters
----------
v : int, list, tensor
The node(s) of source type.
form : str, optional
The return form. Currently support:
- 'all' : a tuple (u, v, eid)
- 'uv' : a pair (u, v), default
- 'eid' : one eid tensor
Returns
-------
A tuple of Tensors ``(eu, ev, eid)`` if ``form == 'all'``.
``eid[i]`` is the ID of an outbound edge from ``eu[i]`` to ``ev[i]``.
All outbound edges from ``v`` are returned.
A pair of Tensors (eu, ev) if form == 'uv'
``ev[i]`` is the destination node of an outbound edge from ``eu[i]``.
All outbound edges from ``v`` are returned.
One Tensor if form == 'eid'
``eid[i]`` is ID of an outbound edge from any of the nodes in ``v``.
Examples
--------
The following example uses PyTorch backend.
Find the gameplay IDs of user #0 (Alice)
>>> g['user', 'game', 'plays'].out_edges(0, 'eid')
tensor([0])
"""
pass
def all_edges(self, form='uv', order=None):
"""Return all the edges.
Only works if the graph has one edge type. For multiple types,
query with
.. code::
g['srctype', 'dsttype', 'edgetype'].edge_ids(u, v)
Parameters
----------
form : str, optional
The return form. Currently support:
- 'all' : a tuple (u, v, eid)
- 'uv' : a pair (u, v), default
- 'eid' : one eid tensor
order : string
The order of the returned edges. Currently support:
- 'srcdst' : sorted by their src and dst ids.
- 'eid' : sorted by edge Ids.
- None : the arbitrary order.
Returns
-------
A tuple of Tensors (u, v, eid) if form == 'all'
``eid[i]`` is the ID of an edge between ``u[i]`` and ``v[i]``.
All edges are returned.
A pair of Tensors (u, v) if form == 'uv'
An edge exists between ``u[i]`` and ``v[i]``.
If ``n`` edges exist between ``u`` and ``v``, then ``u`` and ``v`` as a pair
will appear ``n`` times.
One Tensor if form == 'eid'
``eid[i]`` is the ID of an edge in the graph.
Examples
--------
The following example uses PyTorch backend.
Find the user-game pairs for all gameplays:
>>> g['user', 'game', 'plays'].all_edges('uv')
(tensor([0, 1, 1, 2]), tensor([0, 0, 1, 1]))
"""
pass
def in_degree(self, v):
"""Return the in-degree of node ``v``.
Only works if the graph has one edge type. For multiple types,
query with
.. code::
g['srctype', 'dsttype', 'edgetype'].edge_ids(u, v)
Parameters
----------
v : int
The node ID of destination type.
Returns
-------
int
The in-degree.
Examples
--------
Find how many users are playing Game #0 (Tetris):
>>> g['user', 'game', 'plays'].in_degree(0)
2
See Also
--------
in_degrees
"""
pass
def in_degrees(self, v=ALL):
"""Return the array `d` of in-degrees of the node array `v`.
`d[i]` is the in-degree of node `v[i]`.
Only works if the graph has one edge type. For multiple types,
query with
.. code::
g['srctype', 'dsttype', 'edgetype'].edge_ids(u, v)
Parameters
----------
v : list, tensor, optional.
The node ID array of destination type. Default is to return the
degrees of all the nodes.
Returns
-------
d : tensor
The in-degree array.
Examples
--------
The following example uses PyTorch backend.
Find how many users are playing Game #0 and #1 (Tetris and Minecraft):
>>> g['user', 'game', 'plays'].in_degrees([0, 1])
tensor([2, 2])
See Also
--------
in_degree
"""
pass
def out_degree(self, v):
"""Return the out-degree of node `v`.
Only works if the graph has one edge type. For multiple types,
query with
.. code::
g['srctype', 'dsttype', 'edgetype'].edge_ids(u, v)
Parameters
----------
v : int
The node ID of source type.
Returns
-------
int
The out-degree.
Examples
--------
Find how many games User #0 Alice is playing
>>> g['user', 'game', 'plays'].out_degree(0)
1
See Also
--------
out_degrees
"""
pass
def out_degrees(self, v=ALL):
"""Return the array `d` of out-degrees of the node array `v`.
`d[i]` is the out-degree of node `v[i]`.
Only works if the graph has one edge type. For multiple types,
query with
.. code::
g['srctype', 'dsttype', 'edgetype'].edge_ids(u, v)
Parameters
----------
v : list, tensor
The node ID array of source type. Default is to return the degrees
of all the nodes.
Returns
-------
d : tensor
The out-degree array.
Examples
--------
The following example uses PyTorch backend.
Find how many games User #0 and #1 (Alice and Bob) are playing
>>> g['user', 'game', 'plays'].out_degrees([0, 1])
tensor([1, 2])
See Also
--------
out_degree
"""
pass
class DGLBaseHeteroGraphView(DGLBaseHeteroGraph):
"""View on a heterogeneous graph, constructed from
DGLBaseHeteroGraph.__getitem__().
It is semantically the same as a subgraph, except that
* The subgraph itself is not materialized until the user explicitly
queries the subgraph structure (e.g. calling ``in_edges``, but not
``update_all``).
"""
pass
class DGLHeteroGraph(DGLBaseHeteroGraph):
"""Base heterogeneous graph class.
The graph stores nodes, edges and also their (type-specific) features.
Heterogeneous graphs are by default multigraphs.
Parameters
----------
metagraph, number_of_nodes_by_type, edge_connections_by_type :
See DGLBaseHeteroGraph
node_frame : dict[str, FrameRef], optional
Node feature storage per type
edge_frame : dict[str, FrameRef], optional
Edge feature storage per type
readonly : bool, optional
Whether the graph structure is read-only (default: False)
"""
# pylint: disable=unused-argument
def __init__(
self,
metagraph,
number_of_nodes_by_type,
edge_connections_by_type,
node_frame=None,
edge_frame=None,
readonly=False):
super(DGLHeteroGraph, self).__init__(
metagraph, number_of_nodes_by_type, edge_connections_by_type)
def from_networkx(
self,
nx_graph,
node_type_attr_name='type',
edge_type_attr_name='type',
node_id_attr_name='id',
edge_id_attr_name='id',
node_attrs=None,
edge_attrs=None):
"""Convert from networkx graph.
The networkx graph must satisfy the metagraph. That is, for any
edge in the networkx graph, the source/destination node type must
be the same as the source/destination node of the edge type in
the metagraph. An error will be raised otherwise.
Parameters
----------
nx_graph : networkx.DiGraph
The networkx graph.
node_type_attr_name : str
The node attribute name for the node type.
The attribute contents must be strings.
edge_type_attr_name : str
The edge attribute name for the edge type.
The attribute contents must be strings.