GraphData
¶
GraphData(
edge_index: array,
node_features: Optional[array] = None,
edge_features: Optional[array] = None,
graph_features: Optional[array] = None,
node_labels: Optional[array] = None,
edge_labels: Optional[array] = None,
graph_labels: Optional[array] = None,
**kwargs,
)
Represents a graph data object with optional features and labels.
PyG-style aliases are available for convenience: x for
node_features, edge_attr for edge_features and y for the
labels (see y). They can be read, assigned and passed to the
constructor (e.g. GraphData(edge_index, x=x, y=y)), but they are not
stored separately: only the mlx-graphs names appear in to_dict(),
keys() and in batches.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_index
|
array
|
edge index representing the topology of the graph, with shape [2, num_edges]. |
required |
node_features
|
Optional[array]
|
Array of shape [num_nodes, num_node_features] containing the features of each node. |
None
|
edge_features
|
Optional[array]
|
Array of shape [num_edges, num_edge_features] containing the features of each edge. |
None
|
graph_features
|
Optional[array]
|
Array of shape [num_graph_features] containing the global features of the graph. |
None
|
node_labels
|
Optional[array]
|
Array of shape [num_nodes, num_node_labels] containing the labels of each node. |
None
|
edge_labels
|
Optional[array]
|
Array of shape [num_edges, num_edge_labels] containing the labels of each edge. |
None
|
graph_labels
|
Optional[array]
|
Array of shape [1, num_graph_labels] containing the global labels of the graph. |
None
|
**kwargs
|
Additional keyword arguments to store any other custom attributes. |
{}
|
Methods:
| Name | Description |
|---|---|
to_dict |
Converts the Data object to a dictionary. |
keys |
Names of the attributes that are set (not |
clone |
Returns a deep copy of the graph, with copies of all the arrays. |
is_node_attr |
Whether the attribute |
is_edge_attr |
Whether the attribute |
subgraph |
Returns the subgraph induced by the nodes |
edge_subgraph |
Returns the subgraph made of the edges |
validate |
Checks the consistency of the graph. |
is_coalesced |
Whether |
coalesce |
Sorts |
__cat_dim__ |
This method can be overriden when batching is used with custom attributes. |
__inc__ |
This method can be overriden when batching is used with custom attributes. |
has_isolated_nodes |
Returns a boolean of whether the graph has isolated nodes or not |
has_self_loops |
Returns a boolean of whether the graph contains self loops. |
is_undirected |
Returns a boolean of whether the graph is undirected or not. |
is_directed |
Returns a boolean of whether the graph is directed or not. |
Attributes:
| Name | Type | Description |
|---|---|---|
num_nodes |
int
|
Number of nodes in the graph. |
num_edges |
int
|
Number of edges in the graph |
num_node_classes |
int
|
Returns the number of node classes in the current graph. |
num_edge_classes |
int
|
Returns the number of edge classes in the current graph. |
num_graph_classes |
int
|
Returns the number of graph classes in the current graph. |
num_node_features |
int
|
Returns the number of node features. |
num_edge_features |
int
|
Returns the number of edge features. |
num_graph_features |
int
|
Returns the number of graph features. |
x |
Optional[array]
|
PyG-style alias of |
edge_attr |
Optional[array]
|
PyG-style alias of |
y |
Optional[array]
|
PyG-style alias of the labels of the graph. |
num_features |
int
|
PyG-style alias of |
Source code in mlx_graphs/data/data.py
num_nodes
property
writable
¶
num_nodes: int
Number of nodes in the graph.
The first available source is used:
- an explicit count, e.g.
GraphData(edge_index, num_nodes=4); - the number of rows of
node_features; - the number of rows of
pos(node positions, e.g. point clouds); - the number of unique node ids in
edge_index.
The last fallback cannot see isolated nodes (nodes without any edge), so
graphs without node features that may contain isolated nodes should set
num_nodes explicitly. Otherwise batching offsets will be wrong.
num_node_classes
property
¶
num_node_classes: int
Returns the number of node classes in the current graph.
num_edge_classes
property
¶
num_edge_classes: int
Returns the number of edge classes in the current graph.
num_graph_classes
property
¶
num_graph_classes: int
Returns the number of graph classes in the current graph.
x
property
writable
¶
x: Optional[array]
PyG-style alias of node_features (readable and assignable).
edge_attr
property
writable
¶
edge_attr: Optional[array]
PyG-style alias of edge_features (readable and assignable).
y
property
writable
¶
y: Optional[array]
PyG-style alias of the labels of the graph.
Reading y returns node_labels if they are set, and
graph_labels otherwise (None if neither is set). Edge labels
are never returned.
Assigning y replaces the labels that reading y returns. If the
graph has no labels yet, the value becomes node_labels if it has
one entry per node and graph_labels otherwise, which is the rule
used by from_pyg(). On a single-node graph, a
one-entry y is ambiguous and becomes graph_labels, so that
graph classification datasets with single-node graphs keep the same
attributes in every graph (and can be batched): set node_labels
directly for the node labels of a single-node graph. Assigning
None clears the labels that reading y returned.
to_dict
¶
to_dict() -> dict
Converts the Data object to a dictionary.
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary with all attributes of the |
keys
¶
Names of the attributes that are set (not None).
Private attributes are omitted, and "num_nodes" is included when
the number of nodes was set explicitly. PyG-style aliases (x,
edge_attr, y) are not listed, only the attributes they refer
to.
Returns:
| Type | Description |
|---|---|
list[str]
|
The list of attribute names. |
Source code in mlx_graphs/data/data.py
is_node_attr
¶
Whether the attribute key holds one entry per node.
An array attribute is per-node if its first dimension is the number of nodes. If the graph has as many nodes as edges, attributes with "edge" in their name are considered per-edge instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Name of the attribute (PyG-style aliases are accepted). |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in mlx_graphs/data/data.py
is_edge_attr
¶
Whether the attribute key holds one entry per edge (excluding
edge_index itself).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Name of the attribute (PyG-style aliases are accepted). |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in mlx_graphs/data/data.py
subgraph
¶
subgraph(subset: IndexLike) -> GraphData
Returns the subgraph induced by the nodes subset.
The edges between nodes of subset are kept and relabeled, and every
node-level and edge-level attribute (see is_node_attr() and
is_edge_attr()) is sliced accordingly. Other attributes are
shared with the original graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subset
|
IndexLike
|
The nodes to keep, as an array (or list) of node indices or
as a boolean mask of shape |
required |
Returns:
| Type | Description |
|---|---|
GraphData
|
A new graph with |
GraphData
|
for a mask). |
Example:
graph = GraphData(
edge_index=mx.array([[0, 1, 2], [1, 2, 3]]),
node_features=mx.array([[0.0], [1.0], [2.0], [3.0]]),
)
sub = graph.subgraph(mx.array([1, 2, 3]))
sub.edge_index
# mx.array([[0, 1], [1, 2]])
sub.node_features
# mx.array([[1.0], [2.0], [3.0]])
Source code in mlx_graphs/data/data.py
edge_subgraph
¶
edge_subgraph(subset: IndexLike) -> GraphData
Returns the subgraph made of the edges subset.
Every node is kept, even if it becomes isolated, and every edge-level
attribute (see is_edge_attr()) is sliced accordingly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subset
|
IndexLike
|
The edges to keep, as an array (or list) of edge indices
or as a boolean mask of shape |
required |
Returns:
| Type | Description |
|---|---|
GraphData
|
A new graph with the selected edges. |
Example:
graph = GraphData(
edge_index=mx.array([[0, 1, 2], [1, 2, 3]]),
edge_features=mx.array([[0.0], [1.0], [2.0]]),
)
sub = graph.edge_subgraph(mx.array([0, 2]))
sub.edge_index
# mx.array([[0, 2], [1, 3]])
sub.num_nodes
# 4
Source code in mlx_graphs/data/data.py
validate
¶
Checks the consistency of the graph.
The following properties are checked:
edge_indexhas shape[2, num_edges]and an integer dtype;edge_indexhas no negative node id, and no node id greater than or equal to the number of nodes when it is known (set explicitly or given bynode_features);- node-level default attributes (
node_features,node_labels) have one entry per node, if the number of nodes is set explicitly; edge_featuresandedge_labelshave one entry per edge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raise_on_error
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in mlx_graphs/data/data.py
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 | |
is_coalesced
¶
is_coalesced() -> bool
Whether edge_index is sorted by source then destination node and
contains no duplicate edge.
Source code in mlx_graphs/data/data.py
coalesce
¶
Sorts edge_index by source then destination node and merges
duplicate edges.
Every edge-level attribute (see is_edge_attr(), e.g.
edge_features, edge_weights) follows the edges, and the values
of merged edges are combined with reduce. The original graph is
left unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reduce
|
str
|
How the attributes of merged edges are combined: |
'add'
|
Returns:
| Type | Description |
|---|---|
GraphData
|
A coalesced copy of the graph (the graph itself if it is already |
GraphData
|
coalesced). |
Example:
graph = GraphData(
edge_index=mx.array([[1, 0, 1], [0, 1, 0]]),
edge_features=mx.array([[1.0], [2.0], [3.0]]),
)
graph = graph.coalesce()
graph.edge_index
# mx.array([[0, 1], [1, 0]])
graph.edge_features
# mx.array([[2.0], [4.0]])
Source code in mlx_graphs/data/data.py
__cat_dim__
¶
This method can be overriden when batching is used with custom attributes.
Given the name of a custom attribute key, returns the dimension where the
concatenation happens during batching.
By default, all attribute names containing "index" (and the mesh
triangles face) will be concatenated on axis 1, e.g. edge_index.
Other attributes are concatenated on axis 0,
e.g. node features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Name of the attribute on which change the default concatenation dimension while using batching. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The dimension where concatenation will happen when batching. |
Source code in mlx_graphs/data/data.py
__inc__
¶
This method can be overriden when batching is used with custom attributes.
Given the name of a custom attribute key, returns an integer indicating the
incrementing value to apply to the elemnts of the attribute.
By default, all attribute names containing "index" (and face) will be
incremented based on
the number of nodes in previous batches to avoid duplicate nodes in the index,
e.g. edge_index. Other attributes are cnot incremented and keep their original
values, e.g. node features.
If incrementation is not used, the return value should be set to None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Name of the attribute on which change the default incrementation behavior while using batching. |
required |
Returns:
| Type | Description |
|---|---|
Union[int, None]
|
Incrementing value for the given attribute or None. |
Source code in mlx_graphs/data/data.py
has_isolated_nodes
¶
has_isolated_nodes() -> bool
Returns a boolean of whether the graph has isolated nodes or not (i.e., nodes that don't have a link to any other nodes)
from mlx_graphs.data.data import GraphData · mlx_graphs/data/data.py