galax.heterograph.HeteroGraph

class galax.heterograph.HeteroGraph(gidx: Optional[galax.heterograph_index.HeteroGraphIndex] = None, node_frames: Optional[NamedTuple] = None, edge_frames: Optional[NamedTuple] = None, metamap: Optional[flax.core.frozen_dict.FrozenDict] = None, graph_frame: Optional[flax.core.frozen_dict.FrozenDict] = None)[source]

Bases: tuple

Class for storing graph structure and node/edge feature data.

Parameters
  • gidx (Optional[HeteroGraphIndex]) – Graph index object.

  • ntypes (Optional[Sequence[str]]) – Node type list. ntypes[i] stores the name of node type i. If a pair is given, the graph created is a uni-directional bipartite graph, and its SRC node types and DST node types are given as in the pair.

  • etypes (Optional[Sequence[str]]) – Edge type list. etypes[i] stores the name of edge type i.

  • node_frames (list[Frame]) – Node feature storage. If None, empty frame is created. Otherwise, node_frames[i] stores the node features of node type i. (default: None)

  • edge_frames (list[Frame], optional) – Edge feature storage. If None, empty frame is created. Otherwise, edge_frames[i] stores the edge features of edge type i. (default: None)

__init__()

Methods

__init__()

add_edges(u, v[, data, etype, srctype, dsttype])

Add multiple new edges for the specified edge type The i-th new edge will be from u[i] to v[i].

add_nodes(num[, data, ntype])

Add new nodes of the same node type

add_self_loop([etype])

Add self loop given etype.

adj([transpose, etype])

Return the adjacency matrix of edges of the given edge type. By default, a row of returned adjacency matrix represents the source of an edge and the column represents the destination. When transpose is True, a row represents the destination and a column represents the source. :Parameters: * transpose (bool, optional) -- A flag to transpose the returned adjacency matrix. (Default: False) * ctx (context, optional) -- The context of returned adjacency matrix. (Default: cpu) * scipy_fmt (str, optional) -- If specified, return a scipy sparse matrix in the given format. Otherwise, return a backend dependent sparse tensor. (Default: None) * etype (str or (str, str, str), optional) -- The type names of the edges. The allowed type name formats are: * (str, str, str) for source node type, edge type and destination node type. * or one str edge type name if the name can uniquely identify a triplet format in the graph. Can be omitted if the graph has only one type of edges.

adjacency_matrix([transpose, etype])

Return the adjacency matrix of edges of the given edge type. By default, a row of returned adjacency matrix represents the source of an edge and the column represents the destination. When transpose is True, a row represents the destination and a column represents the source. :Parameters: * transpose (bool, optional) -- A flag to transpose the returned adjacency matrix. (Default: False) * ctx (context, optional) -- The context of returned adjacency matrix. (Default: cpu) * scipy_fmt (str, optional) -- If specified, return a scipy sparse matrix in the given format. Otherwise, return a backend dependent sparse tensor. (Default: None) * etype (str or (str, str, str), optional) -- The type names of the edges. The allowed type name formats are: * (str, str, str) for source node type, edge type and destination node type. * or one str edge type name if the name can uniquely identify a triplet format in the graph. Can be omitted if the graph has only one type of edges.

apply_edges(apply_function[, etype])

Alias to function.apply_edges.

apply_nodes(apply_function[, ntype])

Alias to function.apply_nodes.

batched_num_nodes([ntype])

Return the batched number of nodes.

canonical_etypes()

Return all the canonical edge types in the graph.

count(value, /)

Return number of occurrences of value.

find_edges(eid[, etype])

Return the source and destination node ID(s) given the edge ID(s). :Parameters: * eid (edge ID(s)) -- The edge IDs. The allowed formats are: * int: A single ID. * Int Tensor: Each element is an ID. The tensor must have the same device type and ID data type as the graph's. * iterable[int]: Each element is an ID. * etype (str or (str, str, str), optional) -- The type names of the edges. The allowed type name formats are: * (str, str, str) for source node type, edge type and destination node type. * or one str edge type name if the name can uniquely identify a triplet format in the graph. Can be omitted if the graph has only one type of edges.

from_dgl(graph)

Construct a heterograph from dgl.DGLGraph

get_etype_id([etype])

Return the id of the given edge type.

