Repository: scipy/scipy

Issue:
Sparse matrix operations crash python interpreter (Trac #1267)
_Original ticket http://projects.scipy.org/scipy/ticket/1267 on 2010-08-18 by trac user lutz, assigned to unknown._

The following code snippet consistently crashes the python interpreter, using python 2.6.5, SciPy 0.7.2 or 0.8.0, on OS-X (32 or 64 bit):

```
import numpy as np
import scipy.sparse

content = np.load("test.npz")
C = scipy.sparse.csr_matrix((content["data"], content["indices"], content["indptr"]), shape=content["shape"])
indices = np.array(C.nonzero()).transpose()
for ind in indices:
    C[ind[0],ind[1]] /= 2
```

The file test.npz contains a sparse matrix of medium size (10000x10000), with about 300000 non-zero elements. The file is attached.

The code does not fail during execution, but upon exiting the python interpreter. Presumably something goes wrong when the memory for the sparse matrix object is deallocated. The backtrace looks like this:

```
#0  0x0000000100503644 in array_dealloc ()
#1  0x00000001005036eb in array_dealloc ()
#2  0x00000001005036eb in array_dealloc ()
#3  0x00000001005036eb in array_dealloc ()
#4  0x00000001005036eb in array_dealloc ()

[ ... many more times the exact same line ... ]

#261938 0x00000001005036eb in array_dealloc ()
#261939 0x00000001005036eb in array_dealloc ()
#261940 0x00000001005036eb in array_dealloc ()
#261941 0x00000001005036eb in array_dealloc ()
#261942 0x000000010004f97b in dict_dealloc ()
#261943 0x000000010006cfa5 in subtype_dealloc ()
#261944 0x000000010004e647 in insertdict ()
#261945 0x0000000100050bd2 in PyDict_SetItem ()
#261946 0x0000000100052dad in _PyModule_Clear ()
#261947 0x00000001000ce66d in PyImport_Cleanup ()
#261948 0x00000001000de4a8 in Py_Finalize ()
#261949 0x00000001000ee097 in Py_Main ()
#261950 0x0000000100000f14 in ?? ()
```



Discussion:

Resolving patch:
diff --git a/scipy/stats/distributions.py b/scipy/stats/distributions.py
--- a/scipy/stats/distributions.py
+++ b/scipy/stats/distributions.py
@@ -1965,10 +1965,10 @@ class ksone_gen(rv_continuous):
     %(default)s
 
     """
-    def _cdf(self,x,n):
-        return 1.0-special.smirnov(n,x)
-    def _ppf(self,q,n):
-        return special.smirnovi(n,1.0-q)
+    def _cdf(self, x, n):
+        return 1.0 - special.smirnov(n, x)
+    def _ppf(self, q, n):
+        return special.smirnovi(n, 1.0 - q)
 ksone = ksone_gen(a=0.0, name='ksone', shapes="n")
 
 class kstwobign_gen(rv_continuous):
@@ -6582,7 +6582,7 @@ def _argcheck(self, p):
     def _pmf(self, k, p):
         return (1-p)**(k-1) * p
     def _logpmf(self, k, p):
-        return (k-1)*log(1-p) + p
+        return (k-1)*log(1-p) + log(p)
     def _cdf(self, x, p):
         k = floor(x)
         return (1.0-(1.0-p)**k)


Test patch:
diff --git a/scipy/stats/tests/test_distributions.py b/scipy/stats/tests/test_distributions.py
--- a/scipy/stats/tests/test_distributions.py
+++ b/scipy/stats/tests/test_distributions.py
@@ -182,6 +182,12 @@ def test_pmf(self):
         vals = stats.geom.pmf([1,2,3],0.5)
         assert_array_almost_equal(vals,[0.5,0.25,0.125])
 
+    def test_logpmf(self):
+        # regression test for ticket 1793
+        vals1 = np.log(stats.geom.pmf([1,2,3], 0.5))
+        vals2 = stats.geom.logpmf([1,2,3], 0.5)
+        assert_allclose(vals1, vals2, rtol=1e-15, atol=0)
+
     def test_cdf_sf(self):
         vals = stats.geom.cdf([1,2,3],0.5)
         vals_sf = stats.geom.sf([1,2,3],0.5)
@@ -885,9 +891,7 @@ def test_powerlaw_stats():
 
 
 def test_ksone_fit_freeze():
-    """Regression test for ticket #1638.
-
-    """
+    #Regression test for ticket #1638.
     d = np.array(
         [-0.18879233,  0.15734249,  0.18695107,  0.27908787, -0.248649,
          -0.2171497 ,  0.12233512,  0.15126419,  0.03119282,  0.4365294 ,
@@ -905,6 +909,7 @@ def test_ksone_fit_freeze():
     warn_ctx.__enter__()
     try:
         warnings.simplefilter('ignore', UserWarning)
+        warnings.simplefilter('ignore', RuntimeWarning)
         stats.ksone.fit(d)
     finally:
         warn_ctx.__exit__()