[Scipy-svn] r4172 - in trunk/scipy/cluster: . tests

scipy-svn@scip... scipy-svn@scip...
Thu Apr 24 16:41:31 CDT 2008


Author: cookedm
Date: 2008-04-24 16:41:27 -0500 (Thu, 24 Apr 2008)
New Revision: 4172

Modified:
   trunk/scipy/cluster/hierarchy.py
   trunk/scipy/cluster/tests/test_hierarchy.py
Log:
scipy.cluster: float96 is not guaranteed to exist.
* If you're testing if a an array, if floating-point, is of doubles, check the dtype against floating and double.
* Use longdouble instead of float96 in the tests
* Fix some other dtype uses


Modified: trunk/scipy/cluster/hierarchy.py
===================================================================
--- trunk/scipy/cluster/hierarchy.py	2008-04-24 05:03:09 UTC (rev 4171)
+++ trunk/scipy/cluster/hierarchy.py	2008-04-24 21:41:27 UTC (rev 4172)
@@ -208,8 +208,8 @@
     """
     if a.base is not None:
         return a.copy()
-    elif (a.dtype == np.float32):
-        return np.float64(a)
+    elif np.issubsctype(a, np.float32):
+        return array(a, dtype=np.double)
     else:
         return a
 
@@ -450,11 +450,10 @@
            same minimum distance. This implementation may chose a
            different minimum than the MATLAB(TM) version.
         """
-    if type(method) != types.StringType:
+    if not isinstance(method, str):
         raise TypeError("Argument 'method' must be a string.")
 
-    if type(y) != _array_type:
-        raise TypeError("Argument 'y' must be a numpy array.")
+    y = np.asarray(y)
 
     s = y.shape
     if len(s) == 1:
@@ -723,10 +722,9 @@
     transformation.
     """
 
-    if type(X) is not _array_type:
-        raise TypeError('The parameter passed must be an array.')
+    X = np.asarray(X)
 
-    if X.dtype != np.double:
+    if not np.issubsctype(X, np.double):
         raise TypeError('A double array must be passed.')
 
     s = X.shape
@@ -744,7 +742,7 @@
             raise ValueError('Incompatible vector size. It must be a binomial coefficient n choose 2 for some integer n >= 2.')
 
         # Allocate memory for the distance matrix.
-        M = np.zeros((d, d), 'double')
+        M = np.zeros((d, d), dtype=np.double)
 
         # Since the C code does not support striding using strides.
         # The dimensions are used instead.
@@ -771,7 +769,7 @@
         d = s[0]
 
         # Create a vector.
-        v = np.zeros(((d * (d - 1) / 2),), 'double')
+        v = np.zeros(((d * (d - 1) / 2),), dtype=np.double)
 
         # Since the C code does not support striding using strides.
         # The dimensions are used instead.
@@ -881,7 +879,9 @@
 
       for k < n.
     """
-    return np.double(scipy.bitwise_and((u != v), scipy.bitwise_or(u != 0, v != 0)).sum()) / np.double(scipy.bitwise_or(u != 0, v != 0).sum())
+    return (np.double(np.bitwise_and((u != v),
+                     np.bitwise_or(u != 0, v != 0)).sum()) 
+            /  np.double(np.bitwise_or(u != 0, v != 0).sum()))
 
 def kulsinski(u, v):
     """
@@ -912,8 +912,9 @@
       n-vectors u and v. V is a m-dimensional vector of component
       variances. It is usually computed among a larger collection vectors.
     """
-    if type(V) is not _array_type or len(V.shape) != 1 or V.shape[0] != u.shape[0] or u.shape[0] != v.shape[0]:
-        raise TypeError('V must be a 1-D numpy array of doubles of the same dimension as u and v.')
+    V = np.asarray(V)
+    if len(V.shape) != 1 or V.shape[0] != u.shape[0] or u.shape[0] != v.shape[0]:
+        raise TypeError('V must be a 1-D array of the same dimension as u and v.')
     return np.sqrt(((u-v)**2 / V).sum())
 
 def cityblock(u, v):
@@ -933,9 +934,8 @@
         (u-v)VI(u-v)^T
       where VI is the inverse covariance matrix.
     """
-    if type(V) is not _array_type:
-        raise TypeError('V must be a 1-D numpy array of doubles of the same dimension as u and v.')
-    return np.sqrt(scipy.dot(scipy.dot((u-v),VI),(u-v).T).sum())
+    V = np.asarray(V)
+    return np.sqrt(np.dot(np.dot((u-v),VI),(u-v).T).sum())
 
 def chebyshev(u, v):
     """
