Skip to content

Commit eb78442

Browse files
authored
Merge pull request #42 from petercorke/refactor/laplacian-lowercase
refactor: rename Laplacian() to laplacian(), deprecating the old name
2 parents 6d07258 + ccf2987 commit eb78442

7 files changed

Lines changed: 75 additions & 14 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Graphs belong to the class `UGraph` or `DGraph` for undirected or directed graph
8585

8686
***
8787
- `g.adjacency()` adjacency matrix
88-
- `g.Laplacian()` Laplacian matrix
88+
- `g.laplacian()` Laplacian matrix
8989
- `g.incidence()` incidence matrix
9090

9191
### Properties and methods of a vertex

‎docs/source/matrices.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ Convert graph to matrix form
44
These methods convert a graph to a matrix representation .
55

66
.. autoclass:: pgraph.PGraph._BaseGraph
7-
:members: distance, adjacency, degree, incidence, Laplacian
7+
:members: distance, adjacency, degree, incidence, laplacian

‎src/pgraph/PGraph.py‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,14 +1966,14 @@ def average_degree(self) -> float:
19661966

19671967
# MATRIX REPRESENTATIONS
19681968

1969-
def Laplacian(self) -> NDArray:
1969+
def laplacian(self) -> NDArray:
19701970
"""
19711971
Laplacian matrix for the graph
19721972
19731973
:return: Laplacian matrix
19741974
:rtype: NumPy ndarray
19751975
1976-
``g.Laplacian()`` is the Laplacian matrix (NxN) of the graph where N
1976+
``g.laplacian()`` is the Laplacian matrix (NxN) of the graph where N
19771977
is the number of vertices.
19781978
19791979
.. runblock:: pycon
@@ -1987,7 +1987,7 @@ def Laplacian(self) -> NDArray:
19871987
>>> for i, j in [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (3, 4)]:
19881988
... g.add_edge(g[i], g[j])
19891989
...
1990-
>>> L = g.Laplacian()
1990+
>>> L = g.laplacian()
19911991
>>> print(L)
19921992
19931993
.. note::
@@ -2023,6 +2023,24 @@ def Laplacian(self) -> NDArray:
20232023
"""
20242024
return np.diag([v.out_degree for v in self]) - self.adjacency()
20252025

2026+
def Laplacian(self) -> NDArray:
2027+
"""
2028+
Laplacian matrix for the graph (deprecated)
2029+
2030+
.. deprecated:: 1.0
2031+
Use :meth:`laplacian` instead. A capitalized name denotes a class
2032+
method that constructs a graph, like :meth:`Adjacency` and
2033+
:meth:`Dict`; this is an instance method that returns a matrix.
2034+
2035+
:seealso: :meth:`laplacian`
2036+
"""
2037+
warnings.warn(
2038+
"Laplacian() is deprecated, use laplacian() instead",
2039+
DeprecationWarning,
2040+
stacklevel=2,
2041+
)
2042+
return self.laplacian()
2043+
20262044
def connectivity(self, vertices: Iterable[BaseVertex] | None = None) -> list[int]:
20272045
"""
20282046
Graph connectivity
@@ -2094,7 +2112,7 @@ def degree(self) -> NDArray:
20942112
For a ``DGraph`` this was the out-degree matrix, counting only the
20952113
edges leaving each vertex. It is now the total, in plus out. The
20962114
old matrix is ``np.diag([v.out_degree for v in g])``, which is what
2097-
:meth:`Laplacian` uses.
2115+
:meth:`laplacian` uses.
20982116
20992117
.. runblock:: pycon
21002118
@@ -2116,7 +2134,7 @@ def degree(self) -> NDArray:
21162134
add ``k`` to the degree of each end. A self-loop adds 2, for both
21172135
kinds of graph, since it has two ends.
21182136
2119-
:seealso: :meth:`adjacency` :meth:`incidence` :meth:`Laplacian`
2137+
:seealso: :meth:`adjacency` :meth:`incidence` :meth:`laplacian`
21202138
:meth:`connectivity` :meth:`average_degree` :attr:`BaseVertex.degree`
21212139
"""
21222140

