save_homograph

gli.io.save_homograph(name: str, edge: ndarray, num_nodes: int | None = None, node_attrs: List[Attribute] | None = None, edge_attrs: List[Attribute] | None = None, graph_node_list: spmatrix | None = None, graph_edge_list: spmatrix | None = None, graph_attrs: List[Attribute] | None = None, description: str = '', citation: str = '', save_dir: str = '.')

Save a homogeneous graph information to metadata.json and numpy data files.

Parameters:
  • name (str) – The name of the graph dataset.

  • edge (array) – An array of shape (num_edges, 2). Each row is an edge between the two nodes with the given node IDs.

  • num_nodes (int, optional) – The number of nodes in the graph, defaults to None. If not specified, the number of nodes will be inferred from edge.

  • node_attrs (list of Attribute, optional) – A list of attributes of the nodes, defaults to None.

  • edge_attrs (list of Attribute, optional) – A list of attributes of the edges, defaults to None.

  • graph_node_list ((sparse) array, optional) – An array of shape (num_graphs, num_nodes). Each row corresponds to a graph and each column corresponds to a node. The value of the element (i, j) is 1 if node j is in graph i, otherwise 0. If not specified, the graph will be considered as a single graph, defaults to None.

  • graph_edge_list ((sparse) array, optional) – An array of shape (num_graphs, num_edges). Each row corresponds to a graph and each column corresponds to an edge. The value of the element (i, j) is 1 if edge j is in graph i, otherwise 0. If not specified, the edges contained in each graph specified by graph_node_list will be considered as all the edges among the nodes in the graph, defaults to None.

  • graph_attrs (list of Attribute, optional) – A list of attributes of the graphs, defaults to None.

  • description (str, optional) – The description of the dataset, defaults to “”.

  • citation (str, optional) – The citation of the dataset, defaults to “”.

  • save_dir (str, optional) – The directory to save the numpy data files and metadata.json, defaults to “.”.

Returns:

The dictionary of the content in metadata.json.

Return type:

dict

Example

from gli.io import save_homograph, Attribute
import numpy as np
from numpy.random import randn, randint
from scipy.sparse import random as sparse_random

# Create a graph with 6 nodes and 5 edges.
edge = np.array([[0, 1], [1, 2], [2, 3], [3, 4], [4, 5]]) # Create attributes of the nodes.
dense_node_feats = Attribute(
    name="DenseNodeFeature", data=randn(6, 5),  # 6 nodes, 5 features
    description="Dense node features.")
sparse_node_feats = Attribute(
    name="SparseNodeFeature", data=sparse_random(6, 500),  # 6 nodes, 500 features
    description="Sparse node features.")
node_labels = Attribute(
    name="NodeLabel", data=randint(0, 4, 6),  # 6 nodes, 4 classes
    description="Node labels.")

# Save the graph dataset.
save_homograph(name="example_dataset",
           edge=edge, node_attrs=[dense_node_feats, sparse_node_feats, node_labels],
           description="An exampmle dataset.",
           citation="some bibtex citation")

The metadata.json and numpy data files will be saved in the current directory. And the metadata.json will look like something below.

{
    "description": "An exampmle dataset.",
    "data": {
        "Node": {
            "DenseNodeFeature": {
                "description": "Dense node features.",
                "type": "float",
                "format": "Tensor",
                "file": "example_dataset__graph__4b7f4a5f08ad24b27423daaa8d445238.npz",
                "key": "Node_DenseNodeFeature"
            },
            "SparseNodeFeature": {
                "description": "Sparse node features.",
                "type": "float",
                "format": "SparseTensor",
                "file": "example_dataset__graph__Node_SparseNodeFeature__118f9d2bbc457f9d64fe610a9510db1c.sparse.npz"
            },
            "NodeLabel": {
                "description": "Node labels.",
                "type": "int",
                "format": "Tensor",
                "file": "example_dataset__graph__4b7f4a5f08ad24b27423daaa8d445238.npz",
                "key": "Node_NodeLabel"
            }
        },
        "Edge": {
            "_Edge": {
                "file": "example_dataset__graph__4b7f4a5f08ad24b27423daaa8d445238.npz",
                "key": "Edge_Edge"
            }
        },
        "Graph": {
            "_NodeList": {
                "file": "example_dataset__graph__4b7f4a5f08ad24b27423daaa8d445238.npz",
                "key": "Graph_NodeList"
            }
        }
    },
    "citation": "some bibtex citation",
    "is_heterogeneous": false
}