roly: double heap moving median

10 views
Skip to first unread message

Keith Goodman

unread,
May 2, 2011, 3:39:04 PM5/2/11
to bottl...@googlegroups.com, Wes McKinney, J. David Lee, Richard Harter
I made a first attempt to cython wrap Richard's double heap moving
median C code.

It runs but it gives the wrong output (see below). Any ideas what
could be causing the problem?

I made some changes to Richard's code as you can see here:
https://github.com/kwgoodman/roly/commit/2c5df3a16e5af3df38151a8b4e7cba3a69ff4293

I changed the array values from int to double. And instead of
supplying a next_value() function that gives the next value of the
array to the double heap initialization, I passed in a pointer to the
first array element.

Here's the output I get:

>> import roly
>> a = np.arange(10).astype('float64')
>> a
array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
>> roly.slow.move_median(a, 3)
array([ nan, nan, 1., 2., 3., 4., 5., 6., 7., 8.])
>> roly.linkedlist.move_median(a, 3)
array([ nan, nan, 1., 2., 3., 4., 5., 6., 7., 8.])
>> roly.doubleheap.move_median(a, 3)
array([ nan, nan, 4., 4., 4., 5., 5., 6., 6., 6.])

Here are the results for some other window values in case they mean anything:

>> roly.doubleheap.move_median(a, 2)
array([ nan, 4., 4., 4., 4., 5., 5., 6., 6., 6.])
>> roly.doubleheap.move_median(a, 3)
array([ nan, nan, 4., 4., 4., 5., 5., 6., 6., 6.])
>> roly.doubleheap.move_median(a, 4)
array([ nan, nan, nan, 4., 4., 5., 5., 5., 6., 7.])
>> roly.doubleheap.move_median(a, 5)
array([ nan, nan, nan, nan, 4., 5., 5., 6., 6., 7.])
>> roly.doubleheap.move_median(a, 6)
array([ nan, nan, nan, nan, nan, 4., 4., 4., 6., 7.])
>> roly.doubleheap.move_median(a, 7)
array([ nan, nan, nan, nan, nan, nan, 4., 4., 4., 7.])
>> roly.doubleheap.move_median(a, 8)
array([ nan, nan, nan, nan, nan, nan, nan, 4., 4., 4.])
>> roly.doubleheap.move_median(a, 9)
array([ nan, nan, nan, nan, nan, nan, nan, nan, 4., 4.])
>> roly.doubleheap.move_median(a, 10)
array([ nan, nan, nan, nan, nan, nan, nan, nan, nan, 4.])

Keith Goodman

unread,
May 2, 2011, 3:52:13 PM5/2/11
to bottl...@googlegroups.com, Wes McKinney, J. David Lee, Richard Harter
On Mon, May 2, 2011 at 12:39 PM, Keith Goodman <kwgo...@gmail.com> wrote:

> I made a first attempt to cython wrap Richard's double heap moving
> median C code.
>
> It runs but it gives the wrong output (see below). Any ideas what
> could be causing the problem?

Found it. I told the double heap thingy to use a window equal to the
length of the input array instead of the window that the user inputs.

Works now:

>> a = np.arange(10).astype('float64')

>> import roly
>> roly.doubleheap.move_median(a, 3)

Reply all
Reply to author
Forward
0 new messages