| 1 |
from itertools import product |
| 2 |
|
| 3 |
import pytest |
| 4 |
import torch |
| 5 |
from torch_cluster import nearest |
| 6 |
from torch_cluster.testing import devices, grad_dtypes, tensor |
| 7 |
|
| 8 |
|
| 9 |
@pytest.mark.parametrize('dtype,device', product(grad_dtypes, devices)) |
| 10 |
def test_nearest(dtype, device): |
| 11 |
x = tensor([ |
| 12 |
[-1, -1], |
| 13 |
[-1, +1], |
| 14 |
[+1, +1], |
| 15 |
[+1, -1], |
| 16 |
[-2, -2], |
| 17 |
[-2, +2], |
| 18 |
[+2, +2], |
| 19 |
[+2, -2], |
| 20 |
], dtype, device) |
| 21 |
y = tensor([ |
| 22 |
[-1, 0], |
| 23 |
[+1, 0], |
| 24 |
[-2, 0], |
| 25 |
[+2, 0], |
| 26 |
], dtype, device) |
| 27 |
|
| 28 |
batch_x = tensor([0, 0, 0, 0, 1, 1, 1, 1], torch.long, device) |
| 29 |
batch_y = tensor([0, 0, 1, 1], torch.long, device) |
| 30 |
|
| 31 |
out = nearest(x, y, batch_x, batch_y) |
| 32 |
assert out.tolist() == [0, 0, 1, 1, 2, 2, 3, 3] |
| 33 |
|
| 34 |
out = nearest(x, y) |
| 35 |
assert out.tolist() == [0, 0, 1, 1, 2, 2, 3, 3] |
| 36 |
|
| 37 |
|
| 38 |
batch_x = tensor([0, 0, 0, 0, 1, 1, 1, 1], torch.long, device) |
| 39 |
batch_y = tensor([0, 0, 0, 0], torch.long, device) |
| 40 |
with pytest.raises(ValueError): |
| 41 |
nearest(x, y, batch_x, batch_y) |
| 42 |
|
| 43 |
|
| 44 |
with pytest.raises(ValueError): |
| 45 |
nearest(x, y, batch_x, batch_y=None) |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
batch_x = tensor([0, 0, 1, 1, 2, 2, 3, 3], torch.long, device) |
| 50 |
batch_y = tensor([0, 1, 3, 3], torch.long, device) |
| 51 |
with pytest.raises(ValueError): |
| 52 |
nearest(x, y, batch_x, batch_y) |
| 53 |
|
| 54 |
|
| 55 |
batch_x = tensor([0, 0, 1, 0, 0, 0, 0], torch.long, device) |
| 56 |
batch_y = tensor([0, 0, 1, 1], torch.long, device) |
| 57 |
with pytest.raises(ValueError): |
| 58 |
nearest(x, y, batch_x, batch_y) |
| 59 |
|
| 60 |
|
| 61 |
batch_x = tensor([0, 0, 0, 0, 1, 1, 1, 1], torch.long, device) |
| 62 |
batch_y = tensor([0, 0, 1, 0], torch.long, device) |
| 63 |
with pytest.raises(ValueError): |
| 64 |
nearest(x, y, batch_x, batch_y) |