[Scipy-svn] r4760 - trunk/scipy/cluster

scipy-svn@scip... scipy-svn@scip...
Tue Sep 30 19:10:18 CDT 2008


Author: damian.eads
Date: 2008-09-30 19:10:16 -0500 (Tue, 30 Sep 2008)
New Revision: 4760

Modified:
   trunk/scipy/cluster/distance.py
Log:
Added order keyword in asarray statements to ensure contiguity of data prior to passing to C functions.

Modified: trunk/scipy/cluster/distance.py
===================================================================
--- trunk/scipy/cluster/distance.py	2008-09-30 23:55:21 UTC (rev 4759)
+++ trunk/scipy/cluster/distance.py	2008-10-01 00:10:16 UTC (rev 4760)
@@ -193,8 +193,8 @@
        d : double
            The Minkowski distance between vectors ``u`` and ``v``.
     """
-    u = np.asarray(u)
-    v = np.asarray(v)
+    u = np.asarray(u, order='c')
+    v = np.asarray(v, order='c')
     if p < 1:
         raise ValueError("p must be at least 1")
     return (abs(u-v)**p).sum() ** (1.0 / p)
@@ -222,8 +222,8 @@
        d : double
            The Minkowski distance between vectors ``u`` and ``v``.
     """
-    u = np.asarray(u)
-    v = np.asarray(v)
+    u = np.asarray(u, order='c')
+    v = np.asarray(v, order='c')
     if p < 1:
         raise ValueError("p must be at least 1")
     return ((w * abs(u-v))**p).sum() ** (1.0 / p)
@@ -247,8 +247,8 @@
        d : double
            The Euclidean distance between vectors ``u`` and ``v``.
     """
-    u = np.asarray(u)
-    v = np.asarray(v)
+    u = np.asarray(u, order='c')
+    v = np.asarray(v, order='c')
     q=np.matrix(u-v)
     return np.sqrt((q*q.T).sum())
 
@@ -272,8 +272,8 @@
        d : double
            The squared Euclidean distance between vectors ``u`` and ``v``.
     """
-    u = np.asarray(u)
-    v = np.asarray(v)
+    u = np.asarray(u, order='c')
+    v = np.asarray(v, order='c')
     return ((u-v)*(u-v).T).sum()
 
 def cosine(u, v):
@@ -295,8 +295,8 @@
        d : double
            The Cosine distance between vectors ``u`` and ``v``.
     """
-    u = np.asarray(u)
-    v = np.asarray(v)
+    u = np.asarray(u, order='c')
+    v = np.asarray(v, order='c')
     return (1.0 - (np.dot(u, v.T) / \
                    (np.sqrt(np.dot(u, u.T)) * np.sqrt(np.dot(v, v.T)))))
 
@@ -356,8 +356,8 @@
        d : double
            The Hamming distance between vectors ``u`` and ``v``.
     """
-    u = np.asarray(u)
-    v = np.asarray(v)
+    u = np.asarray(u, order='c')
+    v = np.asarray(v, order='c')
     return (u != v).mean()
 
 def jaccard(u, v):
@@ -384,8 +384,8 @@
        d : double
            The Jaccard distance between vectors ``u`` and ``v``.
     """
-    u = np.asarray(u)
-    v = np.asarray(v)
+    u = np.asarray(u, order='c')
+    v = np.asarray(v, order='c')
     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()))
@@ -414,8 +414,8 @@
        d : double
            The Kulsinski distance between vectors ``u`` and ``v``.
     """
-    u = np.asarray(u)
-    v = np.asarray(v)
+    u = np.asarray(u, order='c')
+    v = np.asarray(v, order='c')
     n = len(u)
     (nff, nft, ntf, ntt) = _nbool_correspond_all(u, v)
 
@@ -438,9 +438,9 @@
        d : double
            The standardized Euclidean distance between vectors ``u`` and ``v``.
     """
-    u = np.asarray(u)
-    v = np.asarray(v)
-    V = np.asarray(V)
+    u = np.asarray(u, order='c')
+    v = np.asarray(v, order='c')
+    V = np.asarray(V, order='c')
     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())
@@ -464,8 +464,8 @@
        d : double
            The City Block distance between vectors ``u`` and ``v``.
     """
-    u = np.asarray(u)
-    v = np.asarray(v)
+    u = np.asarray(u, order='c')
+    v = np.asarray(v, order='c')
     return abs(u-v).sum()
 
 def mahalanobis(u, v, VI):
@@ -488,9 +488,9 @@
        d : double
            The Mahalanobis distance between vectors ``u`` and ``v``.
     """
-    u = np.asarray(u)
-    v = np.asarray(v)
-    VI = np.asarray(VI)
+    u = np.asarray(u, order='c')
+    v = np.asarray(v, order='c')
+    VI = np.asarray(VI, order='c')
     return np.sqrt(np.dot(np.dot((u-v),VI),(u-v).T).sum())
 
 def chebyshev(u, v):
@@ -511,8 +511,8 @@
        d : double
            The Chebyshev distance between vectors ``u`` and ``v``.
     """