get_meta_edge(eid)

get_ntype_id([ntype])

Return the ID of the given node type.

has_edges_between(u, v[, etype])

Return whether the graph contains the given edges.

has_nodes(vid, ntype)

Return whether the graph contains the given nodes.

in_degrees([v, etype])

Return the in-degree(s) of the given nodes.

inc(typestr[, etype])

Return the incidence matrix representation of edges with the given edge type. An incidence matrix is an n-by-m sparse matrix, where n is the number of nodes and m is the number of edges. Each nnz value indicating whether the edge is incident to the node or not. There are three types of incidence matrices \(I\): * in: - \(I[v, e] = 1\) if \(e\) is the in-edge of \(v\) (or \(v\) is the dst node of \(e\)); - \(I[v, e] = 0\) otherwise. * out: - \(I[v, e] = 1\) if \(e\) is the out-edge of \(v\) (or \(v\) is the src node of \(e\)); - \(I[v, e] = 0\) otherwise. * both (only if source and destination node type are the same): - \(I[v, e] = 1\) if \(e\) is the in-edge of \(v\); - \(I[v, e] = -1\) if \(e\) is the out-edge of \(v\); - \(I[v, e] = 0\) otherwise (including self-loop). :Parameters: * typestr (str) -- Can be either in, out or both * ctx (context, optional) -- The context of returned incidence matrix. (Default: cpu) * etype (str or (str, str, str), optional) -- The type names of the edges. The allowed type name formats are: * (str, str, str) for source node type, edge type and destination node type. * or one str edge type name if the name can uniquely identify a triplet format in the graph. Can be omitted if the graph has only one type of edges.

incidence_matrix(typestr[, etype])

Return the incidence matrix representation of edges with the given edge type. An incidence matrix is an n-by-m sparse matrix, where n is the number of nodes and m is the number of edges. Each nnz value indicating whether the edge is incident to the node or not. There are three types of incidence matrices \(I\): * in: - \(I[v, e] = 1\) if \(e\) is the in-edge of \(v\) (or \(v\) is the dst node of \(e\)); - \(I[v, e] = 0\) otherwise. * out: - \(I[v, e] = 1\) if \(e\) is the out-edge of \(v\) (or \(v\) is the src node of \(e\)); - \(I[v, e] = 0\) otherwise. * both (only if source and destination node type are the same): - \(I[v, e] = 1\) if \(e\) is the in-edge of \(v\); - \(I[v, e] = -1\) if \(e\) is the out-edge of \(v\); - \(I[v, e] = 0\) otherwise (including self-loop). :Parameters: * typestr (str) -- Can be either in, out or both * ctx (context, optional) -- The context of returned incidence matrix. (Default: cpu) * etype (str or (str, str, str), optional) -- The type names of the edges. The allowed type name formats are: * (str, str, str) for source node type, edge type and destination node type. * or one str edge type name if the name can uniquely identify a triplet format in the graph. Can be omitted if the graph has only one type of edges.

index(value[, start, stop])

Return first index of value.

init([gidx, ntypes, etypes, node_frames, ...])

is_homogeneous()

Return whether the graph is a homogeneous graph.

is_multigraph()

Return whether the graph is a multigraph with parallel edges.

is_not_dummy([ntype])

Return the mask where the nodes are not dummy.

number_of_edges([etype])

Return the number of nodes with ntype.

number_of_nodes([ntype])

Return the number of nodes with ntype.

out_degrees([u, etype])

Return the out-degree(s) of the given nodes.

remove_edges([eids, etype])

Remove multiple edges with the specified edge type Nodes will not be removed.

remove_nodes(nids[, ntype])

Remove multiple nodes with the specified node type Edges that connect to the nodes will be removed as well.

remove_self_loop([etype])

Add self loop given etype.

set_edata(key, data[, etype])

Set edge data.

set_gdata(key, data)

Set global data.

set_ndata(key, data[, ntype])

Set node data.

sum_nodes([field, ntype, dummy])

Sum the representations of nodes.

to_canonincal_etype(etype)

Convert an edge type to the corresponding canonical edge type in the graph.

update_all(mfunc, rfunc[, afunc, etype])

Alias to core.message_passing.

Attributes

dstdata

Return a node data view for setting/getting destination node features.

edata

Edge data.

edge_frames

