Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #263 from bthirion/fix-type-graph

MRG: Fix type graph

This PR aims at solving issue #256 by casting to float 64 the arrays that need to.
I took this opportunity to clean the tests in nipy.algorithms.graph.
  • Loading branch information...
commit 9df4e65ffcc691c5defcab26590767779ad4148f 2 parents 343b8cf + 0523deb
@matthew-brett matthew-brett authored
View
24 nipy/algorithms/graph/field.py
@@ -12,6 +12,7 @@
Author:Bertrand Thirion, 2006--2011
"""
+from warnings import warn
import numpy as np
from .graph import WeightedGraph
@@ -148,18 +149,20 @@ def opening(self, nbiter=1):
self.dilation(nbiter)
def dilation(self, nbiter=1, fast=True):
- """
- Morphological dimlation of the field data. self.field is changed
+ """Morphological dilation of the field data, changed in place
Parameters
----------
nbiter: int, optional, the number of iterations required
- fixme
- -----
- cython
+ Note
+ ----
+ When data dtype is not float64, a slow version of the code is used
"""
nbiter = int(nbiter)
+ if self.field.dtype != np.float64:
+ warn('data type is not float64; a slower version is used')
+ fast = False
if fast:
from ._graph import dilation
if self.E > 0:
@@ -177,16 +180,17 @@ def dilation(self, nbiter=1, fast=True):
self.field = np.array([self.field[row].max(0) for row in rows])
def highest_neighbor(self, refdim=0):
- """ Computes the neighbor with highest field value along refdim
+ """Computes the neighbor with highest field value along refdim
Parameters
----------
- refdim: int optiontal, the dimension to consider
+ refdim: int, optional,
+ the dimension of the field under consideration
Returns
-------
- hneighb: array of shape(self.V), index of the neighbor with highest
- value
+ hneighb: array of shape(self.V),
+ index of the neighbor with highest value
"""
from scipy.sparse import dia_matrix
refdim = int(refdim)
@@ -263,7 +267,7 @@ def local_maxima(self, refdim=0, th=NEGINF):
# create a subfield(thresholding)
sf = self.subfield(self.field.T[refdim] >= th)
initial_field = sf.field.T[refdim]
- sf.field = initial_field.copy()
+ sf.field = initial_field.astype(np.float64)
# compute the depth in the subgraph
ldepth = sf.V * np.ones(sf.V, np.int)
View
501 nipy/algorithms/graph/tests/test_field.py
@@ -1,5 +1,6 @@
#!/usr/bin/env python
import numpy as np
+import numpy.random as nr
from ..field import (field_from_coo_matrix_and_data,
field_from_graph_and_data)
@@ -7,273 +8,291 @@
from nose.tools import assert_true, assert_equal
-from numpy.testing import TestCase, assert_array_equal
+from numpy.testing import assert_array_equal
def basic_field(nx=10, ny=10, nz=10):
xyz = np.reshape(np.indices((nx, ny, nz)), (3, nx * ny * nz)).T
- data = np.sum(xyz, 1).astype('d')
- F = field_from_graph_and_data(wgraph_from_3d_grid(xyz, 26), data)
- return F
+ data = np.sum(xyz, 1).astype(np.float)
+ myfield = field_from_graph_and_data(wgraph_from_3d_grid(xyz, 26), data)
+ return myfield
def basic_field_random(nx=10, ny=10, nz=1):
- import numpy.random as nr
xyz = np.reshape(np.indices((nx, ny, nz)), (3, nx * ny * nz)).T
- data = 0.5 * nr.randn(nx * ny * nz, 1) + np.sum(xyz, 1).astype('d')
- F = field_from_graph_and_data(wgraph_from_3d_grid(xyz, 26), data)
- return F
+ data = 0.5 * nr.randn(nx * ny * nz, 1) + np.sum(xyz, 1).astype(np.float)
+ myfield = field_from_graph_and_data(wgraph_from_3d_grid(xyz, 26), data)
+ return myfield
def basic_field_2(nx=10, ny=10, nz=10):
xyz = np.reshape(np.indices((nx, ny, nz)), (3, nx * ny * nz)).T
toto = xyz - np.array([5, 5, 5])
data = np.sum(toto ** 2, 1)
- F = field_from_graph_and_data(wgraph_from_3d_grid(xyz, 26), data)
- return F
+ myfield = field_from_graph_and_data(wgraph_from_3d_grid(xyz, 26), data)
+ return myfield
def basic_field_3(nx=10, ny=10, nz=10):
xyz = np.reshape(np.indices((nx, ny, nz)), (3, nx * ny * nz)).T
toto = xyz - np.array([5, 5, 5])
data = np.abs(np.sum(toto ** 2, 1) - 11 )
- F = field_from_graph_and_data(wgraph_from_3d_grid(xyz, 26), data)
- return F
+ myfield = field_from_graph_and_data(wgraph_from_3d_grid(xyz, 26), data)
+ return myfield
def basic_graph(nx=10, ny=10, nz=10):
xyz = np.reshape(np.indices((nx, ny, nz)), (3, nx * ny * nz)).T
data = np.zeros(xyz.shape[0])
- F = field_from_graph_and_data(wgraph_from_3d_grid(xyz, 26), data)
- return F
-
-
-class test_Field(TestCase):
-
- def test_max_1(self):
- F = basic_field()
- F.field[555] = 30
- depth = F.local_maxima()
- dep = np.zeros(1000, np.int)
- dep[555] = 5
- dep[999] = 3
- assert_true(sum(np.absolute(dep-depth))<1.e-7)
-
- def test_max_2(self):
- F = basic_field()
- F.field[555] = 28
- idx,depth = F.get_local_maxima()
- self.assert_(len(idx) == 2)
- self.assert_(np.alltrue( idx == (555, 999)))
- self.assert_(np.alltrue( depth == (5, 3)))
-
- def test_max_3(self):
- F = basic_field()
- F.field[555] = 27
- idx, depth = F.get_local_maxima()
- assert_true(np.size(idx) == 2)
- assert_true(idx[0] == 555)
- assert_true(idx[1] == 999)
- assert_true(depth[0] == 5)
- assert_true(depth[1] == 5)
-
- def test_max_4(self):
- F = basic_field()
- F.field[555] = 28
- idx, depth = F.get_local_maxima(0, 27.5)
- assert_true(np.size(idx) == 1)
- assert_true(idx[0] == 555)
- assert_true(depth[0] == 1)
-
- def test_smooth_1(self):
- G = basic_graph()
- field = np.zeros((1000,1))
- field[555,0] = 1
- G.set_field(field)
- G.diffusion()
- sfield = G.get_field()
- OK1 = (sfield[555]==0)
- OK2 = (sfield[554]==1)
- OK3 = (np.absolute(sfield[566]-np.sqrt(2))<1.e-7)
- OK4 = (np.absolute(sfield[446]-np.sqrt(3))<1.e-7)
- OK = OK1 & OK2 & OK3 & OK4
- self.assert_(OK)
-
- def test_smooth_2(self):
- G = basic_graph()
- field = np.zeros((1000,1))
- field[555,0] = 1
- G.set_field(field)
- G.diffusion(1)
- sfield = G.get_field()
- OK1 = (sfield[555]==0)
- OK2 = (sfield[554]==1)
- OK3 = (np.absolute(sfield[566]-np.sqrt(2))<1.e-7)
- OK4 = (np.absolute(sfield[446]-np.sqrt(3))<1.e-7)
- OK = OK1 & OK2 & OK3 & OK4
- self.assert_(OK)
-
- def test_dilation(self):
- F = basic_field()
- F.field[555] = 30
- F.field[664] = 0
- F.dilation(2)
- assert_true(F.field[737] == 30)
- assert_true(F.field[0] == 6)
- assert_true(F.field[999] == 27)
- assert_true(F.field[664] == 30)
-
- def test_dilation2(self):
- # test equality of cython and python versions
- F = basic_field()
- F.field[555] = 30
- F.field[664] = 0
- h = F.copy()
- h.dilation(2)
- g = F.copy()
- g.dilation(2, False)
- assert_array_equal(h.field, g.field)
-
- def test_erosion(self):
- F = basic_field()
- F.field[555] = 30
- F.field[664] = 0
- F.erosion(2)
- field = F.get_field()
- assert_true(field[737] == 11)
- assert_true(field[0] == 0)
- assert_true(field[999] == 21)
- assert_true(field[664] == 0)
-
- def test_opening(self):
- F = basic_field()
- F.field[555] = 30
- F.field[664] = 0
- F.opening(2)
- field = F.get_field()
- assert_true(field[737] == 17)
- assert_true(field[0] == 0)
- assert_true(field[999] == 21)
- assert_true(field[555] == 16)
-
- def test_closing(self):
- F = basic_field()
- F.field[555] = 30
- F.field[664] = 0
- F.closing(2)
- field = F.get_field()
- assert_true(field[737] == 17)
- assert_true(field[0] == 6)
- assert_true(field[999] == 27)
- assert_true(field[555] == 30)
-
- def test_watershed_1(self):
- F = basic_field()
- F.field[555] = 28
- F.field[664] = 0
- idx, label = F.custom_watershed()
- assert_equal(np.size(idx), 2)
- assert_equal(tuple(idx), (555, 999))
- assert_equal((label[776], label[666], label[123]), (1, 0, 0))
-
- def test_watershed_4(self):
- F = basic_field_3()
- idx, label = F.custom_watershed()
- assert_true(np.size(idx) == 9)
- assert_true(np.unique(
+ myfield = field_from_graph_and_data(wgraph_from_3d_grid(xyz, 26), data)
+ return myfield
+
+
+def test_type_local_max():
+ f = basic_field()
+ f.field = f.field.astype(np.float32)
+ idx, depth = f.get_local_maxima(th=0)
+ assert_array_equal(idx, np.array([999]))
+
+
+def test_max_1():
+ myfield = basic_field()
+ myfield.field[555] = 30
+ depth = myfield.local_maxima()
+ dep = np.zeros(1000, np.int)
+ dep[555] = 5
+ dep[999] = 3
+ assert_true(sum(np.absolute(dep-depth)) < 1.e-7)
+
+
+def test_max_2():
+ myfield = basic_field()
+ myfield.field[555] = 28
+ idx, depth = myfield.get_local_maxima()
+ assert_true(len(idx) == 2)
+ assert_array_equal(idx, np.array([555, 999]))
+ assert_array_equal(depth, np.array([5, 3]))
+
+
+def test_max_3():
+ myfield = basic_field()
+ myfield.field[555] = 27
+ idx, depth = myfield.get_local_maxima()
+ assert_equal(np.size(idx), 2)
+ assert_equal(idx[0], 555)
+ assert_equal(idx[1], 999)
+ assert_equal(depth[0], 5)
+ assert_equal(depth[1], 5)
+
+
+def test_max_4():
+ myfield = basic_field()
+ myfield.field[555] = 28
+ idx, depth = myfield.get_local_maxima(0, 27.5)
+ assert_equal(np.size(idx), 1)
+ assert_equal(idx[0], 555)
+ assert_equal(depth[0], 1)
+
+
+def test_smooth_1():
+ G = basic_graph()
+ field = np.zeros((1000,1))
+ field[555,0] = 1
+ G.set_field(field)
+ G.diffusion()
+ sfield = G.get_field()
+ assert_equal(sfield[555], 0)
+ assert_equal(sfield[554], 1)
+ assert_true(np.abs(sfield[566] - np.sqrt(2)) < 1.e-7)
+ assert_true(np.abs(sfield[446] - np.sqrt(3)) < 1.e-7)
+
+
+def test_smooth_2():
+ G = basic_graph()
+ field = np.zeros((1000, 1))
+ field[555, 0] = 1
+ G.set_field(field)
+ G.diffusion(1)
+ sfield = G.get_field()
+ assert_equal(sfield[555], 0)
+ assert_equal(sfield[554], 1)
+ assert_true(np.abs(sfield[566] - np.sqrt(2)) < 1.e-7)
+ assert_true(np.abs(sfield[446] - np.sqrt(3)) < 1.e-7)
+
+
+def test_dilation():
+ myfield = basic_field()
+ myfield.field[555] = 30
+ myfield.field[664] = 0
+ myfield.dilation(2)
+ assert_true(myfield.field[737] == 30)
+ assert_true(myfield.field[0] == 6)
+ assert_true(myfield.field[999] == 27)
+ assert_true(myfield.field[664] == 30)
+
+
+def test_dilation2():
+ # test equality of cython and python versions
+ myfield = basic_field()
+ myfield.field[555] = 30
+ myfield.field[664] = 0
+ h = myfield.copy()
+ h.dilation(2)
+ g = myfield.copy()
+ g.dilation(2, False)
+ assert_array_equal(h.field, g.field)
+
+
+def test_erosion():
+ myfield = basic_field()
+ myfield.field[555] = 30
+ myfield.field[664] = 0
+ myfield.erosion(2)
+ field = myfield.get_field()
+ assert_true(field[737] == 11)
+ assert_true(field[0] == 0)
+ assert_true(field[999] == 21)
+ assert_true(field[664] == 0)
+
+
+def test_opening():
+ myfield = basic_field()
+ myfield.field[555] = 30
+ myfield.field[664] = 0
+ myfield.opening(2)
+ field = myfield.get_field()
+ assert_true(field[737] == 17)
+ assert_true(field[0] == 0)
+ assert_true(field[999] == 21)
+ assert_true(field[555] == 16)
+
+
+def test_closing():
+ myfield = basic_field()
+ myfield.field[555] = 30
+ myfield.field[664] = 0
+ myfield.closing(2)
+ field = myfield.get_field()
+ assert_true(field[737] == 17)
+ assert_true(field[0] == 6)
+ assert_true(field[999] == 27)
+ assert_true(field[555] == 30)
+
+
+def test_watershed_1():
+ myfield = basic_field()
+ myfield.field[555] = 28
+ myfield.field[664] = 0
+ idx, label = myfield.custom_watershed()
+ assert_equal(np.size(idx), 2)
+ assert_equal(tuple(idx), (555, 999))
+ assert_equal((label[776], label[666], label[123]), (1, 0, 0))
+
+
+def test_watershed_4():
+ myfield = basic_field_3()
+ idx, label = myfield.custom_watershed()
+ assert_true(np.size(idx) == 9)
+ assert_true(np.unique(
[label[555], label[0], label[9], label[90], label[99], label[900],
- label[909], label[990], label[999]])
- .size == 9)
-
- def test_watershed_2(self):
- F = basic_field_2()
- F.field[555] = 10
- F.field[664] = 0
- idx, label = F.custom_watershed()
- assert_true(np.size(idx)==9)
-
- def test_watershed_3(self):
- F = basic_field_2()
- F.field[555] = 10
- F.field[664] = 0
- idx, label = F.custom_watershed(0,11)
- assert_true(np.size(idx)==8)
-
- def test_bifurcations_1(self):
- F = basic_field()
- idx, parent,label = F.threshold_bifurcations()
- assert_true(idx == 999)
- assert_true(parent == 0)
-
- def test_bifurcations_2(self):
- F = basic_field_2()
- idx, parent, label = F.threshold_bifurcations()
- assert_true(np.size(idx) == 15)
-
- def test_geodesic_kmeans(self, nbseeds=3):
- # Test the geodisc k-means algorithm
- import numpy.random as nr
- F = basic_field_random(5, 5, 1)
- seeds = np.argsort(nr.rand(F.V))[:nbseeds]
- seeds, label, inertia = F.geodesic_kmeans(seeds)
- for i in range(nbseeds):
- assert_true(label[seeds[i]] == i)
- print np.unique(label), np.arange(nbseeds)
- assert_true(np.array([i in np.unique(label)
- for i in np.arange(nbseeds)]).all())
-
- def test_constrained_voronoi(self, nbseeds=3):
- # Test the geodisc k-means algorithm
- import numpy.random as nr
- F = basic_field_random()
- seeds = np.argsort(nr.rand(F.V))[:nbseeds]
- label = F.constrained_voronoi(seeds)
- for i in range(nbseeds):
- assert_true(label[seeds[i]] == i)
- assert_true(np.array([i in np.unique(label)
- for i in np.arange(nbseeds)]).all())
-
- def test_constrained_voronoi_2(self, nbseeds=3):
- # Test the geodisc k-means algorithm
- xyz, x = np.zeros((30, 3)), np.arange(30)
- xyz[:, 0] = x
- y = np.array((x // 10), np.float)
- F = field_from_graph_and_data(wgraph_from_3d_grid(xyz, 6), y)
- seeds = np.array([1, 18, 25])
- label = F.constrained_voronoi(seeds)
- assert_array_equal(label, x // 10)
-
- def test_subfield(self):
- import numpy.random as nr
- F = basic_field_random()
- valid = nr.rand(F.V)>0.1
- sf = F.subfield(valid)
- self.assert_(sf.V==np.sum(valid))
-
- def test_subfield2(self):
- F = basic_field_random()
- valid = np.zeros(F.V)
- sf = F.subfield(valid)
- self.assert_(sf==None)
-
- def test_ward1(self):
- F = basic_field_random()
- Lab, J = F.ward(10)
- self.assert_(Lab.max()==9)
-
- def test_ward2(self):
- F = basic_field_random()
- Lab, J1 = F.ward(5)
- Lab, J2 = F.ward(10)
- self.assert_(J1>J2)
-
- def test_field_from_coo_matrix(self):
- import scipy.sparse as sps
- V = 10
- a = np.random.rand(V, V)>.9
- fi = field_from_coo_matrix_and_data(sps.coo_matrix(a), a)
- print fi.E , a.sum()
- self.assert_(fi.E==a.sum())
+ label[909], label[990], label[999]]).size == 9)
+
+
+def test_watershed_2():
+ myfield = basic_field_2()
+ myfield.field[555] = 10
+ myfield.field[664] = 0
+ idx, label = myfield.custom_watershed()
+ assert_true(np.size(idx) == 9)
+
+
+def test_watershed_3():
+ myfield = basic_field_2()
+ myfield.field[555] = 10
+ myfield.field[664] = 0
+ idx, label = myfield.custom_watershed(0,11)
+ assert_true(np.size(idx)==8)
+
+
+def test_bifurcations_1():
+ myfield = basic_field()
+ idx, parent,label = myfield.threshold_bifurcations()
+ assert_true(idx == 999)
+ assert_true(parent == 0)
+
+
+def test_bifurcations_2():
+ myfield = basic_field_2()
+ idx, parent, label = myfield.threshold_bifurcations()
+ assert_true(np.size(idx) == 15)
+
+
+
+def test_geodesic_kmeans(nbseeds=3):
+ # Test the geodisc k-means algorithm
+ myfield = basic_field_random(5, 5, 1)
+ seeds = np.argsort(nr.rand(myfield.V))[:nbseeds]
+ seeds, label, inertia = myfield.geodesic_kmeans(seeds)
+ assert_array_equal(label[seeds], np.arange(nbseeds))
+ assert_true(np.array([i in np.unique(label)
+ for i in np.arange(nbseeds)]).all())
+
+
+def test_constrained_voronoi(nbseeds=3):
+ # Test the geodisc k-means algorithm
+ myfield = basic_field_random()
+ seeds = np.argsort(nr.rand(myfield.V))[:nbseeds]
+ label = myfield.constrained_voronoi(seeds)
+ assert_array_equal(label[seeds], np.arange(nbseeds))
+ assert_true(np.array([i in np.unique(label)
+ for i in np.arange(nbseeds)]).all())
+
+
+def test_constrained_voronoi_2(nbseeds=3):
+ # Test the geodisc k-means algorithm
+ xyz, x = np.zeros((30, 3)), np.arange(30)
+ xyz[:, 0] = x
+ y = np.array((x // 10), np.float)
+ myfield = field_from_graph_and_data(wgraph_from_3d_grid(xyz, 6), y)
+ seeds = np.array([1, 18, 25])
+ label = myfield.constrained_voronoi(seeds)
+ assert_array_equal(label, x // 10)
+
+
+def test_subfield():
+ myfield = basic_field_random()
+ valid = nr.rand(myfield.V) > 0.1
+ sf = myfield.subfield(valid)
+ assert_equal(sf.V, np.sum(valid))
+
+
+def test_subfield2():
+ myfield = basic_field_random()
+ valid = np.zeros(myfield.V)
+ sf = myfield.subfield(valid)
+ assert_true(sf == None)
+
+
+def test_ward1():
+ myfield = basic_field_random()
+ lab, J = myfield.ward(10)
+ assert_equal(lab.max(), 9)
+
+
+def test_ward2():
+ myfield = basic_field_random()
+ Lab, J1 = myfield.ward(5)
+ Lab, J2 = myfield.ward(10)
+ assert_true(J1 > J2)
+
+
+def test_field_from_coo_matrix():
+ import scipy.sparse as sps
+ V = 10
+ a = np.random.rand(V, V) > .9
+ fi = field_from_coo_matrix_and_data(sps.coo_matrix(a), a)
+ assert_equal(fi.E, a.sum())
+
if __name__ == '__main__':
import nose
View
886 nipy/algorithms/graph/tests/test_graph.py
@@ -2,476 +2,496 @@
import numpy as np
import numpy.random as nr
-from unittest import TestCase
+from numpy.testing import(assert_array_equal, assert_array_almost_equal,
+ assert_almost_equal)
+from nose.tools import assert_true, assert_equal
from ..graph import (WeightedGraph, complete_graph, mst, knn, eps_nn,
wgraph_from_adjacency, wgraph_from_coo_matrix,
concatenate_graphs, wgraph_from_3d_grid)
+
def basicdata():
- x = np.array( [[-1.998,-2.024], [-0.117,-1.010], [1.099,-0.057],
- [ 1.729,-0.252], [1.003,-0.021], [1.703,-0.739],
- [-0.557,1.382],[-1.200,-0.446],[-0.331,-0.256],
- [-0.800,-1.584]])
+ x = np.array( [[- 1.998, - 2.024], [- 0.117, - 1.010], [1.099, - 0.057],
+ [ 1.729, - 0.252], [1.003, - 0.021], [1.703, - 0.739],
+ [- 0.557, 1.382],[- 1.200, - 0.446],[- 0.331, - 0.256],
+ [- 0.800, - 1.584]])
return x
+
def basic_graph():
l = np.linspace(0, 2 * np.pi, 20, endpoint=False)
x = np.column_stack((np.cos(l), np.sin(l)))
G = knn(x, 2)
return G
+
def basic_graph_2():
l = np.linspace(0, 2 * np.pi, 20, endpoint=False)
x = np.column_stack((np.cos(l), np.sin(l)))
G = knn(x, 2)
return G, x
-class test_Graph(TestCase):
+
+def test_complete():
+ v = 10
+ G = complete_graph(v)
+ a = G.get_edges()[:, 0]
+ b = G.get_edges()[:, 1]
+ inds = np.indices((v, v)).reshape( (2, v * v) )
+ assert_array_equal(inds, (a, b))
+
+
+def test_knn_1():
+ x = basicdata()
+ G = knn(x, 1)
+ A = G.get_edges()[:, 0]
+ assert_equal(np.shape(A)[0], 14)
+
- def test_complete(self):
- v = 10
- G = complete_graph(v)
- a = G.get_edges()[:, 0]
- b = G.get_edges()[:, 1]
- inds = np.indices((v, v)).reshape( (2, v * v) )
- self.assert_( ( inds == (a, b) ).all() )
-
- def test_knn_1(self):
- x = basicdata()
- G = knn(x, 1)
- A = G.get_edges()[:,0]
- OK = (np.shape(A)[0] == (14))
- self.assert_(OK)
+def test_set_euclidian():
+ G, x = basic_graph_2()
+ d = G.weights
+ G.set_euclidian(x / 10)
+ D = G.weights
+ assert_true(np.allclose(D, d / 10, 1e-7))
+
+
+def test_set_gaussian():
+ G, x = basic_graph_2()
+ d = G.weights
+ G.set_gaussian(x, 1.0)
+ D = G.weights
+ assert_true(np.allclose(D, np.exp(- d * d / 2), 1e-7))
+
+
+def test_set_gaussian_2():
+ G, x = basic_graph_2()
+ d = G.weights
+ G.set_gaussian(x)
+ D = G.weights
+ sigma = np.sum(d * d) / len(d)
+ assert_true(np.allclose(D, np.exp(-d * d / (2 * sigma)), 1e-7))
- def test_set_euclidian(self):
- G,x = basic_graph_2()
- d = G.weights
- G.set_euclidian(x / 10)
- D = G.weights
- OK = np.allclose(D, d / 10, 1e-7)
- self.assert_(OK)
-
- def test_set_gaussian(self):
- G,x = basic_graph_2()
- d = G.weights
- G.set_gaussian(x, 1.0)
- D = G.weights
- OK = np.allclose(D, np.exp(- d * d / 2), 1e-7)
- self.assert_(OK)
-
- def test_set_gaussian_2(self):
- G,x = basic_graph_2()
- d = G.weights
- G.set_gaussian(x)
- D = G.weights
- sigma = sum(d * d) / len(d)
- OK = np.allclose(D, np.exp(-d * d / (2 * sigma)), 1e-7)
- self.assert_(OK)
-
- def test_eps_1(self):
- x = basicdata()
- G = eps_nn(x, 1.)
- D = G.weights
- OK = (np.size(D) == 16)
- self.assert_(OK)
- OK = (D < 1).all()
- self.assert_(OK)
-
- def test_mst_1(self):
- x = basicdata()
- G = mst(x)
- D = G.weights
- OK = (np.size(D) == 18)
- self.assert_(OK)
-
- def test_3d_grid(self):
- """test the 6nn graph
- """
- x0 = np.array([0, 0, 0])
- x1 = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [-1, 0, 0], [0, -1, 0],
- [0, 0, -1]])
- x2 = np.array([[1, 1, 0], [0, 1, 1], [1, 0, 1], [1, -1, 0], [0, 1, -1],
- [1, 0, -1], [-1, 1, 0], [0, -1, 1], [-1, 0, 1],
- [-1, -1, 0], [-1, 0, -1], [0, -1, -1]])
- x3 = np.array([[1, 1, 1], [1, 1, -1], [1, -1, 1], [1, -1, -1],
- [-1, 1, 1], [-1, 1, -1], [-1, -1, 1], [-1, -1, -1]])
- for x in x1:
- xyz = np.vstack((x0, x))
- assert wgraph_from_3d_grid(xyz, 6).E == 2
- assert wgraph_from_3d_grid(xyz, 18).E == 2
- assert wgraph_from_3d_grid(xyz, 26).E == 2
+
+def test_eps_1():
+ x = basicdata()
+ G = eps_nn(x, 1.)
+ D = G.weights
+ assert_equal(np.size(D), 16)
+ assert_true((D < 1).all())
+
+
+def test_mst_1():
+ x = basicdata()
+ G = mst(x)
+ D = G.weights
+ assert_equal(np.size(D), 18)
+
+
+def test_3d_grid():
+ """test the 6nn graph
+ """
+ x0 = np.array([0, 0, 0])
+ x1 = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [-1, 0, 0], [0, -1, 0],
+ [0, 0, -1]])
+ x2 = np.array([[1, 1, 0], [0, 1, 1], [1, 0, 1], [1, -1, 0], [0, 1, -1],
+ [1, 0, -1], [-1, 1, 0], [0, -1, 1], [-1, 0, 1],
+ [-1, -1, 0], [-1, 0, -1], [0, -1, -1]])
+ x3 = np.array([[1, 1, 1], [1, 1, -1], [1, -1, 1], [1, -1, -1],
+ [-1, 1, 1], [-1, 1, -1], [-1, -1, 1], [-1, -1, -1]])
+ for x in x1:
+ xyz = np.vstack((x0, x))
+ assert_equal(wgraph_from_3d_grid(xyz, 6).E, 2)
+ assert_equal(wgraph_from_3d_grid(xyz, 18).E, 2)
+ assert_equal(wgraph_from_3d_grid(xyz, 26).E, 2)
for x in x2:
xyz = np.vstack((x0, x))
- assert wgraph_from_3d_grid(xyz, 6).E == 0
- assert wgraph_from_3d_grid(xyz, 18).E == 2
- assert wgraph_from_3d_grid(xyz, 26).E == 2
+ assert_equal(wgraph_from_3d_grid(xyz, 6).E, 0)
+ assert_equal(wgraph_from_3d_grid(xyz, 18).E, 2)
+ assert_equal(wgraph_from_3d_grid(xyz, 26).E, 2)
for x in x3:
xyz = np.vstack((x0, x))
- assert wgraph_from_3d_grid(xyz, 6).E == 0
- assert wgraph_from_3d_grid(xyz, 18).E == 0
- assert wgraph_from_3d_grid(xyz, 26).E == 2
+ assert_equal(wgraph_from_3d_grid(xyz, 6).E, 0)
+ assert_equal(wgraph_from_3d_grid(xyz, 18).E, 0)
+ assert_equal(wgraph_from_3d_grid(xyz, 26).E, 2)
- def test_grid_3d_1(self):
- """ Test the 6 nn graphs on 3d grid
- """
- nx, ny, nz = 9, 6, 1
- xyz = np.mgrid[0:nx, 0:ny, 0:nz]
- xyz = np.reshape(xyz, (3, nx * ny * nz)).T
- G = wgraph_from_3d_grid(xyz, 6)
- self.assert_(G.E == 186)
+def test_grid_3d_1():
+ """ Test the 6 nn graphs on 3d grid
+ """
+ nx, ny, nz = 9, 6, 1
+ xyz = np.mgrid[0:nx, 0:ny, 0:nz]
+ xyz = np.reshape(xyz, (3, nx * ny * nz)).T
+ G = wgraph_from_3d_grid(xyz, 6)
+ assert_equal(G.E, 186)
+
- def test_grid_3d_2(self):
- """ Test the 18-nn graph on a 3d grid
- """
- nx, ny, nz = 9, 6, 1
- xyz = np.mgrid[0:nx, 0:ny, 0:nz]
- xyz = np.reshape(xyz,(3, nx * ny * nz)).T
- G = wgraph_from_3d_grid(xyz, 18)
- self.assert_(G.E == 346)
-
- def test_grid_3d_3(self):
- """ Test the 26-nn graph on a 3d grid
- """
- nx, ny, nz = 9, 6, 1
- xyz = np.mgrid[0:nx, 0:ny, 0:nz]
- xyz = np.reshape(xyz,(3, nx * ny * nz)).T
- G = wgraph_from_3d_grid(xyz, 26)
- self.assert_(G.E == 346)
-
- def test_grid_3d_4(self):
- nx, ny, nz = 10, 10, 10
- xyz = np.reshape(np.indices((nx, ny, nz)), (3, nx * ny * nz)).T
- G = wgraph_from_3d_grid(xyz, 26)
- D = G.weights
- # 6 * 9 * 10 * 10
- self.assert_(sum(D == 1)==5400 )
- # 26 * 8 ** 3 + 6 * 8 ** 2 * 17 + 12 * 8 * 11 + 8 * 7
- self.assert_(np.size(D) == 20952 )
- # 18 * 8 ** 3 + 6 * 8 ** 2 * 13 + 12 * 8 * 9 + 8 * 6
- self.assert_(sum(D < 1.5) == 15120)
-
- def test_grid_3d_5(self):
- nx, ny, nz = 5, 5, 5
- xyz = np.reshape(np.indices((nx, ny, nz)), (3, nx * ny * nz)).T
- G = wgraph_from_3d_grid(xyz, 26)
- D = G.weights.copy()
- G.set_euclidian(xyz)
- assert (np.allclose(G.weights, D, 1.e-7))
-
- def test_grid_3d_6(self):
- nx, ny, nz = 5, 5, 5
- xyz = np.reshape(np.indices((nx, ny, nz)), (3, nx * ny * nz)).T
- adj = wgraph_from_3d_grid(xyz, 26).to_coo_matrix().tolil()
- assert len(adj.rows[63]) == 26
- for i in [62, 64, 58, 68, 38, 88, 57, 67, 37, 87, 59, 69, 39, 89, 33,
- 83, 43, 93, 32, 82, 42, 92, 34, 84, 44, 94]:
- assert i in adj.rows[63]
-
- def test_grid_3d_7(self):
- """ Check that the grid graph is symmetric
- """
- xyz = np.array(np.where(np.random.rand(5, 5, 5) > 0.5)).T
- adj = wgraph_from_3d_grid(xyz, 6).to_coo_matrix()
- assert (adj - adj.T).nnz == 0
- adj = wgraph_from_3d_grid(xyz, 18).to_coo_matrix()
- assert (adj - adj.T).nnz == 0
- adj = wgraph_from_3d_grid(xyz, 26).to_coo_matrix()
- assert (adj - adj.T).nnz == 0
+def test_grid_3d_2():
+ """ Test the 18-nn graph on a 3d grid
+ """
+ nx, ny, nz = 9, 6, 1
+ xyz = np.mgrid[0:nx, 0:ny, 0:nz]
+ xyz = np.reshape(xyz,(3, nx * ny * nz)).T
+ G = wgraph_from_3d_grid(xyz, 18)
+ assert_equal(G.E, 346)
- def test_cut_redundancies(self):
- G = basic_graph()
- e = G.E
- edges = G.get_edges()
- weights = G.weights
- G.E = 2 * G.E
- G.edges = np.concatenate((edges, edges))
- G.weights = np.concatenate((weights, weights))
- K = G.cut_redundancies()
- OK = (K.E == e)
- self.assert_(OK)
-
- def test_degrees(self):
- G = basic_graph()
- (r,l) = G.degrees()
- self.assert_(( r == 2 ).all())
- self.assert_(( l == 2 ).all())
-
- def test_normalize(self):
- G = basic_graph()
- G.normalize()
- M = G.to_coo_matrix()
- sM = np.array(M.sum(1)).ravel()
- test = np.absolute(sM - 1) < 1.e-7
- OK = np.size(np.nonzero(test) == 0)
- self.assert_(OK)
-
- def test_normalize_2(self):
- G = basic_graph()
- G.normalize(0)
- M = G.to_coo_matrix()
- sM = np.array(M.sum(1)).ravel()
- test = np.absolute(sM - 1) < 1.e-7
- OK = np.size(np.nonzero(test)==0)
- self.assert_(OK)
-
- def test_normalize_3(self):
- G = basic_graph()
- G.normalize(1)
- M = G.to_coo_matrix()
- sM = np.array(M.sum(0)).ravel()
- test = np.absolute(sM - 1) < 1.e-7
- OK = np.size(np.nonzero(test)==0)
- self.assert_(OK)
-
- def test_adjacency(self):
- G = basic_graph()
- M = G.to_coo_matrix()
- self.assert_(( M.diagonal() == 0 ).all())
- A = M.toarray()
- self.assert_(( np.diag(A, 1) != 0 ).all())
- self.assert_(( np.diag(A, -1) != 0 ).all())
-
- def test_cc(self):
- G = basic_graph()
- l = G.cc()
- L = np.array(l==0)
- OK = L.all()
- self.assert_(OK)
-
- def test_isconnected(self):
- G = basic_graph()
- self.assert_(G.is_connected())
-
- def test_main_cc(self):
- x = basicdata()
- G = knn(x, 1)
- l = G.cc()
- l = G.main_cc()
- assert np.size(l)==6
-
- def test_dijkstra(self):
- """ Test dijkstra's algorithm
- """
- G = basic_graph()
- l = G.dijkstra(0)
- assert (np.absolute(l[10] - 20 * np.sin(np.pi / 20)) < 1.e-7)
-
- def test_dijkstra_multiseed(self):
- """ Test dijkstra's algorithm, multi_seed version
- """
- G = basic_graph()
- l = G.dijkstra([0, 1])
- assert (np.absolute(l[10] - 18 * np.sin(np.pi / 20)) < 1.e-7)
-
-
- def test_dijkstra2(self):
- """ Test dijkstra's algorithm, API detail
- """
- G = basic_graph()
- l = G.dijkstra()
- assert (np.absolute(l[10] - 20 * np.sin(np.pi / 20)) < 1.e-7)
+def test_grid_3d_3():
+ """ Test the 26-nn graph on a 3d grid
+ """
+ nx, ny, nz = 9, 6, 1
+ xyz = np.mgrid[0:nx, 0:ny, 0:nz]
+ xyz = np.reshape(xyz,(3, nx * ny * nz)).T
+ G = wgraph_from_3d_grid(xyz, 26)
+ assert_equal(G.E, 346)
+
+
+def test_grid_3d_4():
+ nx, ny, nz = 10, 10, 10
+ xyz = np.reshape(np.indices((nx, ny, nz)), (3, nx * ny * nz)).T
+ G = wgraph_from_3d_grid(xyz, 26)
+ D = G.weights
+ # 6 * 9 * 10 * 10
+ assert_equal(sum(D == 1), 5400 )
+ # 26 * 8 ** 3 + 6 * 8 ** 2 * 17 + 12 * 8 * 11 + 8 * 7
+ assert_equal(np.size(D), 20952 )
+ # 18 * 8 ** 3 + 6 * 8 ** 2 * 13 + 12 * 8 * 9 + 8 * 6
+ assert_equal(sum(D < 1.5), 15120)
+
+
+def test_grid_3d_5():
+ nx, ny, nz = 5, 5, 5
+ xyz = np.reshape(np.indices((nx, ny, nz)), (3, nx * ny * nz)).T
+ G = wgraph_from_3d_grid(xyz, 26)
+ D = G.weights.copy()
+ G.set_euclidian(xyz)
+ assert_array_almost_equal(G.weights, D)
+
+
+def test_grid_3d_6():
+ nx, ny, nz = 5, 5, 5
+ xyz = np.reshape(np.indices((nx, ny, nz)), (3, nx * ny * nz)).T
+ adj = wgraph_from_3d_grid(xyz, 26).to_coo_matrix().tolil()
+ assert_equal(len(adj.rows[63]), 26)
+ for i in [62, 64, 58, 68, 38, 88, 57, 67, 37, 87, 59, 69, 39, 89, 33,
+ 83, 43, 93, 32, 82, 42, 92, 34, 84, 44, 94]:
+ assert_true(i in adj.rows[63])
+
+
+def test_grid_3d_7():
+ """ Check that the grid graph is symmetric
+ """
+ xyz = np.array(np.where(np.random.rand(5, 5, 5) > 0.5)).T
+ adj = wgraph_from_3d_grid(xyz, 6).to_coo_matrix()
+ assert_equal((adj - adj.T).nnz, 0)
+ adj = wgraph_from_3d_grid(xyz, 18).to_coo_matrix()
+ assert_equal((adj - adj.T).nnz, 0)
+ adj = wgraph_from_3d_grid(xyz, 26).to_coo_matrix()
+ assert_equal((adj - adj.T).nnz, 0)
- def test_compact_representation(self):
- """ Test that the compact representation of the graph is indeed correct
- """
- G = basic_graph()
- idx, ne, we = G.compact_neighb()
- assert len(idx) == 21
- assert idx[0] == 0
- assert idx[20] == G.E
- assert len(ne) == G.E
- assert len(we) == G.E
-
- def test_floyd_1(self):
- """ Test Floyd's algo without seed
- """
- G = basic_graph()
- l = G.floyd()
- for i in range(10):
- plop = np.absolute(np.diag(l, i) - 2 * i * np.sin(2 * np.pi / 40))
- assert(plop.max()<1.e-4)
-
- def test_floyd_2(self):
- """ Test Floyd's algo, with seed
- """
- G = basic_graph()
- seeds = np.array([0,10])
- l = G.floyd(seeds)
+
+def test_cut_redundancies():
+ G = basic_graph()
+ e = G.E
+ edges = G.get_edges()
+ weights = G.weights
+ G.E = 2 * G.E
+ G.edges = np.concatenate((edges, edges))
+ G.weights = np.concatenate((weights, weights))
+ K = G.cut_redundancies()
+ assert_equal(K.E, e)
+
+
+def test_degrees():
+ G = basic_graph()
+ (r, l) = G.degrees()
+ assert_true((r == 2).all())
+ assert_true((l == 2).all())
+
+
+def test_normalize():
+ G = basic_graph()
+ G.normalize()
+ M = G.to_coo_matrix()
+ sM = np.array(M.sum(1)).ravel()
+ assert_true((np.abs(sM - 1) < 1.e-7).all())
+
+
+def test_normalize_2():
+ G = basic_graph()
+ G.normalize(0)
+ M = G.to_coo_matrix()
+ sM = np.array(M.sum(1)).ravel()
+ assert_true((np.abs(sM - 1) < 1.e-7).all())
+
+
+def test_normalize_3():
+ G = basic_graph()
+ G.normalize(1)
+ M = G.to_coo_matrix()
+ sM = np.array(M.sum(0)).ravel()
+ assert_true((np.abs(sM - 1) < 1.e-7).all())
+
+
+def test_adjacency():
+ G = basic_graph()
+ M = G.to_coo_matrix()
+ assert_true(( M.diagonal() == 0 ).all())
+ A = M.toarray()
+ assert_true(( np.diag(A, 1) != 0 ).all())
+ assert_true(( np.diag(A, -1) != 0 ).all())
+
+
+def test_cc():
+ G = basic_graph()
+ l = G.cc()
+ L = np.array(l==0)
+ assert_true(L.all())
+
+
+def test_isconnected():
+ G = basic_graph()
+ assert_true(G.is_connected())
+
+
+def test_main_cc():
+ x = basicdata()
+ G = knn(x, 1)
+ l = G.cc()
+ l = G.main_cc()
+ assert_equal(np.size(l), 6)
+
+def test_dijkstra():
+ """ Test dijkstra's algorithm
+ """
+ G = basic_graph()
+ l = G.dijkstra(0)
+ assert_true(np.abs(l[10] - 20 * np.sin(np.pi / 20)) < 1.e-7)
+
+def test_dijkstra_multiseed():
+ """ Test dijkstra's algorithm, multi_seed version
+ """
+ G = basic_graph()
+ l = G.dijkstra([0, 1])
+ assert_true(np.abs(l[10] - 18 * np.sin(np.pi / 20)) < 1.e-7)
+
+
+def test_dijkstra2():
+ """ Test dijkstra's algorithm, API detail
+ """
+ G = basic_graph()
+ l = G.dijkstra()
+ assert_true(np.abs(l[10] - 20 * np.sin(np.pi / 20)) < 1.e-7)
+
+
+def test_compact_representation():
+ """ Test that the compact representation of the graph is indeed correct
+ """
+ G = basic_graph()
+ idx, ne, we = G.compact_neighb()
+ assert_equal(len(idx), 21)
+ assert_equal(idx[0], 0)
+ assert_equal(idx[20], G.E)
+ assert_equal(len(ne), G.E)
+ assert_equal(len(we), G.E)
+
+
+def test_floyd_1():
+ """ Test Floyd's algo without seed
+ """
+ G = basic_graph()
+ l = G.floyd()
+ for i in range(10):
+ plop = np.abs(np.diag(l, i) - 2 * i * np.sin(2 * np.pi / 40))
+ assert_true(plop.max() < 1.e-4)
- for i in range(10):
- plop = np.absolute(l[0,i]-2*i*np.sin(2*np.pi/40))
- assert (plop.max()<1.e-4)
- plop = np.absolute(l[0,19-i]-2*(i+1)*np.sin(2*np.pi/40))
- assert (plop.max()<1.e-4)
-
- for i in range(10):
- plop = np.absolute(l[1,i]-2*(10-i)*np.sin(2*np.pi/40))
- assert (plop.max()<1.e-4)
- plop = np.absolute(l[1,19-i]-2*(9-i)*np.sin(2*np.pi/40))
- assert (plop.max()<1.e-4)
+def test_floyd_2():
+ """ Test Floyd's algo, with seed
+ """
+ G = basic_graph()
+ seeds = np.array([0,10])
+ l = G.floyd(seeds)
+
+ for i in range(10):
+ plop = np.abs(l[0, i] - 2 * i * np.sin(2 * np.pi / 40))
+ assert_true(plop.max() < 1.e-4)
+ plop = np.abs(l[0,19 - i] - 2 * (i + 1) * np.sin(2 * np.pi / 40))
+ assert_true(plop.max() < 1.e-4)
+
+ for i in range(10):
+ plop = np.abs(l[1, i] - 2 * (10 - i) * np.sin(2 * np.pi / 40))
+ assert_true(plop.max() < 1.e-4)
+ plop = np.abs(l[1, 19 - i] - 2 * (9 - i) * np.sin(2 * np.pi / 40))
+ assert_true(plop.max() < 1.e-4)
- def test_symmeterize(self):
- a = np.array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6])
- b = np.array([1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 0, 0, 1])
- edges = np.vstack((a, b)).T
- d = np.ones(14)
- G = WeightedGraph(7, edges, d)
- G.symmeterize()
- d = G.weights
- ok = (d == 0.5)
- self.assert_(ok.all())
-
- def test_voronoi(self):
- """ test voronoi labelling with 2 seeds
- """
- a = np.array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6])
- b = np.array([1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 0, 0, 1])
- d = np.array([1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]);
- edges = np.transpose(np.vstack((a, b)))
- G = WeightedGraph(7, edges,d)
- G.symmeterize()
- seed = np.array([0, 6])
- label = G.voronoi_labelling(seed)
- assert(label[1] == 0)
-
- def test_voronoi2(self):
- """ test voronoi labelling with one seed
- """
- a = np.array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6])
- b = np.array([1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 0, 0, 1])
- d = np.array([1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]);
- edges = np.vstack((a, b)).T
- G = WeightedGraph(7, edges,d)
- G.symmeterize()
- seed = np.array([0])
- label = G.voronoi_labelling(seed)
- assert(label[4] == 0)
-
- def test_voronoi3(self):
- """ test voronoi labelling with non-connected components
- """
- a = np.array([0, 1, 2, 5, 6])
- b = np.array([1, 2, 3, 6, 0])
- d = np.array([1, 1, 1, 1, 1]);
- edges = np.vstack((a, b)).T
- G = WeightedGraph(7, edges,d)
- G.symmeterize()
- seed = np.array([0])
- label = G.voronoi_labelling(seed)
- assert(label[4] == - 1)
-
- def test_concatenate1(self,n=10,verbose=0):
- x1 = nr.randn(n,2)
- x2 = nr.randn(n,2)
- G1 = knn(x1, 5)
- G2 = knn(x2, 5)
- G = concatenate_graphs(G1, G2)
- if verbose:
- G.plot(np.hstack((x1, x2)))
- self.assert_(G.cc().max()>0)
-
- def test_concatenate2(self,n=10,verbose=0):
- G1 = complete_graph(n)
- G2 = complete_graph(n)
- G = concatenate_graphs(G1, G2)
- self.assert_(G.cc().max() == 1)
-
- def test_anti_symmeterize(self,verbose=0):
- n = 10
- eps = 1.e-7
- M = (nr.rand(n, n) > 0.7).astype(np.float)
- C = M - M.T
- G = wgraph_from_adjacency(M)
- G.anti_symmeterize()
- A = G.to_coo_matrix()
- self.assert_(np.sum(C - A) ** 2 < eps)
-
- def test_subgraph_1(self,n=10,verbose=0):
- x = nr.randn(n, 2)
- G = WeightedGraph(x.shape[0])
- valid = np.zeros(n)
- g = G.subgraph(valid)
- self.assert_(g is None)
-
- def test_subgraph_2(self,n=10,verbose=0):
- x = nr.randn(n,2)
- G = knn(x, 5)
- valid = np.zeros(n)
- valid[:n/2] = 1
- g = G.subgraph(valid)
- self.assert_(g.edges.max() < n / 2)
-
- def test_graph_create_from_array(self):
- """
- Test the creation of a graph from a sparse coo_matrix
- """
- a = np.random.randn(5, 5)
- wg = wgraph_from_adjacency(a)
- b = wg.to_coo_matrix()
- self.assert_((a == b.todense()).all())
-
- def test_graph_create_from_coo_matrix(self):
- """
- Test the creation of a graph from a sparse coo_matrix
- """
- import scipy.sparse as spp
- a = (np.random.randn(5, 5) > .8).astype(np.float)
- s = spp.coo_matrix(a)
- wg = wgraph_from_coo_matrix(s)
- b = wg.to_coo_matrix()
- self.assert_((b.todense() == a).all())
-
- def test_to_coo_matrix(self):
- """ Test the generation of a sparse matrix as output
- """
- a = (np.random.randn(5, 5)>.8).astype(np.float)
- wg = wgraph_from_adjacency(a)
- b = wg.to_coo_matrix().todense()
- self.assert_((a==b).all())
+def test_symmeterize():
+ a = np.array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6])
+ b = np.array([1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 0, 0, 1])
+ edges = np.vstack((a, b)).T
+ d = np.ones(14)
+ G = WeightedGraph(7, edges, d)
+ G.symmeterize()
+ d = G.weights
+ assert_true((d == 0.5).all())
+
+
+def test_voronoi():
+ """ test voronoi labelling with 2 seeds
+ """
+ a = np.array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6])
+ b = np.array([1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 0, 0, 1])
+ d = np.array([1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]);
+ edges = np.transpose(np.vstack((a, b)))
+ G = WeightedGraph(7, edges,d)
+ G.symmeterize()
+ seed = np.array([0, 6])
+ label = G.voronoi_labelling(seed)
+ assert_equal(label[1], 0)
- def test_list_neighbours(self):
- """ test the generation of neighbours list
- """
- bg = basic_graph()
- nl = bg.list_of_neighbors()
- assert(len(nl) == bg.V)
- for ni in nl:
- assert len(ni)== 2
- def test_kruskal(self):
- """ test Kruskal's algor to thin the graph
- """
- x = basicdata()
- dmax = np.sqrt((x ** 2).sum())
- m = mst(x)
- g = eps_nn(x, dmax)
- k = g.kruskal()
- assert np.abs(k.weights.sum() - m.weights.sum() < 1.e-7)
-
- def test_concatenate3(self):
- """ test the graph concatenation utlitity
- """
- bg = basic_graph()
- cg = concatenate_graphs(bg, bg)
- valid = np.zeros(cg.V)
- valid[:bg.V] = 1
- sg = cg.subgraph(valid)
- assert (sg.edges == bg.edges).all()
- assert (sg.weights == bg.weights).all()
-
- def test_cliques(self):
- """ test the computation of cliques
- """
- x = np.random.rand(20, 2)
- x[15:] += 2.
- g = knn(x, 5)
- g.set_gaussian(x, 1.)
- cliques = g.cliques()
- assert len(np.unique(cliques)) > 1
+def test_voronoi2():
+ """ test voronoi labelling with one seed
+ """
+ a = np.array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6])
+ b = np.array([1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 0, 0, 1])
+ d = np.array([1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]);
+ edges = np.vstack((a, b)).T
+ G = WeightedGraph(7, edges,d)
+ G.symmeterize()
+ seed = np.array([0])
+ label = G.voronoi_labelling(seed)
+ assert_equal(label[4], 0)
+
+
+def test_voronoi3():
+ """ test voronoi labelling with non-connected components
+ """
+ a = np.array([0, 1, 2, 5, 6])
+ b = np.array([1, 2, 3, 6, 0])
+ d = np.array([1, 1, 1, 1, 1]);
+ edges = np.vstack((a, b)).T
+ G = WeightedGraph(7, edges,d)
+ G.symmeterize()
+ seed = np.array([0])
+ label = G.voronoi_labelling(seed)
+ assert_equal(label[4], - 1)
+
+def test_concatenate1(n=10):
+ x1 = nr.randn(n, 2)
+ x2 = nr.randn(n, 2)
+ G1 = knn(x1, 5)
+ G2 = knn(x2, 5)
+ G = concatenate_graphs(G1, G2)
+ assert_true(G.cc().max() > 0)
+
+
+def test_concatenate2(n=10):
+ G1 = complete_graph(n)
+ G2 = complete_graph(n)
+ G = concatenate_graphs(G1, G2)
+ assert_true(G.cc().max() == 1)
+
+
+def test_anti_symmeterize():
+ n = 10
+ eps = 1.e-7
+ M = (nr.rand(n, n) > 0.7).astype(np.float)
+ C = M - M.T
+ G = wgraph_from_adjacency(M)
+ G.anti_symmeterize()
+ A = G.to_coo_matrix()
+ assert_true(np.sum(C - A) ** 2 < eps)
+
+
+def test_subgraph_1(n=10):
+ x = nr.randn(n, 2)
+ G = WeightedGraph(x.shape[0])
+ valid = np.zeros(n)
+ assert(G.subgraph(valid) is None)
+
+
+def test_subgraph_2(n=10):
+ x = nr.randn(n, 2)
+ G = knn(x, 5)
+ valid = np.zeros(n)
+ valid[:n / 2] = 1
+ assert_true(G.subgraph(valid).edges.max() < n / 2)
+
+
+def test_graph_create_from_array():
+ """Test the creation of a graph from a sparse coo_matrix
+ """
+ a = np.random.randn(5, 5)
+ wg = wgraph_from_adjacency(a)
+ b = wg.to_coo_matrix()
+ assert_array_equal(a, b.todense())
+
+
+def test_graph_create_from_coo_matrix():
+ """Test the creation of a graph from a sparse coo_matrix
+ """
+ import scipy.sparse as spp
+ a = (np.random.randn(5, 5) > .8).astype(np.float)
+ s = spp.coo_matrix(a)
+ wg = wgraph_from_coo_matrix(s)
+ b = wg.to_coo_matrix()
+ assert_array_equal(b.todense(), a)
+
+
+def test_to_coo_matrix():
+ """ Test the generation of a sparse matrix as output
+ """
+ a = (np.random.randn(5, 5)>.8).astype(np.float)
+ wg = wgraph_from_adjacency(a)
+ b = wg.to_coo_matrix().todense()
+ assert_array_equal(a, b)
+
+
+def test_list_neighbours():
+ """ test the generation of neighbours list
+ """
+ bg = basic_graph()
+ nl = bg.list_of_neighbors()
+ assert_equal(len(nl), bg.V)
+ for ni in nl:
+ assert_equal(len(ni), 2)
+
+
+def test_kruskal():
+ """ test Kruskal's algor to thin the graph
+ """
+ x = basicdata()
+ dmax = np.sqrt((x ** 2).sum())
+ m = mst(x)
+ g = eps_nn(x, dmax)
+ k = g.kruskal()
+ assert_almost_equal(k.weights.sum(), m.weights.sum())
+
+
+def test_concatenate3():
+ """ test the graph concatenation utlitity
+ """
+ bg = basic_graph()
+ cg = concatenate_graphs(bg, bg)
+ valid = np.zeros(cg.V)
+ valid[:bg.V] = 1
+ sg = cg.subgraph(valid)
+ assert_array_equal(sg.edges, bg.edges)
+ assert_array_equal(sg.weights, bg.weights)
+
+
+def test_cliques():
+ """ test the computation of cliques
+ """
+ x = np.random.rand(20, 2)
+ x[15:] += 2.
+ g = knn(x, 5)
+ g.set_gaussian(x, 1.)
+ cliques = g.cliques()
+ assert_true(len(np.unique(cliques)) > 1)
+
if __name__ == '__main__':
import nose
Please sign in to comment.
Something went wrong with that request. Please try again.