Debian Codegraph @

test/test_rw.py

raw symbols SCIP
1 import pytest
2 import torch
3 from torch_cluster import random_walk
4 from torch_cluster.testing import devices, tensor
5
6
7 @pytest.mark.parametrize('device', devices)
8 def test_rw_large(device):
9 row = tensor([0, 1, 1, 1, 2, 2, 3, 3, 4, 4], torch.long, device)
10 col = tensor([1, 0, 2, 3, 1, 4, 1, 4, 2, 3], torch.long, device)
11 start = tensor([0, 1, 2, 3, 4], torch.long, device)
12 walk_length = 10
13
14 out = random_walk(row, col, start, walk_length)
15 assert out[:, 0].tolist() == start.tolist()
16
17 for n in range(start.size(0)):
18 cur = start[n].item()
19 for i in range(1, walk_length):
20 assert out[n, i].item() in col[row == cur].tolist()
21 cur = out[n, i].item()
22
23
24 @pytest.mark.parametrize('device', devices)
25 def test_rw_small(device):
26 row = tensor([0, 1], torch.long, device)
27 col = tensor([1, 0], torch.long, device)
28 start = tensor([0, 1, 2], torch.long, device)
29 walk_length = 4
30
31 out = random_walk(row, col, start, walk_length, num_nodes=3)
32 assert out.tolist() == [[0, 1, 0, 1, 0], [1, 0, 1, 0, 1], [2, 2, 2, 2, 2]]
33
34 jit = torch.jit.script(random_walk)
35 assert torch.equal(jit(row, col, start, walk_length, num_nodes=3), out)
36
37
38 @pytest.mark.parametrize('device', devices)
39 def test_rw_large_with_edge_indices(device):
40 row = tensor([0, 1, 1, 1, 2, 2, 3, 3, 4, 4], torch.long, device)
41 col = tensor([1, 0, 2, 3, 1, 4, 1, 4, 2, 3], torch.long, device)
42 start = tensor([0, 1, 2, 3, 4], torch.long, device)
43 walk_length = 10
44
45 node_seq, edge_seq = random_walk(
46 row,
47 col,
48 start,
49 walk_length,
50 return_edge_indices=True,
51 )
52 assert node_seq[:, 0].tolist() == start.tolist()
53
54 for n in range(start.size(0)):
55 cur = start[n].item()
56 for i in range(1, walk_length):
57 assert node_seq[n, i].item() in col[row == cur].tolist()
58 cur = node_seq[n, i].item()
59
60 assert (edge_seq != -1).all()
61
62
63 @pytest.mark.parametrize('device', devices)
64 def test_rw_small_with_edge_indices(device):
65 row = tensor([0, 1], torch.long, device)
66 col = tensor([1, 0], torch.long, device)
67 start = tensor([0, 1, 2], torch.long, device)
68 walk_length = 4
69
70 node_seq, edge_seq = random_walk(
71 row,
72 col,
73 start,
74 walk_length,
75 num_nodes=3,
76 return_edge_indices=True,
77 )
78 assert node_seq.tolist() == [
79 [0, 1, 0, 1, 0],
80 [1, 0, 1, 0, 1],
81 [2, 2, 2, 2, 2],
82 ]
83 assert edge_seq.tolist() == [
84 [0, 1, 0, 1],
85 [1, 0, 1, 0],
86 [-1, -1, -1, -1],
87 ]