Alias for field number 2

edges

etypes

gdata

Graph data.

gidx

Alias for field number 0

graph_frame

Alias for field number 4

metamap

Alias for field number 3

ndata

Node data.

node_frames

Alias for field number 1

nodes

ntypes

srcdata

Return a node data view for setting/getting source node features.

add_edges(u: jax._src.numpy.ndarray.ndarray, v: jax._src.numpy.ndarray.ndarray, data: Optional[jax._src.numpy.ndarray.ndarray] = None, etype: Optional[str] = None, srctype: Optional[str] = None, dsttype: Optional[str] = None)[source]

Add multiple new edges for the specified edge type The i-th new edge will be from u[i] to v[i].

Parameters
  • u (jnp.ndarray) – Source node IDs, u[i] gives the source node for the i-th new edge.

  • v (jnp.ndarray) – Destination node IDs, ` v[i]` gives the destination node for the i-th new edge.

  • data (dict, optional) – Feature data of the added edges. The i-th row of the feature data corresponds to the i-th new edge.

  • etype (str or tuple of str, optional) – The type of the new edges. Can be omitted if there is only one edge type in the graph.

Notes

  • If the key of data does not contain some existing feature fields, those features for the new edges will be filled with zeros.

  • If the key of data contains new feature fields, those features for the old edges will be filled with zeros.

Examples

>>> g = graph(((0, 1), (1, 2)))
>>> g.number_of_edges()
2
>>> g = g.add_nodes(2)
>>> g = g.add_edges((1, 3), (0, 1))
>>> g.number_of_edges()
4

If the graph has some edge features and new edges are added without features, their features will be create. >>> g = g.set_edata(“h”, jnp.ones((4, 1))) >>> g = g.add_edges((1, ), (1, )) >>> g.edata[‘h’].flatten().tolist() [1.0, 1.0, 1.0, 1.0, 0.0]

We can also assign features for the new edges in adding new edges. >>> g = g.add_edges(jnp.array([0, 0]), jnp.array([2, 2]), … {‘h’: jnp.array([[1.], [2.]]), ‘w’: jnp.ones((2, 1))}) >>> g.edata[‘h’].flatten().tolist() [1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 2.0]

Since data contains new feature fields, the features for old edges will be created. >>> g.edata[‘w’].flatten().tolist() [0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0]

Heterogeneous Graphs with Multiple Edge Types >>> g = graph({ … (‘user’, ‘plays’, ‘game’): ((0, 1, 1, 2), … (0, 0, 1, 1)), … (‘developer’, ‘develops’, ‘game’): ((0, 1), … (0, 1)) … }) >>> g.number_of_edges(‘plays’) 4 >>> g = g.add_edges(jnp.array([2]), jnp.array([1]), etype=’plays’) >>> g.number_of_edges(‘plays’) 5

add_nodes(num: int, data: Optional[dict] = None, ntype: Optional[dict] = None)[source]

Add new nodes of the same node type

Parameters
  • num (int) – Number of nodes to add.

  • data (Mapping, optional) – Feature data of the added nodes.

  • ntype (str, optional) – The type of the new nodes. Can be omitted if there is only one node type in the graph.

Examples

Homogeneous Graphs or Heterogeneous Graphs with A Single Node Type >>> g = graph(((0, 1), (1, 2))) >>> int(g.number_of_nodes()) 3 >>> g = g.add_nodes(2) >>> int(g.number_of_nodes()) 5

If the graph has some node features and new nodes are added without features, their features will be zeros. >>> g = g.set_ndata(“h”, jnp.ones((5, 1))) >>> g.ndata[“h”].flatten().tolist() [1.0, 1.0, 1.0, 1.0, 1.0]

>>> g = g.add_nodes(1)
>>> g.ndata["h"].flatten().tolist()
[1.0, 1.0, 1.0, 1.0, 1.0, 0.0]

We can also assign features for the new nodes in adding new nodes. >>> g = g.add_nodes(1, {‘h’: jnp.ones((1, 1)), ‘w’: jnp.ones((1, 1))}) >>> g.ndata[‘h’].flatten().tolist() [1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0] >>> g.ndata[‘w’].flatten().tolist() [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0]

