test_minibatch.py 23.3 KB
Newer Older
1
2
import dgl
import dgl.graphbolt as gb
peizhou001's avatar
peizhou001 committed
3
import pytest
4
5
6
import torch


peizhou001's avatar
peizhou001 committed
7
8
9
10
relation = "A:r:B"
reverse_relation = "B:rr:A"


11
12
13
@pytest.mark.parametrize("indptr_dtype", [torch.int32, torch.int64])
@pytest.mark.parametrize("indices_dtype", [torch.int32, torch.int64])
def test_minibatch_representation_homo(indptr_dtype, indices_dtype):
14
    seeds = torch.tensor([10, 11])
15
16
    csc_formats = [
        gb.CSCFormatBase(
17
18
            indptr=torch.tensor([0, 1, 3, 5, 6], dtype=indptr_dtype),
            indices=torch.tensor([0, 1, 2, 2, 1, 2], dtype=indices_dtype),
19
        ),
20
        gb.CSCFormatBase(
21
22
            indptr=torch.tensor([0, 2, 3], dtype=indptr_dtype),
            indices=torch.tensor([1, 2, 0], dtype=indices_dtype),
23
24
        ),
    ]
25
    original_column_node_ids = [
26
27
28
        torch.tensor([10, 11, 12, 13]),
        torch.tensor([10, 11]),
    ]
29
    original_row_node_ids = [
30
31
32
        torch.tensor([10, 11, 12, 13]),
        torch.tensor([10, 11, 12]),
    ]
33
    original_edge_ids = [
34
35
36
        torch.tensor([19, 20, 21, 22, 25, 30]),
        torch.tensor([10, 15, 17]),
    ]
37
    node_features = {"x": torch.tensor([5, 0, 2, 1])}
38
    edge_features = [
39
40
        {"x": torch.tensor([9, 0, 1, 1, 7, 4])},
        {"x": torch.tensor([0, 2, 2])},
41
42
43
44
    ]
    subgraphs = []
    for i in range(2):
        subgraphs.append(
45
            gb.SampledSubgraphImpl(
46
                sampled_csc=csc_formats[i],
47
48
49
                original_column_node_ids=original_column_node_ids[i],
                original_row_node_ids=original_row_node_ids[i],
                original_edge_ids=original_edge_ids[i],
50
51
52
            )
        )
    input_nodes = torch.tensor([8, 1, 6, 5, 9, 0, 2, 4])
53
54
    compacted_seeds = torch.tensor([0, 1])
    labels = torch.tensor([1.0, 2.0])
55
56
57
    # Test minibatch without data.
    minibatch = gb.MiniBatch()
    expect_result = str(
58
        """MiniBatch(seeds=None,
59
60
61
62
          sampled_subgraphs=None,
          node_features=None,
          labels=None,
          input_nodes=None,
63
          indexes=None,
64
          edge_features=None,
65
          compacted_seeds=None,
66
          blocks=None,
67
68
69
       )"""
    )
    result = str(minibatch)
70
    assert result == expect_result, print(expect_result, result)
