[SciPy-User] Strange behaviour of scipy.interpolate.interp1d in with kind="zero"

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

[SciPy-User] Strange behaviour of scipy.interpolate.interp1d in with kind="zero"

Propadovic Nenad
Hello everybody,

please consider the following code:

---
import numpy
from scipy.interpolate import interp1d                   
                   
t = numpy.array([0.000, 0.004, 0.008, 0.012, 0.016, 0.02])
sig = numpy.array([0, 1, 2, 3, 4, 5])
t_new = numpy.array([0.0, 0.001, 0.003,  0.004,  0.008, 0.012,  0.016,  0.0196, 0.02])
               
f = interp1d(t, sig, kind='zero')
s_resampled = f(t_new)
print s_resampled
---

The result I get on my machine is:
>C:\Anaconda2\pythonw -u "test_zero_order_hold.py"
[ 0.  0.  0.  1.  2.  3.  4.  4.  4.]
>Exit code: 0

Now why is the last value 4, and not 5? I'd expect it to be 5, as the last timestamps in t and t_new are identical. Is this a bug in interp1d or am I missing something important - if so, what?

Thanks a lot, in advance,

Nenad

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Strange behaviour of scipy.interpolate.interp1d in with kind="zero"

Oleksandr Huziy
Hi Nenad:

This probably how the kind="zero" is implemented (uses a constant value from the left side of the interval) and since in your case you do not have any data to the right of the point, it is never used. 

Maybe it could be called bug.... (You could try to submit an issue on github)

Apparently, now for it to work as you want, you would need to extend your sig a bit:

import numpy
from scipy.interpolate import interp1d
t = numpy.array([0.000, 0.004, 0.008, 0.012, 0.016, 0.02, 0.03])
sig = numpy.array([0, 1, 2, 3, 4, 5, 6])
t_new = numpy.array([0.0, 0.001, 0.003, 0.004, 0.008, 0.012, 0.016, 0.0196, 0.02])
f = interp1d(t, sig, kind='zero')
s_resampled = f(t_new)
print(s_resampled)
[ 0.  0.  0.  1.  2.  3.  4.  4.  5.]


Cheers






2017-06-21 9:41 GMT-04:00 Propadovic Nenad <[hidden email]>:
Hello everybody,

please consider the following code:

---
import numpy
from scipy.interpolate import interp1d                   
                   
t = numpy.array([0.000, 0.004, 0.008, 0.012, 0.016, 0.02])
sig = numpy.array([0, 1, 2, 3, 4, 5])
t_new = numpy.array([0.0, 0.001, 0.003,  0.004,  0.008, 0.012,  0.016,  0.0196, 0.02])
               
f = interp1d(t, sig, kind='zero')
s_resampled = f(t_new)
print s_resampled
---

The result I get on my machine is:
>C:\Anaconda2\pythonw -u "test_zero_order_hold.py"
[ 0.  0.  0.  1.  2.  3.  4.  4.  4.]
>Exit code: 0

Now why is the last value 4, and not 5? I'd expect it to be 5, as the last timestamps in t and t_new are identical. Is this a bug in interp1d or am I missing something important - if so, what?

Thanks a lot, in advance,

Nenad

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user




--
Sasha

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Strange behaviour of scipy.interpolate.interp1d in with kind="zero"

Evgeni Burovski
On Wed, Jun 21, 2017 at 5:19 PM, Oleksandr Huziy <[hidden email]> wrote:

