File: test_sampler_base.py

package info (click to toggle)
pytorch-geometric 2.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,172 kB
  • sloc: python: 144,911; sh: 247; cpp: 27; makefile: 18; javascript: 16
file content (431 lines) | stat: -rw-r--r-- 15,367 bytes parent folder | download
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
import pytest
import torch

from torch_geometric.sampler.base import (
    HeteroSamplerOutput,
    NumNeighbors,
    SamplerOutput,
)
from torch_geometric.sampler.utils import global_to_local_node_idx
from torch_geometric.testing import get_random_edge_index
from torch_geometric.utils import is_undirected


def test_homogeneous_num_neighbors():
    with pytest.raises(ValueError, match="'default' must be set to 'None'"):
        num_neighbors = NumNeighbors([25, 10], default=[-1, -1])

    num_neighbors = NumNeighbors([25, 10])
    assert str(num_neighbors) == 'NumNeighbors(values=[25, 10], default=None)'

    assert num_neighbors.get_values() == [25, 10]
    assert num_neighbors.__dict__['_values'] == [25, 10]
    assert num_neighbors.get_values() == [25, 10]  # Test caching.

    assert num_neighbors.get_mapped_values() == [25, 10]
    assert num_neighbors.__dict__['_mapped_values'] == [25, 10]
    assert num_neighbors.get_mapped_values() == [25, 10]  # Test caching.

    assert num_neighbors.num_hops == 2
    assert num_neighbors.__dict__['_num_hops'] == 2
    assert num_neighbors.num_hops == 2  # Test caching.


'''
Merge and collate tests use the following graph:

    #############                    ###########
    # Alice (0) # -> "works with" -> # Bob (1) #
    #############                    ###########
         |
         v
      "leads"
         |
         v
    #############                    ############
    # Carol (2) # -> "works with" -> # Dave (3) #
    #############                    ############

'''


def _init_merge_sampler_outputs(hetero=False, disjoint=False):
    if not hetero:
        output1 = SamplerOutput(
            node=torch.tensor([0, 1, 2]),
            row=torch.tensor([0, 0]),
            col=torch.tensor([1, 2]),
            edge=torch.tensor([0, 1]),
            batch=torch.tensor([0, 0, 0]) if disjoint else None,
            num_sampled_nodes=list([1, 2]),
            num_sampled_edges=list([2]),
            orig_row=None,
            orig_col=None,
            metadata=(None, None),
        )
        output2 = SamplerOutput(
            node=torch.tensor([0, 2, 3]),
            row=torch.tensor([0, 1]),
            col=torch.tensor([1, 2]),
            edge=torch.tensor([1, 2]),
            batch=torch.tensor([0, 0, 0]) if disjoint else None,
            num_sampled_nodes=list([1, 1, 1]),
            num_sampled_edges=list([1, 1]),
            orig_row=None,
            orig_col=None,
            metadata=(None, None),
        )

        return output1, output2
    else:
        # TODO(zaristei)
        raise NotImplementedError("Heterogeneous merge not implemented")


@pytest.mark.parametrize("disjoint", [True, False])
@pytest.mark.parametrize("bidirectional", [True, False])
def test_homogeneous_merge(disjoint, bidirectional):
    """Merge an output representing 1<-0->2 with one representing 0->2->3."""
    output1, output2 = _init_merge_sampler_outputs(disjoint=disjoint)
    if bidirectional:
        output1 = output1.to_bidirectional(keep_orig_edges=True)
        output2 = output2.to_bidirectional(keep_orig_edges=True)

    expected_output = SamplerOutput(
        node=torch.tensor([0, 1, 2, 3]),
        row=torch.tensor([0, 0, 2]),
        col=torch.tensor([1, 2, 3]),
        edge=torch.tensor([0, 1, 2]),
        batch=torch.tensor([0, 0, 0, 0]) if disjoint else None,
        num_sampled_nodes=[1, 2, 0, 0, 1],
        num_sampled_edges=[2, 0, 1],
        orig_row=None,
        orig_col=None,
        metadata=[(None, None), (None, None)],
    )
    if bidirectional:
        expected_output = expected_output.to_bidirectional(
            keep_orig_edges=True)
    merged_output = output1.merge_with(output2)

    assert str(merged_output) == str(expected_output)