71
72
    # Test minibatch with all attributes.
    minibatch = gb.MiniBatch(
73
        seeds=seeds,
74
75
76
77
        sampled_subgraphs=subgraphs,
        labels=labels,
        node_features=node_features,
        edge_features=edge_features,
78
        compacted_seeds=compacted_seeds,
79
80
81
        input_nodes=input_nodes,
    )
    expect_result = str(
82
        """MiniBatch(seeds=tensor([10, 11]),
83
84
          sampled_subgraphs=[SampledSubgraphImpl(sampled_csc=CSCFormatBase(indptr=tensor([0, 1, 3, 5, 6], dtype=torch.int32),
                                                                         indices=tensor([0, 1, 2, 2, 1, 2], dtype=torch.int32),
85
86
                                                           ),
                                               original_row_node_ids=tensor([10, 11, 12, 13]),
87
88
89
                                               original_edge_ids=tensor([19, 20, 21, 22, 25, 30]),
                                               original_column_node_ids=tensor([10, 11, 12, 13]),
                            ),
90
91
                            SampledSubgraphImpl(sampled_csc=CSCFormatBase(indptr=tensor([0, 2, 3], dtype=torch.int32),
                                                                         indices=tensor([1, 2, 0], dtype=torch.int32),
92
93
                                                           ),
                                               original_row_node_ids=tensor([10, 11, 12]),
94
95
96
97
                                               original_edge_ids=tensor([10, 15, 17]),
                                               original_column_node_ids=tensor([10, 11]),
                            )],
          node_features={'x': tensor([5, 0, 2, 1])},
98
          labels=tensor([1., 2.]),
99
          input_nodes=tensor([8, 1, 6, 5, 9, 0, 2, 4]),
100
          indexes=None,
101
102
          edge_features=[{'x': tensor([9, 0, 1, 1, 7, 4])},
                        {'x': tensor([0, 2, 2])}],
103
          compacted_seeds=tensor([0, 1]),
104
105
          blocks=[Block(num_src_nodes=4, num_dst_nodes=4, num_edges=6),
                 Block(num_src_nodes=3, num_dst_nodes=2, num_edges=3)],
106
107
108
109
       )"""
    )
    result = str(minibatch)
    assert result == expect_result, print(expect_result, result)
peizhou001's avatar
peizhou001 committed
110
111


112
113
114
@pytest.mark.parametrize("indptr_dtype", [torch.int32, torch.int64])
@pytest.mark.parametrize("indices_dtype", [torch.int32, torch.int64])
def test_minibatch_representation_hetero(indptr_dtype, indices_dtype):
115
    seeds = {relation: torch.tensor([10, 11])}
116
117
118
    csc_formats = [
        {
            relation: gb.CSCFormatBase(
119
120
                indptr=torch.tensor([0, 1, 2, 3], dtype=indptr_dtype),
                indices=torch.tensor([0, 1, 1], dtype=indices_dtype),
121
122
            ),
            reverse_relation: gb.CSCFormatBase(
123
124
                indptr=torch.tensor([0, 0, 0, 1, 2], dtype=indptr_dtype),
                indices=torch.tensor([1, 0], dtype=indices_dtype),
125
126
127
128
            ),
        },
        {
            relation: gb.CSCFormatBase(
129
130
                indptr=torch.tensor([0, 1, 2], dtype=indptr_dtype),
                indices=torch.tensor([1, 0], dtype=indices_dtype),
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
            )
        },
    ]
    original_column_node_ids = [
        {"B": torch.tensor([10, 11, 12]), "A": torch.tensor([5, 7, 9, 11])},
        {"B": torch.tensor([10, 11])},
    ]
    original_row_node_ids = [
        {
            "A": torch.tensor([5, 7, 9, 11]),
            "B": torch.tensor([10, 11, 12]),
        },
        {
            "A": torch.tensor([5, 7]),
            "B": torch.tensor([10, 11]),
        },
    ]
    original_edge_ids = [
        {
            relation: torch.tensor([19, 20, 21]),
            reverse_relation: torch.tensor([23, 26]),
        },
        {relation: torch.tensor([10, 12])},
    ]
    node_features = {
        ("A", "x"): torch.tensor([6, 4, 0, 1]),
    }
    edge_features = [
        {(relation, "x"): torch.tensor([4, 2, 4])},
        {(relation, "x"): torch.tensor([0, 6])},
    ]
    subgraphs = []
    for i in range(2):
        subgraphs.append(
            gb.SampledSubgraphImpl(
166
                sampled_csc=csc_formats[i],
167
168
169
170
171
                original_column_node_ids=original_column_node_ids[i],
                original_row_node_ids=original_row_node_ids[i],
                original_edge_ids=original_edge_ids[i],
            )
        )
172
    compacted_seeds = {relation: torch.tensor([0, 1])}
173
    # Test minibatch with all attributes.