Heterogeneous Graphs with Multiple Node Types >>> g = graph({ … (‘user’, ‘plays’, ‘game’): (jnp.array([0, 1, 1, 2]), … jnp.array([0, 0, 1, 1])), … (‘developer’, ‘develops’, ‘game’): (jnp.array([0, 1]), … jnp.array([0, 1])) … }) >>> g.number_of_nodes(“user”).item() 3 >>> g = g.add_nodes(2, ntype=”user”) >>> g.number_of_nodes(“user”).item() 5

add_self_loop(etype: Optional[str] = None)[source]

Add self loop given etype.

Parameters

etype (Optional[str] = None) – Edge type.

Returns

The resulting graph.

Return type

HeteroGraph

Examples

>>> g = graph(((0, 1), (1, 2)))
>>> g = g.add_self_loop()
>>> g.number_of_edges()
5
>>> src, dst = g.edges()
>>> src.tolist(), dst.tolist()
([0, 1, 0, 1, 2], [1, 2, 0, 1, 2])
adj(transpose: bool = False, etype: Optional[str] = None)

Return the adjacency matrix of edges of the given edge type. By default, a row of returned adjacency matrix represents the source of an edge and the column represents the destination. When transpose is True, a row represents the destination and a column represents the source. :Parameters: * transpose (bool, optional) – A flag to transpose the returned adjacency matrix. (Default: False)

  • ctx (context, optional) – The context of returned adjacency matrix. (Default: cpu)

  • scipy_fmt (str, optional) – If specified, return a scipy sparse matrix in the given format. Otherwise, return a backend dependent sparse tensor. (Default: None)

  • etype (str or (str, str, str), optional) – The type names of the edges. The allowed type name formats are: * (str, str, str) for source node type,

    edge type and destination node type.

    • or one str edge type name if the name can uniquely identify a triplet format in the graph.

    Can be omitted if the graph has only one type of edges.

Returns

Adjacency matrix.

Return type

SparseTensor or scipy.sparse.spmatrix

adjacency_matrix(transpose: bool = False, etype: Optional[str] = None)[source]

Return the adjacency matrix of edges of the given edge type. By default, a row of returned adjacency matrix represents the source of an edge and the column represents the destination. When transpose is True, a row represents the destination and a column represents the source. :Parameters: * transpose (bool, optional) – A flag to transpose the returned adjacency matrix. (Default: False)

  • ctx (context, optional) – The context of returned adjacency matrix. (Default: cpu)

  • scipy_fmt (str, optional) – If specified, return a scipy sparse matrix in the given format. Otherwise, return a backend dependent sparse tensor. (Default: None)

  • etype (str or (str, str, str), optional) – The type names of the edges. The allowed type name formats are: * (str, str, str) for source node type,

    edge type and destination node type.

    • or one str edge type name if the name can uniquely identify a triplet format in the graph.

    Can be omitted if the graph has only one type of edges.

Returns

Adjacency matrix.

Return type

SparseTensor or scipy.sparse.spmatrix

apply_edges(apply_function: Callable, etype: Optional[str] = None)[source]

Alias to function.apply_edges.

apply_nodes(apply_function: Callable, ntype: Optional[str] = None)[source]

Alias to function.apply_nodes.

batched_num_nodes(ntype: Optional[str] = None) jax._src.numpy.ndarray.ndarray[source]

Return the batched number of nodes.

Parameters

ntype (Optional[str])

Returns

The number of nodes, batched.

Return type

jnp.ndarray

canonical_etypes()[source]

Return all the canonical edge types in the graph.

A canonical edge type is a string triplet (str, str, str) for source node type, edge type and destination node type.

Returns

All the canonical edge type triplets in a list.

Return type

list[(str, str, str)]

Notes

DGL internally assigns an integer ID for each edge type. The returned edge type names are sorted according to their IDs.

See also

etypes

count(value, /)

Return number of occurrences of value.

property dstdata

Return a node data view for setting/getting destination node features. Let g be a Graph.

Parameters

etype (Optional[str]) – Edge type.

Examples

>>> g = graph({
...     ('user', 'plays', 'game'): ([0, 1], [1, 2])})
>>> g = g.set_ndata("h", jnp.ones(3), "game")
>>> g.dstdata["h"].flatten().tolist()
[1.0, 1.0]
property edata

Edge data.

property edge_frames

Alias for field number 2

