-
Notifications
You must be signed in to change notification settings - Fork 3
/
distance.py
85 lines (66 loc) · 3.28 KB
/
distance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
import torch
import pdb
class Distance(object):
r"""Saves the Euclidean distance of linked nodes in its edge attributes.
Args:
norm (bool, optional): If set to :obj:`False`, the output will not be
normalized to the interval :math:`[0, 1]`. (default: :obj:`True`)
max_value (float, optional): If set and :obj:`norm=True`, normalization
will be performed based on this value instead of the maximum value
found in the data. (default: :obj:`None`)
cat (bool, optional): If set to :obj:`False`, all existing edge
attributes will be replaced. (default: :obj:`True`)
"""
def __init__(self, norm=True, max_value=None, cat=True, relative_pos=False,
squared=False):
self.norm = norm
self.max = max_value
self.cat = cat
self.relative_pos = relative_pos
self.squared = squared
def __call__(self, data):
if type(data) == dict:
return {key: self.__call__(data_) for key, data_ in data.items()}
(row, col), pos, pseudo = data.edge_index, data.pos, data.edge_attr
if self.squared:
dist = ((pos[col] - pos[row]) ** 2).sum(1).view(-1, 1)
else:
dist = torch.norm(pos[col] - pos[row], p=2, dim=-1).view(-1, 1)
if self.norm and dist.numel() > 0:
dist = dist / (dist.max() if self.max is None else self.max)
if pseudo is not None and self.cat:
pseudo = pseudo.view(-1, 1) if pseudo.dim() == 1 else pseudo
data.edge_attr = torch.cat([pseudo, dist.type_as(pseudo)], dim=-1)
else:
data.edge_attr = dist
if self.relative_pos:
relative_pos = pos[col] - pos[row]
data.edge_attr = torch.cat([data.edge_attr, relative_pos], dim=-1)
if "original_edge_index" in data:
(row, col), pos, pseudo = (
data.original_edge_index, data.original_pos, data.original_edge_attr
)
dist = torch.norm(pos[col] - pos[row], p=2, dim=-1).view(-1, 1)
if self.norm and dist.numel() > 0:
dist = dist / (dist.max() if self.max is None else self.max)
if pseudo is not None and self.cat:
pseudo = pseudo.view(-1, 1) if pseudo.dim() == 1 else pseudo
data.original_edge_attr = torch.cat([pseudo, dist.type_as(pseudo)], dim=-1)
else:
data.original_edge_attr = dist
if "subgraph_edge_index" in data:
(row, col), pos, pseudo = (
data.subgraph_edge_index, data.subgraph_pos, data.subgraph_edge_attr
)
dist = torch.norm(pos[col] - pos[row], p=2, dim=-1).view(-1, 1)
if self.norm and dist.numel() > 0:
dist = dist / (dist.max() if self.max is None else self.max)
if pseudo is not None and self.cat:
pseudo = pseudo.view(-1, 1) if pseudo.dim() == 1 else pseudo
data.subgraph_edge_attr = torch.cat([pseudo, dist.type_as(pseudo)], dim=-1)
else:
data.subgraph_edge_attr = dist
return data
def __repr__(self):
return '{}(norm={}, max_value={})'.format(self.__class__.__name__,
self.norm, self.max)