174
    minibatch = gb.MiniBatch(
175
        seeds=seeds,
176
177
178
179
        sampled_subgraphs=subgraphs,
        node_features=node_features,
        edge_features=edge_features,
        labels={"B": torch.tensor([2, 5])},
180
        compacted_seeds=compacted_seeds,
181
182
183
184
185
186
        input_nodes={
            "A": torch.tensor([5, 7, 9, 11]),
            "B": torch.tensor([10, 11, 12]),
        },
    )
    expect_result = str(
187
        """MiniBatch(seeds={'A:r:B': tensor([10, 11])},
188
189
190
191
          sampled_subgraphs=[SampledSubgraphImpl(sampled_csc={'A:r:B': CSCFormatBase(indptr=tensor([0, 1, 2, 3], dtype=torch.int32),
                                                                         indices=tensor([0, 1, 1], dtype=torch.int32),
                                                           ), 'B:rr:A': CSCFormatBase(indptr=tensor([0, 0, 0, 1, 2], dtype=torch.int32),
                                                                         indices=tensor([1, 0], dtype=torch.int32),
192
193
                                                           )},
                                               original_row_node_ids={'A': tensor([ 5,  7,  9, 11]), 'B': tensor([10, 11, 12])},
194
195
196
                                               original_edge_ids={'A:r:B': tensor([19, 20, 21]), 'B:rr:A': tensor([23, 26])},
                                               original_column_node_ids={'B': tensor([10, 11, 12]), 'A': tensor([ 5,  7,  9, 11])},
                            ),
197
198
                            SampledSubgraphImpl(sampled_csc={'A:r:B': CSCFormatBase(indptr=tensor([0, 1, 2], dtype=torch.int32),
                                                                         indices=tensor([1, 0], dtype=torch.int32),
199
200
                                                           )},
                                               original_row_node_ids={'A': tensor([5, 7]), 'B': tensor([10, 11])},
201
202
203
204
205
206
                                               original_edge_ids={'A:r:B': tensor([10, 12])},
                                               original_column_node_ids={'B': tensor([10, 11])},
                            )],
          node_features={('A', 'x'): tensor([6, 4, 0, 1])},
          labels={'B': tensor([2, 5])},
          input_nodes={'A': tensor([ 5,  7,  9, 11]), 'B': tensor([10, 11, 12])},
207
          indexes=None,
208
209
          edge_features=[{('A:r:B', 'x'): tensor([4, 2, 4])},
                        {('A:r:B', 'x'): tensor([0, 6])}],
210
          compacted_seeds={'A:r:B': tensor([0, 1])},
211
212
213
214
215
216
217
218
          blocks=[Block(num_src_nodes={'A': 4, 'B': 3},
                       num_dst_nodes={'A': 4, 'B': 3},
                       num_edges={('A', 'r', 'B'): 3, ('B', 'rr', 'A'): 2},
                       metagraph=[('A', 'B', 'r'), ('B', 'A', 'rr')]),
                 Block(num_src_nodes={'A': 2, 'B': 2},
                       num_dst_nodes={'B': 2},
                       num_edges={('A', 'r', 'B'): 2},
                       metagraph=[('A', 'B', 'r')])],
219
220
221
       )"""
    )
    result = str(minibatch)
222
    assert result == expect_result, print(result)
223
224


225
226
227
@pytest.mark.parametrize("indptr_dtype", [torch.int32, torch.int64])
@pytest.mark.parametrize("indices_dtype", [torch.int32, torch.int64])
def test_get_dgl_blocks_homo(indptr_dtype, indices_dtype):
228
229
    csc_formats = [
        gb.CSCFormatBase(
230
231
            indptr=torch.tensor([0, 1, 3, 5, 6], dtype=indptr_dtype),
            indices=torch.tensor([0, 1, 2, 2, 1, 2], dtype=indices_dtype),
232
233
        ),
        gb.CSCFormatBase(
234
235
            indptr=torch.tensor([0, 1, 3], dtype=indptr_dtype),
            indices=torch.tensor([0, 1, 2], dtype=indices_dtype),
236
237
        ),
    ]
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
    original_column_node_ids = [
        torch.tensor([10, 11, 12, 13]),
        torch.tensor([10, 11]),
    ]
    original_row_node_ids = [
        torch.tensor([10, 11, 12, 13]),
        torch.tensor([10, 11, 12]),
    ]
    original_edge_ids = [
        torch.tensor([19, 20, 21, 22, 25, 30]),
        torch.tensor([10, 15, 17]),
    ]
    subgraphs = []
    for i in range(2):
        subgraphs.append(
253
254
            gb.SampledSubgraphImpl(
                sampled_csc=csc_formats[i],
255
256
257
258
259
                original_column_node_ids=original_column_node_ids[i],
                original_row_node_ids=original_row_node_ids[i],
                original_edge_ids=original_edge_ids[i],
            )
        )
