test_rw.py 708 Bytes
Newer Older
rusty1s's avatar
rusty1s committed
1
2
3
4
import pytest
import torch
from torch_cluster import random_walk

rusty1s's avatar
rusty1s committed
5
from .utils import devices, tensor
rusty1s's avatar
rusty1s committed
6
7


rusty1s's avatar
rusty1s committed
8
9
@pytest.mark.parametrize('device', devices)
def test_rw(device):
rusty1s's avatar
rusty1s committed
10
11
    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
12
    start = tensor([0, 1, 2, 3, 4], torch.long, device)
rusty1s's avatar
rusty1s committed
13
    walk_length = 10
rusty1s's avatar
rusty1s committed
14

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

    for n in range(start.size(0)):
        cur = start[n].item()
20
21
22
        for le in range(1, walk_length):
            assert out[n, le].item() in col[row == cur].tolist()
            cur = out[n, le].item()