algorithms
Algorithms for temporal path calculation and graph metrics.
The functions and submodules in this module allow to compute time-respecting or causal paths in temporal graphs and to calculate (temporal) and higher-order graph metrics like centralities.
Example
# Import pathpyG and configure your torch device if you want to use GPU .
import pathpyG as pp
pp.config['torch']['device'] = 'cuda'
# Generate a toy example for a temporal graph.
g = pp.TemporalGraph.from_edge_list([
('b', 'c', 2),
('a', 'b', 1),
('c', 'd', 3),
('d', 'a', 4),
('b', 'd', 2),
('d', 'a', 6),
('a', 'b', 7)
])
# Extract DAG capturing causal interaction sequences in temporal graph.
e_i = pp.algorithms.lift_order_temporal(g, delta=1)
dag = pp.Graph.from_edge_index(e_i)
print(dag)
# Calculate shortest time-respecting pathas
dist, pred = pp.algorithms.temporal.temporal_shortest_paths(g, delta=1)
Graph
¶
A graph object storing nodes, edges, and attributes.
An object than be be used to store directed or undirected graphs with node
and edge attributes. Data on nodes and edges are stored in an underlying instance of
torch_geometric.Data
.
Source code in src/pathpyG/core/graph.py
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 |
|
edges
property
¶
Return all edges in the graph.
This method returns a list object that contains all edges, where each edge is a tuple of two elements. If an IndexMap is used to map node indices to string IDs, edges are returned as tuples of string IDs. If no mapping is used, edges are returned as tuples of integer indices.
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
list object yielding all edges using IDs or indices (if no mapping is used) |
in_degrees
property
¶
Return in-degrees of nodes in directed network.
Returns:
Name | Type | Description |
---|---|---|
dict |
typing.Dict[str, float]
|
dictionary containing in-degrees of nodes |
m
property
¶
Return number of edges.
Returns the number of edges in the graph. For an undirected graph, the number of directed edges is returned.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
number of edges in the graph |
n
property
¶
Return number of nodes.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
number of nodes in the graph |
nodes
property
¶
Return indices or IDs of all nodes in the graph.
This method returns a list object that contains all nodes. If an IndexMap is used, nodes are returned as string IDs. If no IndexMap is used, nodes are returned as integer indices.
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
list of all nodes using IDs or indices (if no mapping is used) |
order
property
¶
Return order of graph.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
order of the (De Bruijn) graph |
out_degrees
property
¶
Return out-degrees of nodes in directed network.
Returns:
Name | Type | Description |
---|---|---|
dict |
typing.Dict[str, float]
|
dictionary containing out-degrees of nodes |
__add__
¶
Combine Graph object with other Graph object.
The semantics of this operation depends on the optional IndexMap of both graphs. If no IndexMap is included, the two underlying data objects are concatenated, thus merging edges from both graphs while leaving node indices unchanged. If both graphs include IndexMaps that assign node IDs to indices, indiced will be adjusted, creating a new mapping for the union of node Ids in both graphs.
Node IDs of graphs to be combined can be disjoint, partly overlapping or non-overlapping.
Examples:
Adding two graphs without node IDs:
>>> g1 = pp.Graph.from_edge_index(torch.Tensor([[0,1,1],[1,2,3]]))
>>> g1 = pp.Graph.from_edge_index(torch.Tensor([[0,2,3],[3,2,1]]))
>>> print(g1 + g2)
Graph with 3 nodes and 6 edges
Adding two graphs with identical node IDs:
>>> g1 = pp.Graph.from_edge_list([('a', 'b'), ('b', 'c')])
>>> g2 = pp.Graph.from_edge_list([('a', 'c'), ('c', 'b')])
>>> print(g1 + g2)
Graph with 3 nodes and 4 edges
Adding two graphs with non-overlapping node IDs:
>>> g1 = pp.Graph.from_edge_list([('a', 'b'), ('b', 'c')])
>>> g2 = pp.Graph.from_edge_list([('c', 'd'), ('d', 'e')])
>>> print(g1 + g2)
Graph with 6 nodes and 4 edges
Adding two graphs with partly overlapping node IDs:
>>> g1 = pp.Graph.from_edge_list([('a', 'b'), ('b', 'c')])
>>> g2 = pp.Graph.from_edge_list([('b', 'd'), ('d', 'e')])
>>> print(g1 + g2)
Graph with 5 nodes and 4 edges
Source code in src/pathpyG/core/graph.py
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 |
|
__getitem__
¶
Return node, edge, or graph attribute.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
typing.Union[tuple, str]
|
name of attribute to be returned |
required |
Source code in src/pathpyG/core/graph.py
__init__
¶
Generate graph instance from a pyG Data
object.
Generate a Graph instance from a torch_geometric.Data
object that contains an EdgeIndex as well as
optional node-, edge- or graph-level attributes. An optional mapping can be used to transparently map
node indices to string identifiers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
torch_geometric.data.Data
|
A pyG Data object containing an EdgeIndex and additional attributes |
required |
mapping
|
typing.Optional[pathpyG.core.index_map.IndexMap]
|
|
None
|
Example
Source code in src/pathpyG/core/graph.py
__setitem__
¶
Store node, edge, or graph attribute.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
name of attribute to be stored |
required |
val
|
torch.Tensor
|
value of attribute |
required |
Source code in src/pathpyG/core/graph.py
__str__
¶
Return a string representation of the graph.
Source code in src/pathpyG/core/graph.py
degrees
¶
Return degrees of nodes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mode
|
str
|
|
'in'
|
Returns:
Name | Type | Description |
---|---|---|
dict |
typing.Dict[str, float]
|
dictionary containing degrees of nodes |
Source code in src/pathpyG/core/graph.py
edge_attrs
¶
Return a list of edge attributes.
This method returns a list containing the names of all edge-level attributes,
ignoring the special edge_index
attribute.
Returns:
Name | Type | Description |
---|---|---|
list |
typing.List[str]
|
list of edge attributes |
Source code in src/pathpyG/core/graph.py
from_edge_index
staticmethod
¶
Construct a graph from a torch Tensor containing an edge index. An optional mapping can be used to transparently map node indices to string identifiers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
edge_index
|
torch.Tensor
|
torch.Tensor or torch_geometric.EdgeIndex object containing an edge_index |
required |
mapping
|
typing.Optional[pathpyG.core.index_map.IndexMap]
|
|
None
|
num_nodes
|
int
|
optional number of nodes (default: None). If None, the number of nodes will be inferred based on the maximum node index in the edge index, i.e. there will be no isolated nodes. |
None
|
Examples:
You can create a graph from an edge index tensor as follows:
>>> import torch
>>> import pathpyG as pp
>>> g = pp.Graph.from_edge_index(torch.LongTensor([[1, 1, 2], [0, 2, 1]]))
>>> print(g)
Directed graph with 3 nodes and 3 edges ...
You can also include a mapping of node IDs:
>>> g = pp.Graph.from_edge_index(torch.LongTensor([[1, 1, 2], [0, 2, 1]]),
>>> mapping=pp.IndexMap(['a', 'b', 'c']))
>>> print(g.mapping)
a -> 0
b -> 1
c -> 2
Source code in src/pathpyG/core/graph.py
from_edge_list
staticmethod
¶
Generate a Graph based on an edge list.
Edges can be given as string or integer tuples. If strings are used and no mapping is given, a mapping of node IDs to indices will be automatically created based on a lexicographic ordering of node IDs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
edge_list
|
typing.Iterable[typing.Tuple[str, str]]
|
Iterable of edges represented as tuples |
required |
is_undirected
|
bool
|
Whether the edge list contains all bidorectional edges |
False
|
mapping
|
typing.Optional[pathpyG.core.index_map.IndexMap]
|
optional mapping of string IDs to node indices |
None
|
num_nodes
|
typing.Optional[int]
|
optional number of nodes (useful in case not all nodes have incident edges) |
None
|
Examples:
>>> import pathpyG as pp
>>> l = [('a', 'b'), ('a', 'c'), ('b', 'c')]
>>> g = pp.Graph.from_edge_list(l)
>>> print(list(g.edges))
[('a', 'b'), ('a', 'c'), ('b', 'c')]
Source code in src/pathpyG/core/graph.py
get_predecessors
¶
Return a tensor containing the indices of all predecessor nodes for a given node identified by an index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
col_idx
|
int
|
Index of node for which predecessors shall be returned. |
required |
Returns:
Name | Type | Description |
---|---|---|
tensor |
torch.Tensor
|
tensor containing indices of all predecessor nodes of the node indexed by |
Source code in src/pathpyG/core/graph.py
get_successors
¶
Return a tensor containing the indices of all successor nodes for a given node identified by an index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
row_idx
|
int
|
Index of node for which predecessors shall be returned. |
required |
Returns:
Name | Type | Description |
---|---|---|
tensor |
torch.Tensor
|
tensor containing indices of all successor nodes of the node indexed by |
Source code in src/pathpyG/core/graph.py
has_self_loops
¶
Return whether graph contains self-loops.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if graph contains self-loops, False otherwise |
is_directed
¶
Return whether graph is directed.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if graph is directed, False otherwise |
is_edge
¶
Return whether edge \((v,w)\) exists in the graph.
If an index to ID mapping is used, nodes are assumed to be string IDs. If no mapping is used, nodes are assumed to be integer indices.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
v
|
typing.Union[str, int]
|
source node of edge as integer index or string ID |
required |
w
|
typing.Union[str, int]
|
target node of edge as integer index or string ID |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if edge exists, False otherwise |
Source code in src/pathpyG/core/graph.py
is_undirected
¶
Return whether graph is undirected.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if graph is undirected, False otherwise |
laplacian
¶
Return Laplacian matrix for a given graph.
This wrapper method will use torch_geometric.utils.laplacian
to return a Laplcian matrix representation of a given graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
normalization
|
typing.Any
|
normalization parameter passed to pyG |
None
|
edge_attr
|
typing.Any
|
optinal name of numerical edge attribute that shall
be passed to pyG |
None
|
Returns:
Type | Description |
---|---|
typing.Any
|
scipy.sparse.coo_matrix: Laplacian matrix representation of graph |
Source code in src/pathpyG/core/graph.py
node_attrs
¶
Return a list of node attributes.
This method returns a list containing the names of all node-level attributes,
ignoring the special node_sequence
attribute.
Returns:
Name | Type | Description |
---|---|---|
list |
typing.List[str]
|
list of node attributes |
Source code in src/pathpyG/core/graph.py
predecessors
¶
Return the predecessors of a given node.
This method returns a generator object that yields all predecessors of a
given node. If a node_id
mapping is used, predecessors will be returned
as string IDs. If no mapping is used, predecessors are returned as indices.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node
|
typing.Union[str, int] | tuple
|
Index or string ID of node for which predecessors shall be returned. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
list with all predecessors of the node identified
by |
Source code in src/pathpyG/core/graph.py
sparse_adj_matrix
¶
Return sparse adjacency matrix representation of (weighted) graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
edge_attr
|
typing.Any
|
the edge attribute that shall be used as edge weight |
None
|
Returns:
Type | Description |
---|---|
typing.Any
|
scipy.sparse.coo_matrix: sparse adjacency matrix representation of graph |
Source code in src/pathpyG/core/graph.py
successors
¶
Return all successors of a given node.
This method returns a generator object that yields all successors of a given node. If an IndexMap is used, successors are returned as string IDs. If no mapping is used, successors are returned as indices.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
node
|
typing.Union[int, str] | tuple
|
Index or string ID of node for which successors shall be returned. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
list with all successors of the node identified
by |
Source code in src/pathpyG/core/graph.py
to_undirected
¶
Returns an undirected version of a directed graph.
This method transforms the current graph instance into an undirected graph by
adding all directed edges in opposite direction. It applies ToUndirected
transform to the underlying torch_geometric.Data
object, which automatically
duplicates edge attributes for newly created directed edges.
Examples:
>>> import pathpyG as pp
>>> g = pp.Graph.from_edge_list([('a', 'b'), ('b', 'c'), ('c', 'a')])
>>> g_u = g.to_undirected()
>>> print(g_u)
Undirected graph with 3 nodes and 6 (directed) edges
Source code in src/pathpyG/core/graph.py
to_weighted_graph
¶
Coalesces multi-edges to single-edges with an additional weight attribute
If the graph contains multiple edges between the same nodes, this method will coalesce
them into a single edge with an additional weight attribute called edge_weight
that
contains the number of coalesced edges. The method returns a new graph instance with
the coalesced edges.
Returns:
Name | Type | Description |
---|---|---|
Graph |
pathpyG.core.graph.Graph
|
Graph with coalesced edges |
Source code in src/pathpyG/core/graph.py
transition_probabilities
¶
Compute transition probabilities based on weighted outdegrees.
Returns:
Name | Type | Description |
---|---|---|
tensor |
torch.Tensor
|
Transition probabilities. |
Source code in src/pathpyG/core/graph.py
weighted_outdegrees
¶
Compute the weighted outdegrees of each node in the graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph
|
pathpyG.core.graph.Graph
|
pathpy graph object. |
required |
Returns:
Name | Type | Description |
---|---|---|
tensor |
torch.Tensor
|
Weighted outdegrees of nodes. |
Source code in src/pathpyG/core/graph.py
TemporalGraph
¶
Source code in src/pathpyG/core/temporal_graph.py
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 |
|
order
property
¶
Return order 1, since all temporal graphs must be order one.
temporal_edges
property
¶
Iterator that yields each edge as a tuple of source and destination node as well as the corresponding timestamp.
__init__
¶
Creates an instance of a temporal graph from a TemporalData
object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
torch_geometric.data.Data
|
xxx |
required |
mapping
|
pathpyG.core.index_map.IndexMap | None
|
xxx |
None
|
Example
Source code in src/pathpyG/core/temporal_graph.py
__str__
¶
Return a string representation of the graph
Source code in src/pathpyG/core/temporal_graph.py
get_batch
¶
Return an instance of the TemporalGraph that captures all time-stamped edges in a given batch defined by start and (non-inclusive) end, where start and end refer to the index of the first and last event in the time-ordered list of events.
Source code in src/pathpyG/core/temporal_graph.py
get_window
¶
Return an instance of the TemporalGraph that captures all time-stamped edges in a given time window defined by start and (non-inclusive) end, where start and end refer to the time stamps
Source code in src/pathpyG/core/temporal_graph.py
shuffle_time
¶
Randomly shuffle the temporal order of edges by randomly permuting timestamps.
to_static_graph
¶
Return weighted time-aggregated instance of Graph
graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
weighted
|
bool
|
whether or not to return a weighted time-aggregated graph |
False
|
time_window
|
typing.Optional[typing.Tuple[int, int]]
|
A tuple with start and end time of the aggregation window |
None
|
Returns:
Name | Type | Description |
---|---|---|
Graph |
pathpyG.Graph
|
A static graph object |
Source code in src/pathpyG/core/temporal_graph.py
to_undirected
¶
Return an undirected version of a directed graph.
This method transforms the current graph instance into an undirected graph by
adding all directed edges in opposite direction. It applies ToUndirected
transform to the underlying torch_geometric.Data
object, which automatically
duplicates edge attributes for newly created directed edges.
Example
Source code in src/pathpyG/core/temporal_graph.py
temporal_shortest_paths
¶
Compute shortest time-respecting paths in a temporal graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
g
|
pathpyG.core.temporal_graph.TemporalGraph
|
Temporal graph to compute shortest paths on. |
required |
delta
|
int
|
Maximum time difference between events in a path. |
required |
Returns:
Type | Description |
---|---|
numpy.ndarray
|
Tuple of two numpy arrays: |
numpy.ndarray
|
|
typing.Tuple[numpy.ndarray, numpy.ndarray]
|
|
Source code in src/pathpyG/algorithms/temporal.py
to_numpy
¶
Convert a tensor or tensor subclasses like torch_geometric.Edge_Index
to numpy.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor
|
torch.Tensor
|
Tensor or tensor subclass. |
required |
Returns:
Type | Description |
---|---|
numpy.ndarray
|
Numpy array. |