260
    # Test minibatch with all attributes.
261
262
263
    minibatch = gb.MiniBatch(
        sampled_subgraphs=subgraphs,
    )
264
    dgl_blocks = minibatch.blocks
265
    expect_result = str(
266
        """[Block(num_src_nodes=4, num_dst_nodes=4, num_edges=6), Block(num_src_nodes=3, num_dst_nodes=2, num_edges=3)]"""
267
    )
268
    result = str(dgl_blocks)
269
    assert result == expect_result
270
271


272
def test_get_dgl_blocks_hetero():
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
    csc_formats = [
        {
            relation: gb.CSCFormatBase(
                indptr=torch.tensor([0, 1, 2, 3]),
                indices=torch.tensor([0, 1, 1]),
            ),
            reverse_relation: gb.CSCFormatBase(
                indptr=torch.tensor([0, 0, 0, 1, 2]),
                indices=torch.tensor([1, 0]),
            ),
        },
        {
            relation: gb.CSCFormatBase(
                indptr=torch.tensor([0, 1, 2]), indices=torch.tensor([1, 0])
            )
        },
    ]
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
    original_column_node_ids = [
        {"B": torch.tensor([10, 11, 12]), "A": torch.tensor([5, 7, 9, 11])},
        {"B": torch.tensor([10, 11])},
    ]
    original_row_node_ids = [
        {
            "A": torch.tensor([5, 7, 9, 11]),
            "B": torch.tensor([10, 11, 12]),
        },
        {
            "A": torch.tensor([5, 7]),
            "B": torch.tensor([10, 11]),
        },
    ]
    original_edge_ids = [
        {
            relation: torch.tensor([19, 20, 21]),
            reverse_relation: torch.tensor([23, 26]),
        },
        {relation: torch.tensor([10, 12])},
    ]
    subgraphs = []
    for i in range(2):
        subgraphs.append(
314
315
            gb.SampledSubgraphImpl(
                sampled_csc=csc_formats[i],
316
317
318
319
320
                original_column_node_ids=original_column_node_ids[i],
                original_row_node_ids=original_row_node_ids[i],
                original_edge_ids=original_edge_ids[i],
            )
        )
321
    # Test minibatch with all attributes.
322
323
324
    minibatch = gb.MiniBatch(
        sampled_subgraphs=subgraphs,
    )
325
    dgl_blocks = minibatch.blocks
326
    expect_result = str(
327
328
329
330
331
332
333
        """[Block(num_src_nodes={'A': 4, 'B': 3},
      num_dst_nodes={'A': 4, 'B': 3},
      num_edges={('A', 'r', 'B'): 3, ('B', 'rr', 'A'): 2},
      metagraph=[('A', 'B', 'r'), ('B', 'A', 'rr')]), Block(num_src_nodes={'A': 2, 'B': 2},
      num_dst_nodes={'B': 2},
      num_edges={('A', 'r', 'B'): 2},
      metagraph=[('A', 'B', 'r')])]"""
334
    )
335
    result = str(dgl_blocks)
336
    assert result == expect_result
337
338


