test_diag.py 2.17 KB
Newer Older
rusty1s's avatar
rusty1s committed
1
2
3
4
5
6
from itertools import product

import pytest
import torch
from torch_sparse.tensor import SparseTensor

rusty1s's avatar
rusty1s committed
7
8
from torch_sparse.diag_cpu import non_diag_mask

rusty1s's avatar
rusty1s committed
9
10
from .utils import dtypes, devices, tensor

rusty1s's avatar
rusty1s committed
11
12
13
dtypes = [torch.float]
devices = ['cpu']

rusty1s's avatar
rusty1s committed
14
15

@pytest.mark.parametrize('dtype,device', product(dtypes, devices))
rusty1s's avatar
rusty1s committed
16
def test_remove_diag(dtype, device):
rusty1s's avatar
rusty1s committed
17
18
19
20
    index = tensor([
        [0, 0, 1, 2],
        [0, 1, 2, 2],
    ], torch.long, device)
rusty1s's avatar
rusty1s committed
21
22
23
24
25
    value = tensor([1, 2, 3, 4], dtype, device)
    mat = SparseTensor(index, value)
    mat.fill_cache_()

    mat = mat.remove_diag()
rusty1s's avatar
rusty1s committed
26
27
    assert mat.storage.index.tolist() == [[0, 1], [1, 2]]
    assert mat.storage.value.tolist() == [2, 3]
rusty1s's avatar
rusty1s committed
28
29
30
    assert len(mat.cached_keys()) == 2
    assert mat.storage.rowcount.tolist() == [1, 1, 0]
    assert mat.storage.colcount.tolist() == [0, 1, 1]
rusty1s's avatar
rusty1s committed
31
32
33
34
35
36
37
38
39
40

    mat = SparseTensor(index, value)
    mat.fill_cache_()

    mat = mat.remove_diag(k=1)
    assert mat.storage.index.tolist() == [[0, 2], [0, 2]]
    assert mat.storage.value.tolist() == [1, 4]
    assert len(mat.cached_keys()) == 2
    assert mat.storage.rowcount.tolist() == [1, 0, 1]
    assert mat.storage.colcount.tolist() == [1, 0, 1]
rusty1s's avatar
rusty1s committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89


@pytest.mark.parametrize('dtype,device', product(dtypes, devices))
def test_set_diag(dtype, device):
    index = tensor([
        [0, 0, 9, 9],
        [0, 1, 0, 1],
    ], torch.long, device)
    value = tensor([1, 2, 3, 4], dtype, device)
    mat = SparseTensor(index, value)

    print()
    k = -8
    print("k = ", k)
    mat = mat.remove_diag(k)
    print(mat.to_dense())

    # row, col = mat.storage.index
    # print('k', k)
    # mask = row != col - k
    # index = index[:, mask]

    # row, col = index
    # print(row)
    # print(col)

    mask = non_diag_mask(mat.storage.index, mat.size(0), mat.size(1), k)
    print(mask)

    # bla = col - row
    # print(bla)

    # DETECT VORZEICHEN WECHSEL

    # mask = row.new_ones(index.size(1) + 3, dtype=torch.bool)
    # mask[1:] = row[1:] != row[:-1]
    # # mask = row[1:] != row[:-1]
    # print(mask)

    # mask = (row <= col)
    # print(row)
    # print(col)
    # print(mask)
    # mask = (row[1:] == row[:-1])
    # print(mask)

    # UNION
    # idx1 = ...
    # idx2 = ...