Skip to content

Commit d524493

Browse files
committed
feat: added write_graph_graphml()
1 parent 4eeddf7 commit d524493

File tree

9 files changed

+116
-8
lines changed

9 files changed

+116
-8
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Writes the graph in plain edge list format to an output stream.
2+
3+
THe plain edge list format records the structure of the graph _only_ and the
4+
vertices of the graph will be referred to as numeric vertex IDs instead of
5+
vertex names.
6+
7+
See `write_graph_ncol()` if you have vertex names and you want to use them
8+
in the output file instead of IDs.
9+
10+
Parameters:
11+
graph: the graph to write
12+
outstream: the output file or stream to write the graph to. May be a
13+
filename, a path-like object or a file-like object if it is backed
14+
by a low-level file handle
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Writes the graph in GraphML format to an output stream.
2+
3+
The GraphML format preserves numeric, string and boolean attributes.
4+
5+
Parameters:
6+
graph: the graph to write
7+
outstream: the output file or stream to write the graph to. May be a
8+
filename, a path-like object or a file-like object if it is backed
9+
by a low-level file handle
10+
prefixattr: whether to put a prefix in front of the attribute names to
11+
ensure uniqueness if the graph has vertex and edge (or graph)
12+
attributes with the same name

poetry.lock

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pytest-cov = "^4.0.0"
1616
richbench = "^1.0.3"
1717
igraph = "^0.10.6"
1818
stimulus = { git = "https://github.com/igraph/stimulus.git", tag = "0.15.0" }
19+
pytest-datadir = "^1.4.1"
1920

2021
[tool.poetry.group.doc.dependencies]
2122
mkdocs-material = "^9.1.21"
@@ -59,6 +60,7 @@ python =
5960
deps =
6061
pytest
6162
pytest-cov
63+
pytest-datadir
6264
commands = pytest tests
6365
6466
[testenv:type]

src/igraph_ctypes/_internal/functions.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5083,7 +5083,21 @@ def read_graph_dl(instream: FileLike, directed: bool = True) -> Graph:
50835083

50845084

50855085
def write_graph_edgelist(graph: Graph, outstream: FileLike) -> None:
5086-
"""Type-annotated wrapper for ``igraph_write_graph_edgelist``."""
5086+
"""Writes the graph in plain edge list format to an output stream.
5087+
5088+
THe plain edge list format records the structure of the graph _only_ and the
5089+
vertices of the graph will be referred to as numeric vertex IDs instead of
5090+
vertex names.
5091+
5092+
See `write_graph_ncol()` if you have vertex names and you want to use them
5093+
in the output file instead of IDs.
5094+
5095+
Parameters:
5096+
graph: the graph to write
5097+
outstream: the output file or stream to write the graph to. May be a
5098+
filename, a path-like object or a file-like object if it is backed
5099+
by a low-level file handle
5100+
"""
50875101
# Create exit stack for graceful cleanup
50885102
with ExitStack() as py__stack:
50895103

@@ -5142,7 +5156,19 @@ def write_graph_leda(graph: Graph, outstream: FileLike, names: str = "name", wei
51425156

51435157

51445158
def write_graph_graphml(graph: Graph, outstream: FileLike, prefixattr: bool = True) -> None:
5145-
"""Type-annotated wrapper for ``igraph_write_graph_graphml``."""
5159+
"""Writes the graph in GraphML format to an output stream.
5160+
5161+
The GraphML format preserves numeric, string and boolean attributes.
5162+
5163+
Parameters:
5164+
graph: the graph to write
5165+
outstream: the output file or stream to write the graph to. May be a
5166+
filename, a path-like object or a file-like object if it is backed
5167+
by a low-level file handle
5168+
prefixattr: whether to put a prefix in front of the attribute names to
5169+
ensure uniqueness if the graph has vertex and edge (or graph)
5170+
attributes with the same name
5171+
"""
51465172
# Create exit stack for graceful cleanup
51475173
with ExitStack() as py__stack:
51485174

src/igraph_ctypes/io.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from ._internal.functions import write_graph_edgelist
1+
from ._internal.functions import write_graph_edgelist, write_graph_graphml
22

3-
__all__ = ("write_graph_edgelist",)
3+
__all__ = ("write_graph_edgelist", "write_graph_graphml")

tests/test_write_graph.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from pytest import fixture, raises
33

44
from igraph_ctypes.constructors import create_graph_from_edge_list
5-
from igraph_ctypes.io import write_graph_edgelist
5+
from igraph_ctypes.io import write_graph_edgelist, write_graph_graphml
66

77

88
@fixture
@@ -11,9 +11,9 @@ def simple_graph():
1111
return g
1212

1313

14-
def test_write_graph_edgelist(simple_graph, tmp_path):
14+
def test_write_graph_edgelist(simple_graph, tmp_path, datadir):
1515
path: Path = tmp_path / "edges.txt"
16-
expected = "0 1\n1 2\n1 3\n2 3\n"
16+
expected = (datadir / "simple_graph.txt").read_text()
1717
path_str = str(path)
1818

1919
# Write to a file specified by its filename
@@ -48,3 +48,14 @@ def test_write_graph_edgelist(simple_graph, tmp_path):
4848
with raises(OSError):
4949
with path.open("r") as fp:
5050
write_graph_edgelist(simple_graph, fp)
51+
52+
53+
def test_write_graph_graphml(simple_graph, tmp_path, datadir):
54+
path: Path = tmp_path / "edges.graphml"
55+
expected = (datadir / "simple_graph.graphml").read_text()
56+
path_str = str(path)
57+
58+
# Write to a file specified by its filename
59+
write_graph_graphml(simple_graph, path_str)
60+
assert path.read_text() == expected
61+
path.unlink()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
5+
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
6+
<!-- Created by igraph -->
7+
<graph id="G" edgedefault="undirected">
8+
<node id="n0">
9+
</node>
10+
<node id="n1">
11+
</node>
12+
<node id="n2">
13+
</node>
14+
<node id="n3">
15+
</node>
16+
<edge source="n0" target="n1">
17+
</edge>
18+
<edge source="n1" target="n2">
19+
</edge>
20+
<edge source="n2" target="n3">
21+
</edge>
22+
<edge source="n1" target="n3">
23+
</edge>
24+
</graph>
25+
</graphml>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0 1
2+
1 2
3+
1 3
4+
2 3

0 commit comments

Comments
 (0)