find_edges(eid: jax._src.numpy.ndarray.ndarray, etype=None)[source]

Return the source and destination node ID(s) given the edge ID(s). :Parameters: * eid (edge ID(s)) – The edge IDs. The allowed formats are:

  • int: A single ID.

  • Int Tensor: Each element is an ID. The tensor must have the same device type and ID data type as the graph’s.

  • iterable[int]: Each element is an ID.

  • etype (str or (str, str, str), optional) – The type names of the edges. The allowed type name formats are: * (str, str, str) for source node type,

    edge type and destination node type.

    • or one str edge type name if the name can uniquely identify a triplet format in the graph.

    Can be omitted if the graph has only one type of edges.

Returns

  • Tensor – The source node IDs of the edges. The i-th element is the source node ID of the i-th edge.

  • Tensor – The destination node IDs of the edges. The i-th element is the destination node ID of the i-th edge.

classmethod from_dgl(graph)[source]

Construct a heterograph from dgl.DGLGraph

Parameters

graph (dgl.DGLGraph) – Input graph.

Returns

The resulting graph.

Return type

HeteroGraph

property gdata

Graph data.

get_etype_id(etype: Optional[str] = None) int[source]

Return the id of the given edge type. etype can also be None. If so, there should be only one edge type in the graph.

Parameters

etype (str or tuple of str) – Edge type

Returns

Return type

int

get_ntype_id(ntype: Optional[str] = None) int[source]

Return the ID of the given node type. ntype can also be None. If so, there should be only one node type in the graph.

Parameters

ntype (str) – Node type

Returns

Return type

int

property gidx

Alias for field number 0

property graph_frame

Alias for field number 4

has_edges_between(u: jax._src.numpy.ndarray.ndarray, v: jax._src.numpy.ndarray.ndarray, etype: Optional[str] = None) jax._src.numpy.ndarray.ndarray[source]

Return whether the graph contains the given edges.

Parameters
  • u (node IDs) – The source node IDs of the edges. The allowed formats are: * int: A single node. * Int Tensor: Each element is a node ID. * iterable[int]: Each element is a node ID.

  • v (node IDs) – The destination node IDs of the edges. The allowed formats are: * int: A single node. * Int Tensor: Each element is a node ID. * iterable[int]: Each element is a node ID.

  • etype (str or (str, str, str), optional) – The type names of the edges. The allowed type name formats are: * (str, str, str) for source node type,

    edge type and destination node type.

    • or one str edge type name if the name can uniquely identify a triplet format in the graph.

    Can be omitted if the graph has only one type of edges.

Returns

A tensor of bool flags where each element is True if the node is in the graph. If the input is a single node, return one bool value.

Return type

bool or bool Tensor

has_nodes(vid: jax._src.numpy.ndarray.ndarray, ntype: Optional[str]) jax._src.numpy.ndarray.ndarray[source]

Return whether the graph contains the given nodes.

Parameters
  • vid (node ID(s)) – The nodes IDs. The allowed nodes ID formats are: * int: The ID of a single node. * Int Tensor: Each element is a node ID. * iterable[int]: Each element is a node ID.

  • ntype (str, optional) – The node type name. Can be omitted if there is only one type of nodes in the graph.

Returns

A tensor of bool flags where each element is True if the node is in the graph. If the input is a single node, return one bool value.

Return type

bool or bool Tensor

in_degrees(v: Optional[jax._src.numpy.ndarray.ndarray] = None, etype: Optional[str] = None)[source]

Return the in-degree(s) of the given nodes. It computes the in-degree(s) w.r.t. to the edges of the given edge type.

Parameters
  • v (node IDs) – The node IDs. The allowed formats are: * int: A single node. * Int Tensor: Each element is a node ID.

    The tensor must have the same device type and ID data type as the graph’s.

    • iterable[int]: Each element is a node ID.

    If not given, return the in-degrees of all the nodes.

  • etype (str or (str, str, str), optional) – The type name of the edges. The allowed type name formats are: * (str, str, str) for source node type,

    edge type and destination node type.

    • or one str edge type name if the name can uniquely identify a triplet format in the graph.

    Can be omitted if the graph has only one type of edges.

Returns

The in-degree(s) of the node(s) in a Tensor. The i-th element is the in-degree of the i-th input node. If v is an int, return an int too.

