mindspore_gl.graph.MindHomoGraph

class mindspore_gl.graph.MindHomoGraph[source]

Build homo graph.

Supported Platforms:

Ascend GPU

Examples

>>> import numpy as np
>>> import networkx
>>> from scipy.sparse import csr_matrix
>>> from mindspore_gl.graph import MindHomoGraph, CsrAdj
>>> node_count = 20
>>> edge_prob = 0.1
>>> graph = networkx.generators.random_graphs.fast_gnp_random_graph(node_count, edge_prob)
>>> edge_array = np.transpose(np.array(list(graph.edges)))
>>> row = edge_array[0]
>>> col = edge_array[1]
>>> data = np.ones(row.shape)
>>> csr_mat = csr_matrix((data, (row, col)), shape=(node_count, node_count))
>>> generated_graph = MindHomoGraph()
>>> node_dict = {idx: idx for idx in range(node_count)}
>>> edge_count = col.shape[0]
>>> edge_ids = np.array(list(range(edge_count))).astype(np.int32)
>>> generated_graph.set_topo(CsrAdj(csr_mat.indptr.astype(np.int32), csr_mat.indices.astype(np.int32)),
... node_dict, edge_ids)
>>> print(generated_graph.neighbors(0))
# results will be random for suffle
[10 14]
property adj_coo

COO adj matrix.

Returns

  • numpy.ndarray, coo graph.

Examples

>>> #dataset is an instance object of Dataset
>>> adj_coo = graph.adj_coo
property adj_csr

CSR adj matrix.

Returns

  • mindspore_gl.graph.csr_adj, CSR graph.

Examples

>>> #dataset is an instance object of Dataset
>>> adj_csr = graph.adj_csr
property batch_meta: mindspore_gl.graph.graph.BatchMeta

Batched graph meta info.

Returns

  • mindspore_gl.graph.BatchMeta, batched graph meta info.

Examples

>>> #dataset is an instance object of Dataset
>>> batch_meta = graph.batch_meta
degree(node)[source]

Query With node degree.

Parameters

node (int) – node index.

Returns

  • int, degree of node.

property edge_count

Edge count of graph.

Returns

  • int, edge numbers.

Examples

>>> #dataset is an instance object of Dataset
>>> edge_count = graph.edge_count
property is_batched: bool

If the graph be batched.

Returns

  • bool, the graph be batched return True, else return False.

Examples

>>> #dataset is an instance object of Dataset
>>> is_batched = graph.is_batched
neighbors(node)[source]

Query neighbors nodes.

Parameters

node (int) – node index.

Returns

  • numpy.ndarray, sampled node.

property node_count

Node count of graph.

Returns

  • int, nodes numbers.

Examples

>>> #dataset is an instance object of Dataset
>>> node_count = graph.node_count
set_topo(adj_csr: CsrAdj, node_dict, edge_ids: np.ndarray)[source]

Initialize CSR Graph.

Parameters
  • adj_csr (CsrAdj) – adjacency matrix of graph, CSR type.

  • node_dict (dict) – node ID dict.

  • edge_ids (numpy.ndarray) – array of edges.

set_topo_coo(adj_coo, node_dict=None, edge_ids: np.ndarray = None)[source]

Initialize COO Graph.

Parameters
  • adj_coo (numpy.ndarray) – adjacency matrix of graph, COO type.

  • node_dict (dict, optional) – node ID dict. Default: None.

  • edge_ids (numpy.ndarray, optional) – array of edges. Default: None.