@@ -2166,7 +2184,7 @@ def adjacency(self) -> NDArray:
21662184
(consistent with :meth:`degree`) and 1 for a ``DGraph``. In every case
21672185
each row of ``A`` sums to the degree of that vertex.
21682186
2169-
:seealso: :meth:`Laplacian` :meth:`incidence` :meth:`degree`
2187+
:seealso: :meth:`laplacian` :meth:`incidence` :meth:`degree`
21702188
"""
21712189
# create a dict mapping vertex to an id
21722190
vdict = {}
@@ -2222,7 +2240,7 @@ def incidence(self) -> NDArray:
22222240
so row sums still equal :meth:`degree`); for a ``DGraph`` it holds a
22232241
single 1.
22242242
2225-
:seealso: :meth:`Laplacian` :meth:`adjacency` :meth:`degree`
2243+
:seealso: :meth:`laplacian` :meth:`adjacency` :meth:`degree`
22262244
"""
22272245
edges = self.edges()
22282246
I = np.zeros((self.n, len(edges)))

‎tests/test_graph.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ def test_matrices(self):
588588
D = g.degree()
589589
nt.assert_array_equal(D, np.diag([2, 1, 1, 0]))
590590

591-
L = g.Laplacian()
591+
L = g.laplacian()
592592
expected_L = np.diag([2, 1, 1, 0]) - expected_A
593593
nt.assert_array_equal(L, expected_L)
594594

@@ -902,7 +902,7 @@ def test_matrices(self):
902902

903903
# the directed Laplacian uses the OUT-degree, D_out - A, whose rows
904904
# sum to zero; it is not degree() - A for a directed graph
905-
L = g.Laplacian()
905+
L = g.laplacian()
906906
expected_L = np.diag([2, 1, 0]) - expected_A
907907
nt.assert_array_equal(L, expected_L)
908908
nt.assert_array_equal(L.sum(axis=1), np.zeros(3))

‎tests/test_in_edges.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def test_average_degree(self):
6565

6666
def test_laplacian_still_has_zero_row_sums(self):
6767
# a directed Laplacian uses the OUT-degree: D_out - A
68-
L = self.g.Laplacian()
68+
L = self.g.laplacian()
6969
np.testing.assert_allclose(L.sum(axis=1), 0)
7070
np.testing.assert_array_equal(
7171
L, np.diag([v.out_degree for v in self.g]) - self.g.adjacency()

‎tests/test_laplacian_name.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""``laplacian()`` is canonical; ``Laplacian()`` is a deprecated alias."""
2+
3+
import unittest
4+
import warnings
5+
6+
import numpy as np
7+
8+
from pgraph import DGraph, UGraph
9+
10+
11+
class _Name:
12+
cls: type
13+
14+
def setUp(self):
15+
self.g = self.cls()
16+
a, b, c = (self.g.add_vertex() for _ in range(3))
17+
self.g.add_edge(a, b)
18+
self.g.add_edge(b, c)
19+
20+
def test_lowercase_does_not_warn(self):
21+
with warnings.catch_warnings():
22+
warnings.simplefilter("error")
23+
L = self.g.laplacian()
24+
self.assertEqual(L.shape, (3, 3))
25+
26+
def test_capitalized_warns_and_agrees(self):
27+
with self.assertWarns(DeprecationWarning) as cm:
28+
L = self.g.Laplacian()
29+
self.assertIn("laplacian()", str(cm.warning))
30+
self.assertEqual(cm.filename, __file__) # stacklevel points at caller
31+
np.testing.assert_array_equal(L, self.g.laplacian())
32+
33+
34+
class TestUGraphName(_Name, unittest.TestCase):
35+
cls = UGraph
36+
37+
38+
class TestDGraphName(_Name, unittest.TestCase):
39+
cls = DGraph
40+
41+
42+
if __name__ == "__main__":
43+
unittest.main()

‎tests/test_multigraph.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
towards the (out-)degree of a ``DGraph`` vertex
2020
- ``adjacency()`` counts edges, so ``A[i, j]`` is the number of edges
2121
from ``i`` to ``j`` and each row of ``A`` sums to the degree of the vertex
22-
- ``Laplacian()`` is therefore ``degree() - adjacency()``, rows sum to zero
22+
- ``laplacian()`` is therefore ``degree() - adjacency()``, rows sum to zero
2323
- ``distance()`` and path lengths use the cheapest of any parallel edges
2424
"""
2525

@@ -118,7 +118,7 @@ def check_matrices_consistent(self):
118118
np.testing.assert_array_equal(out, deg)
119119

120120
# the Laplacian is D_out - A, so each row sums to zero for any graph
121-
L = g.Laplacian()
121+
L = g.laplacian()
122122
np.testing.assert_array_equal(L, np.diag(out) - A)
123123
np.testing.assert_allclose(L.sum(axis=1), 0)
124124
# and degrees sum to twice the edges (every edge has two ends)

0 commit comments

Comments
 (0)