Return type

int or Tensor

inc(typestr: str, etype: Optional[str] = None)

Return the incidence matrix representation of edges with the given edge type. An incidence matrix is an n-by-m sparse matrix, where n is the number of nodes and m is the number of edges. Each nnz value indicating whether the edge is incident to the node or not. There are three types of incidence matrices \(I\): * in:

  • \(I[v, e] = 1\) if \(e\) is the in-edge of \(v\) (or \(v\) is the dst node of \(e\));

  • \(I[v, e] = 0\) otherwise.

  • out:
    • \(I[v, e] = 1\) if \(e\) is the out-edge of \(v\) (or \(v\) is the src node of \(e\));

    • \(I[v, e] = 0\) otherwise.

  • both (only if source and destination node type are the same):
    • \(I[v, e] = 1\) if \(e\) is the in-edge of \(v\);

    • \(I[v, e] = -1\) if \(e\) is the out-edge of \(v\);

    • \(I[v, e] = 0\) otherwise (including self-loop).

Parameters
  • typestr (str) – Can be either in, out or both

  • ctx (context, optional) – The context of returned incidence matrix. (Default: cpu)

  • etype (str or (str, str, str), optional) – The type names of the edges. The allowed type name formats are: * (str, str, str) for source node type,

    edge type and destination node type.

    • or one str edge type name if the name can uniquely identify a triplet format in the graph.

    Can be omitted if the graph has only one type of edges.

Returns

The incidence matrix.

Return type

Framework SparseTensor

incidence_matrix(typestr: str, etype: Optional[str] = None)[source]

Return the incidence matrix representation of edges with the given edge type. An incidence matrix is an n-by-m sparse matrix, where n is the number of nodes and m is the number of edges. Each nnz value indicating whether the edge is incident to the node or not. There are three types of incidence matrices \(I\): * in:

  • \(I[v, e] = 1\) if \(e\) is the in-edge of \(v\) (or \(v\) is the dst node of \(e\));

  • \(I[v, e] = 0\) otherwise.

  • out:
    • \(I[v, e] = 1\) if \(e\) is the out-edge of \(v\) (or \(v\) is the src node of \(e\));

    • \(I[v, e] = 0\) otherwise.

  • both (only if source and destination node type are the same):
    • \(I[v, e] = 1\) if \(e\) is the in-edge of \(v\);

    • \(I[v, e] = -1\) if \(e\) is the out-edge of \(v\);

    • \(I[v, e] = 0\) otherwise (including self-loop).

Parameters
  • typestr (str) – Can be either in, out or both

  • ctx (context, optional) – The context of returned incidence matrix. (Default: cpu)

  • etype (str or (str, str, str), optional) – The type names of the edges. The allowed type name formats are: * (str, str, str) for source node type,

    edge type and destination node type.

    • or one str edge type name if the name can uniquely identify a triplet format in the graph.

    Can be omitted if the graph has only one type of edges.

Returns

The incidence matrix.

Return type

Framework SparseTensor

index(value, start=0, stop=9223372036854775807, /)

Return first index of value.

Raises ValueError if the value is not present.

is_homogeneous()[source]

Return whether the graph is a homogeneous graph. A homogeneous graph only has one node type and one edge type.

Returns

True if the graph is a homogeneous graph.

Return type

bool

is_multigraph()[source]

Return whether the graph is a multigraph with parallel edges. A multigraph has more than one edges between the same pair of nodes, called parallel edges. For heterogeneous graphs, parallel edge further requires the canonical edge type to be the same (see canonical_etypes() for the definition).

Returns

True if the graph is a multigraph.

Return type

bool

is_not_dummy(ntype: Optional[str] = None)[source]

Return the mask where the nodes are not dummy.

Parameters

ntype (Optional[str]) – Node type.

Returns

A boolean ask indicating whether a node is dummy.

Return type

jnp.ndarray

Examples

>>> g = graph(((0, 1), (1, 2)))
>>> import galax
>>> g = galax.pad(g, 5, 8)
>>> g.is_not_dummy().tolist()
[True, True, True, False, False]
property metamap

Alias for field number 3

property ndata

Node data.

property node_frames

Alias for field number 1

number_of_edges(etype: Optional[str] = None)[source]

Return the number of nodes with ntype.

Parameters

etype (str) – Edge type.