-    u = np.asarray(u)
-    v = np.asarray(v)
+    u = np.asarray(u, order='c')
+    v = np.asarray(v, order='c')
     return max(abs(u-v))
 
 def braycurtis(u, v):
@@ -534,8 +534,8 @@
        d : double
            The Bray-Curtis distance between vectors ``u`` and ``v``.
     """
-    u = np.asarray(u)
-    v = np.asarray(v)
+    u = np.asarray(u, order='c')
+    v = np.asarray(v, order='c')
     return abs(u-v).sum() / abs(u+v).sum()
 
 def canberra(u, v):
@@ -559,8 +559,8 @@
        d : double
            The Canberra distance between vectors ``u`` and ``v``.
     """
-    u = np.asarray(u)
-    v = np.asarray(v)
+    u = np.asarray(u, order='c')
+    v = np.asarray(v, order='c')
     return abs(u-v).sum() / (abs(u).sum() + abs(v).sum())
 
 def _nbool_correspond_all(u, v):
@@ -626,8 +626,8 @@
        d : double
            The Yule dissimilarity between vectors ``u`` and ``v``.
     """
-    u = np.asarray(u)
-    v = np.asarray(v)
+    u = np.asarray(u, order='c')
+    v = np.asarray(v, order='c')
     (nff, nft, ntf, ntt) = _nbool_correspond_all(u, v)
     return float(2.0 * ntf * nft) / float(ntt * nff + ntf * nft)
 
@@ -654,8 +654,8 @@
        d : double
            The Matching dissimilarity between vectors ``u`` and ``v``.
     """
-    u = np.asarray(u)
-    v = np.asarray(v)
+    u = np.asarray(u, order='c')
+    v = np.asarray(v, order='c')
     (nft, ntf) = _nbool_correspond_ft_tf(u, v)
     return float(nft + ntf) / float(len(u))
 
@@ -683,8 +683,8 @@
        d : double
            The Dice dissimilarity between vectors ``u`` and ``v``.
     """
-    u = np.asarray(u)
-    v = np.asarray(v)
+    u = np.asarray(u, order='c')
+    v = np.asarray(v, order='c')
     if u.dtype == np.bool:
         ntt = (u & v).sum()
     else:
@@ -716,8 +716,8 @@
            The Rogers-Tanimoto dissimilarity between vectors
            ``u`` and ``v``.
     """
-    u = np.asarray(u)
-    v = np.asarray(v)
+    u = np.asarray(u, order='c')
+    v = np.asarray(v, order='c')
     (nff, nft, ntf, ntt) = _nbool_correspond_all(u, v)
     return float(2.0 * (ntf + nft)) / float(ntt + nff + (2.0 * (ntf + nft)))
 
@@ -745,8 +745,8 @@
        d : double
            The Russell-Rao dissimilarity between vectors ``u`` and ``v``.
     """
-    u = np.asarray(u)
-    v = np.asarray(v)
+    u = np.asarray(u, order='c')
+    v = np.asarray(v, order='c')
     if u.dtype == np.bool:
         ntt = (u & v).sum()
     else:
@@ -778,8 +778,8 @@
        d : double
            The Sokal-Michener dissimilarity between vectors ``u`` and ``v``.
     """
-    u = np.asarray(u)
-    v = np.asarray(v)
+    u = np.asarray(u, order='c')
+    v = np.asarray(v, order='c')
     if u.dtype == np.bool:
         ntt = (u & v).sum()
         nff = (~u & ~v).sum()
