Hi Zach,

I have a similar loop which I wrote using scipy.weave. This was my first
foray into weave, and I had to dig through the intermediate C sources to
find the macros that did the indexing in the way I make use of here, but
this snipped may get you started. There are 2 functions, which each do
the same thing, but one is in python, the other is in C. Note that this
is for a 3D histogram -- presumably you could remove B and C from this
example.

I'm sure there are better (more documented) ways to do this using weave
-- but I had this code written, it works, and it appears it may be
useful to you... (Sorry it's not documented, however.)

-Andrew

Zachary Pincus wrote:
> Hi,
>
>   
>> How about a combination of sort, followed by searchsorted right/left  
>> using the bin boundaries as keys? The difference of the two  
>> resulting vectors is the bin value. Something like:
>>
>> In [1]: data = arange(100)
>>
>> In [2]: bins = [0,10,50,70,100]
>>
>> In [3]: lind = data.searchsorted(bins)
>>
>> In [4]: print lind[1:] - lind[:-1]
>> [10 40 20 30]
>>
>> This won't be as fast as a c implementation, but at least avoids the  
>> loop.
>>     
>
> This is, more or less, what the current numpy.histogram does, no? I  
> was hoping to avoid the O(n log n) sorting, because the image arrays  
> are pretty big, and numpy.histogram doesn't get close to video rate  
> for me...
>
> Perhaps, though, some of the slow-down from numpy.histogram is from  
> other overhead, and not the sorting. I'll try this, but I think I'll  
> probably just have to write the c loop...
>
> Zach
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>   

from scipy import weave
import numpy

def increment_slow(final_array_shape,  A,B,C):
    counts = numpy.zeros(final_array_shape, dtype=numpy.uint64)
    for i in range(len(A)):
        a=A[i]; b=B[i]; c=C[i]
        counts[a,b,c] += 1
    return counts

def increment_fast(final_array_shape,  idxa,idxb,idxc):
    counts = numpy.zeros(final_array_shape, dtype=numpy.uint64)
    assert len(idxa.shape)==1
    assert len(idxa)==len(idxb)
    assert len(idxa)==len(idxc)

    code = r"""
    for (int i=0; i<Nidxa[0]; i++) {
        COUNTS3( IDXA1(i), IDXB1(i), IDXC1(i) )++;
    }
    """
    weave.inline( code, ['counts', 'idxa','idxb','idxc'])
    return counts

#increment = increment_slow
increment = increment_fast
_______________________________________________
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to