[Scipy-svn] r5007 - in trunk/scipy/cluster: . tests
scipy-svn at scipy.org
scipy-svn at scipy.org
Fri Nov 7 14:52:57 EST 2008
Author: damian.eads
Date: 2008-11-07 13:52:54 -0600 (Fri, 07 Nov 2008)
New Revision: 5007
Modified:
trunk/scipy/cluster/hierarchy.py
trunk/scipy/cluster/tests/test_hierarchy.py
Log:
Added tests for to_mlab_linkage function.
Modified: trunk/scipy/cluster/hierarchy.py
===================================================================
--- trunk/scipy/cluster/hierarchy.py 2008-11-07 19:41:42 UTC (rev 5006)
+++ trunk/scipy/cluster/hierarchy.py 2008-11-07 19:52:54 UTC (rev 5007)
@@ -1071,11 +1071,17 @@
A linkage matrix compatible with MATLAB(TM)'s hierarchical
clustering functions.
"""
- Z = np.asarray(Z, order='c')
+ Z = np.asarray(Z, order='c', dtype=np.double)
+ Zs = Z.shape
+ if len(Zs) == 0 or (len(Zs) == 1 and Zs[0] == 0):
+ return Z.copy()
is_valid_linkage(Z, throw=True, name='Z')
- return np.hstack([Z[:,0:2] + 1, Z[:,2]])
+ ZP = Z[:, 0:3].copy()
+ ZP[:,0:2] += 1.0
+ return ZP
+
def is_monotonic(Z):
"""
Returns ``True`` if the linkage passed is monotonic. The linkage
Modified: trunk/scipy/cluster/tests/test_hierarchy.py
===================================================================
--- trunk/scipy/cluster/tests/test_hierarchy.py 2008-11-07 19:41:42 UTC (rev 5006)
+++ trunk/scipy/cluster/tests/test_hierarchy.py 2008-11-07 19:52:54 UTC (rev 5007)
@@ -38,7 +38,7 @@
import numpy as np
from numpy.testing import *
-from scipy.cluster.hierarchy import linkage, from_mlab_linkage, numobs_linkage, inconsistent, cophenet, from_mlab_linkage
+from scipy.cluster.hierarchy import linkage, from_mlab_linkage, to_mlab_linkage, numobs_linkage, inconsistent, cophenet, from_mlab_linkage
from scipy.spatial.distance import squareform, pdist, numobs_dm, numobs_y
_tdist = np.array([[0, 662, 877, 255, 412, 996],
@@ -216,6 +216,37 @@
print expectedZS, ZS
self.failUnless((expectedZS == ZS).all())
+
+class TestToMLabLinkage(TestCase):
+
+ def test_to_mlab_linkage_empty(self):
+ "Testing to_mlab_linkage on empty linkage array."
+ X = np.asarray([])
+ R = to_mlab_linkage([])
+ self.failUnless((R == X).all())
+
+ def test_to_mlab_linkage_single_row(self):
+ "Testing to_mlab_linkage on linkage array with single row."
+ Z = np.asarray([[ 0., 1., 3., 2.]])
+ expectedZP = np.asarray([[1,2,3]])
+ ZP = to_mlab_linkage(Z)
+ return self.failUnless((ZP == expectedZP).all())
+
+ def test_from_mlab_linkage_multiple_rows(self):
+ "Testing to_mlab_linkage on linkage array with multiple rows."
+ expectedZM = np.asarray([[3, 6, 138], [4, 5, 219],
+ [1, 8, 255], [2, 9, 268], [7, 10, 295]])
+ Z = np.array([[ 2., 5., 138., 2.],
+ [ 3., 4., 219., 2.],
+ [ 0., 7., 255., 3.],
+ [ 1., 8., 268., 4.],
+ [ 6., 9., 295., 6.]],
+ dtype=np.double)
+ ZM = to_mlab_linkage(Z)
+ print expectedZM, ZM
+ self.failUnless((expectedZM == ZM).all())
+
+
def help_single_inconsistent_depth(self, i):
Y = squareform(_tdist)
Z = linkage(Y, 'single')
More information about the Scipy-svn
mailing list