# [Scipy-svn] r6945 - in trunk/scipy/spatial: . tests

scipy-svn@scip... scipy-svn@scip...
Fri Nov 26 18:31:04 CST 2010

Author: warren.weckesser
Date: 2010-11-26 18:31:04 -0600 (Fri, 26 Nov 2010)
New Revision: 6945

Modified:
trunk/scipy/spatial/distance.py
trunk/scipy/spatial/tests/test_distance.py
Log:
BUG: spatial: Raise a ValueError in sokalsneath if both vectors are entirely false (ticket #876).  Also fixed the double appearance of the factor 2 in the docstrings for sokalsneath and sokalmichener.

Modified: trunk/scipy/spatial/distance.py
===================================================================
--- trunk/scipy/spatial/distance.py	2010-11-26 23:41:53 UTC (rev 6944)
+++ trunk/scipy/spatial/distance.py	2010-11-27 00:31:04 UTC (rev 6945)
@@ -739,8 +739,8 @@

.. math::

-       \frac{2R}
-            {S + 2R}
+       \frac{R}
+            {S + R}

where :math:c_{ij} is the number of occurrences of
:math:\mathtt{u[k]} = i and :math:\mathtt{v[k]} = j for
@@ -775,8 +775,8 @@

.. math::

-       \frac{2R}
-            {c_{TT} + 2R}
+       \frac{R}
+            {c_{TT} + R}

where :math:c_{ij} is the number of occurrences of
:math:\mathtt{u[k]} = i and :math:\mathtt{v[k]} = j for
@@ -799,7 +799,11 @@
else:
ntt = (u * v).sum()
(nft, ntf) = _nbool_correspond_ft_tf(u, v)
-    return float(2.0 * (ntf + nft))/float(ntt + 2.0 * (ntf + nft))
+    denom = ntt + 2.0 * (ntf + nft)
+    if denom == 0:
+        raise ValueError('Sokal-Sneath dissimilarity is not defined for '
+                            'vectors that are entirely false.')
+    return float(2.0 * (ntf + nft)) / denom

def pdist(X, metric='euclidean', p=2, V=None, VI=None):

Modified: trunk/scipy/spatial/tests/test_distance.py
===================================================================
--- trunk/scipy/spatial/tests/test_distance.py	2010-11-26 23:41:53 UTC (rev 6944)
+++ trunk/scipy/spatial/tests/test_distance.py	2010-11-27 00:31:04 UTC (rev 6945)
@@ -37,7 +37,8 @@
import os.path

import numpy as np
-from numpy.testing import verbose, TestCase, run_module_suite
+from numpy.testing import verbose, TestCase, run_module_suite, \
+        assert_raises
from scipy.spatial.distance import squareform, pdist, cdist, matching, \
jaccard, dice, sokalsneath, rogerstanimoto, \
russellrao, yule, num_obs_y, num_obs_dm, \
@@ -1700,5 +1701,11 @@
y = np.random.rand(n*(n-1)/2)
return y

+
+def test_sokalsneath_all_false():
+    """Regression test for ticket #876"""
+    assert_raises(ValueError, sokalsneath, [False, False, False], [False, False, False])
+
+
if __name__=="__main__":
run_module_suite()