@pytest.mark.parametrize("disjoint", [True, False])
@pytest.mark.parametrize("bidirectional", [True, False])
def test_homogeneous_merge_no_replace(disjoint, bidirectional):
    """Merge an output representing 1<-0->2 with one representing 0->2->3.
    replace=True makes it so that merged output is a simple concatenation
    instead of removing already sampled nodes/edges.
    """
    output1, output2 = _init_merge_sampler_outputs(disjoint=disjoint)
    if bidirectional:
        output1 = output1.to_bidirectional(keep_orig_edges=True)
        output2 = output2.to_bidirectional(keep_orig_edges=True)

    expected_output = SamplerOutput(
        node=torch.tensor([0, 1, 2, 0, 2, 3]),
        row=torch.tensor([0, 0, 3, 4]),
        col=torch.tensor([1, 2, 4, 5]),
        edge=torch.tensor([0, 1, 1, 2]),
        batch=torch.tensor([0, 0, 0, 3, 3, 3]) if disjoint else None,
        num_sampled_nodes=[1, 2, 1, 1, 1],
        num_sampled_edges=[2, 1, 1],
        orig_row=None,
        orig_col=None,
        metadata=[(None, None), (None, None)],
    )
    if bidirectional:
        expected_output = expected_output.to_bidirectional(
            keep_orig_edges=True)
    merged_output = output1.merge_with(output2, replace=False)

    assert str(merged_output) == str(expected_output)


def _init_collate_sampler_outputs(disjoint=False):
    output1, output2 = _init_merge_sampler_outputs(disjoint=disjoint)
    # new edge not present in graph above
    output3 = SamplerOutput(
        node=torch.tensor([3, 4]),
        row=torch.tensor([0]),
        col=torch.tensor([1]),
        edge=torch.tensor([3]),
        batch=torch.tensor([0, 0]) if disjoint else None,
        num_sampled_nodes=list([1, 1]),
        num_sampled_edges=list([1]),
        orig_row=None,
        orig_col=None,
        metadata=(None, None),
    )
    return [output1, output2, output3]


@pytest.mark.parametrize("replace", [True, False])
@pytest.mark.parametrize("disjoint", [True, False])
def test_homogeneous_collate(disjoint, replace):
    output1, output2, output3 = _init_collate_sampler_outputs(disjoint)
    collated = SamplerOutput.collate([output1, output2, output3],
                                     replace=replace)
    assert str(collated) == str(
        (output1.merge_with(output2, replace=replace)).merge_with(
            output3, replace=replace))


def test_homogeneous_collate_empty():
    with pytest.raises(ValueError,
                       match="Cannot collate an empty list of SamplerOutputs"):
        SamplerOutput.collate([])


def test_homogeneous_collate_single():
    output, _ = _init_merge_sampler_outputs()
    collated = SamplerOutput.collate([output])
    assert str(collated) == str(output)


def test_homogeneous_collate_missing_fields():
    output1, output2, output3 = _init_collate_sampler_outputs()
    output3.edge = None
    with pytest.raises(
            ValueError,
            match="Output 3 has a different field than the first output"):
        SamplerOutput.collate([output1, output2, output3])


def test_heterogeneous_num_neighbors_list():
    num_neighbors = NumNeighbors([25, 10])

    values = num_neighbors.get_values([('A', 'B'), ('B', 'A')])
    assert values == {('A', 'B'): [25, 10], ('B', 'A'): [25, 10]}

    values = num_neighbors.get_mapped_values([('A', 'B'), ('B', 'A')])
    assert values == {'A__to__B': [25, 10], 'B__to__A': [25, 10]}

    assert num_neighbors.num_hops == 2


