cfpq_data.graphs.readwrite.mtx#

Read (and write) a graph from (and to) a directory of MatrixMarket files.

Functions

filename_to_label(filename)

Returns the edge label of a MatrixMarket file name.

graph_from_mtx_dir(path)

Loads a graph from a directory of MatrixMarket files.

graph_to_mtx_dir(graph, path)

Saves the graph to a directory of MatrixMarket files by path.

label_to_filename(label)

Returns the canonical MatrixMarket file name of an edge label.

filename_to_label(filename: Path | str) str[source]#

Returns the edge label of a MatrixMarket file name.

The file name is the label itself (load_5.mtx holds the edges labeled load_5).

Parameters:
filenameUnion[Path, str]

The name of a MatrixMarket file.

Returns:
labelstr

The edge label of the file.

Examples

>>> filename_to_label("type.mtx")
'type'
>>> filename_to_label("alloc_r.mtx")
'alloc_r'
>>> filename_to_label("load_5.mtx")
'load_5'
graph_from_mtx_dir(path: Path | str) MultiDiGraph[source]#

Loads a graph from a directory of MatrixMarket files.

Each *.mtx file in the directory is a Boolean pattern matrix with one entry per edge of a single label (0-based indices, no values); the label is derived from the file name by filename_to_label().

Parameters:
pathUnion[Path, str]

The path to the directory with the MatrixMarket files.

Returns:
gMultiDiGraph

Loaded graph.

Examples

>>> import pathlib, tempfile
>>> d = pathlib.Path(tempfile.mkdtemp()) / "graph"
>>> _ = d.mkdir(parents=True)
>>> _ = (d / "a.mtx").write_text(
...     "%%MatrixMarket matrix coordinate pattern general\n"
...     "%%GraphBLAS type bool\n3 3 2\n0 1\n1 2\n"
... )
>>> _ = (d / "b_5.mtx").write_text(
...     "%%MatrixMarket matrix coordinate pattern general\n"
...     "%%GraphBLAS type bool\n3 3 1\n2 0\n"
... )
>>> g = graph_from_mtx_dir(d)
>>> sorted((u, v, e["label"]) for u, v, e in g.edges(data=True))
[(0, 1, 'a'), (1, 2, 'a'), (2, 0, 'b_5')]
graph_to_mtx_dir(graph: MultiDiGraph, path: Path | str) Path[source]#

Saves the graph to a directory of MatrixMarket files by path.

One file per edge label (the canonical name from label_to_filename()); each file is a Boolean pattern matrix with 0-based indices and no values. The node ids must be non-negative integers (the matrix dimensions are the largest node id plus one).

Parameters:
graphMultiDiGraph

Graph to save.

pathUnion[Path, str]

The path to the directory where the MatrixMarket files will be saved.

Returns:
pathPath

Path to the directory where the graph will be saved.

Examples

>>> import pathlib, tempfile
>>> g = nx.MultiDiGraph()
>>> _ = g.add_edges_from(
...     [(0, 1, {"label": "a"}), (1, 2, {"label": "a"}),
...      (2, 0, {"label": "b_5"})]
... )
>>> d = pathlib.Path(tempfile.mkdtemp())
>>> path = graph_to_mtx_dir(g, d / "graph")
>>> g2 = graph_from_mtx_dir(path)
>>> sorted((u, v, e["label"]) for u, v, e in g2.edges(data=True)) == \
...     sorted((u, v, e["label"]) for u, v, e in g.edges(data=True))
True
label_to_filename(label: str) str[source]#

Returns the canonical MatrixMarket file name of an edge label.

The canonical style is <label>.mtx; reading it back with filename_to_label() returns the same label.

Parameters:
labelstr

The edge label.

Returns:
filenamestr

The file name of the label.

Examples

>>> label_to_filename("load_5")
'load_5.mtx'
>>> label_to_filename("alloc_r")
'alloc_r.mtx'