flpq_data.graphs.converters#

Format conversion over edge streams.

Functions

convert_graph(src, dst, *, src_format, ...)

Converts a graph between formats over an edge stream.

convert_graph(src: Path | str | MultiDiGraph, dst: Path | str, *, src_format: str, dst_format: str, **writer_options: Any) → Path[source]#

Converts a graph between formats over an edge stream.

The conversion runs over a lazy (u, label, v) edge stream — one streaming reader and one streaming writer per format — without building an in-memory networkx.MultiDiGraph (the “Format conversion” section of docs/graphs/index.rst). Formats are explicit strings; the file extension is not inspected (an MTX directory is ambiguous with a single results.mtx file).

Parameters:
srcUnion[Path, str, MultiDiGraph]

The source: a path for the file formats (mtx, txt, rdf) or an in-memory graph for graph.

dstUnion[Path, str]

The destination path (a directory for mtx, a file otherwise).

src_formatstr

The format of src: one of mtx, txt, rdf, graph.

dst_formatstr

The format of dst: one of mtx, txt, rdf, g.

**writer_optionsAny

Forwarded to the destination writer (e.g. quoting=True for txt, dimension=... for mtx).

Returns:
pathPath

The resolved destination path.

Raises:
ValueError

If a format is unknown or the direction is unsupported (g is write-only, graph is read-only).

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"
... )
>>> out = pathlib.Path(tempfile.mkdtemp()) / "g.txt"
>>> _ = convert_graph(d, out, src_format="mtx", dst_format="txt")
>>> out.read_text()
'0 a 1\n1 a 2\n'