test_knn.py 2.76 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
3
from itertools import product

import pytest
rusty1s's avatar
rusty1s committed
4
import scipy.spatial
Matthias Fey's avatar
Matthias Fey committed
5
import torch
rusty1s's avatar
rusty1s committed
6
from torch_cluster import knn, knn_graph
Matthias Fey's avatar
Matthias Fey committed
7
from torch_cluster.testing import devices, grad_dtypes, tensor
rusty1s's avatar
rusty1s committed
8
9


rusty1s's avatar
rusty1s committed
10
11
12
13
def to_set(edge_index):
    return set([(i, j) for i, j in edge_index.t().tolist()])


rusty1s's avatar
rusty1s committed
14
@pytest.mark.parametrize('dtype,device', product(grad_dtypes, devices))
rusty1s's avatar
rusty1s committed
15
def test_knn(dtype, device):
rusty1s's avatar
rusty1s committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    x = tensor([
        [-1, -1],
        [-1, +1],
        [+1, +1],
        [+1, -1],
        [-1, -1],
        [-1, +1],
        [+1, +1],
        [+1, -1],
    ], dtype, device)
    y = tensor([
        [1, 0],
        [-1, 0],
    ], dtype, device)

    batch_x = tensor([0, 0, 0, 0, 1, 1, 1, 1], torch.long, device)
    batch_y = tensor([0, 1], torch.long, device)

rusty1s's avatar
rusty1s committed
34
35
    edge_index = knn(x, y, 2)
    assert to_set(edge_index) == set([(0, 2), (0, 3), (1, 0), (1, 1)])
rusty1s's avatar
rusty1s committed
36

37
38
39
40
    jit = torch.jit.script(knn)
    edge_index = jit(x, y, 2)
    assert to_set(edge_index) == set([(0, 2), (0, 3), (1, 0), (1, 1)])

rusty1s's avatar
rusty1s committed
41
42
    edge_index = knn(x, y, 2, batch_x, batch_y)
    assert to_set(edge_index) == set([(0, 2), (0, 3), (1, 4), (1, 5)])
rusty1s's avatar
rusty1s committed
43

rusty1s's avatar
rusty1s committed
44
    if x.is_cuda:
rusty1s's avatar
rusty1s committed
45
        edge_index = knn(x, y, 2, batch_x, batch_y, cosine=True)
rusty1s's avatar
rusty1s committed
46
        assert to_set(edge_index) == set([(0, 2), (0, 3), (1, 4), (1, 5)])
rusty1s's avatar
rusty1s committed
47

48
49
50
51
52
53
    # Skipping a batch
    batch_x = tensor([0, 0, 0, 0, 2, 2, 2, 2], torch.long, device)
    batch_y = tensor([0, 2], torch.long, device)
    edge_index = knn(x, y, 2, batch_x, batch_y)
    assert to_set(edge_index) == set([(0, 2), (0, 3), (1, 4), (1, 5)])

rusty1s's avatar
rusty1s committed
54
55
56
57
58
59
60
61
62
63

@pytest.mark.parametrize('dtype,device', product(grad_dtypes, devices))
def test_knn_graph(dtype, device):
    x = tensor([
        [-1, -1],
        [-1, +1],
        [+1, +1],
        [+1, -1],
    ], dtype, device)

rusty1s's avatar
rusty1s committed
64
65
66
    edge_index = knn_graph(x, k=2, flow='target_to_source')
    assert to_set(edge_index) == set([(0, 1), (0, 3), (1, 0), (1, 2), (2, 1),
                                      (2, 3), (3, 0), (3, 2)])
rusty1s's avatar
rusty1s committed
67

rusty1s's avatar
rusty1s committed
68
69
70
    edge_index = knn_graph(x, k=2, flow='source_to_target')
    assert to_set(edge_index) == set([(1, 0), (3, 0), (0, 1), (2, 1), (1, 2),
                                      (3, 2), (0, 3), (2, 3)])
71

72
73
74
75
76
    jit = torch.jit.script(knn_graph)
    edge_index = jit(x, k=2, flow='source_to_target')
    assert to_set(edge_index) == set([(1, 0), (3, 0), (0, 1), (2, 1), (1, 2),
                                      (3, 2), (0, 3), (2, 3)])

77

Matthias Fey's avatar
Matthias Fey committed
78
@pytest.mark.parametrize('dtype,device', product([torch.float], devices))
79
def test_knn_graph_large(dtype, device):
rusty1s's avatar
rusty1s committed
80
    x = torch.randn(1000, 3, dtype=dtype, device=device)
rusty1s's avatar
rusty1s committed
81

rusty1s's avatar
rusty1s committed
82
    edge_index = knn_graph(x, k=5, flow='target_to_source', loop=True)
rusty1s's avatar
rusty1s committed
83

rusty1s's avatar
rusty1s committed
84
    tree = scipy.spatial.cKDTree(x.cpu().numpy())
rusty1s's avatar
rusty1s committed
85
86
87
    _, col = tree.query(x.cpu(), k=5)
    truth = set([(i, j) for i, ns in enumerate(col) for j in ns])

rusty1s's avatar
rusty1s committed
88
    assert to_set(edge_index.cpu()) == truth