flpq_data.graphs.converters#
Format conversion over edge streams.
Functions
|
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-memorynetworkx.MultiDiGraph(the “Format conversion” section ofdocs/graphs/index.rst). Formats are explicit strings; the file extension is not inspected (an MTX directory is ambiguous with a singleresults.mtxfile).- Parameters:
- srcUnion[Path, str, MultiDiGraph]
The source: a path for the file formats (
mtx,txt,rdf) or an in-memory graph forgraph.- dstUnion[Path, str]
The destination path (a directory for
mtx, a file otherwise).- src_formatstr
The format of
src: one ofmtx,txt,rdf,graph.- dst_formatstr
The format of
dst: one ofmtx,txt,rdf,g.- **writer_optionsAny
Forwarded to the destination writer (e.g.
quoting=Truefortxt,dimension=...formtx).
- Returns:
- pathPath
The resolved destination path.
- Raises:
- ValueError
If a format is unknown or the direction is unsupported (
gis write-only,graphis 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'