@@ -813,8 +813,8 @@
        d : double
            The Sokal-Sneath dissimilarity between vectors ``u`` and ``v``.
     """
-    u = np.asarray(u)
-    v = np.asarray(v)
+    u = np.asarray(u, order='c')
+    v = np.asarray(v, order='c')
     if u.dtype == np.bool:
         ntt = (u & v).sum()
     else:
@@ -1052,7 +1052,7 @@
 #           verifiable, but less efficient implementation.
 
 
-    X = np.asarray(X)
+    X = np.asarray(X, order='c')
 
     #if np.issubsctype(X, np.floating) and not np.issubsctype(X, np.double):
     #    raise TypeError('Floating point arrays must be 64-bit (got %r).' %
@@ -1131,7 +1131,7 @@
                                                          dm, p, w)
         elif mstr in set(['seuclidean', 'se', 's']):
             if V is not None:
-                V = np.asarray(V)
+                V = np.asarray(V, order='c')
                 if type(V) != np.ndarray:
                     raise TypeError('Variance vector V must be a numpy array')
                 if V.dtype != np.double:
@@ -1169,7 +1169,7 @@
             _distance_wrap.pdist_cosine_wrap(_convert_to_double(X2), _convert_to_double(dm), _convert_to_double(norms))
         elif mstr in set(['mahalanobis', 'mahal', 'mah']):
             if VI is not None:
-                VI = _convert_to_double(np.asarray(VI))
+                VI = _convert_to_double(np.asarray(VI, order='c'))
                 if type(VI) != np.ndarray:
                     raise TypeError('VI must be a numpy array.')
                 if VI.dtype != np.double:
@@ -1206,7 +1206,7 @@
             if V is None:
                 V = np.var(X, axis=0, ddof=1)
             else:
-                V = np.asarray(V)
+                V = np.asarray(V, order='c')
             dm = pdist(X, lambda u, v: seuclidean(u, v, V))
         elif metric == 'test_braycurtis':
             dm = pdist(X, braycurtis)
@@ -1215,7 +1215,7 @@
                 V = np.cov(X.T)
                 VI = np.linalg.inv(V)
             else:
-                VI = np.asarray(VI)
+                VI = np.asarray(VI, order='c')
             [VI] = _copy_arrays_if_base_present([VI])
             # (u-v)V^(-1)(u-v)^T
             dm = pdist(X, (lambda u, v: mahalanobis(u, v, VI)))
@@ -1310,7 +1310,7 @@
 
     """
 
-    X = _convert_to_double(np.asarray(X))
+    X = _convert_to_double(np.asarray(X, order='c'))
 
     if not np.issubsctype(X, np.double):
         raise TypeError('A double array must be passed.')
@@ -1408,7 +1408,7 @@
        ``D.T`` and non-zeroness of the diagonal are ignored if they are
        within the tolerance specified by ``tol``.
     """
-    D = np.asarray(D)
+    D = np.asarray(D, order='c')
     valid = True
     try:
         if type(D) != np.ndarray:
@@ -1484,7 +1484,7 @@
            warning or exception message.
 
     """
-    y = np.asarray(y)
+    y = np.asarray(y, order='c')
     valid = True
     try:
         if type(y) != np.ndarray:
@@ -1529,7 +1529,7 @@
     :Returns:
        The number of observations in the redundant distance matrix.
     """
-    D = np.asarray(D)
+    D = np.asarray(D, order='c')
     is_valid_dm(D, tol=np.inf, throw=True, name='D')
     return D.shape[0]
 
@@ -1548,7 +1548,7 @@
            The number of observations in the condensed distance matrix
            passed.
     """
-    Y = np.asarray(Y)
+    Y = np.asarray(Y, order='c')
     is_valid_y(Y, throw=True, name='Y')
     d = int(np.ceil(np.sqrt(Y.shape[0] * 2)))
     return d
@@ -1791,8 +1791,8 @@
 #           verifiable, but less efficient implementation.
 
 
-    XA = np.asarray(XA)
-    XB = np.asarray(XB)
+    XA = np.asarray(XA, order='c')
+    XB = np.asarray(XB, order='c')
 
     #if np.issubsctype(X, np.floating) and not np.issubsctype(X, np.double):
     #    raise TypeError('Floating point arrays must be 64-bit (got %r).' %
@@ -1880,7 +1880,7 @@
                                                          _convert_to_double(XB), dm, p, _convert_to_double(w))
         elif mstr in set(['seuclidean', 'se', 's']):
             if V is not None:
-                V = np.asarray(V)
+                V = np.asarray(V, order='c')
                 if type(V) != np.ndarray:
                     raise TypeError('Variance vector V must be a numpy array')
                 if V.dtype != np.double:
@@ -1922,7 +1922,7 @@
                                              _convert_to_double(normsB))
         elif mstr in set(['mahalanobis', 'mahal', 'mah']):
             if VI is not None:
-                VI = _convert_to_double(np.asarray(VI))
+                VI = _convert_to_double(np.asarray(VI, order='c'))
                 if type(VI) != np.ndarray:
                     raise TypeError('VI must be a numpy array.')
                 if VI.dtype != np.double:
@@ -1973,7 +1973,7 @@
             if V is None:
                 V = np.var(np.vstack([XA, XB]), axis=0, ddof=1)
             else:
-                V = np.asarray(V)
+                V = np.asarray(V, order='c')
             dm = cdist(XA, XB, lambda u, v: seuclidean(u, v, V))
         elif metric == 'test_sqeuclidean':
             dm = cdist(XA, XB, lambda u, v: sqeuclidean(u, v))
@@ -1987,7 +1987,7 @@
                 X = None
                 del X
             else:
-                VI = np.asarray(VI)
+                VI = np.asarray(VI, order='c')
             [VI] = _copy_arrays_if_base_present([VI])
             # (u-v)V^(-1)(u-v)^T
             dm = cdist(XA, XB, (lambda u, v: mahalanobis(u, v, VI)))



More information about the Scipy-svn mailing list