Types¶
hypertorch.types
¶
GraphReductionStrategy = GraphReductionStrategyEnum | GraphReductionStrategyLiteral
module-attribute
¶
Type for supported graph reduction strategies, either as an enum or a string literal.
GraphReductionStrategyLiteral = Literal['clique_expansion']
module-attribute
¶
Literal type for supported hypergraph-to-graph reduction strategies.
Neighborhood = set[int]
module-attribute
¶
Set of node IDs adjacent to a node or hyperedge.
Task = TaskEnum | TaskLiteral
module-attribute
¶
Type for supported hypergraph learning tasks, either as a TaskEnum or a string literal.
TaskLiteral = Literal['hyperlink-prediction', 'node-classification']
module-attribute
¶
Literal type for supported hypergraph learning tasks.
CkptStrategy = str | Path
module-attribute
¶
Checkpoint selection strategy ("best" or "last") or checkpoint path.
TestResult = Mapping[str, float]
module-attribute
¶
Mapping from metric names to scalar test results.
EdgeIndex
¶
A wrapper for edge index representation of a graph.
Edge index is a tensor of shape (2, num_edges) where the first row contains source
node indices
and the second row contains destination node indices for each edge.
Examples:
This represents a graph with edges (0, 1), (1, 0), and (2, 3). The number of nodes in this graph is 4 (nodes 0, 1, 2, and 3) and the number of edges is 3.
Source code in hypertorch/types/graph.py
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 | |
item
property
¶
Return the edge index tensor.
edge_weights
property
¶
Return the edge weight tensor, if present.
max_node_id
property
¶
Return the maximum node ID in the edge index.
num_edges
property
¶
Return the number of edges in the graph.
num_nodes
property
¶
Return the number of nodes in the graph.
__init__(edge_index, edge_weights=None)
¶
Initialize the edge index wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_index
|
Tensor
|
Tensor of shape |
required |
edge_weights
|
Tensor | None
|
Optional tensor of shape |
None
|
Source code in hypertorch/types/graph.py
add_selfloops(num_nodes=None, with_duplicate_removal=True)
¶
Add self-loops to each node in the edge index.
Examples:
>>> edge_index = [[0, 1, 2],
... [1, 0, 3]]
>>> edge_index_with_selfloops = [[0, 1, 2, 0, 1, 2, 3],
... [1, 0, 3, 0, 1, 2, 3]]
When num_nodes is higher than the number of nodes in edge_index,
self-loops are added for all nodes from 0 to num_nodes - 1,
including nodes not present in the original edges:
>>> edge_index = [[0, 1, 2],
... [1, 0, 3]]
>>> num_nodes = 6
>>> edge_index_with_selfloops = [[0, 1, 2, 0, 1, 2, 3, 4, 5],
... [1, 0, 3, 0, 1, 2, 3, 4, 5]]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_nodes
|
int | None
|
Total number of nodes. When provided, self-loops are added for nodes |
None
|
with_duplicate_removal
|
bool
|
Whether to remove duplicate edges after adding self-loops.
Defaults to |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
edge_index |
EdgeIndex
|
This |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input edge index has no edges (i.e., |
Source code in hypertorch/types/graph.py
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 | |
get_sparse_adjacency_matrix(num_nodes=None, use_edge_weights=False)
¶
Compute the sparse adjacency matrix from a graph edge index. To get the normalized adjacency matrix, add self-loops to the edge_index.
Examples:
>>> edge_index = [[0, 1, 2],
... [1, 0, 3]]
>>> num_nodes = 4
>>> adj_values = [1, 1, 1]
>>> adj_indices = [[0, 1, 2],
... [1, 0, 3]]
>>> 0 1 2 3
... adj_matrix = [[0, 1, 0, 0], 0
... [1, 0, 0, 0], 1
... [0, 0, 0, 1], 2
... [0, 0, 1, 0]] 3
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_nodes
|
int | None
|
The number of nodes in the graph. Defaults to |
None
|
use_edge_weights
|
bool
|
Whether to use edge weights if they are present.
If |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
adjacency |
Tensor
|
The sparse adjacency matrix of shape |
Source code in hypertorch/types/graph.py
get_sparse_identity_matrix(num_nodes=None)
¶
Compute the sparse identity matrix I of shape (num_nodes, num_nodes).
Examples:
>>> num_nodes = 3
>>> identity_indices = [[0, 1, 2],
... [0, 1, 2]]
>>> values = [1, 1, 1]
>>> I = [[1, 0, 0],
... [0, 1, 0],
... [0, 0, 1]]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_nodes
|
int | None
|
The number of nodes in the graph. Defaults to |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
identity |
Tensor
|
The sparse identity matrix I of shape |
Source code in hypertorch/types/graph.py
get_sparse_normalized_degree_matrix(num_nodes=None, use_edge_weights=False)
¶
Compute the sparse normalized degree matrix D^-½ from a graph edge index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_nodes
|
int | None
|
The number of nodes in the graph.
If |
None
|
use_edge_weights
|
bool
|
If |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
degree_matrix |
Tensor
|
The sparse normalized degree matrix D^-½ of
shape |
Source code in hypertorch/types/graph.py
get_sparse_normalized_laplacian(num_nodes=None)
¶
Compute the sparse symmetric normalized Laplacian matrix: L = I - D^{-1/2} A D^{-1/2}.
Unlike get_sparse_normalized_gcn_laplacian, this method does not add self-loops
and computes the standard Laplacian (not the GCN propagation matrix).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_nodes
|
int | None
|
The number of nodes in the graph. If |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
laplacian |
Tensor
|
The sparse symmetric normalized Laplacian
matrix of shape |
Source code in hypertorch/types/graph.py
get_sparse_normalized_gcn_laplacian(num_nodes=None, use_edge_weights=False)
¶
Compute the sparse Laplacian matrix from a graph edge index.
The GCN Laplacian is defined as: L_GCN = D_hat^-1/2 * A_hat * D_hat^-1/2,
where A_hat = A + I (adjacency with self-loops) and D_hat is the degree matrix
of A_hat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_nodes
|
int | None
|
The number of nodes in the graph. If |
None
|
use_edge_weights
|
bool
|
If |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
laplacian |
Tensor
|
The sparse symmetrically normalized Laplacian matrix of
shape |
Source code in hypertorch/types/graph.py
remove_selfloops()
¶
Remove self-loops from the edge index.
Source code in hypertorch/types/graph.py
remove_duplicate_edges(num_nodes=None)
¶
Remove duplicate edges from the edge index. Keeps the tensor contiguous in memory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_nodes
|
int | None
|
The number of nodes in the graph. If |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
edge_index |
EdgeIndex
|
This |
Source code in hypertorch/types/graph.py
to_undirected(with_selfloops=False, num_nodes=None)
¶
Convert the edge index to an undirected edge index by adding reverse edges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
with_selfloops
|
bool
|
Whether to add self-loops to each node. Defaults to |
False
|
num_nodes
|
int | None
|
Total number of nodes. Propagated to |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
edge_index |
EdgeIndex
|
This |
Source code in hypertorch/types/graph.py
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 | |
__validate_edge_weights(edge_weights)
¶
Validate edge weight tensor shape.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_weights
|
Tensor | None
|
Optional edge weight tensor to validate. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If edge weights are not one-dimensional or do not match edge count. |
Source code in hypertorch/types/graph.py
__validate_num_nodes(num_nodes)
¶
Validate that an explicit node count can contain the edge index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_nodes
|
int
|
Explicit number of nodes. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in hypertorch/types/graph.py
Graph
¶
A simple graph data structure using edge list representation.
Attributes:
| Name | Type | Description |
|---|---|---|
edges |
list[list[int]]
|
A list of edges, where each edge is represented as a list of two integers (source_node, destination_node). |
Source code in hypertorch/types/graph.py
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 | |
edge_weights
property
¶
Return the edge weights, if present.
edge_weights_tensor
property
¶
Return the edge weights as a tensor, if present.
num_nodes
property
¶
Return the number of nodes in the graph.
num_edges
property
¶
Return the number of edges in the graph.
__init__(edges, edge_weights=None)
¶
Initialize the graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edges
|
list[list[int]]
|
Edge list where each edge is |
required |
edge_weights
|
list[float] | None
|
Optional edge weights matching |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If edge weights are provided but their length does not match the number of edges. |
Source code in hypertorch/types/graph.py
remove_selfloops()
¶
Remove self-loops from the graph.
Returns:
| Name | Type | Description |
|---|---|---|
edges |
Graph
|
List of edges without self-loops. |
Source code in hypertorch/types/graph.py
to_edge_index()
¶
Convert the graph to edge index representation.
Returns:
| Name | Type | Description |
|---|---|---|
edge_index |
Tensor
|
Tensor of shape (2, |E|) representing edges. |
Source code in hypertorch/types/graph.py
__validate_edge_weights(edge_weights)
¶
Validate graph edge weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_weights
|
list[float] | None
|
Optional edge weights to validate. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the number of weights does not match the number of edges. |
Source code in hypertorch/types/graph.py
smoothing_with_laplacian_matrix(x, laplacian_matrix, drop_rate=0.0)
staticmethod
¶
Return the feature matrix smoothed with a Laplacian matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Node feature matrix. Size |
required |
laplacian_matrix
|
Tensor
|
The Laplacian matrix. Size |
required |
drop_rate
|
float
|
Randomly dropout the connections in the Laplacian with probability
|
0.0
|
Returns:
| Name | Type | Description |
|---|---|---|
x |
Tensor
|
The smoothed feature matrix. Size |
Source code in hypertorch/types/graph.py
HIFHypergraph
¶
A hypergraph data structure that supports directed/undirected hyperedges with incidence-based representation.
Attributes:
| Name | Type | Description |
|---|---|---|
network_type |
Literal['asc', 'directed', 'undirected'] | None
|
The type of hypergraph, which can be "asc" (or "directed") for directed hyperedges, or "undirected" for undirected hyperedges. |
metadata |
dict[str, Any]
|
Optional dictionary of metadata about the hypergraph. |
incidences |
list[dict[str, Any]]
|
A list of incidences, where each incidence is a dictionary with keys "node" and "edge" representing the relationship between a node and a hyperedge. |
nodes |
list[dict[str, Any]]
|
A list of node dictionaries, where each dictionary contains information about a node (e.g., id, features). |
hyperedges |
list[dict[str, Any]]
|
A list of edge dictionaries, where each dictionary contains information about a hyperedge (e.g., id, features). |
Source code in hypertorch/types/hypergraph.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 | |
num_nodes
property
¶
Return the number of nodes in the hypergraph.
num_hyperedges
property
¶
Return the number of hyperedges in the hypergraph.
__init__(network_type=None, metadata=None, incidences=None, nodes=None, hyperedges=None)
¶
Initialize the HIF hypergraph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network_type
|
Literal['asc', 'directed', 'undirected'] | None
|
The type of hypergraph, which can be "asc" (or "directed") for directed hyperedges, or "undirected" for undirected hyperedges. |
None
|
metadata
|
dict[str, Any] | None
|
Optional dictionary of metadata about the hypergraph. |
None
|
incidences
|
list[dict[str, Any]] | None
|
A list of incidences, where each incidence is a dictionary with keys "node" and "edge" representing the relationship between a node and a hyperedge. |
None
|
nodes
|
list[dict[str, Any]] | None
|
A list of node dictionaries, where each dictionary contains information about a node (e.g., id, features). |
None
|
hyperedges
|
list[dict[str, Any]] | None
|
A list of edge dictionaries, where each dictionary contains information about a hyperedge (e.g., id, features). |
None
|
Source code in hypertorch/types/hypergraph.py
empty()
classmethod
¶
Create an empty undirected HIF hypergraph.
Returns:
| Name | Type | Description |
|---|---|---|
hypergraph |
HIFHypergraph
|
Empty HIF hypergraph. |
Source code in hypertorch/types/hypergraph.py
from_hif(data)
classmethod
¶
Create a Hypergraph from a HIF (Hypergraph Interchange Format).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any]
|
Dictionary with keys: network-type, metadata, incidences, nodes, hyperedges |
required |
Returns:
| Name | Type | Description |
|---|---|---|
hypergraph |
HIFHypergraph
|
Hypergraph instance |
Source code in hypertorch/types/hypergraph.py
stats()
¶
Compute statistics for the HIFhypergraph.
Fields
num_nodes: The number of nodes in the hypergraph.num_hyperedges: The number of hyperedges in the hypergraph.avg_degree_node_raw: The average degree of nodes, calculated as the mean number of hyperedges each node belongs to.avg_degree_node: The floored node average degree.avg_degree_hyperedge_raw: The average size of hyperedges, calculated as the mean number of nodes each hyperedge contains.avg_degree_hyperedge: The floored hyperedge average size.node_degree_max: The maximum degree of any node in the hypergraph.hyperedge_degree_max: The maximum size of any hyperedge in the hypergraph.node_degree_median: The median degree of nodes in the hypergraph.hyperedge_degree_median: The median size of hyperedges in the hypergraph.distribution_node_degree: A list where the value at indexirepresents the count of nodes with degreei.distribution_hyperedge_size: A list where the value at indexirepresents the count of hyperedges with sizei.distribution_node_degree_hist: A dictionary where the keys are node degrees and the values are the count of nodes with that degree.distribution_hyperedge_size_hist: A dictionary where the keys are hyperedge sizes and the values are the count of hyperedges with that size.
Returns:
| Name | Type | Description |
|---|---|---|
stats |
dict[str, Any]
|
A dictionary containing various statistics about the hypergraph. |
Source code in hypertorch/types/hypergraph.py
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 | |
HyperedgeIndex
¶
A wrapper for hyperedge index representation.
Hyperedge index is a tensor of shape (2, num_incidences) that encodes the relationships
between nodes and hyperedges.
Each column in the tensor represents an incidence between a node and a hyperedge, with the
first row containing node indices and the second row containing corresponding hyperedge indices.
Examples:
This represents two hyperedges:
The number of nodes in this hypergraph is 3 (nodes 0, 1, and 2). The number of hyperedges is 2 (hyperedges 0 and 1).
Source code in hypertorch/types/hypergraph.py
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 | |
all_node_ids
property
¶
Return the tensor of all node IDs in the hyperedge index.
all_hyperedge_ids
property
¶
Return the tensor of all hyperedge IDs in the hyperedge index.
item
property
¶
Return the hyperedge index tensor.
node_ids
property
¶
Return the sorted unique node IDs from the hyperedge index.
hyperedge_ids
property
¶
Return the sorted unique hyperedge IDs from the hyperedge index.
num_hyperedges
property
¶
Return the number of hyperedges in the hypergraph.
num_nodes
property
¶
Return the number of nodes in the hypergraph.
num_incidences
property
¶
Return the number of incidences in the hypergraph, which is the number of columns in the hyperedge index.
__init__(hyperedge_index)
¶
Initialize the hyperedge index wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_index
|
Tensor
|
Tensor of shape |
required |
Source code in hypertorch/types/hypergraph.py
nodes_in(hyperedge_id)
¶
Return the list of node IDs that belong to the given hyperedge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_id
|
int
|
The ID of the hyperedge to query. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
node_ids |
list[int]
|
A list of node IDs that belong to the specified hyperedge. |
Source code in hypertorch/types/hypergraph.py
num_nodes_if_isolated_exist(num_nodes)
¶
Return the number of nodes in the hypergraph, accounting for isolated nodes that may not appear in the hyperedge index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_nodes
|
int
|
The total number of nodes in the hypergraph, including isolated nodes. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
num_nodes |
int
|
The number of nodes in the hypergraph, which is the maximum of the number of
unique nodes in the hyperedge index and the provided |
Source code in hypertorch/types/hypergraph.py
get_clique_expansion_adjacency_list(num_nodes=None)
¶
Build an adjacency list for the clique-expanded underlying graph.
For each hyperedge, every pair of member nodes becomes an undirected graph edge. Self-loops are not included in the returned neighbor sets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_nodes
|
int | None
|
Total number of nodes to include in the adjacency list.
If |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
adjacency |
list[set[int]]
|
A list where |
Source code in hypertorch/types/hypergraph.py
get_sparse_incidence_matrix(num_nodes=None, num_hyperedges=None)
¶
Compute the sparse incidence matrix H of shape (num_nodes, num_hyperedges).
Each entry H[v, e] = 1 if node v belongs to hyperedge e, and 0 otherwise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_nodes
|
int | None
|
Total number of nodes. If |
None
|
num_hyperedges
|
int | None
|
Total number of hyperedges. If |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
incidence_matrix |
Tensor
|
The sparse incidence matrix H of
shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the provided dimensions cannot contain the raw node or hyperedge IDs. |
Source code in hypertorch/types/hypergraph.py
get_sparse_normalized_node_degree_matrix(incidence_matrix, power, num_nodes=None)
¶
Compute a sparse diagonal node degree matrix from row-sums of the incidence matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
incidence_matrix
|
Tensor
|
The sparse incidence matrix H of
shape |
required |
power
|
float
|
Exponent applied to node degrees before placing them on the diagonal. |
required |
num_nodes
|
int | None
|
Total number of nodes. If |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
degree_matrix |
Tensor
|
The sparse diagonal matrix of shape |
Source code in hypertorch/types/hypergraph.py
get_sparse_rownormalized_node_degree_matrix(incidence_matrix, num_nodes=None)
¶
Compute the sparse normalized node degree matrix D_n^-1.
The node degree d_n[i] is the number of hyperedges containing node i
(i.e., the row-sum of the incidence matrix H).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
incidence_matrix
|
Tensor
|
The sparse incidence matrix H of
shape |
required |
num_nodes
|
int | None
|
Total number of nodes. If |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
degree_matrix |
Tensor
|
The sparse diagonal matrix |
Source code in hypertorch/types/hypergraph.py
get_sparse_symnormalized_node_degree_matrix(incidence_matrix, num_nodes=None)
¶
Compute the sparse normalized node degree matrix D_n^-1/2.
The node degree d_n[i] is the number of hyperedges containing node i
(i.e., the row-sum of the incidence matrix H).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
incidence_matrix
|
Tensor
|
The sparse incidence matrix H of
shape |
required |
num_nodes
|
int | None
|
Total number of nodes. If |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
degree_matrix |
Tensor
|
The sparse diagonal matrix |
Source code in hypertorch/types/hypergraph.py
get_sparse_normalized_hyperedge_degree_matrix(incidence_matrix, num_hyperedges=None)
¶
Compute the sparse normalized hyperedge degree matrix D_e^-1.
The hyperedge degree d_e[j] is the number of nodes in hyperedge j
(i.e., the column-sum of the incidence matrix H).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
incidence_matrix
|
Tensor
|
The sparse incidence matrix H of
shape |
required |
num_hyperedges
|
int | None
|
Total number of hyperedges. If |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
degree_matrix |
Tensor
|
The sparse diagonal matrix |
Source code in hypertorch/types/hypergraph.py
get_sparse_hgnn_smoothing_matrix(num_nodes=None, num_hyperedges=None)
¶
Compute the sparse HGNN Laplacian matrix for hypergraph spectral convolution.
Implements: L_HGNN = D_n^{-1/2} H D_e^{-1} H^T D_n^{-1/2}
where
- H is the incidence matrix of shape
(num_nodes, num_hyperedges) D_n^-1/2is the normalized node degree matrixD_e^-1is the inverse hyperedge degree matrix (with W = I)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_nodes
|
int | None
|
Total number of nodes. If |
None
|
num_hyperedges
|
int | None
|
Total number of hyperedges. If |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
laplacian |
Tensor
|
The sparse HGNN Laplacian matrix of shape |
Source code in hypertorch/types/hypergraph.py
get_sparse_hgnnp_smoothing_matrix(num_nodes=None, num_hyperedges=None)
¶
Compute the sparse HGNN+ smoothing matrix for hypergraph mean aggregation.
Implements: M_HGNN+ = D_v^{-1} H D_e^{-1} H^T
This matrix is row-stochastic for non-isolated nodes and corresponds to
the two-stage mean aggregation used by HGNN+:
1. D_e^{-1} H^T X: mean over nodes in each hyperedge.
2. D_v^{-1} H (...): mean over hyperedges incident to each node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_nodes
|
int | None
|
Total number of nodes. If |
None
|
num_hyperedges
|
int | None
|
Total number of hyperedges.
If |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
laplacian |
Tensor
|
The sparse HGNN+ smoothing matrix of shape |
Source code in hypertorch/types/hypergraph.py
reduce(strategy, **kwargs)
¶
Reduce the hypergraph to a graph represented by edge index using the specified strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy
|
GraphReductionStrategy
|
The reduction strategy to use. Defaults to |
required |
**kwargs
|
Any
|
Additional keyword arguments for specific strategies. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
edge_index |
Tensor
|
The edge index of the reduced graph. Size |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in hypertorch/types/hypergraph.py
reduce_to_edge_index_on_clique_expansion(num_nodes=None, num_hyperedges=None)
¶
Construct a graph from a hypergraph via clique expansion using H @ H^T,
where H is the incidence matrix of the hypergraph.
In clique expansion, each hyperedge is replaced by a clique connecting all its member nodes.
For each hyperedge, all pairs of member nodes become edges in the resulting graph.
This is computed efficiently using the incidence matrix: A = H @ H^T, where H is
the sparse incidence matrix of shape [num_nodes, num_hyperedges] and A is
the adjacency matrix of the clique-expanded graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_nodes
|
int | None
|
Total number of nodes. If |
None
|
num_hyperedges
|
int | None
|
Total number of hyperedges. If |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
edge_index |
Tensor
|
The edge index of the clique-expanded graph. Size |
Source code in hypertorch/types/hypergraph.py
reduce_to_edge_index_on_random_direction(x, with_mediators=False, remove_selfloops=True, return_weights=False, seed=None)
¶
References
- Construct a graph from a hypergraph with methods proposed in HyperGCN: A New Method of Training Graph Convolutional Networks on Hypergraphs paper.
- Reference implementation: source.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Node feature matrix. Size |
required |
with_mediators
|
bool
|
Whether to use mediator to transform the hyperedges to edges in the
graph. Defaults to |
False
|
remove_selfloops
|
bool
|
Whether to remove self-loops. Defaults to |
True
|
return_weights
|
bool
|
Whether to return the DHG-style reduced-edge weights alongside the
edge index. Defaults to |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
edge_index |
Tensor
|
The edge index of the reduced graph. Size |
edge_weights |
Tensor | None
|
The edge weights of the reduced graph. Size |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any hyperedge contains fewer than 2 nodes. |
Source code in hypertorch/types/hypergraph.py
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 | |
remove_duplicate_edges()
¶
Remove duplicate edges from the hyperedge index.
Keeps the tensor contiguous in memory.
Source code in hypertorch/types/hypergraph.py
remove_hyperedges_with_fewer_than_k_nodes(k)
¶
Remove hyperedges that contain fewer than k nodes.
Examples:
>>> k = 3
>>> unique_hyperedge_ids: [0, 1, 2]
... # inverse -> idx_to_hyperedge_id, counts -> num_nodes_per_hyperedge
... # (index into unique_hyperedge_ids per column)
... inverse = [0, 0, 1, 1, 2, 1]
... counts = [2, 3, 1]
>>> # counts[inverse] is equivalent to:
... # for i, inv in enumerate(inverse): keep_mask[i] = counts[inv]
>>> counts[inverse] = [2, 2, 3, 3, 1, 3]
>>> keep_mask = [F, F, T, T, F, T]
>>> # after filtering hyperedges with fewer than k=3 nodes:
>>> hyperedge_index = [[2, 3, 4],
... [1, 1, 1]], shape (2, |E'| = 3)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
int
|
The minimum number of nodes a hyperedge must contain to be kept. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
hyperedge_index |
HyperedgeIndex
|
A new |
Source code in hypertorch/types/hypergraph.py
to_0based(node_ids_to_rebase=None, hyperedge_ids_to_rebase=None)
¶
Convert hyperedge index to the 0-based format by rebasing node IDs to the range [0,
num_nodes-1] and hyperedge IDs [0, num_hyperedges-1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_ids_to_rebase
|
Tensor | None
|
Tensor of shape |
None
|
hyperedge_ids_to_rebase
|
Tensor | None
|
Tensor of shape |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
hyperedge_index |
HyperedgeIndex
|
A new |
Source code in hypertorch/types/hypergraph.py
to_global(global_node_ids=None)
¶
Convert hyperedge index to the global format by rebasing node IDs to the original IDs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
global_node_ids
|
Tensor | None
|
Tensor of shape |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
hyperedge_index |
HyperedgeIndex
|
A new |
Source code in hypertorch/types/hypergraph.py
__validate_num_hyperedges(num_hyperedges)
¶
Validate that an explicit hyperedge count can contain the index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_hyperedges
|
int | None
|
Optional explicit number of hyperedges. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in hypertorch/types/hypergraph.py
__validate_num_nodes(num_nodes)
¶
Validate that an explicit node count can contain the index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_nodes
|
int | None
|
Optional explicit number of nodes. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in hypertorch/types/hypergraph.py
__validate_degree_matrix_dimension(name, value, expected)
¶
Validate a requested degree-matrix dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the dimension being validated. |
required |
value
|
int
|
Requested dimension value. |
required |
expected
|
int
|
Required dimension value. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the value is negative or does not match the expected dimension. |
Source code in hypertorch/types/hypergraph.py
Hypergraph
¶
A simple hypergraph data structure using edge list representation.
Attributes:
| Name | Type | Description |
|---|---|---|
hyperedges |
list[list[int]]
|
A list of hyperedges, where each hyperedge is represented as a list of node IDs. |
Source code in hypertorch/types/hypergraph.py
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 | |
num_nodes
property
¶
Return the number of nodes in the hypergraph.
num_hyperedges
property
¶
Return the number of hyperedges in the hypergraph.
__init__(hyperedges)
¶
Initialize the hypergraph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedges
|
list[list[int]]
|
List of hyperedges represented as lists of node IDs. |
required |
neighbors_of(node)
¶
Return the set of nodes that share at least one hyperedge with node.
A node u is a neighbor of v if there exists a hyperedge e such that both u and v are in e. The node itself is excluded from the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
int
|
The node ID to find neighbors for. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
neighbors |
Neighborhood
|
A set of neighbor node IDs (excluding the node itself). |
Source code in hypertorch/types/hypergraph.py
neighbors_of_all()
¶
Build a mapping from every node to its neighbors.
This precomputes neighbors_of for all nodes at once, which is
more efficient when scoring many candidate hyperedges.
Returns:
| Name | Type | Description |
|---|---|---|
neighbors |
dict[int, Neighborhood]
|
A dictionary mapping each node ID to its set of neighbors. |
Source code in hypertorch/types/hypergraph.py
stats()
¶
Return basic statistics about the hypergraph.
Source code in hypertorch/types/hypergraph.py
from_hyperedge_index(hyperedge_index)
classmethod
¶
Create a Hypergraph from a hyperedge index representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_index
|
Tensor
|
Tensor of shape (2, |E|) representing hyperedges, where each column is (node, hyperedge). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
hypergraph |
Hypergraph
|
Hypergraph instance |
Source code in hypertorch/types/hypergraph.py
smoothing_with_matrix(x, matrix, drop_rate=0.0)
staticmethod
¶
Return the feature matrix smoothed with a smoothing matrix.
Computes M @ X where M is the smoothing matrix and X is the node feature matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Node feature matrix. Size |
required |
matrix
|
Tensor
|
The smoothing matrix. Size |
required |
drop_rate
|
float
|
Randomly dropout the connections in the smoothing matrix with
probability |
0.0
|
Returns:
| Name | Type | Description |
|---|---|---|
x |
Tensor
|
The smoothed feature matrix. Size |
Source code in hypertorch/types/hypergraph.py
GraphReductionStrategyEnum
¶
HData
¶
Class for representing hypergraph data in a format suitable for hypergraph learning tasks.
Examples:
>>> x = torch.randn(10, 16) # 10 nodes with 16 features each
>>> hyperedge_index = torch.tensor([[0, 0, 1, 1, 1], # node IDs
... [0, 1, 2, 3, 4]]) # hyperedge IDs
>>> data = HData(x=x, hyperedge_index=hyperedge_index)
Attributes:
| Name | Type | Description |
|---|---|---|
x |
Tensor
|
Node feature matrix of shape |
hyperedge_index |
Tensor
|
Sparse node-hyperedge incidence matrix of shape
|
hyperedge_weights |
Tensor | None
|
Optional tensor of shape |
hyperedge_attr |
Tensor | None
|
Optional hyperedge attributes of
shape |
num_nodes |
int
|
Number of nodes in the hypergraph. If |
num_hyperedges |
int
|
Number of hyperedges in the hypergraph.
If |
global_node_ids |
Tensor
|
Optional node IDs of shape |
target_node_mask |
Tensor
|
Optional boolean tensor of shape |
target_hyperedge_mask |
Tensor
|
Optional boolean tensor of shape |
y |
Tensor
|
Labels for nodes or hyperedges, it has shape |
task |
Task
|
Learning task used to determine whether operations work on nodes or hyperedges.
If |
device |
device
|
Device shared by all tensors in the instance. |
Source code in hypertorch/types/hdata.py
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 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 | |
is_hyperedge_related_task
property
¶
Check if the task uses hyperedge-level targets and operations.
Returns:
| Name | Type | Description |
|---|---|---|
is_hyperedge_related |
bool
|
True if the task is hyperedge-related, False otherwise. |
is_node_related_task
property
¶
Check if the task uses node-level targets and operations.
Returns:
| Name | Type | Description |
|---|---|---|
is_node_related |
bool
|
True if the task is node-related, False otherwise. |
num_sampleable_nodes
property
¶
Return the number of nodes that are eligible for sampling based on this HData instance's task.
num_sampleable_hyperedges
property
¶
Return the number of hyperedges that are eligible for sampling based on this HData instance's task.
sampleable_node_ids
property
¶
Return node IDs that are eligible for sampling based on this HData instance's task.
sampleable_hyperedge_ids
property
¶
Return hyperedge IDs eligible for sampling based on this HData instance's task.
__init__(x, hyperedge_index, hyperedge_weights=None, hyperedge_attr=None, num_nodes=None, num_hyperedges=None, global_node_ids=None, target_node_mask=None, target_hyperedge_mask=None, y=None, task=TaskEnum.HYPERLINK_PREDICTION)
¶
Initialize hypergraph learning data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Node feature matrix of shape |
required |
hyperedge_index
|
Tensor
|
Sparse node-hyperedge incidence matrix of shape
|
required |
hyperedge_weights
|
Tensor | None
|
Optional tensor of shape |
None
|
hyperedge_attr
|
Tensor | None
|
Optional hyperedge attributes of
shape |
None
|
num_nodes
|
int | None
|
Number of nodes in the hypergraph. If |
None
|
num_hyperedges
|
int | None
|
Number of hyperedges in the hypergraph.
If |
None
|
global_node_ids
|
Tensor | None
|
Optional node IDs of shape |
None
|
target_node_mask
|
Tensor | None
|
Optional boolean tensor of shape |
None
|
target_hyperedge_mask
|
Tensor | None
|
Optional boolean tensor of shape |
None
|
y
|
Tensor | None
|
Labels for nodes or hyperedges, it has shape |
None
|
task
|
Task
|
Learning task used to determine whether operations work on nodes or hyperedges.
If |
HYPERLINK_PREDICTION
|
Source code in hypertorch/types/hdata.py
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 | |
__repr__()
¶
Return a shape-oriented representation of the data object.
Returns:
| Name | Type | Description |
|---|---|---|
representation |
str
|
Human-readable summary of tensor shapes and counts. |
Source code in hypertorch/types/hdata.py
cat_same_node_space(hdatas, x=None, global_node_ids=None)
classmethod
¶
Concatenate HData instances that share the same node space, meaning nodes with
the same ID in different instances are the same node.
This is useful when combining positive and negative hyperedges that reference
the same set of nodes.
Notes
xis derived from the instance with the largest number of nodes, if not provided explicitly. If there are conflicting features for the same node ID across instances, the features from the instance with the largest number of nodes will be used. Ifglobal_node_idsis provided explicitly,xmust also be provided to ensure consistency.hyperedge_indexis the concatenation of all input hyperedge indices.hyperedge_weightsis the concatenation of all input hyperedge weights, if present. If some instances have hyperedge weights and others do not, the resultinghyperedge_weightswill be set toNone.hyperedge_attris the concatenation of all input hyperedge attributes, if present. If some instances have hyperedge attributes and others do not, the resultinghyperedge_attrwill be set toNone.global_node_idsis derived from the instance with the largest number of nodes, if not provided explicitly. Ifxis provided explicitly,global_node_idsmust be provided explicitly as well to ensure consistency.target_node_maskis derived from the instance with the largest number of nodes.target_hyperedge_maskis the concatenation of all input hyperedge target masks.yis the concatenation of all input labels.
Examples:
>>> x = torch.randn(5, 8)
>>> pos = HData(x=x, hyperedge_index=torch.tensor([[0, 1, 2, 3, 4], [0, 0, 1, 2, 2]]))
>>> neg = HData(x=x, hyperedge_index=torch.tensor([[0, 2], [3, 3]]))
>>> new = HData.cat_same_node_space([pos, neg])
>>> new.num_nodes # 5 — nodes [0, 1, 2, 3, 4]
>>> new.num_hyperedges # 4 — hyperedges [0, 1, 2, 3]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdatas
|
Sequence[HData]
|
One or more |
required |
x
|
Tensor | None
|
Optional node feature matrix to use for the resulting |
None
|
global_node_ids
|
Tensor | None
|
Optional global node IDs for the resulting |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
A new |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no HData instances are provided, if there are overlapping
hyperedge IDs across instances,
or if |
Source code in hypertorch/types/hdata.py
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 | |
add_negative_samples(negative_sampler, seed=None)
¶
Return a new HData with sampled negative hyperedges added.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
negative_sampler
|
NegativeSampler
|
Sampler used to generate negative hyperedges from this instance. |
required |
seed
|
int | None
|
Optional random seed used for both negative sampling and the final shuffle.
Defaults to |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
A new |
Source code in hypertorch/types/hdata.py
empty(task=TaskEnum.HYPERLINK_PREDICTION)
classmethod
¶
Create an empty HData instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task
|
Task
|
Learning task for the empty HData. Defaults to |
HYPERLINK_PREDICTION
|
Returns:
| Name | Type | Description |
|---|---|---|
task |
HData
|
Learning task for the empty HData. |
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
Empty HData. |
Source code in hypertorch/types/hdata.py
from_hyperedge_index(hyperedge_index, task=TaskEnum.HYPERLINK_PREDICTION)
classmethod
¶
Build an HData from a given hyperedge index, with empty node features and
hyperedge attributes.
- Node features are initialized as an empty tensor of shape
[0, 0]. - Hyperedge attributes are set to
None. - Hyperedge weights are set to
None. - The number of nodes and hyperedges are inferred from the hyperedge index.
Examples:
>>> hyperedge_index = [[0, 0, 1, 2, 3, 4],
... [0, 0, 0, 1, 2, 2]]
>>> num_nodes = 5
>>> num_hyperedges = 3
>>> x = [] # Empty node features with shape [0, 0]
>>> hyperedge_attr = None
>>> hyperedge_weights = None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hyperedge_index
|
Tensor
|
Tensor of shape |
required |
task
|
Task
|
Learning task for the resulting |
HYPERLINK_PREDICTION
|
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
An |
Source code in hypertorch/types/hdata.py
split(hdata, split_hyperedge_ids=None, node_space_setting='transductive', splitter=None)
classmethod
¶
Build an HData for a single split from the given hyperedge IDs.
Examples:
Transductive split (default) preserving the full node space:
>>> split_hdata = HData.split(
... hdata,
... torch.tensor([1]),
... node_space_setting="transductive")
>>> split_hdata.x.shape[0] == hdata.x.shape[0]
>>> split_hdata.hyperedge_index
... # node IDs stay in the original row space, hyperedge IDs are rebased
Inductive split:
>>> split_hdata = HData.split(hdata, torch.tensor([1]), node_space_setting="inductive")
>>> split_hdata.x.shape[0] # only nodes incident to hyperedge 1
... 2
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdata
|
HData
|
The original |
required |
split_hyperedge_ids
|
Tensor | None
|
Tensor of hyperedge IDs to include in this split.
It is assumed that the provided hyperedge IDs are valid and exist
in |
None
|
node_space_setting
|
NodeSpaceSetting
|
Whether to preserve the full node space in the splits.
|
'transductive'
|
splitter
|
Splitter[HData, Any] | None
|
Optional HData splitter. When provided, it owns split materialization.
Defaults to |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
The splitted instance with remapped node and hyperedge IDs. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in hypertorch/types/hdata.py
enrich_node_features(enricher, enrichment_mode='replace')
¶
Enrich node features using the provided node feature enricher.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enricher
|
NodeEnricher
|
An instance of NodeEnricher to generate structural node features from hypergraph topology. |
required |
enrichment_mode
|
EnrichmentMode | None
|
How to combine generated features with existing |
'replace'
|
Source code in hypertorch/types/hdata.py
enrich_node_features_from(hdata_with_features, node_space_setting='transductive', fill_value=None)
¶
Copy node features from another HData by aligning features by global_node_ids.
Examples:
Transductive enrichment (default) expecting the same node space in both source and target:
Inductive with a scalar fill value:
>>> target = target.enrich_node_features_from(
... source,
... node_space_setting="inductive",
... fill_value=0.0,
... )
Inductive with a feature vector fill value:
>>> target = target.enrich_node_features_from(
... source,
... node_space_setting="inductive",
... fill_value=[0.0, 1.0, 0.0],
... )
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdata_with_features
|
HData
|
Source |
required |
node_space_setting
|
NodeSpaceSetting
|
The setting for the node space, determining how nodes are handled.
If |
'transductive'
|
fill_value
|
NodeSpaceFiller | None
|
Scalar or vector used to fill missing node features when
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
A new |
Raises:
| Type | Description |
|---|---|
ValueError
|
If either instance lacks |
Source code in hypertorch/types/hdata.py
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 | |
enrich_hyperedge_weights(enricher, enrichment_mode='replace')
¶
Enrich hyperedge weights using the provided hyperedge weight enricher.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enricher
|
HyperedgeEnricher
|
An instance of HyperedgeEnricher to generate hyperedge weights from hypergraph topology. |
required |
enrichment_mode
|
EnrichmentMode | None
|
How to combine generated weights with
existing |
'replace'
|
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
A new |
Source code in hypertorch/types/hdata.py
enrich_hyperedge_attr(enricher, enrichment_mode='replace')
¶
Enrich hyperedge features using the provided hyperedge feature enricher.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enricher
|
HyperedgeEnricher
|
An instance of HyperedgeEnricher to generate structural hyperedge features from hypergraph topology. |
required |
enrichment_mode
|
EnrichmentMode | None
|
How to combine generated features with
existing |
'replace'
|
Source code in hypertorch/types/hdata.py
get_device_if_all_consistent()
¶
Check that all tensors are on the same device and return that device.
If there are no tensors or if they are on different devices, return CPU.
Returns:
| Name | Type | Description |
|---|---|---|
device |
device
|
The common device if all tensors are on the same device, otherwise CPU. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If tensors are on different devices. |
Source code in hypertorch/types/hdata.py
remove_hyperedges_with_fewer_than_k_nodes(k, preserve_global_node_ids=False)
¶
Remove hyperedges that have fewer than k incident nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
int
|
The minimum number of nodes a hyperedge must have to be retained. |
required |
preserve_global_node_ids
|
bool
|
Whether to preserve the global node IDs after
removing hyperedges. Defaults to |
False
|
Source code in hypertorch/types/hdata.py
shuffle(seed=None)
¶
Return a new HData instance with hyperedge IDs randomly reassigned.
Each hyperedge keeps its original set of nodes, but is assigned a new ID
via a random permutation.
y and hyperedge_attr are reordered to match, so that y[new_id]
still corresponds to the correct hyperedge.
Same for hyperedge_attr[new_id] if hyperedge attributes are present.
Examples:
>>> hyperedge_index = torch.tensor([[0, 1, 2, 3], [0, 0, 1, 1]])
>>> y = torch.tensor([1, 0])
>>> hdata = HData(x=x, hyperedge_index=hyperedge_index, y=y)
>>> shuffled_hdata = hdata.shuffle(seed=42)
>>> shuffled_hdata.hyperedge_index # hyperedges may be reassigned
... # e.g.,
... [[0, 1, 2, 3],
... [1, 1, 0, 0]]
>>> shuffled_hdata.y # labels are permuted to match new hyperedge IDs, e.g., [0, 1]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int | None
|
Optional random seed for reproducibility. If |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
A new |
Source code in hypertorch/types/hdata.py
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 | |
clone()
¶
Return a deep copy of this HData.
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
A new |
Source code in hypertorch/types/hdata.py
to(device, non_blocking=False)
¶
Move all tensors to the specified device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
device | str
|
The target device (e.g., 'cpu', 'cuda:0'). |
required |
non_blocking
|
bool
|
If |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
The |
Source code in hypertorch/types/hdata.py
with_target_node_mask(target_node_mask)
¶
Return a copy of this instance with a target_node_mask attribute set to the given mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_node_mask
|
Tensor
|
A boolean tensor indicating which nodes are considered target nodes. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
A new |
Source code in hypertorch/types/hdata.py
with_target_hyperedge_mask(target_hyperedge_mask)
¶
Return a copy of this instance with target_hyperedge_mask set to the given mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_hyperedge_mask
|
Tensor
|
Boolean tensor indicating target hyperedges. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
A new |
Source code in hypertorch/types/hdata.py
with_y_to(value, size=None)
¶
Return a copy of this instance with a y attribute set to the given value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float
|
The value to set for all entries in the y attribute. |
required |
size
|
int | None
|
The size of the y tensor. If |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
A new |
Source code in hypertorch/types/hdata.py
with_y_ones(size=None)
¶
Return a copy of this instance with a y attribute of all ones.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
int | None
|
The size of the y tensor. If |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
A new |
Source code in hypertorch/types/hdata.py
with_y_zeros(size=None)
¶
Return a copy of this instance with a y attribute of all zeros.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
int | None
|
The size of the y tensor. If |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
hdata |
HData
|
A new |
Source code in hypertorch/types/hdata.py
stats()
¶
Compute statistics for the hypergraph data.
Fields
shape_x: The shape of the node feature matrixx.shape_hyperedge_weights: The shape of the hyperedge weights tensor, orNoneif hyperedge weights are not present.shape_hyperedge_attr: The shape of the hyperedge attribute matrix, orNoneif hyperedge attributes are not present.num_nodes: The number of nodes in the hypergraph.num_hyperedges: The number of hyperedges in the hypergraph.avg_degree_node_raw: The average degree of nodes, calculated as the mean number of hyperedges each node belongs to.avg_degree_node: The floored node average degree.avg_degree_hyperedge_raw: The average size of hyperedges, calculated as the mean number of nodes each hyperedge contains.avg_degree_hyperedge: The floored hyperedge average size.node_degree_max: The maximum degree of any node in the hypergraph.hyperedge_degree_max: The maximum size of any hyperedge in the hypergraph.node_degree_median: The median degree of nodes in the hypergraph.hyperedge_degree_median: The median size of hyperedges in the hypergraph.distribution_node_degree: A list where the value at indexirepresents the count of nodes with degreei.distribution_hyperedge_size: A list where the value at indexirepresents the count of hyperedges with sizei.distribution_node_degree_hist: A dictionary where the keys are node degrees and the values are the count of nodes with that degree.distribution_hyperedge_size_hist: A dictionary where the keys are hyperedge sizes and the values are the count of hyperedges with that size.
Returns:
| Name | Type | Description |
|---|---|---|
stats |
dict[str, Any]
|
A dictionary containing various statistics about the hypergraph. |
Source code in hypertorch/types/hdata.py
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 1295 1296 1297 1298 1299 1300 1301 1302 1303 | |
__validate_can_perform_cat_same_node_space(hdatas, x, global_node_ids)
classmethod
¶
Validate inputs for concatenating HData objects in the same node space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdatas
|
Sequence[HData]
|
HData objects to concatenate. |
required |
x
|
Tensor | None
|
Optional shared node feature matrix. |
required |
global_node_ids
|
Tensor | None
|
Optional shared global node IDs. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If required paired arguments are missing or hyperedge IDs overlap. |
Source code in hypertorch/types/hdata.py
__to_fill_features(fill_value, num_features, dtype, device)
¶
Convert a fill value into a feature vector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fill_value
|
NodeSpaceFiller | None
|
Scalar or vector fill value. |
required |
num_features
|
int
|
Required number of feature values. |
required |
dtype
|
dtype
|
Desired tensor dtype. |
required |
device
|
device
|
Desired tensor device. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
fill_features |
Tensor
|
Tensor of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the fill value cannot be broadcast to the requested feature count. |
Source code in hypertorch/types/hdata.py
__validate()
¶
Validate all HData tensor fields.
Raises:
| Type | Description |
|---|---|
ValueError
|
If any field has an invalid shape, dtype, or count. |
Source code in hypertorch/types/hdata.py
__validate_enrichment_mode(enrichment_mode)
¶
Validate a feature enrichment mode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enrichment_mode
|
EnrichmentMode | None
|
Optional enrichment mode to validate. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the mode is unsupported. |
Source code in hypertorch/types/hdata.py
__validate_hyperedge_attr()
¶
Validate optional hyperedge attributes.
Raises:
| Type | Description |
|---|---|
ValueError
|
If hyperedge attributes have an invalid dtype or shape. |
Source code in hypertorch/types/hdata.py
__validate_hyperedge_index()
¶
Validate hyperedge index IDs against configured node and hyperedge counts.
Raises:
| Type | Description |
|---|---|
ValueError
|
If IDs are negative or counts are too small. |
Source code in hypertorch/types/hdata.py
__validate_hyperedge_weights()
¶
Validate optional hyperedge weights.
Raises:
| Type | Description |
|---|---|
ValueError
|
If hyperedge weights have an invalid dtype or shape. |
Source code in hypertorch/types/hdata.py
__validate_global_node_ids()
¶
Validate global node IDs.
Raises:
| Type | Description |
|---|---|
ValueError
|
If global node IDs have an invalid dtype, shape, or length. |
Source code in hypertorch/types/hdata.py
__validate_labels()
¶
Validate labels.
Raises:
| Type | Description |
|---|---|
ValueError
|
If labels have an invalid dtype, shape, or length. |
Source code in hypertorch/types/hdata.py
__validate_target_node_mask()
¶
Validate the optional supervised-node mask.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the mask is incompatible with the configured task or node count. |
Source code in hypertorch/types/hdata.py
__validate_target_hyperedge_mask()
¶
Validate the optional supervised-hyperedge mask.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the mask is incompatible with the configured task or hyperedge count. |
Source code in hypertorch/types/hdata.py
__validate_task()
¶
Validate the learning task.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the task is unsupported. |
Source code in hypertorch/types/hdata.py
__validate_x()
¶
Validate node feature row count.
Raises:
| Type | Description |
|---|---|
ValueError
|
If node features do not match the configured node count. |
Source code in hypertorch/types/hdata.py
__validate_node_space_setting(node_space_setting, fill_value)
¶
Validate node-space enrichment settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_space_setting
|
NodeSpaceSetting
|
Node-space setting to validate. |
required |
fill_value
|
NodeSpaceFiller | None
|
Optional fill value for missing nodes. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the setting and fill value are incompatible. |
Source code in hypertorch/types/hdata.py
__validate_x_and_hyperedge_index_type_and_dim()
¶
Validate core tensor dtypes and dimensions.
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in hypertorch/types/hdata.py
__assign_y_for_task(y=None)
¶
Return labels as non-None tensor on the correct device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
Tensor | None
|
Optional labels tensor. If |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
y |
Tensor
|
Labels tensor on the correct device. |
Source code in hypertorch/types/hdata.py
TaskEnum
¶
ModelConfig
¶
A class representing the configuration of a model for training.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The name of the model. |
version |
str
|
The version of the model. |
model |
LightningModule
|
a LightningModule instance. |
is_trainable |
bool
|
Whether the model is trainable. |
trainer |
Trainer | None
|
A Trainer instance used for |
test_trainer |
Trainer | None
|
Optional Trainer instance used by |
train_dataloader |
DataLoader | None
|
Optional per-model train dataloader. When set, |
val_dataloader |
DataLoader | None
|
Optional per-model validation dataloader. When set, |
test_dataloader |
DataLoader | None
|
Optional per-model test dataloader. When set, |
Source code in hypertorch/types/model.py
__init__(name, model, version='default', is_trainable=True, trainer=None, test_trainer=None, train_dataloader=None, val_dataloader=None, test_dataloader=None)
¶
Initialize the model configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the model. |
required |
version
|
str
|
The version of the model. |
'default'
|
model
|
LightningModule
|
a LightningModule instance. |
required |
is_trainable
|
bool
|
Whether the model is trainable. |
True
|
trainer
|
Trainer | None
|
A Trainer instance used for |
None
|
test_trainer
|
Trainer | None
|
Optional Trainer instance used by |
None
|
train_dataloader
|
DataLoader | None
|
Optional per-model train dataloader. When set, |
None
|
val_dataloader
|
DataLoader | None
|
Optional per-model validation dataloader. When set, |
None
|
test_dataloader
|
DataLoader | None
|
Optional per-model test dataloader. When set, |
None
|
Source code in hypertorch/types/model.py
full_model_name()
¶
Return the combined model name and version.
Returns:
| Name | Type | Description |
|---|---|---|
full_model_name |
str
|
Name formatted as |