Interchange: HIF, HyperGodot files and other Python libraries#

hatvis reads and writes the Hypergraph Interchange Format (HIF), the text format of HyperGodot, SPMF itemsets, CSV and Excel incidence data, and converts to and from HyperNetX, XGI and Hypergraphx. A round trip through every format must give back the same hypergraph.

import pathlib
import tempfile

import matplotlib.pyplot as plt

import hatvis as hv
from hatvis import io

H = hv.datasets.load_manufacturing_cps()
tmp = pathlib.Path(tempfile.mkdtemp())
# [snippet:roundtrip]
io.write_hif(H, tmp / "cps.hif.json")              # shared by HyperNetX, XGI, Hypergraphx, HAT
io.write_hypergodot(H, tmp / "cps.txt")             # "W1 C1 SW #WEIGHT: 15 #GROUP: H"
print((tmp / "cps.txt").read_text().splitlines()[0])
back = io.read_hif(tmp / "cps.hif.json")
print("HIF round trip identical:", back == H)
print("HyperGodot round trip, same hyperedges:", io.read_hypergodot(tmp / "cps.txt").edge_sets() == H.edge_sets())
# [/snippet:roundtrip]

# [snippet:libraries]
for lib in ("hypernetx", "xgi", "hypergraphx"):
    try:
        other = getattr(io, f"to_{lib}")(H)
        again = getattr(io, f"from_{lib}")(other)
        print(f"{lib:>11}: {type(other).__module__.split('.')[0]} object, "
              f"same sets = {sorted(map(sorted, again.edge_sets())) == sorted(map(sorted, H.edge_sets()))}")
    except ImportError:
        print(f"{lib:>11}: not installed")
# [/snippet:libraries]
W1 C1 SW #WEIGHT: 15 #GROUP: H
HIF round trip identical: True
HyperGodot round trip, same hyperedges: True
  hypernetx: hypernetx object, same sets = True
        xgi: xgi object, same sets = True
hypergraphx: hypergraphx object, same sets = True
fig, axes = plt.subplots(1, 2, figsize=(9, 3.2))
hv.plotting.plot_distributions(H, axes=axes)
plt.tight_layout()
plot 04 interchange

Total running time of the script: (0 minutes 0.575 seconds)

Gallery generated by Sphinx-Gallery