[Scipy-tickets] [SciPy] #1533: toarray on sparse matrices can't handle dtype=bool

SciPy Trac scipy-tickets@scipy....
Sat Oct 8 17:07:04 CDT 2011

#1533: toarray on sparse matrices can't handle dtype=bool
 Reporter:  larsmans      |       Owner:  wnbell     
     Type:  defect        |      Status:  new        
 Priority:  normal        |   Milestone:  Unscheduled
Component:  scipy.sparse  |     Version:  0.9.0      
 Keywords:                |  
 It seems `toarray` on sparse matrices doesn't work if they were
 constructed with an explicit `dtype=bool`.

 The following works:

 >>> import numpy as np
 >>> from scipy.sparse import *
 >>> X = np.ones(10, dtype=bool)
 >>> csr_matrix(X).toarray()
 array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=int8)

 But now the `dtype` has changed. I guess the space requirements are the
 same, but it prints `1` instead of `True`. So,

 >>> csr_matrix(X, dtype=bool)
 <1x10 sparse matrix of type '<type 'numpy.bool_'>'
         with 10 stored elements in Compressed Sparse Row format>

 Ok, that works too, but I want to print the elements...

 >>> csr_matrix(X, dtype=bool).toarray()
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/scratch/lars/apps/lib/python2.6/site-
 line 550, in toarray
     return self.tocoo(copy=False).toarray()
   File "/scratch/lars/apps/lib/python2.6/site-
 packages/scipy-0.9.0-py2.6-linux-x86_64.egg/scipy/sparse/coo.py", line
 221, in toarray
     coo_todense(M, N, self.nnz, self.row, self.col, self.data, B.ravel())
   File "/scratch/lars/apps/lib/python2.6/site-
 line 172, in coo_todense
     return _coo.coo_todense(*args)
 TypeError: Array of type 'byte' required.  Array of type 'bool' given

 The problem seems to be somewhere in the `coo_matrix` code; the same
 happens with all other sparse matrix types, except for `lil_matrix`:

 >>> lil_matrix(X, dtype=bool).toarray()
 array([[ True,  True,  True,  True,  True,  True,  True,  True,  True,
          True]], dtype=bool)

Ticket URL: <http://projects.scipy.org/scipy/ticket/1533>
SciPy <http://www.scipy.org>
SciPy is open-source software for mathematics, science, and engineering.

More information about the Scipy-tickets mailing list