def test_heterogeneous_num_neighbors_dict_and_default():
    num_neighbors = NumNeighbors({('A', 'B'): [25, 10]}, default=[-1])
    with pytest.raises(ValueError, match="hops must be the same across all"):
        values = num_neighbors.get_values([('A', 'B'), ('B', 'A')])

    num_neighbors = NumNeighbors({('A', 'B'): [25, 10]}, default=[-1, -1])

    with pytest.raises(ValueError, match="Not all edge types"):
        num_neighbors.get_values([('A', 'C'), ('B', 'A')])

    values = num_neighbors.get_values([('A', 'B'), ('B', 'A')])
    assert values == {('A', 'B'): [25, 10], ('B', 'A'): [-1, -1]}

    values = num_neighbors.get_mapped_values([('A', 'B'), ('B', 'A')])
    assert values == {'A__to__B': [25, 10], 'B__to__A': [-1, -1]}

    assert num_neighbors.num_hops == 2


def test_heterogeneous_num_neighbors_empty_dict():
    num_neighbors = NumNeighbors({}, default=[25, 10])

    values = num_neighbors.get_values([('A', 'B'), ('B', 'A')])
    assert values == {('A', 'B'): [25, 10], ('B', 'A'): [25, 10]}

    values = num_neighbors.get_mapped_values([('A', 'B'), ('B', 'A')])
    assert values == {'A__to__B': [25, 10], 'B__to__A': [25, 10]}

    assert num_neighbors.num_hops == 2


def test_homogeneous_to_bidirectional():
    edge_index = get_random_edge_index(10, 10, num_edges=20)

    obj = SamplerOutput(
        node=torch.arange(10),
        row=edge_index[0],
        col=edge_index[0],
        edge=torch.arange(edge_index.size(1)),
    ).to_bidirectional()

    assert is_undirected(torch.stack([obj.row, obj.col], dim=0))


def test_heterogeneous_to_bidirectional():
    edge_index1 = get_random_edge_index(10, 5, num_edges=20)
    edge_index2 = get_random_edge_index(5, 10, num_edges=20)
    edge_index3 = get_random_edge_index(10, 10, num_edges=20)

    obj = HeteroSamplerOutput(
        node={
            'v1': torch.arange(10),
            'v2': torch.arange(5)
        },
        row={
            ('v1', 'to', 'v2'): edge_index1[0],
            ('v2', 'rev_to', 'v1'): edge_index2[0],
            ('v1', 'to', 'v1'): edge_index3[0],
        },
        col={
            ('v1', 'to', 'v2'): edge_index1[1],
            ('v2', 'rev_to', 'v1'): edge_index2[1],
            ('v1', 'to', 'v1'): edge_index3[1],
        },
        edge={},
    ).to_bidirectional()

    assert torch.equal(
        obj.row['v1', 'to', 'v2'].sort().values,
        obj.col['v2', 'rev_to', 'v1'].sort().values,
    )
    assert torch.equal(
        obj.col['v1', 'to', 'v2'].sort().values,
        obj.row['v2', 'rev_to', 'v1'].sort().values,
    )
    assert is_undirected(
        torch.stack([obj.row['v1', 'to', 'v1'], obj.col['v1', 'to', 'v1']], 0))


def test_homogeneous_sampler_output_global_fields():
    output = SamplerOutput(
        node=torch.tensor([0, 2, 3]),
        row=torch.tensor([0, 1]),
        col=torch.tensor([1, 2]),
        edge=torch.tensor([1, 2]),
        batch=torch.tensor([0, 0, 0]),
        num_sampled_nodes=[1, 1, 1],
        num_sampled_edges=[1, 1],
        orig_row=None,
        orig_col=None,
        metadata=(None, None),
    )

    local_values = []
    global_values = []

    global_row, global_col = output.global_row, output.global_col
    assert torch.equal(global_row, torch.tensor([0, 2]))
    assert torch.equal(global_col, torch.tensor([2, 3]))
    local_values.append(output.row)
    local_values.append(output.col)
    global_values.append(global_row)
    global_values.append(global_col)

    seed_node = output.seed_node
    assert torch.equal(seed_node, torch.tensor([0, 0, 0]))
    local_values.append(output.batch)
    global_values.append(seed_node)

    output_bidirectional = output.to_bidirectional(keep_orig_edges=True)
    global_bidir_row, global_bidir_col = \
        output_bidirectional.global_row, output_bidirectional.global_col
    assert torch.equal(global_bidir_row, torch.tensor([2, 0, 3, 2]))
    assert torch.equal(global_bidir_col, torch.tensor([0, 2, 2, 3]))
    local_values.append(output_bidirectional.row)
    local_values.append(output_bidirectional.col)
    global_values.append(global_bidir_row)
    global_values.append(global_bidir_col)

    assert torch.equal(output.global_row, output_bidirectional.global_orig_row)
    assert torch.equal(output.global_col, output_bidirectional.global_orig_col)

    # Make sure reverse mapping is correct
    for local_value, global_value in zip(local_values, global_values):
        assert torch.equal(global_to_local_node_idx(output.node, global_value),
                           local_value)


