HeteroGraphData
¶
HeteroGraphData(
edge_index_dict: dict[Any, array],
node_features_dict: Optional[dict[str, array]] = None,
edge_features_dict: Optional[dict[Any, array]] = None,
graph_features: Optional[array] = None,
node_labels_dict: Optional[dict[str, array]] = None,
edge_labels_dict: Optional[dict[Any, array]] = None,
edge_labels_index_dict: Optional[dict[Any, array]] = None,
graph_labels: Optional[array] = None,
**kwargs,
)
Represents a graph structure with multiple node and edge types
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_index_dict
|
dict[Any, array]
|
A dictionary mapping edge types
to their corresponding edge indices. The edge indices are
represented as a 2D array of shape |
required |
node_features_dict
|
Optional[dict[str, array]]
|
A dictionary
mapping node types to their corresponding node feature. Each node
feature has shape |
None
|
edge_features_dict
|
Optional[dict[Any, array]]
|
A dictionary
mapping edge types to their corresponding edge feature matrices.
Each edge feature matrix has shape |
None
|
graph_features
|
Optional[array]
|
A 1D array containing graph-level features. |
None
|
node_labels_dict
|
Optional[dict[str, array]]
|
A dictionary mapping
node types to their corresponding node label arrays.
Each node label array has shape |
None
|
edge_labels_dict
|
Optional[dict[Any, array]]
|
A dictionary mapping
edge types to their corresponding edge label arrays.
Each edge label array has shape |
None
|
edge_labels_index_dict
|
Optional[dict[Any, array]]
|
A dictionary mapping edge types to their corresponding edge label index arrays. The edge label indices indicate the edges for which labels are available. |
None
|
graph_labels
|
Optional[array]
|
A 1D array containing graph-level labels. |
None
|
**kwargs
|
Additional keyword arguments to store custom attributes.
Pass |
{}
|
Example:
edge_index_dict = {
("user", "rates", "movie"): mx.array([[0, 1], [0, 1]]),
("movie", "rev_rates", "user"): mx.array([[0, 1], [0, 1]]),
}
node_features_dict = {
"user": mx.array([[0.2], [0.8]]),
"movie": mx.array([[0.5], [0.3]]),
}
data = HeteroGraphData(edge_index_dict, node_features_dict)
Methods:
| Name | Description |
|---|---|
to_dict |
Converts the Data object to a dictionary. |
metadata |
Returns the heterogeneous metadata |
to_homogeneous |
Converts the heterogeneous graph to a homogeneous |
to_csc_dict |
Converts every edge type to CSC format (edges grouped by destination). |
has_isolated_nodes |
Returns a boolean of whether the graph has isolated nodes of the |
has_self_loops |
Returns a boolean of whether the graph |
is_undirected |
Returns a boolean indicating whether all edge types in the graph are undirected. |
is_directed |
Returns a boolean of whether all |
Attributes:
| Name | Type | Description |
|---|---|---|
node_types |
list[str]
|
All node types: those with features, those appearing in any edge |
edge_types |
list[tuple]
|
All edge types |
num_nodes |
dict[str, int]
|
Number of nodes per node type. |
num_graph_features |
int
|
Returns the number of graph features. |
num_edges |
dict[Any, int]
|
dictionary of number of edges for each edge type in the graph. |
num_node_classes |
Union[dict[str, int], None]
|
Returns a dictionary of the number of node classes |
num_edge_classes |
dict[Any, int]
|
Returns a dictionary of the number of edge classes |
num_node_features |
dict[str, int]
|
Returns a dictionary of the number of node features for each node type. |
num_edge_features |
dict[str, int]
|
Returns a dictionary of the number of edge features for each edge type. |
Source code in mlx_graphs/data/data.py
node_types
property
¶
All node types: those with features, those appearing in any edge
type, and those listed in an optional num_nodes_dict attribute.
edge_types
property
¶
All edge types (src_node_type, relation, dst_node_type), in the
order of edge_index_dict.
num_nodes
property
¶
Number of nodes per node type.
For each node type, the first available source is used:
- an explicit
num_nodes_dictattribute, e.g.HeteroGraphData(..., num_nodes_dict={"conference": 20}), for node types without features; - the number of rows of its node features;
- the largest node id used by any edge type touching it, plus one.
num_edges
property
¶
dictionary of number of edges for each edge type in the graph.
num_node_classes
property
¶
Returns a dictionary of the number of node classes for each node type in the current graph.
num_edge_classes
property
¶
Returns a dictionary of the number of edge classes for each edge type in the current graph.
num_node_features
property
¶
Returns a dictionary of the number of node features for each node type.
num_edge_features
property
¶
Returns a dictionary of the number of edge features for each edge type.
to_dict
¶
to_dict() -> dict
Converts the Data object to a dictionary.
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary with all attributes of the |
metadata
¶
to_homogeneous
¶
to_homogeneous(
node_attrs: Optional[list[str]] = None,
edge_attrs: Optional[list[str]] = None,
add_node_type: bool = True,
add_edge_type: bool = True,
dummy_values: bool = True,
) -> GraphData
Converts the heterogeneous graph to a homogeneous GraphData,
as PyG's HeteroData.to_homogeneous().
Nodes are stacked by node type in the order of node_types, and
the edges of every edge type (in the order of edge_types) are
offset accordingly and concatenated.
Per-type attributes are the dict attributes named <name>_dict whose
keys are node types (e.g. node_features_dict,
node_labels_dict, train_mask_dict) or edge types (e.g.
edge_features_dict). They are merged into a single <name>
attribute (e.g. node_features) if every type has them with the
same number of dimensions and the same shape beyond the first one
(2D node attributes of different widths are zero-padded to the widest
one). With dummy_values, types missing an attribute are first
filled with NaN (floats), False (booleans) or -1
(integers), provided the types that have it agree on its shape.
If edge_labels_index_dict is set, edge_labels_dict is aligned
with it rather than with the edges: both are then offset and
concatenated into edge_labels_index and edge_labels.
Graph-level attributes (e.g. graph_labels) are copied as they are,
and the other dict attributes are dropped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_attrs
|
Optional[list[str]]
|
Names (without |
None
|
edge_attrs
|
Optional[list[str]]
|
Names (without |
None
|
add_node_type
|
bool
|
Whether to add a |
True
|
add_edge_type
|
bool
|
Whether to add an |
True
|
dummy_values
|
bool
|
Whether to fill missing attributes with dummy
values. Default: |
True
|
Returns:
| Type | Description |
|---|---|
GraphData
|
The homogeneous graph, with an explicit |
Example:
data = HeteroGraphData(
edge_index_dict={
("user", "rates", "movie"): mx.array([[0, 1], [0, 1]]),
},
node_features_dict={
"user": mx.array([[0.2], [0.8]]),
"movie": mx.array([[0.5], [0.3]]),
},
)
graph = data.to_homogeneous()
graph.edge_index
# mx.array([[0, 1], [2, 3]])
graph.node_type
# mx.array([0, 0, 1, 1])
Source code in mlx_graphs/data/data.py
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 | |
to_csc_dict
¶
Converts every edge type to CSC format (edges grouped by destination).
This is the input format of mlx_cluster.hetero_neighbor_sample.
Returns:
| Type | Description |
|---|---|
dict[Any, array]
|
A tuple |
type `(src, rel, dst)`
|
|
- `colptr_dict[et]`
|
|
- `row_dict[et]`
|
|
- `perm_dict[et]`
|
|
Source code in mlx_graphs/data/data.py
has_isolated_nodes
¶
Returns a boolean of whether the graph has isolated nodes of the given type (i.e., nodes that don't have a link to any other nodes)
Source code in mlx_graphs/data/data.py
has_self_loops
¶
has_self_loops() -> bool
Returns a boolean of whether the graph contains self loops for the given edge type.
Source code in mlx_graphs/data/data.py
is_undirected
¶
is_undirected() -> bool
Returns a boolean indicating whether all edge types in the graph are undirected.
Source code in mlx_graphs/data/data.py
from mlx_graphs.data.data import HeteroGraphData · mlx_graphs/data/data.py