Returns

Number of edges.

Return type

int

number_of_nodes(ntype: Optional[str] = None)[source]

Return the number of nodes with ntype.

Parameters

ntype (str) – Node type.

Returns

Number of nodes.

Return type

int

Examples

out_degrees(u: Optional[jax._src.numpy.ndarray.ndarray] = None, etype: Optional[str] = None)[source]

Return the out-degree(s) of the given nodes. It computes the out-degree(s) w.r.t. to the edges of the given edge type.

Parameters
  • u (node IDs) – The node IDs. The allowed formats are: * int: A single node. * Int Tensor: Each element is a node ID.

    The tensor must have the same device type and ID data type as the graph’s.

    • iterable[int]: Each element is a node ID.

    If not given, return the in-degrees of all the nodes.

  • etype (str or (str, str, str), optional) – The type names of the edges. The allowed type name formats are: * (str, str, str) for source node type, edge type

    and destination node type.

    • or one str edge type name if the name can uniquely identify a triplet format in the graph.

    Can be omitted if the graph has only one type of edges.

Returns

The out-degree(s) of the node(s) in a Tensor. The i-th element is the out-degree of the i-th input node. If v is an int, return an int too.

Return type

int or Tensor

remove_edges(eids: Optional[jax._src.numpy.lax_numpy.array] = None, etype: Optional[str] = None)[source]

Remove multiple edges with the specified edge type Nodes will not be removed. After removing edges, the rest edges will be re-indexed using consecutive integers from 0, with their relative order preserved. The features for the removed edges will be removed accordingly.

Parameters
  • eids (int, tensor, numpy.ndarray, list) – IDs for the edges to remove.

  • etype (str or tuple of str, optional) – The type of the edges to remove. Can be omitted if there is only one edge type in the graph.

Examples

>>> g = graph(((0, 0, 2), (0, 1, 2)))
>>> g = g.set_edata("he", jnp.array([0.0, 1.0, 2.0]))
>>> g = g.remove_edges((0, 1))
>>> int(g.number_of_edges())
1

Heterogeneous Graphs with Multiple Edge Types >>> g = graph({ … (‘user’, ‘plays’, ‘game’): ([0, 1, 1, 2], [0, 0, 1, 1]), … (‘developer’, ‘develops’, ‘game’): ([0, 1], [0, 1]), … })

>>> g = g.remove_edges([0, 1], 'plays')
>>> int(g.number_of_edges("plays"))
2
remove_nodes(nids: jax._src.numpy.ndarray.ndarray, ntype: Optional[str] = None)[source]

Remove multiple nodes with the specified node type Edges that connect to the nodes will be removed as well. After removing nodes and edges, the rest nodes and edges will be re-indexed using consecutive integers from 0, with their relative order preserved. The features for the removed nodes/edges will be removed accordingly.

Parameters
  • nids (int, tensor, numpy.ndarray, list) – Nodes to remove.

  • ntype (str, optional) – The type of the nodes to remove. Can be omitted if there is only one node type in the graph.

Notes

  • This does not remove the etype entirely.

Examples

Homogeneous Graphs or Heterogeneous Graphs with A Single Node Type >>> g = graph(([0, 0, 2], [0, 1, 2])) >>> g = g.set_ndata(“hv”, jnp.array([0.0, 1.0, 2.0])) >>> g = g.set_edata(“he”, jnp.array([0.0, 1.0, 2.0])) >>> g = g.remove_nodes((0, 1)) >>> int(g.number_of_nodes()) 1

>>> g.ndata["hv"].flatten().tolist()
[2.0]
>>> g.edata["he"].flatten().tolist()
[2.0]

Heterogeneous Graphs with Multiple Node Types >>> g = graph({ … (‘user’, ‘plays’, ‘game’): ([0, 1, 1, 2], [0, 0, 1, 1]), … (‘developer’, ‘develops’, ‘game’): ([0, 1], [0, 1]) … }) >>> g = g.remove_nodes([0, 1], ntype=’game’) >>> g.number_of_nodes(‘user’).item() 3 >>> ‘game’ in g.ntypes False

remove_self_loop(etype: Optional[str] = None)[source]

Add self loop given etype.

Parameters

etype (Optional[str] = None) – Edge type.

Returns

The resulting graph.

Return type

