test_rw.py 2.53 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
3
import pytest
import torch
from torch_cluster import random_walk
Matthias Fey's avatar
Matthias Fey committed
4
from torch_cluster.testing import devices, tensor
rusty1s's avatar
rusty1s committed
5
6


rusty1s's avatar
rusty1s committed
7
@pytest.mark.parametrize('device', devices)
8
def test_rw_large(device):
rusty1s's avatar
rusty1s committed
9
10
    row = tensor([0, 1, 1, 1, 2, 2, 3, 3, 4, 4], torch.long, device)
    col = tensor([1, 0, 2, 3, 1, 4, 1, 4, 2, 3], torch.long, device)
rusty1s's avatar
rusty1s committed
11
    start = tensor([0, 1, 2, 3, 4], torch.long, device)
rusty1s's avatar
rusty1s committed
12
    walk_length = 10
rusty1s's avatar
rusty1s committed
13

rusty1s's avatar
rusty1s committed
14
    out = random_walk(row, col, start, walk_length)
rusty1s's avatar
rusty1s committed
15
16
17
18
    assert out[:, 0].tolist() == start.tolist()

    for n in range(start.size(0)):
        cur = start[n].item()
rusty1s's avatar
rusty1s committed
19
20
21
        for i in range(1, walk_length):
            assert out[n, i].item() in col[row == cur].tolist()
            cur = out[n, i].item()
rusty1s's avatar
rusty1s committed
22

23
24
25

@pytest.mark.parametrize('device', devices)
def test_rw_small(device):
rusty1s's avatar
rusty1s committed
26
27
28
29
30
31
    row = tensor([0, 1], torch.long, device)
    col = tensor([1, 0], torch.long, device)
    start = tensor([0, 1, 2], torch.long, device)
    walk_length = 4

    out = random_walk(row, col, start, walk_length, num_nodes=3)
rusty1s's avatar
rusty1s committed
32
    assert out.tolist() == [[0, 1, 0, 1, 0], [1, 0, 1, 0, 1], [2, 2, 2, 2, 2]]
33

34
35
36
    jit = torch.jit.script(random_walk)
    assert torch.equal(jit(row, col, start, walk_length, num_nodes=3), out)

37
38
39
40
41
42
43
44
45

@pytest.mark.parametrize('device', devices)
def test_rw_large_with_edge_indices(device):
    row = tensor([0, 1, 1, 1, 2, 2, 3, 3, 4, 4], torch.long, device)
    col = tensor([1, 0, 2, 3, 1, 4, 1, 4, 2, 3], torch.long, device)
    start = tensor([0, 1, 2, 3, 4], torch.long, device)
    walk_length = 10

    node_seq, edge_seq = random_walk(
Matthias Fey's avatar
Matthias Fey committed
46
47
48
49
        row,
        col,
        start,
        walk_length,
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
        return_edge_indices=True,
    )
    assert node_seq[:, 0].tolist() == start.tolist()

    for n in range(start.size(0)):
        cur = start[n].item()
        for i in range(1, walk_length):
            assert node_seq[n, i].item() in col[row == cur].tolist()
            cur = node_seq[n, i].item()

    assert (edge_seq != -1).all()


@pytest.mark.parametrize('device', devices)
def test_rw_small_with_edge_indices(device):
    row = tensor([0, 1], torch.long, device)
    col = tensor([1, 0], torch.long, device)
    start = tensor([0, 1, 2], torch.long, device)
    walk_length = 4

    node_seq, edge_seq = random_walk(
Matthias Fey's avatar
Matthias Fey committed
71
72
73
74
        row,
        col,
        start,
        walk_length,
75
76
77
78
79
80
81
82
83
84
85
86
87
        num_nodes=3,
        return_edge_indices=True,
    )
    assert node_seq.tolist() == [
        [0, 1, 0, 1, 0],
        [1, 0, 1, 0, 1],
        [2, 2, 2, 2, 2],
    ]
    assert edge_seq.tolist() == [
        [0, 1, 0, 1],
        [1, 0, 1, 0],
        [-1, -1, -1, -1],
    ]