@@ -1301,11 +1301,11 @@
 #           verifiable, but less efficient implementation.
 
 
-    if type(X) is not _array_type:
-        raise TypeError('The parameter passed must be an array.')
+    X = np.asarray(X)
 
-    if X.dtype == np.float32 or X.dtype == np.float96:
-        raise TypeError('Floating point arrays must be 64-bit.')
+    if np.issubsctype(X, np.floating) and not np.issubsctype(X, np.double):
+        raise TypeError('Floating point arrays must be 64-bit (got %r).' %
+        (X.dtype.type,))
 
     # The C code doesn't do striding.
     [X] = _copy_arrays_if_base_present([X])
@@ -1380,7 +1380,7 @@
             if V is not None:
                 if type(V) is not _array_type:
                     raise TypeError('Variance vector V must be a numpy array')
-                if V.dtype != np.float64:
+                if V.dtype != np.double:
                     raise TypeError('Variance vector V must contain doubles.')
                 if len(V.shape) != 1:
                     raise ValueError('Variance vector V must be one-dimensional.')
@@ -1417,7 +1417,7 @@
             if VI is not None:
                 if type(VI) != _array_type:
                     raise TypeError('VI must be a numpy array.')
-                if VI.dtype != np.float64:
+                if VI.dtype != np.double:
                     raise TypeError('The array must contain 64-bit floats.')
                 [VI] = _copy_arrays_if_base_present([VI])
             else:

Modified: trunk/scipy/cluster/tests/test_hierarchy.py
===================================================================
--- trunk/scipy/cluster/tests/test_hierarchy.py	2008-04-24 05:03:09 UTC (rev 4171)
+++ trunk/scipy/cluster/tests/test_hierarchy.py	2008-04-24 21:41:27 UTC (rev 4172)
@@ -99,15 +99,15 @@
         except:
             self.fail("float32 observation matrices should generate an error in pdist.")
 
-    def test_pdist_raises_type_error_float96(self):
-        "Testing whether passing a float96 observation array generates an exception."
-        X = numpy.zeros((10, 10), dtype=numpy.float96)
+    def test_pdist_raises_type_error_longdouble(self):
+        "Testing whether passing a longdouble observation array generates an exception."
+        X = numpy.zeros((10, 10), dtype=numpy.longdouble)
         try:
             pdist(X, 'euclidean')
         except TypeError:
             pass
         except:
-            self.fail("float96 observation matrices should generate an error in pdist.")
+            self.fail("longdouble observation matrices should generate an error in pdist.")
 
     def test_pdist_var_raises_type_error_float32(self):
         "Testing whether passing a float32 variance matrix generates an exception."
@@ -120,17 +120,17 @@
         except:
             self.fail("float32 V matrices should generate an error in pdist('seuclidean').")
 
-    def test_pdist_var_raises_type_error_float96(self):
-        "Testing whether passing a float96 variance matrix generates an exception."
+    def test_pdist_var_raises_type_error_longdouble(self):
+        "Testing whether passing a longdouble variance matrix generates an exception."
         X = numpy.zeros((10, 10))
-        V = numpy.zeros((10, 10), dtype=numpy.float96)
+        V = numpy.zeros((10, 10), dtype=numpy.longdouble)
 
         try:
             pdist(X, 'seuclidean', V=V)
         except TypeError:
             pass
         except:
-            self.fail("float96 matrices should generate an error in pdist('seuclidean').")
+            self.fail("longdouble matrices should generate an error in pdist('seuclidean').")
 
     def test_pdist_ivar_raises_type_error_float32(self):
         "Testing whether passing a float32 variance matrix generates an exception."
@@ -143,17 +143,17 @@
         except:
             self.fail("float32 matrices should generate an error in pdist('mahalanobis').")
 
-    def test_pdist_ivar_raises_type_error_float96(self):
-        "Testing whether passing a float96 variance matrix generates an exception."
+    def test_pdist_ivar_raises_type_error_longdouble(self):
+        "Testing whether passing a longdouble variance matrix generates an exception."
         X = numpy.zeros((10, 10))
-        VI = numpy.zeros((10, 10), dtype=numpy.float96)
+        VI = numpy.zeros((10, 10), dtype=numpy.longdouble)
 
         try:
             pdist(X, 'mahalanobis', VI=VI)
         except TypeError:
             pass
         except:
-            self.fail("float96 matrices should generate an error in pdist('mahalanobis').")
+            self.fail("longdouble matrices should generate an error in pdist('mahalanobis').")
 
     ################### pdist: euclidean
     def test_pdist_euclidean_random(self):



More information about the Scipy-svn mailing list