HeteroGraph

Examples

>>> g = graph(((0, 1, 0, 1, 2), (1, 2, 0, 1, 2)))
>>> g = g.remove_self_loop()
>>> g.number_of_edges()
2
>>> src, dst = g.edges()
>>> src.tolist(), dst.tolist()
([0, 1], [1, 2])
set_edata(key, data, etype=None)[source]

Set edge data.

Parameters
  • key (str) – Name of the data field.

  • data (jnp.array) – Node data.

  • etype (str) – Edge type.

Returns

A new graph with ndata.

Return type

HeteroGraph

Examples

>>> g = graph(((0, 1), (1, 2)))
>>> g = g.set_edata('h', jnp.zeros(3))
set_gdata(key, data)[source]

Set global data.

Parameters
  • key (str) – Name of the data field.

  • data (jnp.array) – Node data.

Returns

A new graph with gdata.

Return type

HeteroGraph

Examples

>>> g = graph(((0, 1), (1, 2)))
>>> g = g.set_gdata('h', jnp.zeros(3))
>>> g.graph_frame['h'].tolist()
[0.0, 0.0, 0.0]
set_ndata(key, data, ntype=None)[source]

Set node data.

Parameters
  • key (str) – Name of the data field.

  • data (jnp.array) – Node data.

  • ntype (str) – Node type.

Returns

A new graph with ndata.

Return type

HeteroGraph

Examples

>>> g = graph(((0, 1), (1, 2)))
>>> g = g.set_ndata('h', jnp.zeros(3))
property srcdata

Return a node data view for setting/getting source node features. Let g be a Graph.

Parameters

etype (Optional[str]) – Edge type.

Examples

>>> g = graph({
...     ('user', 'plays', 'game'): ([0, 1], [1, 2])})
>>> g = g.set_ndata("h", jnp.ones(2), "user")
>>> g.srcdata["h"].flatten().tolist()
[1.0, 1.0]
sum_nodes(field: str = 'h', ntype: Optional[str] = None, dummy: Optional[bool] = None)[source]

Sum the representations of nodes.

Parameters
  • field (str = “h”) – Ndata field.

  • ntype (Optional[str] = None) – Node type.

  • dummy (Optional[bool] = None) – Whether dummy graph is present.

Returns

The resulting summed data.

Return type

jnp.ndarray

Examples

>>> g = graph(((0, 1), (1, 2)))
>>> g = g.ndata.set("h", jnp.ones(3))
>>> float(g.sum_nodes())
3.0
>>> import galax
>>> _g = galax.batch([g, g])
>>> _g.sum_nodes().tolist()
[3.0, 3.0]
>>> _g = galax.pad(_g, 8, 9)
>>> _g.sum_nodes().tolist()
[3.0, 3.0]
>>> _sum_nodes = jax.jit(lambda _g: _g.sum_nodes())
>>> _sum_nodes(_g).tolist()
[3.0, 3.0]
to_canonincal_etype(etype: str) Tuple[str][source]

Convert an edge type to the corresponding canonical edge type in the graph.

A canonical edge type is a string triplet (str, str, str) for source node type, edge type and destination node type. The function expects the given edge type name can uniquely identify a canonical edge type.

Parameters

etype (str) – If etype is an edge type (str), it returns the corresponding canonical edge type in the graph.

Returns

The canonical edge type corresponding to the edge type.

Return type

(str, str, str)

update_all(mfunc: Optional[Callable], rfunc: Optional[galax.function.ReduceFunction], afunc: Optional[Callable] = None, etype: Optional[Callable] = None)[source]

Alias to core.message_passing.

Parameters
  • mfunc (Callable) – Message function.

  • rfunc (Callable) – Reduce function.

  • afunc (Callable) – Apply function.

Returns

The resulting graph.

Return type

HeteroGraph

Examples

>>> import galax
>>> import jax
>>> import jax.numpy as jnp
>>> g = galax.graph(((0, 1), (1, 2)))
>>> g = g.ndata.set("h", jnp.ones(3))
>>> mfunc = galax.function.copy_u("h", "m")
>>> rfunc = galax.function.sum("m", "h1")
>>> _g = g.update_all(mfunc, rfunc)
>>> _g.ndata['h1'].flatten().tolist()
[0.0, 1.0, 1.0]