Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

move scipy.sparse.graph to scipy.sparse.csgraph in order to follow pr…

…evious convention
  • Loading branch information...
commit 2305de17e4cb6b04859d8b06555fa00f5abdc0a2 1 parent 01480ed
Jake Vanderplas jakevdp authored
13 scipy/sparse/csgraph/__init__.py
View
@@ -0,0 +1,13 @@
+"""Compressed Sparse graph algorithms"""
+
+__docformat__ = "restructuredtext en"
+
+__all__ = ['cs_graph_components',
+ 'cs_graph_shortest_path',
+ 'floyd_warshall',
+ 'dijkstra',
+ 'cs_graph_laplacian']
+
+from graph_components import cs_graph_components
+from graph_shortest_path import cs_graph_shortest_path, floyd_warshall, dijkstra
+from graph_laplacian import cs_graph_laplacian
12 scipy/sparse/csgraph.py → scipy/sparse/csgraph/graph_components.py
View
@@ -1,15 +1,9 @@
-"""Compressed Sparse graph algorithms"""
-
-__docformat__ = "restructuredtext en"
-
-__all__ = ['cs_graph_components']
-
import numpy as np
-from sparsetools import cs_graph_components as _cs_graph_components
+from scipy.sparse.sparsetools import cs_graph_components as _cs_graph_components
-from csr import csr_matrix
-from base import isspmatrix
+from scipy.sparse.csr import csr_matrix
+from scipy.sparse.base import isspmatrix
_msg0 = 'x must be a symmetric square matrix!'
_msg1 = _msg0 + '(has shape %s)'
6 scipy/sparse/graph/graph_laplacian.py → scipy/sparse/csgraph/graph_laplacian.py
View
@@ -10,7 +10,7 @@
# License: BSD
import numpy as np
-from scipy import sparse
+from scipy.sparse import isspmatrix
###############################################################################
# Graph laplacian
@@ -68,13 +68,13 @@ def _graph_laplacian_dense(graph, normed=False, return_diag=False):
return lap
-def graph_laplacian(graph, normed=False, return_diag=False):
+def cs_graph_laplacian(graph, normed=False, return_diag=False):
""" Return the Laplacian of the given graph.
"""
if normed and (np.issubdtype(graph.dtype, np.int)
or np.issubdtype(graph.dtype, np.uint)):
graph = graph.astype(np.float)
- if sparse.isspmatrix(graph):
+ if isspmatrix(graph):
return _graph_laplacian_sparse(graph, normed=normed,
return_diag=return_diag)
else:
984 scipy/sparse/graph/graph_shortest_path.c → scipy/sparse/csgraph/graph_shortest_path.c
View
492 additions, 492 deletions not shown
6 scipy/sparse/graph/graph_shortest_path.pyx → scipy/sparse/csgraph/graph_shortest_path.pyx
View
@@ -1,7 +1,7 @@
"""
Routines for performing shortest-path graph searches
-The main interface is in the function `graph_shortest_path`. This
+The main interface is in the function `cs_graph_shortest_path`. This
calls cython routines that compute the shortest path using either
the Floyd-Warshall algorithm, or Dykstra's algorithm with Fibonacci Heaps.
"""
@@ -52,8 +52,8 @@ def _check_dist_matrix(dist_matrix,
-def graph_shortest_path(dist_matrix, directed=True,
- method='auto', overwrite=True):
+def cs_graph_shortest_path(dist_matrix, directed=True,
+ method='auto', overwrite=True):
"""
Perform a shortest-path graph search on a positive directed or
undirected graph.
2  scipy/sparse/graph/setup.py → scipy/sparse/csgraph/setup.py
View
@@ -7,7 +7,7 @@ def configuration(parent_package='', top_path=None):
import numpy
from numpy.distutils.misc_util import Configuration
- config = Configuration('graph', parent_package, top_path)
+ config = Configuration('csgraph', parent_package, top_path)
config.add_extension('graph_shortest_path',
sources=['graph_shortest_path.c'],
22 scipy/sparse/csgraph/tests/test_graph_components.py
View
@@ -0,0 +1,22 @@
+import numpy as np
+from numpy.testing import assert_, assert_equal
+from scipy.sparse import csr_matrix, cs_graph_components
+
+def test_cs_graph_components():
+ D = np.eye(4, dtype=np.bool)
+
+ n_comp, flag = cs_graph_components(csr_matrix(D))
+ assert_(n_comp == 4)
+ assert_equal(flag, [0, 1, 2, 3])
+
+ D[0,1] = D[1,0] = 1
+
+ n_comp, flag = cs_graph_components(csr_matrix(D))
+ assert_(n_comp == 3)
+ assert_equal(flag, [0, 0, 1, 2])
+
+ # A pathological case...
+ D[2,2] = 0
+ n_comp, flag = cs_graph_components(csr_matrix(D))
+ assert_(n_comp == 2)
+ assert_equal(flag, [0, 0, -2, 1])
13 scipy/sparse/graph/tests/test_graph_laplacian.py → scipy/sparse/csgraph/tests/test_graph_laplacian.py
View
@@ -4,7 +4,7 @@
import numpy as np
from scipy import sparse
-from scipy.sparse.graph import graph_laplacian
+from scipy.sparse.csgraph import cs_graph_laplacian
def test_graph_laplacian():
@@ -15,12 +15,13 @@ def test_graph_laplacian():
):
sp_mat = sparse.csr_matrix(mat)
for normed in (True, False):
- laplacian = graph_laplacian(mat, normed=normed)
+ laplacian = cs_graph_laplacian(mat, normed=normed)
n_nodes = mat.shape[0]
if not normed:
np.testing.assert_array_almost_equal(laplacian.sum(axis=0),
- np.zeros(n_nodes))
+ np.zeros(n_nodes))
np.testing.assert_array_almost_equal(laplacian.T,
- laplacian)
- np.testing.assert_array_almost_equal(laplacian,
- graph_laplacian(sp_mat, normed=normed).todense())
+ laplacian)
+ np.testing.assert_array_almost_equal(\
+ laplacian,
+ cs_graph_laplacian(sp_mat, normed=normed).todense())
9 scipy/sparse/graph/tests/test_shortest_path.py → scipy/sparse/csgraph/tests/test_shortest_path.py
View
@@ -1,6 +1,7 @@
import numpy as np
from numpy.testing import assert_array_almost_equal
-from scipy.sparse.graph import graph_shortest_path, dijkstra, floyd_warshall
+from scipy.sparse.csgraph import \
+ cs_graph_shortest_path, dijkstra, floyd_warshall
def floyd_warshall_slow(graph, directed=False):
@@ -47,7 +48,7 @@ def test_floyd_warshall():
dist_matrix = generate_graph(20)
for directed in (True, False):
- graph_FW = graph_shortest_path(dist_matrix, directed, 'FW')
+ graph_FW = cs_graph_shortest_path(dist_matrix, directed, 'FW')
graph_py = floyd_warshall_slow(dist_matrix.copy(), directed)
assert_array_almost_equal(graph_FW, graph_py)
@@ -57,7 +58,7 @@ def test_dijkstra():
dist_matrix = generate_graph(20)
for directed in (True, False):
- graph_D = graph_shortest_path(dist_matrix, directed, 'D')
+ graph_D = cs_graph_shortest_path(dist_matrix, directed, 'D')
graph_py = floyd_warshall_slow(dist_matrix.copy(), directed)
assert_array_almost_equal(graph_D, graph_py)
@@ -69,7 +70,7 @@ def test_dijkstra_ind():
for directed in (True, False):
graph_D = dijkstra(dist_matrix, directed, indices=indices)
- graph_FW = graph_shortest_path(dist_matrix, directed, 'FW')
+ graph_FW = cs_graph_shortest_path(dist_matrix, directed, 'FW')
print graph_D
print graph_FW[:5]
4 scipy/sparse/graph/__init__.py
View
@@ -1,4 +0,0 @@
-"""Sparse graph routines"""
-
-from graph_shortest_path import graph_shortest_path, floyd_warshall, dijkstra
-from graph_laplacian import graph_laplacian
2  scipy/sparse/setup.py
View
@@ -11,7 +11,7 @@ def configuration(parent_package='',top_path=None):
config.add_subpackage('linalg')
config.add_subpackage('sparsetools')
- config.add_subpackage('graph')
+ config.add_subpackage('csgraph')
return config
1  scipy/sparse/setupscons.py
View
@@ -13,6 +13,7 @@ def configuration(parent_package='',top_path=None):
config.add_data_dir('tests')
config.add_subpackage('linalg')
config.add_subpackage('sparsetools')
+ config.add_subpackage('csgraph')
return config
22 scipy/sparse/tests/test_spfuncs.py
View
@@ -97,27 +97,5 @@ def gold(A,bs):
assert_equal(spfuncs.count_blocks(X, (1, 2)), gold(X, (1, 2)))
assert_equal(spfuncs.count_blocks(Y, (1, 2)), gold(X, (1, 2)))
- def test_cs_graph_components(self):
- import numpy as np
- from scipy.sparse import csr_matrix, cs_graph_components
-
- D = np.eye(4, dtype=np.bool)
-
- n_comp, flag = cs_graph_components(csr_matrix(D))
- assert_(n_comp == 4)
- assert_equal(flag, [0, 1, 2, 3])
-
- D[0,1] = D[1,0] = 1
-
- n_comp, flag = cs_graph_components(csr_matrix(D))
- assert_(n_comp == 3)
- assert_equal(flag, [0, 0, 1, 2])
-
- # A pathological case...
- D[2,2] = 0
- n_comp, flag = cs_graph_components(csr_matrix(D))
- assert_(n_comp == 2)
- assert_equal(flag, [0, 0, -2, 1])
-
if __name__ == "__main__":
run_module_suite()
Please sign in to comment.
Something went wrong with that request. Please try again.