> Hi Nenad:
>
> This probably how the kind="zero" is implemented (uses a constant value from
> the left side of the interval) and since in your case you do not have any
> data to the right of the point, it is never used.
>
> Maybe it could be called bug.... (You could try to submit an issue on
> github)
>
> Apparently, now for it to work as you want, you would need to extend your
> sig a bit:
>
> import numpy
> from scipy.interpolate import interp1d
> t = numpy.array([0.000, 0.004, 0.008, 0.012, 0.016, 0.02, 0.03])
> sig = numpy.array([0, 1, 2, 3, 4, 5, 6])
> t_new = numpy.array([0.0, 0.001, 0.003, 0.004, 0.008, 0.012, 0.016, 0.0196,
> 0.02])
> f = interp1d(t, sig, kind='zero')
> s_resampled = f(t_new)
> print(s_resampled)
> [ 0.  0.  0.  1.  2.  3.  4.  4.  5.]
>
>
> Cheers
>
>
>
>
>
>
> 2017-06-21 9:41 GMT-04:00 Propadovic Nenad <[hidden email]>:
>>
>> Hello everybody,
>>
>> please consider the following code:
>>
>> ---
>> import numpy
>> from scipy.interpolate import interp1d
>>
>> t = numpy.array([0.000, 0.004, 0.008, 0.012, 0.016, 0.02])
>> sig = numpy.array([0, 1, 2, 3, 4, 5])
>> t_new = numpy.array([0.0, 0.001, 0.003,  0.004,  0.008, 0.012,  0.016,
>> 0.0196, 0.02])
>>
>> f = interp1d(t, sig, kind='zero')
>> s_resampled = f(t_new)
>> print s_resampled
>> ---
>>
>> The result I get on my machine is:
>> >C:\Anaconda2\pythonw -u "test_zero_order_hold.py"
>> [ 0.  0.  0.  1.  2.  3.  4.  4.  4.]
>> >Exit code: 0
>>
>> Now why is the last value 4, and not 5? I'd expect it to be 5, as the last
>> timestamps in t and t_new are identical. Is this a bug in interp1d or am I
>> missing something important - if so, what?
>>
>> Thanks a lot, in advance,
>>
>> Nenad
>>
>> _______________________________________________
>> SciPy-User mailing list
>> [hidden email]
>> https://mail.python.org/mailman/listinfo/scipy-user
>>
>
>
>
> --
> Sasha
>
> _______________________________________________
> SciPy-User mailing list
> [hidden email]
> https://mail.python.org/mailman/listinfo/scipy-user
>


Cannot repro -- what numpy/scipy versions, OS?



In [2]: %paste
import numpy
from scipy.interpolate import interp1d

t = numpy.array([0.000, 0.004, 0.008, 0.012, 0.016, 0.02])
sig = numpy.array([0, 1, 2, 3, 4, 5])
t_new = numpy.array([0.0, 0.001, 0.003,  0.004,  0.008, 0.012,  0.016,
 0.0196, 0.02])

f = interp1d(t, sig, kind='zero')
s_resampled = f(t_new)
print s_resampled
## -- End pasted text --
[ 0.  0.  0.  1.  2.  3.  4.  4.  5.]

In [3]: f(t) == sig
Out[3]: array([ True,  True,  True,  True,  True,  True], dtype=bool)

In [4]: numpy.__version__
Out[4]: '1.10.4'

In [5]: import scipy

In [6]: scipy.__version__
Out[6]: '1.0.0.dev0+f3e4e17'


Also note that exact equality in floating point is brittle anyway:

In [9]: f(t_new.astype(numpy.float32))
Out[9]: array([ 0.,  0.,  0.,  1.,  2.,  3.,  4.,  4.,  4.])

In [10]: f(t_new)
Out[10]: array([ 0.,  0.,  0.,  1.,  2.,  3.,  4.,  4.,  5.])


Evgeni
_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Strange behaviour of scipy.interpolate.interp1d in with kind="zero"

Propadovic Nenad
In reply to this post by Oleksandr Huziy
Hello Oleksandr,

thank you for the swift answer.

I think we can settle for an untreated edge case and/or an inconsistency. The values at times 0.004, 0.008, 0.012, 0.016 are exactly what I expect, after all: the exact values that the original signal at the exact points in time.

I will file a bug, as you proposed.

Regards,

Nenad


Hi Nenad:

This probably how the kind="zero" is implemented (uses a constant value
from the left side of the interval) and since in your case you do not have
any data to the right of the point, it is never used.

