API Reference¶
Complete API documentation for all Hyperbench modules.
Core Modules¶
Data Module¶
hyperbench.data.dataset
¶
DatasetNames
¶
HIFConverter
¶
Docstring for HIFConverter A utility class to load hypergraphs from HIF format.
Source code in hyperbench/data/dataset.py
Dataset
¶
Bases: Dataset
Base Dataset class for hypergraph datasets, extending PyTorch's Dataset. Attributes: GDRIVE_FILE_ID (str): Google Drive file ID for the dataset. DATASET_NAME (str): Name of the dataset. hypergraph (HIFHypergraph): Loaded hypergraph instance. Methods: download(): Downloads and loads the hypergraph from HIF. process(): Processes the hypergraph into HData format.
Source code in hyperbench/data/dataset.py
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 | |
download()
¶
Load the hypergraph from HIF format using HIFConverter class.
Source code in hyperbench/data/dataset.py
process()
¶
Process the loaded hypergraph into HData format, mapping HIF structure to tensors. Returns: HData: Processed hypergraph data.
Source code in hyperbench/data/dataset.py
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 | |
transform_attrs(attrs, attr_keys=None)
¶
Extract and encode numeric node attributes to tensor. Non-numeric attributes are discarded. Missing attributes are filled with 0.0.
Args: attrs: Dictionary of node attributes attr_keys: Optional list of attribute keys to encode. If provided, ensures consistent ordering and fill missing with 0.0.
Returns: Tensor of numeric attribute values
Source code in hyperbench/data/dataset.py
__collect_attr_keys(attr_keys)
¶
Collect unique numeric attribute keys from a list of attribute dictionaries. Args: attrs_list: List of attribute dictionaries. Returns: List of unique numeric attribute keys.
Source code in hyperbench/data/dataset.py
__new_edge_index(sampled_edge_index, sampled_node_ids, sampled_edge_ids)
¶
Create new edge_index with 0-based node and edge IDs. Args: sampled_edge_index: Original edge_index tensor with sampled incidences. sampled_node_ids: List of sampled original node IDs. sampled_edge_ids: List of sampled original edge IDs. Returns: New edge_index tensor with 0-based node and edge IDs.
Source code in hyperbench/data/dataset.py
__to_0based_ids(original_ids, ids_to_keep, n)
¶
Map original IDs to 0-based ids. Example: original_ids: [1, 3, 3, 7] ids_to_keep: [3, 7] n = 8 # total number of elements (nodes or edges) in the original hypergraph Returned 0-based IDs: [0, 0, 1] # the size is sum of occurrences of ids_to_keep in original_ids Args: original_ids: Tensor of original IDs. n: Total number of original IDs. ids_to_keep: List of selected original IDs to be mapped to 0-based. Returns: Tensor of 0-based ids.
Source code in hyperbench/data/dataset.py
Types Module¶
HData¶
hyperbench.types.hdata
¶
HData
¶
Container for hypergraph data.
Attributes: x (Tensor): Node feature matrix of shape [num_nodes, num_features]. edge_index (Tensor): Hyperedge connectivity in COO format of shape [2, num_incidences], where edge_index[0] contains node IDs and edge_index[1] contains hyperedge IDs. edge_attr (Tensor, optional): Hyperedge feature matrix of shape [num_edges, num_edge_features]. Features associated with each hyperedge (e.g., weights, timestamps, types). num_nodes (int, optional): Number of nodes in the hypergraph. If None, inferred as x.size(0). num_edges (int, optional): Number of hyperedges in the hypergraph. If None, inferred as edge_index[1].max().item() + 1.
Example: >>> x = torch.randn(10, 16) # 10 nodes with 16 features each >>> edge_index = torch.tensor([[0, 0, 1, 1, 1], # node IDs ... [0, 1, 2, 3, 4]]) # hyperedge IDs >>> data = HData(x, edge_index=edge_index)
Source code in hyperbench/types/hdata.py
Hypergraph¶
hyperbench.types.hypergraph
¶
HIFHypergraph
¶
A hypergraph data structure that supports directed/undirected hyperedges with incidence-based representation.
Source code in hyperbench/types/hypergraph.py
num_nodes
property
¶
Return the number of nodes in the hypergraph.
num_edges
property
¶
Return the number of edges in the hypergraph.
from_hif(data)
classmethod
¶
Create a Hypergraph from a HIF (Hypergraph Interchange Format).
Args: data: Dictionary with keys: network-type, metadata, incidences, nodes, edges
Returns: Hypergraph instance
Source code in hyperbench/types/hypergraph.py
Utils Module¶
Data Utils¶
hyperbench.utils.data_utils
¶
HIF Utils¶
hyperbench.utils.hif_utils
¶
Data Loader¶
hyperbench.data.loader
¶
DataLoader
¶
Bases: DataLoader
Source code in hyperbench/data/loader.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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
collate(batch)
¶
Collates a list of HData objects into a single batched HData object.
This function combines multiple separate hypergraph samples into a single batched representation suitable for mini-batch training. It handles: - Concatenating node features from all samples - Concatenating and offsetting hyperedge from all samples - Concatenating edge attributes from all samples, if present
Example: Given batch = [HData_0, HData_1].
For node features:
HData_0.x.shape = (3, 64) # 3 nodes with 64 features
HData_1.x.shape = (2, 64) # 2 nodes with 64 features
The output will be HData with:
x.shape = (5, 64) # All 5 nodes concatenated
For edge index:
HData_0 (3 nodes, 2 hyperedges):
edge_index = [[0, 1, 1, 2], # Nodes 0, 1, 1, 2
[0, 0, 1, 1]] # Hyperedge 0 contains {0,1}, Hyperedge 1 contains {1,2}
HData_1 (2 nodes, 1 hyperedge):
edge_index = [[0, 1], # Nodes 0, 1
[0, 0]] # Hyperedge 0 contains {0,1}
Batched result:
edge_index = [[0, 1, 1, 2, 3, 4], # Node indices: original then offset by 3, so 0->3, 1->4
[0, 0, 1, 1, 2, 2]] # Hyperedge IDs: original then offset by 2, so 0->2, 0->2
^^^^^^^^^^ ^^^^
Sample 0 Sample 1 (nodes +3, edges +2)
Args: batch: List of HData objects to collate.
Returns: HData: A single HData object containing the batched data.
Source code in hyperbench/data/loader.py
__batch_node_features(batch)
¶
Concatenates node features from all samples in the batch.
Example: With shape being (num_nodes_in_sample, num_features).
If batch contains 3 sample with node features:
Sample 0: x = [[1, 2], [3, 4]] , shape: (2, 2)
Sample 1: x = [[5, 6]] , shape: (1, 2)
Sample 2: x = [[7, 8], [9, 10], [11, 12]], shape: (3, 2)
Result:
x: [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]]
shape: (6, 2), where 6 = 2 + 1 + 3 total nodes.
Args: batch: List of HData objects.
Returns: Tensor: Concatenated node features with shape (total_nodes, num_features).
Source code in hyperbench/data/loader.py
__batch_edges(batch)
¶
Batches hyperedge indices and attributes, adjusting indices for concatenated nodes. Hyperedge indices must be offset so they point to the correct nodes in the batched node tensor.
Example: Sample 0 (3 nodes, 2 hyperedges): edge_index = [[0, 1, 1, 2], # Nodes 0, 1, 1, 2 [0, 0, 1, 1]] # Hyperedge 0 contains {0,1}, Hyperedge 1 contains {1,2} node_offset = 0 edge_offset = 0
Sample 1 (2 nodes, 1 hyperedge):
edge_index = [[0, 1], # Nodes 0, 1
[0, 0]] # Hyperedge 0 contains {0,1}
node_offset = 3 # Previous samples have 3 nodes total
edge_offset = 2 # Previous samples have 2 hyperedges total
Result:
edge_index = [[0, 1, 1, 2, 3, 4], # Node indices: original then offset by 3, so 0->3, 1->4
[0, 0, 1, 1, 2, 2]] # Hyperedge IDs: original then offset by 2, so 0->2, 0->2
^^^^^^^^^^ ^^^^
Sample 0 Sample 1 (nodes +3, edges +2)
Args: batch: List of HData objects.
Returns: Tuple containing: - batched_edge_index: Concatenated and offset hyperedge indices, or None - batched_edge_attr: Concatenated hyperedge attributes, or None - total_edges: Total number of hyperedges across all batched samples
Source code in hyperbench/data/loader.py
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 | |