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

import pytest
Alexander Liao's avatar
Alexander Liao 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 radius, radius_graph
Matthias Fey's avatar
Matthias Fey committed
7
from torch_cluster.testing import devices, grad_dtypes, tensor
rusty1s's avatar
no sort  
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
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
@pytest.mark.parametrize('dtype,device', product(grad_dtypes, devices))
def test_radius(dtype, device):
    x = tensor([
        [-1, -1],
        [-1, +1],
        [+1, +1],
        [+1, -1],
        [-1, -1],
        [-1, +1],
        [+1, +1],
        [+1, -1],
    ], dtype, device)
    y = tensor([
        [0, 0],
        [0, 1],
    ], 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
36
    edge_index = radius(x, y, 2, max_num_neighbors=4)
    assert to_set(edge_index) == set([(0, 0), (0, 1), (0, 2), (0, 3), (1, 1),
                                      (1, 2), (1, 5), (1, 6)])
rusty1s's avatar
rusty1s committed
37

rusty1s's avatar
rusty1s committed
38
39
40
    edge_index = radius(x, y, 2, batch_x, batch_y, max_num_neighbors=4)
    assert to_set(edge_index) == set([(0, 0), (0, 1), (0, 2), (0, 3), (1, 5),
                                      (1, 6)])
rusty1s's avatar
rusty1s committed
41

42
43
44
45
46
47
48
    # 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 = radius(x, y, 2, batch_x, batch_y, max_num_neighbors=4)
    assert to_set(edge_index) == set([(0, 0), (0, 1), (0, 2), (0, 3), (1, 5),
                                      (1, 6)])

rusty1s's avatar
rusty1s committed
49
50
51
52

@pytest.mark.parametrize('dtype,device', product(grad_dtypes, devices))
def test_radius_graph(dtype, device):
    x = tensor([
rusty1s's avatar
rusty1s committed
53
54
55
56
        [-1, -1],
        [-1, +1],
        [+1, +1],
        [+1, -1],
rusty1s's avatar
rusty1s committed
57
58
    ], dtype, device)

rusty1s's avatar
rusty1s committed
59
    edge_index = radius_graph(x, r=2.5, flow='target_to_source')
rusty1s's avatar
rusty1s committed
60
61
    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
62

rusty1s's avatar
rusty1s committed
63
    edge_index = radius_graph(x, r=2.5, flow='source_to_target')
rusty1s's avatar
rusty1s committed
64
65
    assert to_set(edge_index) == set([(1, 0), (3, 0), (0, 1), (2, 1), (1, 2),
                                      (3, 2), (0, 3), (2, 3)])
66
67


Matthias Fey's avatar
Matthias Fey committed
68
@pytest.mark.parametrize('dtype,device', product([torch.float], devices))
69
def test_radius_graph_large(dtype, device):
rusty1s's avatar
rusty1s committed
70
    x = torch.randn(1000, 3, dtype=dtype, device=device)
71

rusty1s's avatar
rusty1s committed
72
    edge_index = radius_graph(x, r=0.5, flow='target_to_source', loop=True,
rusty1s's avatar
rusty1s committed
73
                              max_num_neighbors=2000)
74

rusty1s's avatar
rusty1s committed
75
    tree = scipy.spatial.cKDTree(x.cpu().numpy())
rusty1s's avatar
rusty1s committed
76
    col = tree.query_ball_point(x.cpu(), r=0.5)
rusty1s's avatar
rusty1s committed
77
    truth = set([(i, j) for i, ns in enumerate(col) for j in ns])
78

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