Graph¶
In this package, the class Graph is the core data representation.
It is used to represent contexts \(x\) (i.e input data),
decisions \(y\) (i.e. output data), and gradients \(\nabla_y f\).
A Graph is composed of multiple HyperEdgeSet objects,
each defined by a series of ports and features.
The class Graph can represent both a single graph instance or a batch of
graphs. The backend (NumPy or JAX) is controlled by passing a NumpyBackend or
JaxBackend instance at construction time.
Note
Here is a typical instance of Graph.
>>> print(graph)
Mass
ports features
node_id weight x y z
object_id
0 0.0 5.322265 0.202435 0.202435 0.242032
1 1.0 3.496568 0.962326 0.962326 0.306690
2 2.0 3.535864 0.060886 0.060886 0.094170
3 3.0 7.213709 0.984766 0.984766 0.068853
Spring
ports features
node1_id node2_id k
object_id
0 0.0 1.0 0.020424
1 1.0 2.0 0.037591
2 2.0 3.0 0.045405
Registry
[0. 1. 2. 3.]
Graph¶
- class Graph(*, backend=None, hyper_edge_sets, true_shape, current_shape, non_fictitious_addresses)[source]¶
Bases:
dictHyper Heterogeneous Multi-Graph (H2MG) container.
Stores hyper-edge sets, shapes, and address masks for single or batched graphs. All array operations are delegated to the provided backend.
- Parameters:
backend (Backend | None) – Array backend (
NumpyBackendorJaxBackend).hyper_edge_sets (dict[str, HyperEdgeSet]) – Dict of hyper-edge sets.
true_shape (GraphShape) – True shape, unaffected by padding.
current_shape (GraphShape) – Current shape, consistent with padding.
non_fictitious_addresses – 1 for real addresses, 0 otherwise.
Build a Graph from a dict of HyperEdgeSet and a number of addresses. |
|
Save this graph to a pickle file. |
|
Load a graph from a pickle file. |
|
True if all hyper-edge sets are batched and the address mask is 2-D. |
|
True if all hyper-edge sets are single and the address mask is 1-D. |
|
Concatenated flat features of all hyper-edge sets. |
|
Pad hyper-edge sets and address mask to |
|
Remove padding to restore the true shape. |
|
Count connected components and return per-address component labels. |
|
Add |
|
Compute quantiles of all hyper-edge set features. |
HyperEdgeSet¶
- class HyperEdgeSet(*, backend=None, port_dict, feature_array, feature_names, non_fictitious)[source]¶
Bases:
dictA collection of hyper-edges of the same class, optionally batched.
Internally this is a dict storing four entries. All array operations are delegated to the provided backend, making instances transparent to both NumPy and JAX pipelines.
- Parameters:
backend (Backend | None) – Array backend (
NumpyBackendorJaxBackend).port_dict (dict | None) – Mapping from a port name to an integer address array of shape
(n_edges,)or(batch, n_edges).feature_array – Array that contains all hyper-edge features.
feature_names (dict | None) – Dictionary from feature names to index in
feature_array.non_fictitious – Mask array set to 1 for real objects and 0 for fictitious ones.
Build a HyperEdgeSet from raw dicts of ports and features. |
|
Concatenate (features, ports) along the last axis. |
|
True if |
|
True if |
|
Number of hyper-edges per instance. |
|
Number of batches; valid only when |
|
Stacked port array of shape |
|
Maps port name to column index in |
|
Unstack |
|
Flatten all features into one long vector per |
|
Pad a single HyperEdgeSet to |
|
Remove padding to restore |
|
Add |
GraphShape¶
- class GraphShape(*, backend=None, hyper_edge_sets, addresses)[source]¶
Bases:
dictRepresents the shape of a graph: per-class object counts and registry size.
- Parameters:
backend (Backend | None) – Array backend.
hyper_edge_sets (dict) – Dict mapping hyper-edge class name to count array.
addresses – Number of addresses in the graph.
Build a GraphShape from a hyper-edge set dictionary and a non-fictitious mask. |
|
Serialize GraphShape to a JSON-friendly dict. |
|
Deserialize GraphShape from a JSON-friendly dictionary. |
|
Return the element-wise maximum of two GraphShape objects. |
|
Return the element-wise sum of two GraphShape objects. |
|
Concatenated hyper-edge-set shape values as a single array. |
|
True if the array is 1-D. |
|
True if the array is 2-D. |
|
Return the batch size; raises if not batched. |
Graph, hyper-edge set, and shape manipulation functions¶
The following functions help to manipulate graphs, hyper-edge sets, shapes objects and to proceed operations on them.
Collate a list of Graphs into a single batched Graph (all must share |
|
Concatenate multiple single Graphs into one single Graph (no new batch dim). |
|
Extract summary statistics from each feature array in the graph. |
|
Split a batched Graph into a list of single Graphs (reverses |
|
Validate that the mapping is a dict of HyperEdgeSet instances. |
|
Collate a list of HyperEdgeSet into a single batched HyperEdgeSet. |
|
Concatenate several single HyperEdgeSet into one single HyperEdgeSet (no new batch dim). |
|
Split a batched HyperEdgeSet into its constituent HyperEdgeSet instances. |
|
Ensure all arrays in a dict share the same last-axis size. |
|
Return a scalar int32 NumPy array with the number of hyper-edges. |
|
Stack a sorted dict of NumPy arrays into a single array along the last axis. |
|
Validate that the input is a dict or None. |
|
Ensure there are no NaN values in port or feature arrays. |
|
Batch a list of GraphShape into one batched GraphShape. |
|
Return the element-wise maximum over a list of GraphShape objects. |
|
Split a batched GraphShape into individual GraphShape instances. |
|
Return the element-wise sum over a list of GraphShape objects. |
|
Converts a NumPy array, JAX array, or tuple of values into a NumPy array preserving its dtype, or converts the values in a dictionary accordingly. |
|
Convert NumPy arrays or dictionary of NumPy arrays to JAX arrays. |
|
Convert JAX arrays or mappings of JAX arrays back to NumPy arrays. |