thouis / numpy-trac-migration

numpy Trac to github issues migration
2 stars 3 forks source link

bincount does not accept input of type > N.uint16 (Trac #225) #4028

Open thouis opened 11 years ago

thouis commented 11 years ago

Original ticket http://projects.scipy.org/numpy/ticket/225 on 2006-08-03 by atmention:stefanv, assigned to atmention:teoliphant.

Under r2944:

In [22]: N.bincount(N.array([1],N.uint16))
Out[22]: array([0, 1])

In [23]: N.bincount(N.array([1],N.uint32))
---------------------------------------------------------------------------
exceptions.TypeError                                 Traceback (most recent call last)

/home/stefan/work/scipy/hough/<ipython console>

TypeError: array cannot be safely cast to required type
thouis commented 11 years ago

atmention:stefanv wrote on 2006-08-03

Can be improved by changing to NPY_UINTP, but that still won't allow 64-bit integers. Is there a way to easily fix this for 32-bit systems?

thouis commented 11 years ago

Milestone changed to 1.1 by atmention:alberts on 2007-05-12

thouis commented 11 years ago

Milestone changed to Unscheduled by atmention:cournape on 2009-03-02

thouis commented 11 years ago

atmention:mwiebe wrote on 2011-03-23

This function still errors (with uint64 on a 64-bit machine). Bincount is a candidate to convert to using the iterator with buffering, since currently it will cause a copy if the input isn't contiguous and the right type.

thouis commented 11 years ago

atmention:bsouthey wrote on 2011-05-09

My C is not very good but hopefully someone with can correct the patch. The less obvious change (at least was to me) was that PyArray_ContiguousFromAny was being called with unsigned int, PyArray_INTP, rather than PyArray_UINTP.

According to the comment on the bincount code in "numpy/lib/src/_compiled_base.c", the first argument must be an array of non-negative integers (relevant lines):

 * bincount accepts one, two or three arguments. The first is an array of
 * non-negative integers The second, if present, is an array of weights,

Consequently I changed bincount to use unsigned ints instead of signed ints.

The patch is incorrect if the mxx and mnx functions can be used outside of bincount. In that case new functions would have to declared.

thouis commented 11 years ago

Attachment added by atmention:bsouthey on 2011-05-09: 0001-bincount-unsigned-ints.patch

thouis commented 11 years ago

Attachment added by atmention:bsouthey on 2011-05-09: 0002-Redo-bincount-signed-int-change.patch

thouis commented 11 years ago

atmention:bsouthey wrote on 2011-05-09

Okay, still was not that simple! I had to change one PyObject variable into PyArrayObject. This allowed to get the input dtype for PyArray?_ContiguousFromAny. Both patches need to be applied because I do not know how to get a single patch with git without redoing everything - sorry!