339
def create_homo_minibatch():
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
    csc_formats = [
        gb.CSCFormatBase(
            indptr=torch.tensor([0, 1, 3, 5, 6]),
            indices=torch.tensor([0, 1, 2, 2, 1, 2]),
        ),
        gb.CSCFormatBase(
            indptr=torch.tensor([0, 2, 3]),
            indices=torch.tensor([1, 2, 0]),
        ),
    ]
    original_column_node_ids = [
        torch.tensor([10, 11, 12, 13]),
        torch.tensor([10, 11]),
    ]
    original_row_node_ids = [
        torch.tensor([10, 11, 12, 13]),
        torch.tensor([10, 11, 12]),
    ]
    original_edge_ids = [
        torch.tensor([19, 20, 21, 22, 25, 30]),
        torch.tensor([10, 15, 17]),
    ]
    node_features = {"x": torch.randint(0, 10, (4,))}
    edge_features = [
        {"x": torch.randint(0, 10, (6,))},
        {"x": torch.randint(0, 10, (3,))},
    ]
    subgraphs = []
    for i in range(2):
        subgraphs.append(
            gb.SampledSubgraphImpl(
371
                sampled_csc=csc_formats[i],
372
373
374
375
376
377
378
379
380
381
382
383
384
                original_column_node_ids=original_column_node_ids[i],
                original_row_node_ids=original_row_node_ids[i],
                original_edge_ids=original_edge_ids[i],
            )
        )
    return gb.MiniBatch(
        sampled_subgraphs=subgraphs,
        node_features=node_features,
        edge_features=edge_features,
        input_nodes=torch.tensor([10, 11, 12, 13]),
    )


385
def create_hetero_minibatch():
386
    sampled_csc = [
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
        {
            relation: gb.CSCFormatBase(
                indptr=torch.tensor([0, 1, 2, 3]),
                indices=torch.tensor([0, 1, 1]),
            ),
            reverse_relation: gb.CSCFormatBase(
                indptr=torch.tensor([0, 0, 0, 1, 2]),
                indices=torch.tensor([1, 0]),
            ),
        },
        {
            relation: gb.CSCFormatBase(
                indptr=torch.tensor([0, 1, 2]), indices=torch.tensor([1, 0])
            )
        },
    ]
    original_column_node_ids = [
        {"B": torch.tensor([10, 11, 12]), "A": torch.tensor([5, 7, 9, 11])},
        {"B": torch.tensor([10, 11])},
    ]
    original_row_node_ids = [
        {
            "A": torch.tensor([5, 7, 9, 11]),
            "B": torch.tensor([10, 11, 12]),
        },
        {
            "A": torch.tensor([5, 7]),
            "B": torch.tensor([10, 11]),
        },
    ]
    original_edge_ids = [
        {
            relation: torch.tensor([19, 20, 21]),
            reverse_relation: torch.tensor([23, 26]),
        },
        {relation: torch.tensor([10, 12])},
    ]
    node_features = {
        ("A", "x"): torch.randint(0, 10, (4,)),
    }
    edge_features = [
        {(relation, "x"): torch.randint(0, 10, (3,))},
        {(relation, "x"): torch.randint(0, 10, (2,))},
    ]
    subgraphs = []
    for i in range(2):
        subgraphs.append(
            gb.SampledSubgraphImpl(
435
                sampled_csc=sampled_csc[i],
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
                original_column_node_ids=original_column_node_ids[i],
                original_row_node_ids=original_row_node_ids[i],
                original_edge_ids=original_edge_ids[i],
            )
        )
    return gb.MiniBatch(
        sampled_subgraphs=subgraphs,
        node_features=node_features,
        edge_features=edge_features,
        input_nodes={
            "A": torch.tensor([5, 7, 9, 11]),
            "B": torch.tensor([10, 11, 12]),
        },
    )


452
def check_dgl_blocks_hetero(minibatch, blocks):
453
    etype = gb.etype_str_to_tuple(relation)
454
455
    sampled_csc = [
        subgraph.sampled_csc for subgraph in minibatch.sampled_subgraphs
456
457
458
459
460
461
462
463
464
465
466
467
    ]
    original_edge_ids = [
        subgraph.original_edge_ids for subgraph in minibatch.sampled_subgraphs
    ]
    original_row_node_ids = [
        subgraph.original_row_node_ids
        for subgraph in minibatch.sampled_subgraphs
    ]

    for i, block in enumerate(blocks):
        edges = block.edges(etype=etype)
        dst_ndoes = torch.arange(
468
            0, len(sampled_csc[i][relation].indptr) - 1
469
        ).repeat_interleave(sampled_csc[i][relation].indptr.diff())
470
        assert torch.equal(edges[0], sampled_csc[i][relation].indices)
