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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
|
import copy
import warnings
from collections import defaultdict
from collections.abc import Mapping, Sequence
from dataclasses import dataclass
from itertools import chain
from typing import (
Any,
Callable,
Dict,
Iterable,
List,
NamedTuple,
Optional,
Tuple,
Union,
overload,
)
import numpy as np
import torch
from torch import Tensor
from typing_extensions import Self
from torch_geometric.data import EdgeAttr, FeatureStore, GraphStore, TensorAttr
from torch_geometric.data.feature_store import _FieldStatus
from torch_geometric.data.graph_store import EdgeLayout
from torch_geometric.data.storage import (
BaseStorage,
EdgeStorage,
GlobalStorage,
NodeStorage,
)
from torch_geometric.deprecation import deprecated
from torch_geometric.index import Index
from torch_geometric.typing import (
EdgeTensorType,
EdgeType,
FeatureTensorType,
NodeType,
OptTensor,
SparseTensor,
TensorFrame,
)
from torch_geometric.utils import is_sparse, select, subgraph
class BaseData:
def __getattr__(self, key: str) -> Any:
raise NotImplementedError
def __setattr__(self, key: str, value: Any):
raise NotImplementedError
def __delattr__(self, key: str):
raise NotImplementedError
def __getitem__(self, key: str) -> Any:
raise NotImplementedError
def __setitem__(self, key: str, value: Any):
raise NotImplementedError
def __delitem__(self, key: str):
raise NotImplementedError
def __copy__(self):
raise NotImplementedError
def __deepcopy__(self, memo):
raise NotImplementedError
def __repr__(self) -> str:
raise NotImplementedError
def stores_as(self, data: Self):
raise NotImplementedError
@property
def stores(self) -> List[BaseStorage]:
raise NotImplementedError
@property
def node_stores(self) -> List[NodeStorage]:
raise NotImplementedError
@property
def edge_stores(self) -> List[EdgeStorage]:
raise NotImplementedError
def to_dict(self) -> Dict[str, Any]:
r"""Returns a dictionary of stored key/value pairs."""
raise NotImplementedError
def to_namedtuple(self) -> NamedTuple:
r"""Returns a :obj:`NamedTuple` of stored key/value pairs."""
raise NotImplementedError
def update(self, data: Self) -> Self:
r"""Updates the data object with the elements from another data object.
Added elements will override existing ones (in case of duplicates).
"""
raise NotImplementedError
def concat(self, data: Self) -> Self:
r"""Concatenates :obj:`self` with another :obj:`data` object.
All values needs to have matching shapes at non-concat dimensions.
"""
out = copy.copy(self)
for store, other_store in zip(out.stores, data.stores):
store.concat(other_store)
return out
def __cat_dim__(self, key: str, value: Any, *args, **kwargs) -> Any:
r"""Returns the dimension for which the value :obj:`value` of the
attribute :obj:`key` will get concatenated when creating mini-batches
using :class:`torch_geometric.loader.DataLoader`.
.. note::
This method is for internal use only, and should only be overridden
in case the mini-batch creation process is corrupted for a specific
attribute.
"""
raise NotImplementedError
def __inc__(self, key: str, value: Any, *args, **kwargs) -> Any:
r"""Returns the incremental count to cumulatively increase the value
:obj:`value` of the attribute :obj:`key` when creating mini-batches
using :class:`torch_geometric.loader.DataLoader`.
.. note::
This method is for internal use only, and should only be overridden
in case the mini-batch creation process is corrupted for a specific
attribute.
"""
raise NotImplementedError
def debug(self):
raise NotImplementedError
###########################################################################
def keys(self) -> List[str]:
r"""Returns a list of all graph attribute names."""
out = []
for store in self.stores:
out += list(store.keys())
return list(set(out))
def __len__(self) -> int:
r"""Returns the number of graph attributes."""
return len(self.keys())
def __contains__(self, key: str) -> bool:
r"""Returns :obj:`True` if the attribute :obj:`key` is present in the
data.
"""
return key in self.keys()
def __getstate__(self) -> Dict[str, Any]:
return self.__dict__
def __setstate__(self, mapping: Dict[str, Any]):
for key, value in mapping.items():
self.__dict__[key] = value
@property
def num_nodes(self) -> Optional[int]:
r"""Returns the number of nodes in the graph.
.. note::
The number of nodes in the data object is automatically inferred
in case node-level attributes are present, *e.g.*, :obj:`data.x`.
In some cases, however, a graph may only be given without any
node-level attributes.
:pyg:`PyG` then *guesses* the number of nodes according to
:obj:`edge_index.max().item() + 1`.
However, in case there exists isolated nodes, this number does not
have to be correct which can result in unexpected behavior.
Thus, we recommend to set the number of nodes in your data object
explicitly via :obj:`data.num_nodes = ...`.
You will be given a warning that requests you to do so.
"""
try:
return sum([v.num_nodes for v in self.node_stores])
except TypeError:
return None
@overload
def size(self) -> Tuple[Optional[int], Optional[int]]:
pass
@overload
def size(self, dim: int) -> Optional[int]:
pass
def size(
self, dim: Optional[int] = None
) -> Union[Tuple[Optional[int], Optional[int]], Optional[int]]:
r"""Returns the size of the adjacency matrix induced by the graph."""
size = (self.num_nodes, self.num_nodes)
return size if dim is None else size[dim]
@property
def num_edges(self) -> int:
r"""Returns the number of edges in the graph.
For undirected graphs, this will return the number of bi-directional
edges, which is double the amount of unique edges.
"""
return sum([v.num_edges for v in self.edge_stores])
def node_attrs(self) -> List[str]:
r"""Returns all node-level tensor attribute names."""
return list(set(chain(*[s.node_attrs() for s in self.node_stores])))
def edge_attrs(self) -> List[str]:
r"""Returns all edge-level tensor attribute names."""
return list(set(chain(*[s.edge_attrs() for s in self.edge_stores])))
@property
def node_offsets(self) -> Dict[NodeType, int]:
out: Dict[NodeType, int] = {}
offset: int = 0
for store in self.node_stores:
out[store._key] = offset
offset = offset + store.num_nodes
return out
def generate_ids(self):
r"""Generates and sets :obj:`n_id` and :obj:`e_id` attributes to assign
each node and edge to a continuously ascending and unique ID.
"""
for store in self.node_stores:
store.n_id = torch.arange(store.num_nodes)
for store in self.edge_stores:
store.e_id = torch.arange(store.num_edges)
def is_sorted(self, sort_by_row: bool = True) -> bool:
r"""Returns :obj:`True` if edge indices :obj:`edge_index` are sorted.
Args:
sort_by_row (bool, optional): If set to :obj:`False`, will require
column-wise order/by destination node order of
:obj:`edge_index`. (default: :obj:`True`)
"""
return all(
[store.is_sorted(sort_by_row) for store in self.edge_stores])
def sort(self, sort_by_row: bool = True) -> Self:
r"""Sorts edge indices :obj:`edge_index` and their corresponding edge
features.
Args:
sort_by_row (bool, optional): If set to :obj:`False`, will sort
:obj:`edge_index` in column-wise order/by destination node.
(default: :obj:`True`)
"""
out = copy.copy(self)
for store in out.edge_stores:
store.sort(sort_by_row)
return out
def is_coalesced(self) -> bool:
r"""Returns :obj:`True` if edge indices :obj:`edge_index` are sorted
and do not contain duplicate entries.
"""
return all([store.is_coalesced() for store in self.edge_stores])
def coalesce(self) -> Self:
r"""Sorts and removes duplicated entries from edge indices
:obj:`edge_index`.
"""
out = copy.copy(self)
for store in out.edge_stores:
store.coalesce()
return out
def is_sorted_by_time(self) -> bool:
r"""Returns :obj:`True` if :obj:`time` is sorted."""
return all([store.is_sorted_by_time() for store in self.stores])
def sort_by_time(self) -> Self:
r"""Sorts data associated with :obj:`time` according to :obj:`time`."""
out = copy.copy(self)
for store in out.stores:
store.sort_by_time()
return out
def snapshot(
self,
start_time: Union[float, int],
end_time: Union[float, int],
attr: str = 'time',
) -> Self:
r"""Returns a snapshot of :obj:`data` to only hold events that occurred
in period :obj:`[start_time, end_time]`.
"""
out = copy.copy(self)
for store in out.stores:
store.snapshot(start_time, end_time, attr)
return out
def up_to(self, end_time: Union[float, int]) -> Self:
r"""Returns a snapshot of :obj:`data` to only hold events that occurred
up to :obj:`end_time` (inclusive of :obj:`edge_time`).
"""
out = copy.copy(self)
for store in out.stores:
store.up_to(end_time)
return out
def has_isolated_nodes(self) -> bool:
r"""Returns :obj:`True` if the graph contains isolated nodes."""
return any([store.has_isolated_nodes() for store in self.edge_stores])
def has_self_loops(self) -> bool:
"""Returns :obj:`True` if the graph contains self-loops."""
return any([store.has_self_loops() for store in self.edge_stores])
def is_undirected(self) -> bool:
r"""Returns :obj:`True` if graph edges are undirected."""
return all([store.is_undirected() for store in self.edge_stores])
def is_directed(self) -> bool:
r"""Returns :obj:`True` if graph edges are directed."""
return not self.is_undirected()
def apply_(self, func: Callable, *args: str):
r"""Applies the in-place function :obj:`func`, either to all attributes
or only the ones given in :obj:`*args`.
"""
for store in self.stores:
store.apply_(func, *args)
return self
def apply(self, func: Callable, *args: str):
r"""Applies the function :obj:`func`, either to all attributes or only
the ones given in :obj:`*args`.
"""
for store in self.stores:
store.apply(func, *args)
return self
def clone(self, *args: str):
r"""Performs cloning of tensors, either for all attributes or only the
ones given in :obj:`*args`.
"""
return copy.copy(self).apply(lambda x: x.clone(), *args)
def contiguous(self, *args: str):
r"""Ensures a contiguous memory layout, either for all attributes or
only the ones given in :obj:`*args`.
"""
return self.apply(lambda x: x.contiguous(), *args)
def to(self, device: Union[int, str, torch.device], *args: str,
non_blocking: bool = False):
r"""Performs tensor device conversion, either for all attributes or
only the ones given in :obj:`*args`.
"""
return self.apply(
lambda x: x.to(device=device, non_blocking=non_blocking), *args)
def cpu(self, *args: str):
r"""Copies attributes to CPU memory, either for all attributes or only
the ones given in :obj:`*args`.
"""
return self.apply(lambda x: x.cpu(), *args)
def cuda(self, device: Optional[Union[int, str]] = None, *args: str,
non_blocking: bool = False):
r"""Copies attributes to CUDA memory, either for all attributes or only
the ones given in :obj:`*args`.
"""
# Some PyTorch tensor like objects require a default value for `cuda`:
device = 'cuda' if device is None else device
return self.apply(lambda x: x.cuda(device, non_blocking=non_blocking),
*args)
def pin_memory(self, *args: str):
r"""Copies attributes to pinned memory, either for all attributes or
only the ones given in :obj:`*args`.
"""
return self.apply(lambda x: x.pin_memory(), *args)
def share_memory_(self, *args: str):
r"""Moves attributes to shared memory, either for all attributes or
only the ones given in :obj:`*args`.
"""
return self.apply_(lambda x: x.share_memory_(), *args)
def detach_(self, *args: str):
r"""Detaches attributes from the computation graph, either for all
attributes or only the ones given in :obj:`*args`.
"""
return self.apply_(lambda x: x.detach_(), *args)
def detach(self, *args: str):
r"""Detaches attributes from the computation graph by creating a new
tensor, either for all attributes or only the ones given in
:obj:`*args`.
"""
return self.apply(lambda x: x.detach(), *args)
def requires_grad_(self, *args: str, requires_grad: bool = True):
r"""Tracks gradient computation, either for all attributes or only the
ones given in :obj:`*args`.
"""
return self.apply_(
lambda x: x.requires_grad_(requires_grad=requires_grad), *args)
def record_stream(self, stream: torch.cuda.Stream, *args: str):
r"""Ensures that the tensor memory is not reused for another tensor
until all current work queued on :obj:`stream` has been completed,
either for all attributes or only the ones given in :obj:`*args`.
"""
return self.apply_(lambda x: x.record_stream(stream), *args)
@property
def is_cuda(self) -> bool:
r"""Returns :obj:`True` if any :class:`torch.Tensor` attribute is
stored on the GPU, :obj:`False` otherwise.
"""
for store in self.stores:
for value in store.values():
if isinstance(value, Tensor) and value.is_cuda:
return True
return False
# Deprecated functions ####################################################
@deprecated(details="use 'has_isolated_nodes' instead")
def contains_isolated_nodes(self) -> bool:
return self.has_isolated_nodes()
@deprecated(details="use 'has_self_loops' instead")
def contains_self_loops(self) -> bool:
return self.has_self_loops()
###############################################################################
@dataclass
class DataTensorAttr(TensorAttr):
r"""Tensor attribute for `Data` without group name."""
def __init__(
self,
attr_name=_FieldStatus.UNSET,
index=None,
):
super().__init__(None, attr_name, index)
@dataclass
class DataEdgeAttr(EdgeAttr):
r"""Edge attribute class for `Data` without edge type."""
def __init__(
self,
layout: Optional[EdgeLayout] = None,
is_sorted: bool = False,
size: Optional[Tuple[int, int]] = None,
):
super().__init__(None, layout, is_sorted, size)
###############################################################################
class Data(BaseData, FeatureStore, GraphStore):
r"""A data object describing a homogeneous graph.
The data object can hold node-level, link-level and graph-level attributes.
In general, :class:`~torch_geometric.data.Data` tries to mimic the
behavior of a regular :python:`Python` dictionary.
In addition, it provides useful functionality for analyzing graph
structures, and provides basic PyTorch tensor functionalities.
See `here <https://pytorch-geometric.readthedocs.io/en/latest/get_started/
introduction.html#data-handling-of-graphs>`__ for the accompanying
tutorial.
.. code-block:: python
from torch_geometric.data import Data
data = Data(x=x, edge_index=edge_index, ...)
# Add additional arguments to `data`:
data.train_idx = torch.tensor([...], dtype=torch.long)
data.test_mask = torch.tensor([...], dtype=torch.bool)
# Analyzing the graph structure:
data.num_nodes
>>> 23
data.is_directed()
>>> False
# PyTorch tensor functionality:
data = data.pin_memory()
data = data.to('cuda:0', non_blocking=True)
Args:
x (torch.Tensor, optional): Node feature matrix with shape
:obj:`[num_nodes, num_node_features]`. (default: :obj:`None`)
edge_index (LongTensor, optional): Graph connectivity in COO format
with shape :obj:`[2, num_edges]`. (default: :obj:`None`)
edge_attr (torch.Tensor, optional): Edge feature matrix with shape
:obj:`[num_edges, num_edge_features]`. (default: :obj:`None`)
y (torch.Tensor, optional): Graph-level or node-level ground-truth
labels with arbitrary shape. (default: :obj:`None`)
pos (torch.Tensor, optional): Node position matrix with shape
:obj:`[num_nodes, num_dimensions]`. (default: :obj:`None`)
time (torch.Tensor, optional): The timestamps for each event with shape
:obj:`[num_edges]` or :obj:`[num_nodes]`. (default: :obj:`None`)
**kwargs (optional): Additional attributes.
"""
def __init__(
self,
x: Optional[Tensor] = None,
edge_index: OptTensor = None,
edge_attr: OptTensor = None,
y: Optional[Union[Tensor, int, float]] = None,
pos: OptTensor = None,
time: OptTensor = None,
**kwargs,
):
# `Data` doesn't support group_name, so we need to adjust `TensorAttr`
# accordingly here to avoid requiring `group_name` to be set:
super().__init__(tensor_attr_cls=DataTensorAttr)
# `Data` doesn't support edge_type, so we need to adjust `EdgeAttr`
# accordingly here to avoid requiring `edge_type` to be set:
GraphStore.__init__(self, edge_attr_cls=DataEdgeAttr)
self.__dict__['_store'] = GlobalStorage(_parent=self)
if x is not None:
self.x = x
if edge_index is not None:
self.edge_index = edge_index
if edge_attr is not None:
self.edge_attr = edge_attr
if y is not None:
self.y = y
if pos is not None:
self.pos = pos
if time is not None:
self.time = time
for key, value in kwargs.items():
setattr(self, key, value)
def __getattr__(self, key: str) -> Any:
if '_store' not in self.__dict__:
raise RuntimeError(
"The 'data' object was created by an older version of PyG. "
"If this error occurred while loading an already existing "
"dataset, remove the 'processed/' directory in the dataset's "
"root folder and try again.")
return getattr(self._store, key)
def __setattr__(self, key: str, value: Any):
propobj = getattr(self.__class__, key, None)
if propobj is not None and getattr(propobj, 'fset', None) is not None:
propobj.fset(self, value)
else:
setattr(self._store, key, value)
def __delattr__(self, key: str):
delattr(self._store, key)
# TODO consider supporting the feature store interface for
# __getitem__, __setitem__, and __delitem__ so, for example, we
# can accept key: Union[str, TensorAttr] in __getitem__.
def __getitem__(self, key: str) -> Any:
return self._store[key]
def __setitem__(self, key: str, value: Any):
self._store[key] = value
def __delitem__(self, key: str):
if key in self._store:
del self._store[key]
def __copy__(self):
out = self.__class__.__new__(self.__class__)
for key, value in self.__dict__.items():
out.__dict__[key] = value
out.__dict__['_store'] = copy.copy(self._store)
out._store._parent = out
return out
def __deepcopy__(self, memo):
out = self.__class__.__new__(self.__class__)
for key, value in self.__dict__.items():
out.__dict__[key] = copy.deepcopy(value, memo)
out._store._parent = out
return out
def __repr__(self) -> str:
cls = self.__class__.__name__
has_dict = any([isinstance(v, Mapping) for v in self._store.values()])
if not has_dict:
info = [size_repr(k, v) for k, v in self._store.items()]
info = ', '.join(info)
return f'{cls}({info})'
else:
info = [size_repr(k, v, indent=2) for k, v in self._store.items()]
info = ',\n'.join(info)
return f'{cls}(\n{info}\n)'
@property
def num_nodes(self) -> Optional[int]:
return super().num_nodes
@num_nodes.setter
def num_nodes(self, num_nodes: Optional[int]):
self._store.num_nodes = num_nodes
def stores_as(self, data: Self):
return self
@property
def stores(self) -> List[BaseStorage]:
return [self._store]
@property
def node_stores(self) -> List[NodeStorage]:
return [self._store]
@property
def edge_stores(self) -> List[EdgeStorage]:
return [self._store]
def to_dict(self) -> Dict[str, Any]:
return self._store.to_dict()
def to_namedtuple(self) -> NamedTuple:
return self._store.to_namedtuple()
def update(self, data: Union[Self, Dict[str, Any]]) -> Self:
for key, value in data.items():
self[key] = value
return self
def __cat_dim__(self, key: str, value: Any, *args, **kwargs) -> Any:
if is_sparse(value) and ('adj' in key or 'edge_index' in key):
return (0, 1)
elif 'index' in key or key == 'face':
return -1
else:
return 0
def __inc__(self, key: str, value: Any, *args, **kwargs) -> Any:
if 'batch' in key and isinstance(value, Tensor):
if isinstance(value, Index):
return value.get_dim_size()
return int(value.max()) + 1
elif 'index' in key or key == 'face':
num_nodes = self.num_nodes
if num_nodes is None:
raise RuntimeError(f"Unable to infer 'num_nodes' from the "
f"attribute '{key}'. Please explicitly set "
f"'num_nodes' as an attribute of 'data' to "
f"prevent this error")
return num_nodes
else:
return 0
def validate(self, raise_on_error: bool = True) -> bool:
r"""Validates the correctness of the data."""
cls_name = self.__class__.__name__
status = True
num_nodes = self.num_nodes
if num_nodes is None:
status = False
warn_or_raise(f"'num_nodes' is undefined in '{cls_name}'",
raise_on_error)
if 'edge_index' in self:
if self.edge_index.dim() != 2 or self.edge_index.size(0) != 2:
status = False
warn_or_raise(
f"'edge_index' needs to be of shape [2, num_edges] in "
f"'{cls_name}' (found {self.edge_index.size()})",
raise_on_error)
if 'edge_index' in self and self.edge_index.numel() > 0:
if self.edge_index.min() < 0:
status = False
warn_or_raise(
f"'edge_index' contains negative indices in "
f"'{cls_name}' (found {int(self.edge_index.min())})",
raise_on_error)
if num_nodes is not None and self.edge_index.max() >= num_nodes:
status = False
warn_or_raise(
f"'edge_index' contains larger indices than the number "
f"of nodes ({num_nodes}) in '{cls_name}' "
f"(found {int(self.edge_index.max())})", raise_on_error)
return status
def debug(self):
pass # TODO
def is_node_attr(self, key: str) -> bool:
r"""Returns :obj:`True` if the object at key :obj:`key` denotes a
node-level tensor attribute.
"""
return self._store.is_node_attr(key)
def is_edge_attr(self, key: str) -> bool:
r"""Returns :obj:`True` if the object at key :obj:`key` denotes an
edge-level tensor attribute.
"""
return self._store.is_edge_attr(key)
def subgraph(self, subset: Tensor) -> Self:
r"""Returns the induced subgraph given by the node indices
:obj:`subset`.
Args:
subset (LongTensor or BoolTensor): The nodes to keep.
"""
if 'edge_index' in self:
edge_index, _, edge_mask = subgraph(
subset,
self.edge_index,
relabel_nodes=True,
num_nodes=self.num_nodes,
return_edge_mask=True,
)
else:
edge_index = None
edge_mask = torch.ones(
self.num_edges,
dtype=torch.bool,
device=subset.device,
)
data = copy.copy(self)
for key, value in self:
if key == 'edge_index':
data.edge_index = edge_index
elif key == 'num_nodes':
if subset.dtype == torch.bool:
data.num_nodes = int(subset.sum())
else:
data.num_nodes = subset.size(0)
elif self.is_node_attr(key):
cat_dim = self.__cat_dim__(key, value)
data[key] = select(value, subset, dim=cat_dim)
elif self.is_edge_attr(key):
cat_dim = self.__cat_dim__(key, value)
data[key] = select(value, edge_mask, dim=cat_dim)
return data
def edge_subgraph(self, subset: Tensor) -> Self:
r"""Returns the induced subgraph given by the edge indices
:obj:`subset`.
Will currently preserve all the nodes in the graph, even if they are
isolated after subgraph computation.
Args:
subset (LongTensor or BoolTensor): The edges to keep.
"""
data = copy.copy(self)
for key, value in self:
if self.is_edge_attr(key):
cat_dim = self.__cat_dim__(key, value)
data[key] = select(value, subset, dim=cat_dim)
return data
def to_heterogeneous(
self,
node_type: Optional[Tensor] = None,
edge_type: Optional[Tensor] = None,
node_type_names: Optional[List[NodeType]] = None,
edge_type_names: Optional[List[EdgeType]] = None,
):
r"""Converts a :class:`~torch_geometric.data.Data` object to a
heterogeneous :class:`~torch_geometric.data.HeteroData` object.
For this, node and edge attributes are splitted according to the
node-level and edge-level vectors :obj:`node_type` and
:obj:`edge_type`, respectively.
:obj:`node_type_names` and :obj:`edge_type_names` can be used to give
meaningful node and edge type names, respectively.
That is, the node_type :obj:`0` is given by :obj:`node_type_names[0]`.
If the :class:`~torch_geometric.data.Data` object was constructed via
:meth:`~torch_geometric.data.HeteroData.to_homogeneous`, the object can
be reconstructed without any need to pass in additional arguments.
Args:
node_type (torch.Tensor, optional): A node-level vector denoting
the type of each node. (default: :obj:`None`)
edge_type (torch.Tensor, optional): An edge-level vector denoting
the type of each edge. (default: :obj:`None`)
node_type_names (List[str], optional): The names of node types.
(default: :obj:`None`)
edge_type_names (List[Tuple[str, str, str]], optional): The names
of edge types. (default: :obj:`None`)
"""
from torch_geometric.data import HeteroData
if node_type is None:
node_type = self._store.get('node_type', None)
if node_type is None:
node_type = torch.zeros(self.num_nodes, dtype=torch.long)
if node_type_names is None:
store = self._store
node_type_names = store.__dict__.get('_node_type_names', None)
if node_type_names is None:
node_type_names = [str(i) for i in node_type.unique().tolist()]
if edge_type is None:
edge_type = self._store.get('edge_type', None)
if edge_type is None:
edge_type = torch.zeros(self.num_edges, dtype=torch.long)
if edge_type_names is None:
store = self._store
edge_type_names = store.__dict__.get('_edge_type_names', None)
if edge_type_names is None:
edge_type_names = []
edge_index = self.edge_index
for i in edge_type.unique().tolist():
src, dst = edge_index[:, edge_type == i]
src_types = node_type[src].unique().tolist()
dst_types = node_type[dst].unique().tolist()
if len(src_types) != 1 and len(dst_types) != 1:
raise ValueError(
"Could not construct a 'HeteroData' object from the "
"'Data' object because single edge types span over "
"multiple node types")
edge_type_names.append((node_type_names[src_types[0]], str(i),
node_type_names[dst_types[0]]))
# We iterate over node types to find the local node indices belonging
# to each node type. Furthermore, we create a global `index_map` vector
# that maps global node indices to local ones in the final
# heterogeneous graph:
node_ids, index_map = {}, torch.empty_like(node_type)
for i in range(len(node_type_names)):
node_ids[i] = (node_type == i).nonzero(as_tuple=False).view(-1)
index_map[node_ids[i]] = torch.arange(len(node_ids[i]),
device=index_map.device)
# We iterate over edge types to find the local edge indices:
edge_ids = {}
for i in range(len(edge_type_names)):
edge_ids[i] = (edge_type == i).nonzero(as_tuple=False).view(-1)
data = HeteroData()
for i, key in enumerate(node_type_names):
for attr, value in self.items():
if attr in {'node_type', 'edge_type', 'ptr'}:
continue
elif isinstance(value, Tensor) and self.is_node_attr(attr):
cat_dim = self.__cat_dim__(attr, value)
data[key][attr] = value.index_select(cat_dim, node_ids[i])
elif (isinstance(value, TensorFrame)
and self.is_node_attr(attr)):
data[key][attr] = value[node_ids[i]]
if len(data[key]) == 0:
data[key].num_nodes = node_ids[i].size(0)
for i, key in enumerate(edge_type_names):
src, _, dst = key
for attr, value in self.items():
if attr in {'node_type', 'edge_type', 'ptr'}:
continue
elif attr == 'edge_index':
edge_index = value[:, edge_ids[i]]
edge_index[0] = index_map[edge_index[0]]
edge_index[1] = index_map[edge_index[1]]
data[key].edge_index = edge_index
elif isinstance(value, Tensor) and self.is_edge_attr(attr):
cat_dim = self.__cat_dim__(attr, value)
data[key][attr] = value.index_select(cat_dim, edge_ids[i])
elif (isinstance(value, TensorFrame)
and self.is_edge_attr(attr)):
data[key][attr] = value[edge_ids[i]]
# Add global attributes.
exclude_keys = set(data.keys()) | {
'node_type', 'edge_type', 'edge_index', 'num_nodes', 'ptr'
}
for attr, value in self.items():
if attr in exclude_keys:
continue
data[attr] = value
return data
def connected_components(self) -> List[Self]:
r"""Extracts connected components of the graph using a union-find
algorithm. The components are returned as a list of
:class:`~torch_geometric.data.Data` objects, where each object
represents a connected component of the graph.
.. code-block::
data = Data()
data.x = torch.tensor([[1.0], [2.0], [3.0], [4.0]])
data.y = torch.tensor([[1.1], [2.1], [3.1], [4.1]])
data.edge_index = torch.tensor(
[[0, 1, 2, 3], [1, 0, 3, 2]], dtype=torch.long
)
components = data.connected_components()
print(len(components))
>>> 2
print(components[0].x)
>>> Data(x=[2, 1], y=[2, 1], edge_index=[2, 2])
Returns:
List[Data]: A list of disconnected components.
"""
# Union-Find algorithm to find connected components
self._parents: Dict[int, int] = {}
self._ranks: Dict[int, int] = {}
for edge in self.edge_index.t().tolist():
self._union(edge[0], edge[1])
# Rerun _find_parent to ensure all nodes are covered correctly
for node in range(self.num_nodes):
self._find_parent(node)
# Group parents
grouped_parents = defaultdict(list)
for node, parent in self._parents.items():
grouped_parents[parent].append(node)
del self._ranks
del self._parents
# Create components based on the found parents (roots)
components: List[Self] = []
for nodes in grouped_parents.values():
# Convert the list of node IDs to a tensor
subset = torch.tensor(nodes, dtype=torch.long)
# Use the existing subgraph function
component_data = self.subgraph(subset)
components.append(component_data)
return components
###########################################################################
@classmethod
def from_dict(cls, mapping: Dict[str, Any]) -> Self:
r"""Creates a :class:`~torch_geometric.data.Data` object from a
dictionary.
"""
return cls(**mapping)
@property
def num_node_features(self) -> int:
r"""Returns the number of features per node in the graph."""
return self._store.num_node_features
@property
def num_features(self) -> int:
r"""Returns the number of features per node in the graph.
Alias for :py:attr:`~num_node_features`.
"""
return self.num_node_features
@property
def num_edge_features(self) -> int:
r"""Returns the number of features per edge in the graph."""
return self._store.num_edge_features
@property
def num_node_types(self) -> int:
r"""Returns the number of node types in the graph."""
return int(self.node_type.max()) + 1 if 'node_type' in self else 1
@property
def num_edge_types(self) -> int:
r"""Returns the number of edge types in the graph."""
return int(self.edge_type.max()) + 1 if 'edge_type' in self else 1
def __iter__(self) -> Iterable:
r"""Iterates over all attributes in the data, yielding their attribute
names and values.
"""
yield from self._store.items()
def __call__(self, *args: str) -> Iterable:
r"""Iterates over all attributes :obj:`*args` in the data, yielding
their attribute names and values.
If :obj:`*args` is not given, will iterate over all attributes.
"""
yield from self._store.items(*args)
@property
def x(self) -> Optional[Tensor]:
return self['x'] if 'x' in self._store else None
@x.setter
def x(self, x: Optional[Tensor]):
self._store.x = x
@property
def edge_index(self) -> Optional[Tensor]:
return self['edge_index'] if 'edge_index' in self._store else None
@edge_index.setter
def edge_index(self, edge_index: Optional[Tensor]):
self._store.edge_index = edge_index
@property
def edge_weight(self) -> Optional[Tensor]:
return self['edge_weight'] if 'edge_weight' in self._store else None
@edge_weight.setter
def edge_weight(self, edge_weight: Optional[Tensor]):
self._store.edge_weight = edge_weight
@property
def edge_attr(self) -> Optional[Tensor]:
return self['edge_attr'] if 'edge_attr' in self._store else None
@edge_attr.setter
def edge_attr(self, edge_attr: Optional[Tensor]):
self._store.edge_attr = edge_attr
@property
def y(self) -> Optional[Union[Tensor, int, float]]:
return self['y'] if 'y' in self._store else None
@y.setter
def y(self, y: Optional[Tensor]):
self._store.y = y
@property
def pos(self) -> Optional[Tensor]:
return self['pos'] if 'pos' in self._store else None
@pos.setter
def pos(self, pos: Optional[Tensor]):
self._store.pos = pos
@property
def batch(self) -> Optional[Tensor]:
return self['batch'] if 'batch' in self._store else None
@batch.setter
def batch(self, batch: Optional[Tensor]):
self._store.batch = batch
@property
def time(self) -> Optional[Tensor]:
return self['time'] if 'time' in self._store else None
@time.setter
def time(self, time: Optional[Tensor]):
self._store.time = time
@property
def face(self) -> Optional[Tensor]:
return self['face'] if 'face' in self._store else None
@face.setter
def face(self, face: Optional[Tensor]):
self._store.face = face
# Deprecated functions ####################################################
@property
@deprecated(details="use 'data.face.size(-1)' instead")
def num_faces(self) -> Optional[int]:
r"""Returns the number of faces in the mesh."""
if 'face' in self._store and isinstance(self.face, Tensor):
return self.face.size(self.__cat_dim__('face', self.face))
return None
# FeatureStore interface ##################################################
def _put_tensor(self, tensor: FeatureTensorType, attr: TensorAttr) -> bool:
out = self.get(attr.attr_name)
if out is not None and attr.index is not None:
out[attr.index] = tensor
else:
assert attr.index is None
setattr(self, attr.attr_name, tensor)
return True
def _get_tensor(self, attr: TensorAttr) -> Optional[FeatureTensorType]:
tensor = getattr(self, attr.attr_name, None)
if tensor is not None:
# TODO this behavior is a bit odd, since TensorAttr requires that
# we set `index`. So, we assume here that indexing by `None` is
# equivalent to not indexing at all, which is not in line with
# Python semantics.
return tensor[attr.index] if attr.index is not None else tensor
return None
def _remove_tensor(self, attr: TensorAttr) -> bool:
if hasattr(self, attr.attr_name):
delattr(self, attr.attr_name)
return True
return False
def _get_tensor_size(self, attr: TensorAttr) -> Tuple:
return self._get_tensor(attr).size()
def get_all_tensor_attrs(self) -> List[TensorAttr]:
r"""Obtains all feature attributes stored in `Data`."""
return [
TensorAttr(attr_name=name) for name in self._store.keys()
if self._store.is_node_attr(name)
]
# GraphStore interface ####################################################
def _put_edge_index(self, edge_index: EdgeTensorType,
edge_attr: EdgeAttr) -> bool:
if not hasattr(self, '_edge_attrs'):
self._edge_attrs = {}
self._edge_attrs[edge_attr.layout] = edge_attr
row, col = edge_index
if edge_attr.layout == EdgeLayout.COO:
self.edge_index = torch.stack([row, col], dim=0)
elif edge_attr.layout == EdgeLayout.CSR:
self.adj = SparseTensor(
rowptr=row,
col=col,
sparse_sizes=edge_attr.size,
is_sorted=True,
trust_data=True,
)
else: # edge_attr.layout == EdgeLayout.CSC:
size = edge_attr.size[::-1] if edge_attr.size is not None else None
self.adj_t = SparseTensor(
rowptr=col,
col=row,
sparse_sizes=size,
is_sorted=True,
trust_data=True,
)
return True
def _get_edge_index(self, edge_attr: EdgeAttr) -> Optional[EdgeTensorType]:
if edge_attr.size is None:
edge_attr.size = self.size() # Modify in-place.
if edge_attr.layout == EdgeLayout.COO and 'edge_index' in self:
row, col = self.edge_index
return row, col
elif edge_attr.layout == EdgeLayout.CSR and 'adj' in self:
rowptr, col, _ = self.adj.csr()
return rowptr, col
elif edge_attr.layout == EdgeLayout.CSC and 'adj_t' in self:
colptr, row, _ = self.adj_t.csr()
return row, colptr
return None
def _remove_edge_index(self, edge_attr: EdgeAttr) -> bool:
if edge_attr.layout == EdgeLayout.COO and 'edge_index' in self:
del self.edge_index
if hasattr(self, '_edge_attrs'):
self._edge_attrs.pop(EdgeLayout.COO, None)
return True
elif edge_attr.layout == EdgeLayout.CSR and 'adj' in self:
del self.adj
if hasattr(self, '_edge_attrs'):
self._edge_attrs.pop(EdgeLayout.CSR, None)
return True
elif edge_attr.layout == EdgeLayout.CSC and 'adj_t' in self:
del self.adj_t
if hasattr(self, '_edge_attrs'):
self._edge_attrs.pop(EdgeLayout.CSC, None)
return True
return False
def get_all_edge_attrs(self) -> List[EdgeAttr]:
edge_attrs = getattr(self, '_edge_attrs', {})
if 'edge_index' in self and EdgeLayout.COO not in edge_attrs:
edge_attrs[EdgeLayout.COO] = DataEdgeAttr('coo', is_sorted=False)
if 'adj' in self and EdgeLayout.CSR not in edge_attrs:
size = self.adj.sparse_sizes()
edge_attrs[EdgeLayout.CSR] = DataEdgeAttr('csr', size=size)
if 'adj_t' in self and EdgeLayout.CSC not in edge_attrs:
size = self.adj_t.sparse_sizes()[::-1]
edge_attrs[EdgeLayout.CSC] = DataEdgeAttr('csc', size=size)
return list(edge_attrs.values())
# Connected Components Helper Functions ###################################
def _find_parent(self, node: int) -> int:
r"""Finds and returns the representative parent of the given node in a
disjoint-set (union-find) data structure. Implements path compression
to optimize future queries.
Args:
node (int): The node for which to find the representative parent.
Returns:
int: The representative parent of the node.
"""
if node not in self._parents:
self._parents[node] = node
self._ranks[node] = 0
if self._parents[node] != node:
self._parents[node] = self._find_parent(self._parents[node])
return self._parents[node]
def _union(self, node1: int, node2: int):
r"""Merges the sets containing node1 and node2 in the disjoint-set
data structure.
Finds the root parents of node1 and node2 using the _find_parent
method. If they belong to different sets, updates the parent of
root2 to be root1, effectively merging the two sets.
Args:
node1 (int): The index of the first node to union.
node2 (int): The index of the second node to union.
"""
root1 = self._find_parent(node1)
root2 = self._find_parent(node2)
if root1 != root2:
if self._ranks[root1] < self._ranks[root2]:
self._parents[root1] = root2
elif self._ranks[root1] > self._ranks[root2]:
self._parents[root2] = root1
else:
self._parents[root2] = root1
self._ranks[root1] += 1
###############################################################################
def size_repr(key: Any, value: Any, indent: int = 0) -> str:
pad = ' ' * indent
if isinstance(value, Tensor) and value.dim() == 0:
out = value.item()
elif isinstance(value, Tensor) and getattr(value, 'is_nested', False):
out = str(list(value.to_padded_tensor(padding=0.0).size()))
elif isinstance(value, Tensor):
out = str(list(value.size()))
elif isinstance(value, np.ndarray):
out = str(list(value.shape))
elif isinstance(value, SparseTensor):
out = str(value.sizes())[:-1] + f', nnz={value.nnz()}]'
elif isinstance(value, TensorFrame):
out = (f'{value.__class__.__name__}('
f'[{value.num_rows}, {value.num_cols}])')
elif isinstance(value, str):
out = f"'{value}'"
elif isinstance(value, (Sequence, set)):
out = str([len(value)])
elif isinstance(value, Mapping) and len(value) == 0:
out = '{}'
elif (isinstance(value, Mapping) and len(value) == 1
and not isinstance(list(value.values())[0], Mapping)):
lines = [size_repr(k, v, 0) for k, v in value.items()]
out = '{ ' + ', '.join(lines) + ' }'
elif isinstance(value, Mapping):
lines = [size_repr(k, v, indent + 2) for k, v in value.items()]
out = '{\n' + ',\n'.join(lines) + ',\n' + pad + '}'
else:
out = str(value)
key = str(key).replace("'", '')
return f'{pad}{key}={out}'
def warn_or_raise(msg: str, raise_on_error: bool = True):
if raise_on_error:
raise ValueError(msg)
else:
warnings.warn(msg, stacklevel=2)
|