Read and Write

[ ]:
import random

import xgi

Importing and exporting hypergraph data

When working with empirical hypergraph data, the following file formats representing hypergraphs are commonly seen in practice: * A hyperedge list, where each line represents a hyperedge * A bipartite edge list where each line contains two entries: a node ID and an edge ID * An incidence matrix

The readwrite module provides functionality to import and export these file formats.

Example hypergraph

[2]:
n = 10
m = 10

min_edge_size = 2
max_edge_size = 10

# hyperedge list
hyperedge_list = [
    random.sample(range(n), random.choice(range(min_edge_size, max_edge_size + 1)))
    for i in range(m)
]
H = xgi.Hypergraph(hyperedge_list)

JSON

These functions import and export the hypergraph to a standardized JSON format.

[3]:
# Write the example hypergraph to a JSON file
xgi.write_json(H, "hypergraph_json.json")
# Load the file just written and store it in a new hypergraph
H_json = xgi.read_json("hypergraph_json.json")

Edge List

These functions import and export the hypergraph as an edge list, with user specified delimiters.

[4]:
# Write the hypergraph to a file as a hyperedge list
xgi.write_edgelist(H, "hyperedge_list.csv", delimiter=",")
# Read the file just written as a new hypergraph
H_el = xgi.read_edgelist("hyperedge_list.csv", delimiter=",", nodetype=int)

Bipartite Edge List

These functions import and export the hypergraph as a bipartite edge list with user-specified delimiters.

[5]:
# Write the hypergraph as a bipartite edge list
xgi.write_bipartite_edgelist(H, "bipartite_edge_list.csv", delimiter=",")
# Read the file just written as a new hypergraph
H_bel = xgi.read_bipartite_edgelist(
    "bipartite_edge_list.csv", delimiter=",", nodetype=int
)