flpq_data.graphs.utils.to_g_text#

Generate FastMatrixCFPQ .g graph files from edge streams.

Functions

g_text_from_edges(edges, path)

Writes an edge stream to a FastMatrixCFPQ .g file.

graph_dir_to_g_text(path)

Generates a FastMatrixCFPQ .g file text from a directory of mtx files.

g_text_from_edges(edges: Iterable[Tuple[Any, str, Any]], path: Path | str) → Path[source]#

Writes an edge stream to a FastMatrixCFPQ .g file.

The file holds the u v label lines with auto-generated reverse edges (see graph_dir_to_g_text() for the format); the memory stays O(1) in the number of edges.

Parameters:
edgesIterable[Tuple[Any, str, Any]]

The (u, label, v) edge tuples to write.

pathUnion[Path, str]

The path to the .g file where the edges will be saved.

Returns:
pathPath

Path to the .g file where the edges will be saved.

Examples

>>> import pathlib, tempfile
>>> p = pathlib.Path(tempfile.mkdtemp()) / "g.g"
>>> _ = g_text_from_edges([(0, "a", 1)], p)
>>> p.read_text()
'0 1 a\n1 0 a_r\n'
graph_dir_to_g_text(path: Path | str) → str[source]#

Generates a FastMatrixCFPQ .g file text from a directory of mtx files.

Each *.mtx file contributes forward edges; reverse edges are auto-generated with the _reverse_label convention. Indexed files (X_N.mtx) produce 4-column lines with label X_i and index N.

Parameters:
pathUnion[Path, str]

Directory containing MatrixMarket files.

Returns:
textstr

The .g file content.

Examples

>>> import pathlib, tempfile
>>> d = pathlib.Path(tempfile.mkdtemp())
>>> _ = (d / "a.mtx").write_text(
...     "%%MatrixMarket matrix coordinate pattern general\n"
...     "%%GraphBLAS type bool\n3 3 1\n0 1\n"
... )
>>> _ = (d / "b_5.mtx").write_text(
...     "%%MatrixMarket matrix coordinate pattern general\n"
...     "%%GraphBLAS type bool\n3 3 1\n2 0\n"
... )
>>> text = graph_dir_to_g_text(d)
>>> lines = text.strip().splitlines()
>>> len(lines)
4
>>> "0 1 a" in lines
True
>>> "1 0 a_r" in lines
True
>>> "2 0 b_i 5" in lines
True
>>> "0 2 b_r_i 5" in lines
True