higher_order_graph
Higher-order De Bruijn graph representation and related operations.
HigherOrderGraph
¶
Bases: pathpyG.core.graph.Graph
A De Bruijn graph of order k, whose nodes are paths of k first-order nodes.
A HigherOrderGraph has one node per distinct path of length k
in the underlying first-order graph. Repeated observations of the same path are
aggregated into an edge_weight. Timestamps are not represented: this
is a model of how paths flow rather than a record of what happened.
The order k is the memory of the model: the next step of a path depends on the
last k first-order nodes visited.
- Order 1 is simply the weighted first-order graph, with plain node IDs rather than tuples.
- Order 0 is the memoryless model, in which every step is an independent draw from a fixed
distribution over first-order nodes. It has a single node, the empty path
(), and one self-loop per first-order node, weighted by how often that node is visited. Since all loops share the same endpoints, the first-order node each loop emits is stored in theedge_first_order_nodeedge attribute. The transition probabilities of the loops are the node visitation probabilities.
Info
In addition to the attributes of Graph, the data object holds:
node_sequence: Tensor of shape(num_nodes, order), the first-order node indices making up the path each higher-order node represents.edge_weight: Tensor with the aggregated weight of each transition.inverse_idx: Tensor mapping each row of the pre-aggregation node sequence to the index of the higher-order node it was merged into.edge_first_order_node(order 0 only): Tensor with the index of the first-order node emitted by each self-loop.
Attributes:
| Name | Type | Description |
|---|---|---|
data |
torch_geometric.data.Data
|
PyG Data object containing edges and attributes. |
mapping |
pathpyG.core.index_map.IndexMap
|
Mapping from higher-order node IDs (tuples, for order other than 1) to indices. |
first_order_mapping |
pathpyG.core.index_map.IndexMap
|
Mapping of the underlying first-order node IDs to indices. |
n_first_order |
int
|
Number of first-order nodes the higher-order nodes are built from. |
Examples:
>>> import pathpyG as pp
>>> from pathpyG.core.higher_order_graph import HigherOrderGraph
>>> g = pp.Graph.from_edge_list([("a", "c"), ("c", "d")])
>>> h = HigherOrderGraph.from_graph(g)
>>> print(h.order, h.nodes)
1 ['a', 'c', 'd']
Source code in src/pathpyG/core/higher_order_graph.py
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 | |
n_first_order
property
¶
Number of first-order nodes underlying the higher-order nodes.
order
property
¶
Return the order of the graph, i.e. the number of first-order nodes in each node's path.
__add__
¶
Combine this higher-order graph with another one of the same order.
Nodes are matched by the paths they represent, as described for
[Graph.__add__][pathpyG.Graph.__add__]. The first-order node sets are joined as well.
Edges that are present in both graphs, i.e. repeated observations of the same path, are
merged into a single edge whose edge_weight is the sum of their weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
pathpyG.core.graph.Graph
|
Higher-order graph of the same order to be combined with this graph |
required |
reduce
|
str
|
Reduction method for node attributes of nodes that are present in both graphs,
and for edge attributes other than |
'sum'
|
Returns:
| Name | Type | Description |
|---|---|---|
HigherOrderGraph |
pathpyG.core.higher_order_graph.HigherOrderGraph
|
The combined higher-order graph. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If the graphs have different orders, or only one of them has first-order node IDs. |
Source code in src/pathpyG/core/higher_order_graph.py
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 | |
__init__
¶
Create a HigherOrderGraph from a Data object carrying a node_sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
torch_geometric.data.Data
|
PyG |
required |
order
|
typing.Optional[int]
|
Expected order |
None
|
first_order_mapping
|
typing.Optional[pathpyG.core.index_map.IndexMap]
|
Mapping of the underlying first-order node IDs. Defaults to an empty mapping. |
None
|
n_first_order
|
typing.Optional[int]
|
Number of first-order nodes. Defaults to the number of IDs in
|
None
|
mapping
|
typing.Optional[pathpyG.core.index_map.IndexMap]
|
Mapping of higher-order node IDs to indices. For order 1 this must use plain IDs; for any other order it must use tuple IDs. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the order, the node sequence, and the mapping disagree, or if the node sequence refers to first-order nodes that do not exist. |
Source code in src/pathpyG/core/higher_order_graph.py
__str__
¶
Return a human-readable summary of the higher-order graph.
Source code in src/pathpyG/core/higher_order_graph.py
aggregate
classmethod
¶
Aggregate a (possibly duplicated) higher-order edge index into a De Bruijn graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_index
|
torch.Tensor
|
Edge index whose nodes are indices into |
required |
node_sequence
|
torch.Tensor
|
Tensor of shape |
required |
first_order_mapping
|
typing.Optional[pathpyG.core.index_map.IndexMap]
|
Mapping of the underlying first-order node IDs. |
None
|
edge_weight
|
typing.Optional[torch.Tensor]
|
Weight of each edge prior to aggregation. Defaults to ones. |
None
|
n_first_order
|
typing.Optional[int]
|
Number of first-order nodes, including isolated ones. |
None
|
aggr
|
str
|
Reduction used for the edge weights. One of "sum", "mean", "min", "max". |
'sum'
|
Returns:
| Name | Type | Description |
|---|---|---|
HigherOrderGraph |
pathpyG.core.higher_order_graph.HigherOrderGraph
|
The aggregated higher-order graph. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/pathpyG/core/higher_order_graph.py
bipartite_edge_index
¶
Return the edge index connecting higher-order nodes to first-order nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
first_order_graph
|
typing.Optional[pathpyG.core.graph.Graph]
|
The first-order graph. Optional; accepted so that call sites read symmetrically, and used only for its device. |
None
|
mapping
|
str
|
Which first-order nodes to connect to. One of "last", "first" or "both". |
'last'
|
device
|
typing.Optional[torch.device]
|
Device on which to create the tensor. |
None
|
Returns:
| Type | Description |
|---|---|
torch.Tensor
|
torch.Tensor: Edge index of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the graph has order 0, whose only node refers to no first-order node. |
Source code in src/pathpyG/core/higher_order_graph.py
from_event_graph
classmethod
¶
Aggregate an EventGraph into an order-k graph.
Equivalent to [from_temporal_graph][pathpyG.HigherOrderGraph.from_temporal_graph]
on the underlying temporal graph with the event graph's delta.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
eg
|
pathpyG.core.event_graph.EventGraph
|
The second-order temporal event graph to aggregate. |
required |
order
|
int
|
The order |
2
|
Returns:
| Name | Type | Description |
|---|---|---|
HigherOrderGraph |
pathpyG.core.higher_order_graph.HigherOrderGraph
|
A higher-order graph of order |
pathpyG.core.higher_order_graph.HigherOrderGraph
|
there is no time-respecting path of that length. |
Note
Each call rebuilds the whole chain of lifts from order 1. To obtain several
orders, build a MultiOrderModel with
cached=True once and read its layers instead.
Source code in src/pathpyG/core/higher_order_graph.py
from_graph
classmethod
¶
Create the order-1 graph corresponding to a first-order graph.
Multi-edges are coalesced into a single weighted edge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
g
|
pathpyG.core.graph.Graph
|
First-order graph. |
required |
weight
|
str
|
Name of the edge attribute to use as edge weight. If absent, each edge counts once. |
'edge_weight'
|
Returns:
| Name | Type | Description |
|---|---|---|
HigherOrderGraph |
pathpyG.core.higher_order_graph.HigherOrderGraph
|
A higher-order graph of order 1. |
Source code in src/pathpyG/core/higher_order_graph.py
from_node_weights
classmethod
¶
Create the order-0 graph for the given visitation weights of first-order nodes.
The result has a single node, the empty path (), with one self-loop per first-order
node. Each loop carries the node's weight as edge_weight and the node's index as
edge_first_order_node. Nodes with weight 0 keep their loop, so that every first-order
node is represented.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_weight
|
torch.Tensor
|
Tensor of shape |
required |
first_order_mapping
|
typing.Optional[pathpyG.core.index_map.IndexMap]
|
Mapping of the underlying first-order node IDs. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
HigherOrderGraph |
pathpyG.core.higher_order_graph.HigherOrderGraph
|
A higher-order graph of order 0. |
Examples:
>>> import torch
>>> import pathpyG as pp
>>> h = pp.HigherOrderGraph.from_node_weights(
... torch.tensor([3.0, 1.0, 4.0, 4.0, 0.0]), first_order_mapping=pp.IndexMap(list("abcde"))
... )
>>> print(h.order, h.nodes, h.m)
0 [()] 5
>>> print(h.transition_probabilities(edge_attr="edge_weight"))
tensor([0.2500, 0.0833, 0.3333, 0.3333, 0.0000])
Source code in src/pathpyG/core/higher_order_graph.py
from_path_data
classmethod
¶
Create the De Bruijn graph of order k modelling paths in PathData.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path_data
|
pathpyG.core.path_data.PathData
|
The observed paths. |
required |
order
|
int
|
The order |
1
|
mode
|
str
|
The process that we assume. Either "diffusion" or "propagation". |
'propagation'
|
Returns:
| Name | Type | Description |
|---|---|---|
HigherOrderGraph |
pathpyG.core.higher_order_graph.HigherOrderGraph
|
A higher-order graph of order |
pathpyG.core.higher_order_graph.HigherOrderGraph
|
no observed path is that long. |
Note
Each call rebuilds the whole chain of lifts from order 1. To obtain several
orders, build a MultiOrderModel with
cached=True once and read its layers instead.
Examples:
>>> import pathpyG as pp
>>> paths = pp.PathData(pp.IndexMap(list("acd")))
>>> paths.append_walk(("a", "c", "d"), weight=2)
>>> print(pp.HigherOrderGraph.from_path_data(paths, order=2).nodes)
[('a', 'c'), ('c', 'd')]
Source code in src/pathpyG/core/higher_order_graph.py
from_temporal_graph
classmethod
¶
Create the De Bruijn graph of order k for time-respecting paths in a temporal graph.
Order 1 is simply the weighted static graph and ignores delta; for higher orders
the nodes are the time-respecting paths of k nodes, i.e. those whose consecutive
interactions are at most delta apart. Orders above 2 are reached by repeatedly
lifting the unaggregated data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
g
|
pathpyG.core.temporal_graph.TemporalGraph
|
The temporal graph. |
required |
order
|
int
|
The order |
1
|
delta
|
float | int
|
The maximum time difference between two consecutive interactions of a path. |
1
|
weight
|
str
|
The edge attribute of |
'edge_weight'
|
Returns:
| Name | Type | Description |
|---|---|---|
HigherOrderGraph |
pathpyG.core.higher_order_graph.HigherOrderGraph
|
A higher-order graph of order |
pathpyG.core.higher_order_graph.HigherOrderGraph
|
there is no time-respecting path of that length. |
Note
Each call rebuilds the whole chain of lifts from order 1. To obtain several
orders, build a MultiOrderModel with
cached=True once and read its layers instead.
Examples:
>>> import pathpyG as pp
>>> t = pp.TemporalGraph.from_edge_list([("a", "c", 1), ("c", "d", 2)])
>>> print(pp.HigherOrderGraph.from_temporal_graph(t, order=2, delta=1).nodes)
[('a', 'c'), ('c', 'd')]
Source code in src/pathpyG/core/higher_order_graph.py
node_id
¶
Return the first-order path represented by the higher-order node idx.
Source code in src/pathpyG/core/higher_order_graph.py
to
¶
Move all tensors to the given device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
torch.device
|
torch device to which all tensors shall be moved |
required |
Returns:
| Name | Type | Description |
|---|---|---|
HigherOrderGraph |
pathpyG.core.higher_order_graph.HigherOrderGraph
|
self |
Source code in src/pathpyG/core/higher_order_graph.py
to_first_order
¶
Project the higher-order graph back onto the first-order nodes.
Each higher-order node is replaced by one of the first-order nodes of its path, and the weights of higher-order edges mapping to the same first-order edge are summed. First-order nodes not traversed by any path remain as isolated nodes.
This is a projection, not an inverse transformation
This method does not reconstruct the original first-order graph from which this higher-order graph was built. Instead, it maps each higher- order node to either the first or last first-order node in its represented path.
Consequently, the result preserves flow encoded by the higher-order model under the selected projection, but may differ from the original graph in its edge set. In particular, isolated first-order edges cannot be recovered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
str
|
Which first-order node of the path represents it. Either "last" or "first". |
'last'
|
Returns:
| Name | Type | Description |
|---|---|---|
Graph |
pathpyG.core.graph.Graph
|
A weighted first-order graph. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the graph has order 0, whose only node refers to no first-order node. |