[Scipy-tickets] [SciPy] #307: Problem with non-Int indices for sparse.lil_matrix

SciPy scipy-tickets at scipy.net
Tue Nov 14 07:21:03 CST 2006

#307: Problem with non-Int indices for sparse.lil_matrix
 Reporter:  NeilenMarais  |       Owner:  somebody
     Type:  defect        |      Status:  new     
 Priority:  normal        |   Milestone:          
Component:  scipy.sparse  |     Version:          
 Severity:  normal        |    Keywords:          
 While {{{sparse.lil_matrix.__setitem__}}} no longer rejects int-castable
 indices, the python list type still seems unhappy with certain types. On
 an AMD64 Ubuntu Dapper machine (Python 2.4.3), scipy rev 2316 and numpy
 rev 3433 I get:

 In [1]: import numpy as N
 In [2]: from scipy import sparse
 In [3]: blah = sparse.lil_matrix((2,2))
 In [4]: a = N.array([0,1], N.int64)
 In [5]: b = N.array([0,1], N.int32)
 In [6]: blah[a[1],a[0]]=10
 In [7]: blah[b[1],b[0]]=10
 exceptions.TypeError                                 Traceback (most
 recent call last)

 /home/brick/akademie/NewCode/working/<ipython console>

 /usr/lib/python2.4/site-packages/scipy/sparse/sparse.py in
 __setitem__(self, index, x)
    2518         # Below here, i is an integer
 -> 2519         row = self.rows[i]
    2520         if isintlike(j):
    2521             if not (j>=0 and j<self.shape[1]):

 TypeError: list indices must be integers

 If I get pdb to stop there, I see

 In [8]: blah[b[1],b[0]]=10
 > /usr/lib/python2.4/site-
 -> row = self.rows[i]
 (Pdb) type(i)
 Out[8]: <type 'numpy.int32'>

 and for the int64 index:

 In [6]: blah[a[1],a[0]]=10
 > /usr/lib/python2.4/site-
 -> row = self.rows[i]
 (Pdb) i
 Out[6]: 1
 (Pdb) type(i)
 Out[6]: <type 'numpy.int64'>

 It seems that python lists don't like non-Int-sized integers as indices.
 Not sure if this is a problem with numpy or if it is an unavoidable python

 The attached patch fixes the problem by casting to int, but I'm not sure
 if this is the correct way to fix it.

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

More information about the Scipy-tickets mailing list