471
472
473
474
475
476
        assert torch.equal(edges[1], dst_ndoes)
        assert torch.equal(
            block.edges[etype].data[dgl.EID], original_edge_ids[i][relation]
        )
    edges = blocks[0].edges(etype=gb.etype_str_to_tuple(reverse_relation))
    dst_ndoes = torch.arange(
477
        0, len(sampled_csc[0][reverse_relation].indptr) - 1
478
    ).repeat_interleave(sampled_csc[0][reverse_relation].indptr.diff())
479
    assert torch.equal(edges[0], sampled_csc[0][reverse_relation].indices)
480
481
482
483
484
485
486
487
488
    assert torch.equal(edges[1], dst_ndoes)
    assert torch.equal(
        blocks[0].srcdata[dgl.NID]["A"], original_row_node_ids[0]["A"]
    )
    assert torch.equal(
        blocks[0].srcdata[dgl.NID]["B"], original_row_node_ids[0]["B"]
    )


489
def check_dgl_blocks_homo(minibatch, blocks):
490
491
    sampled_csc = [
        subgraph.sampled_csc for subgraph in minibatch.sampled_subgraphs
492
493
494
495
496
497
498
499
500
501
    ]
    original_edge_ids = [
        subgraph.original_edge_ids for subgraph in minibatch.sampled_subgraphs
    ]
    original_row_node_ids = [
        subgraph.original_row_node_ids
        for subgraph in minibatch.sampled_subgraphs
    ]
    for i, block in enumerate(blocks):
        dst_ndoes = torch.arange(
502
            0, len(sampled_csc[i].indptr) - 1
503
        ).repeat_interleave(sampled_csc[i].indptr.diff())
504
505
506
507
        assert torch.equal(block.edges()[0], sampled_csc[i].indices)
        assert torch.equal(block.edges()[1], dst_ndoes)
        assert torch.equal(block.edata[dgl.EID], original_edge_ids[i])
    assert torch.equal(blocks[0].srcdata[dgl.NID], original_row_node_ids[0])
508
509


510
def test_dgl_node_classification_without_feature():
511
    # Arrange
512
    minibatch = create_homo_minibatch()
513
514
    minibatch.node_features = None
    minibatch.labels = None
515
    minibatch.seeds = torch.tensor([10, 15])
516
    # Act
517
    dgl_blocks = minibatch.blocks
518
519

    # Assert
520
521
522
    assert len(dgl_blocks) == 2
    assert minibatch.node_features is None
    assert minibatch.labels is None
523
    check_dgl_blocks_homo(minibatch, dgl_blocks)
524
525


526
def test_dgl_node_classification_homo():
527
    # Arrange
528
    minibatch = create_homo_minibatch()
529
    minibatch.seeds = torch.tensor([10, 15])
530
531
    minibatch.labels = torch.tensor([2, 5])
    # Act
532
    dgl_blocks = minibatch.blocks
533
534

    # Assert
535
    assert len(dgl_blocks) == 2
536
    check_dgl_blocks_homo(minibatch, dgl_blocks)
537
538


539
540
def test_dgl_node_classification_hetero():
    minibatch = create_hetero_minibatch()
541
    minibatch.labels = {"B": torch.tensor([2, 5])}
542
    minibatch.seeds = {"B": torch.tensor([10, 15])}
543
544
    # Act
    dgl_blocks = minibatch.blocks
545
546

    # Assert
547
    assert len(dgl_blocks) == 2
548
    check_dgl_blocks_hetero(minibatch, dgl_blocks)
549
550


551
def test_dgl_link_predication_homo():
552
    # Arrange
553
    minibatch = create_homo_minibatch()
554
555
    minibatch.compacted_seeds = (
        torch.tensor([[0, 1, 0, 0, 1, 1], [1, 0, 1, 1, 0, 0]]).T,
556
    )
557
    minibatch.labels = torch.tensor([1, 1, 0, 0, 0, 0])
558
    # Act
559
    dgl_blocks = minibatch.blocks
560
561

    # Assert
562
    assert len(dgl_blocks) == 2
563
    check_dgl_blocks_homo(minibatch, dgl_blocks)