def test_heterogeneous_sampler_output_global_fields():
    def _tensor_dict_equal(dict1, dict2):
        is_equal = True
        is_equal &= dict1.keys() == dict2.keys()
        for key in dict1.keys():
            is_equal &= torch.equal(dict1[key], dict2[key])
        return is_equal

    output = HeteroSamplerOutput(
        node={"person": torch.tensor([0, 2, 3])},
        row={
            ("person", "works_with", "person"): torch.tensor([1]),
            ("person", "leads", "person"): torch.tensor([0])
        },
        col={
            ("person", "works_with", "person"): torch.tensor([2]),
            ("person", "leads", "person"): torch.tensor([1])
        },
        edge={
            ("person", "works_with", "person"): torch.tensor([1]),
            ("person", "leads", "person"): torch.tensor([0])
        },
        batch={"person": torch.tensor([0, 0, 0])},
        num_sampled_nodes={"person": torch.tensor([1, 1, 1])},
        num_sampled_edges={
            ("person", "works_with", "person"): torch.tensor([1]),
            ("person", "leads", "person"): torch.tensor([1])
        },
        orig_row=None,
        orig_col=None,
        metadata=(None, None),
    )

    global_row, global_col = output.global_row, output.global_col
    assert _tensor_dict_equal(
        global_row, {
            ("person", "works_with", "person"): torch.tensor([2]),
            ("person", "leads", "person"): torch.tensor([0])
        })
    assert _tensor_dict_equal(
        global_col, {
            ("person", "works_with", "person"): torch.tensor([3]),
            ("person", "leads", "person"): torch.tensor([2])
        })

    local_row_dict = {
        k: global_to_local_node_idx(output.node[k[0]], v)
        for k, v in global_row.items()
    }
    assert _tensor_dict_equal(local_row_dict, output.row)

    local_col_dict = {
        k: global_to_local_node_idx(output.node[k[2]], v)
        for k, v in global_col.items()
    }
    assert _tensor_dict_equal(local_col_dict, output.col)

    seed_node = output.seed_node
    assert _tensor_dict_equal(seed_node, {"person": torch.tensor([0, 0, 0])})

    local_batch_dict = {
        k: global_to_local_node_idx(output.node[k], v)
        for k, v in seed_node.items()
    }
    assert _tensor_dict_equal(local_batch_dict, output.batch)

    output_bidirectional = output.to_bidirectional(keep_orig_edges=True)
    global_bidir_row, global_bidir_col = \
        output_bidirectional.global_row, output_bidirectional.global_col
    assert _tensor_dict_equal(
        global_bidir_row, {
            ("person", "works_with", "person"): torch.tensor([3, 2]),
            ("person", "leads", "person"): torch.tensor([2, 0])
        })
    assert _tensor_dict_equal(
        global_bidir_col, {
            ("person", "works_with", "person"): torch.tensor([2, 3]),
            ("person", "leads", "person"): torch.tensor([0, 2])
        })

    local_bidir_row_dict = {
        k: global_to_local_node_idx(output_bidirectional.node[k[0]], v)
        for k, v in global_bidir_row.items()
    }
    assert _tensor_dict_equal(local_bidir_row_dict, output_bidirectional.row)

    local_bidir_col_dict = {
        k: global_to_local_node_idx(output_bidirectional.node[k[2]], v)
        for k, v in global_bidir_col.items()
    }
    assert _tensor_dict_equal(local_bidir_col_dict, output_bidirectional.col)

    assert _tensor_dict_equal(output.global_row,
                              output_bidirectional.global_orig_row)
    assert _tensor_dict_equal(output.global_col,
                              output_bidirectional.global_orig_col)