Maybe it could be called bug.... (You could try to submit an issue on
github)

Apparently, now for it to work as you want, you would need to extend your
sig a bit:

import numpy
from scipy.interpolate import interp1d
t = numpy.array([0.000, 0.004, 0.008, 0.012, 0.016, 0.02, 0.03])
sig = numpy.array([0, 1, 2, 3, 4, 5, 6])
t_new = numpy.array([0.0, 0.001, 0.003, 0.004, 0.008, 0.012, 0.016, 0.0196,
0.02])
f = interp1d(t, sig, kind='zero')
s_resampled = f(t_new)
print(s_resampled)
[ 0.  0.  0.  1.  2.  3.  4.  4.  5.]


Cheers


_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Strange behaviour of scipy.interpolate.interp1d in with kind="zero"

Propadovic Nenad
Hello Evgeni,
sorry, somehow initially I had not seen your post.
My OS is Windows 7 Professional N 64 bit.

Python 2.7.13 |Anaconda 4.3.0 (64-bit)
(yes I know Python 3.x is preferred, but that's the company choice that I can't influence... )

numpy.__version__
Out[2]: '1.11.3'

scipy.__version__
Out[4]: '0.18.1'

Yeah, maybe I can update Anaconda before calling out a bug. But the scipy-version is not soooo old.

And, yes, I'm aware of the brittle nature of exact equality of floats... I often use code like following in my unit-tests:
assert (numpy.all(abs(x-x_maybe_equal ) < 1e-12))

Cheers,

Nenad
Cannot repro -- what numpy/scipy versions, OS?



In [2]: %paste
import numpy
from scipy.interpolate import interp1d

t = numpy.array([0.000, 0.004, 0.008, 0.012, 0.016, 0.02])
sig = numpy.array([0, 1, 2, 3, 4, 5])
t_new = numpy.array([0.0, 0.001, 0.003,  0.004,  0.008, 0.012,  0.016,
 0.0196, 0.02])

f = interp1d(t, sig, kind='zero')
s_resampled = f(t_new)
print s_resampled
## -- End pasted text --
[ 0.  0.  0.  1.  2.  3.  4.  4.  5.]

In [3]: f(t) == sig
Out[3]: array([ True,  True,  True,  True,  True,  True], dtype=bool)

In [4]: numpy.__version__
Out[4]: '1.10.4'

In [5]: import scipy

In [6]: scipy.__version__
Out[6]: '1.0.0.dev0+f3e4e17'


Also note that exact equality in floating point is brittle anyway:

In [9]: f(t_new.astype(numpy.float32))
Out[9]: array([ 0.,  0.,  0.,  1.,  2.,  3.,  4.,  4.,  4.])

In [10]: f(t_new)
Out[10]: array([ 0.,  0.,  0.,  1.,  2.,  3.,  4.,  4.,  5.])


Evgeni



_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Strange behaviour of scipy.interpolate.interp1d in with kind="zero"