564
565


566
def test_dgl_link_predication_hetero():
567
    # Arrange
568
    minibatch = create_hetero_minibatch()
569
    minibatch.compacted_seeds = {
570
        relation: (torch.tensor([[1, 1, 2, 0, 1, 2], [1, 0, 1, 1, 0, 0]]).T,),
571
        reverse_relation: (
572
            torch.tensor([[0, 1, 1, 2, 0, 2], [1, 0, 1, 1, 0, 0]]).T,
573
574
        ),
    }
575
576
577
578
    minibatch.labels = {
        relation: (torch.tensor([1, 1, 0, 0, 0, 0]),),
        reverse_relation: (torch.tensor([1, 1, 0, 0, 0, 0]),),
    }
579
    # Act
580
    dgl_blocks = minibatch.blocks
581
582

    # Assert
583
    assert len(dgl_blocks) == 2
584
    check_dgl_blocks_hetero(minibatch, dgl_blocks)
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


def test_to_pyg_data():
    test_minibatch = create_homo_minibatch()
    test_minibatch.seeds = torch.tensor([0, 1])
    test_minibatch.labels = torch.tensor([7, 8])

    expected_edge_index = torch.tensor(
        [[0, 0, 1, 1, 1, 2, 2, 2, 2], [0, 1, 0, 1, 2, 0, 1, 2, 3]]
    )
    expected_node_features = next(iter(test_minibatch.node_features.values()))
    expected_labels = torch.tensor([7, 8])
    expected_batch_size = 2
    expected_n_id = torch.tensor([10, 11, 12, 13])

    pyg_data = test_minibatch.to_pyg_data()
    pyg_data.validate()
    assert torch.equal(pyg_data.edge_index, expected_edge_index)
    assert torch.equal(pyg_data.x, expected_node_features)
    assert torch.equal(pyg_data.y, expected_labels)
    assert pyg_data.batch_size == expected_batch_size
    assert torch.equal(pyg_data.n_id, expected_n_id)

    test_minibatch.seeds = torch.tensor([[0, 1], [2, 3]])
    assert pyg_data.batch_size == expected_batch_size

    test_minibatch.seeds = {"A": torch.tensor([0, 1])}
    assert pyg_data.batch_size == expected_batch_size

    test_minibatch.seeds = {"A": torch.tensor([[0, 1], [2, 3]])}
    assert pyg_data.batch_size == expected_batch_size

    subgraph = test_minibatch.sampled_subgraphs[0]
    # Test with sampled_csc as None.
    test_minibatch = gb.MiniBatch(
        sampled_subgraphs=None,
        node_features={"feat": expected_node_features},
        labels=expected_labels,
    )
    pyg_data = test_minibatch.to_pyg_data()
    assert pyg_data.edge_index is None, "Edge index should be none."

627
628
    # Test with node_features as None.
    test_minibatch = gb.MiniBatch(
629
        sampled_subgraphs=[subgraph],
630
631
632
633
634
635
636
637
        node_features=None,
        labels=expected_labels,
    )
    pyg_data = test_minibatch.to_pyg_data()
    assert pyg_data.x is None, "Node features should be None."

    # Test with labels as None.
    test_minibatch = gb.MiniBatch(
638
        sampled_subgraphs=[subgraph],
639
640
641
642
643
644
645
646
        node_features={"feat": expected_node_features},
        labels=None,
    )
    pyg_data = test_minibatch.to_pyg_data()
    assert pyg_data.y is None, "Labels should be None."

    # Test with multiple features.
    test_minibatch = gb.MiniBatch(
647
        sampled_subgraphs=[subgraph],
648
649
650
651
652
653
654
655
656
        node_features={
            "feat": expected_node_features,
            "extra_feat": torch.tensor([[3], [4]]),
        },
        labels=expected_labels,
    )
    try:
        pyg_data = test_minibatch.to_pyg_data()
        assert (
657
            pyg_data.x is None
658
659
660
661
662
663
        ), "Multiple features case should raise an error."
    except AssertionError as e:
        assert (
            str(e)
            == "`to_pyg_data` only supports single feature homogeneous graph."
        )