Propadovic Nenad
Hello again,
OK, the problem went away when I upgraded scipy from 0.18.1 to 0.19.0 (Anaconda didn't offer 1.0).
So I guess I have to apologize for bothering.
Regards,
Nenad

2017-06-22 10:45 GMT+02:00 Propadovic Nenad <[hidden email]>:
Hello Evgeni,
sorry, somehow initially I had not seen your post.
My OS is Windows 7 Professional N 64 bit.

Python 2.7.13 |Anaconda 4.3.0 (64-bit)
(yes I know Python 3.x is preferred, but that's the company choice that I can't influence... )

numpy.__version__
Out[2]: '1.11.3'

scipy.__version__
Out[4]: '0.18.1'

Yeah, maybe I can update Anaconda before calling out a bug. But the scipy-version is not soooo old.

And, yes, I'm aware of the brittle nature of exact equality of floats... I often use code like following in my unit-tests:
assert (numpy.all(abs(x-x_maybe_equal ) < 1e-12))

Cheers,

Nenad
Cannot repro -- what numpy/scipy versions, OS?



In [2]: %paste
import numpy
from scipy.interpolate import interp1d

t = numpy.array([0.000, 0.004, 0.008, 0.012, 0.016, 0.02])
sig = numpy.array([0, 1, 2, 3, 4, 5])
t_new = numpy.array([0.0, 0.001, 0.003,  0.004,  0.008, 0.012,  0.016,
 0.0196, 0.02])

f = interp1d(t, sig, kind='zero')
s_resampled = f(t_new)
print s_resampled
## -- End pasted text --
[ 0.  0.  0.  1.  2.  3.  4.  4.  5.]

In [3]: f(t) == sig
Out[3]: array([ True,  True,  True,  True,  True,  True], dtype=bool)

In [4]: numpy.__version__
Out[4]: '1.10.4'

In [5]: import scipy

In [6]: scipy.__version__
Out[6]: '1.0.0.dev0+f3e4e17'


Also note that exact equality in floating point is brittle anyway:

In [9]: f(t_new.astype(numpy.float32))
Out[9]: array([ 0.,  0.,  0.,  1.,  2.,  3.,  4.,  4.,  4.])

In [10]: f(t_new)
Out[10]: array([ 0.,  0.,  0.,  1.,  2.,  3.,  4.,  4.,  5.])


Evgeni




_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: Strange behaviour of scipy.interpolate.interp1d in with kind="zero"

Oleksandr Huziy
Hi:

I can confirm that the problem disappeared after upgrading scipy to 0.19.0 (numpy got updated as well)

Cheers

2017-06-22 5:23 GMT-04:00 Propadovic Nenad <[hidden email]>:
Hello again,
OK, the problem went away when I upgraded scipy from 0.18.1 to 0.19.0 (Anaconda didn't offer 1.0).
So I guess I have to apologize for bothering.
Regards,
Nenad

2017-06-22 10:45 GMT+02:00 Propadovic Nenad <[hidden email]>:
Hello Evgeni,
sorry, somehow initially I had not seen your post.
My OS is Windows 7 Professional N 64 bit.

Python 2.7.13 |Anaconda 4.3.0 (64-bit)
(yes I know Python 3.x is preferred, but that's the company choice that I can't influence... )

numpy.__version__
Out[2]: '1.11.3'

scipy.__version__
Out[4]: '0.18.1'

Yeah, maybe I can update Anaconda before calling out a bug. But the scipy-version is not soooo old.

And, yes, I'm aware of the brittle nature of exact equality of floats... I often use code like following in my unit-tests:
assert (numpy.all(abs(x-x_maybe_equal ) < 1e-12))

Cheers,

Nenad
Cannot repro -- what numpy/scipy versions, OS?



In [2]: %paste
import numpy
from scipy.interpolate import interp1d

t = numpy.array([0.000, 0.004, 0.008, 0.012, 0.016, 0.02])
sig = numpy.array([0, 1, 2, 3, 4, 5])
t_new = numpy.array([0.0, 0.001, 0.003,  0.004,  0.008, 0.012,  0.016,
 0.0196, 0.02])

f = interp1d(t, sig, kind='zero')
s_resampled = f(t_new)
print s_resampled
## -- End pasted text --
[ 0.  0.  0.  1.  2.  3.  4.  4.  5.]

In [3]: f(t) == sig
Out[3]: array([ True,  True,  True,  True,  True,  True], dtype=bool)

In [4]: numpy.__version__
Out[4]: '1.10.4'

In [5]: import scipy

In [6]: scipy.__version__
Out[6]: '1.0.0.dev0+f3e4e17'


Also note that exact equality in floating point is brittle anyway:

In [9]: f(t_new.astype(numpy.float32))
Out[9]: array([ 0.,  0.,  0.,  1.,  2.,  3.,  4.,  4.,  4.])

In [10]: f(t_new)
Out[10]: array([ 0.,  0.,  0.,  1.,  2.,  3.,  4.,  4.,  5.])


Evgeni




_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user




--
Sasha

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/scipy-user