Discussion:
[Numpy-discussion] Possible bug in Numpy.convolve
Thøger Emil Rivera-Thorsen
2017-03-23 02:06:02 UTC
Permalink
Dear list;

I am honestly not certain whether this, or the SciPy list, is the
appropriate place to post this; please let me know if I got it wrong.

I am convolving a 1D data set containing a relatively narrow peak, with
a relatively narrow Gaussian kernel, in order to emulate the effect of
atmospheric seeing on astrophysical observations.

I have a 1D data array 45 pixels long, and a Gaussian kernel, and run
np.convolve(data, kernel, mode='same') on the two arrays, the resulting
array's peak is shifted relative to the origin. I have attached a plot
to illustrate.

The original data is shown in blue. When I convolve it with a symmetric
kernel (black), I get an offset resulting peak (magenta). If I flip the
kernel -- even though it is perfectly symmetric -- the resulting curve
is offset in the opposite direction (yellow). However, if I offset the
kernel so it is centered exactly one pixel below the central value, the
output array gets centered correct (red), even if I flip the (now no
longer symmetric) kernel.

This is using Numpy 1.11.3, python 2.7.13, on Anaconda 4.3.0 64-bit on
Ubuntu 16.10

Using astropy.convolution, reproduces the correct red curve, so I can
use that for now, but it seems to me this is either a bug or, if it is
indeed the intended behavior, a word of caution would be merited in the
docstring.

Cheers,

Emil Rivera-Thorsen
j***@gmail.com
2017-03-23 02:38:47 UTC
Permalink
On Wed, Mar 22, 2017 at 10:06 PM, ThÞger Emil Rivera-Thorsen <
Post by Thøger Emil Rivera-Thorsen
Dear list;
I am honestly not certain whether this, or the SciPy list, is the
appropriate place to post this; please let me know if I got it wrong.
I am convolving a 1D data set containing a relatively narrow peak, with a
relatively narrow Gaussian kernel, in order to emulate the effect of
atmospheric seeing on astrophysical observations.
I have a 1D data array 45 pixels long, and a Gaussian kernel, and run
np.convolve(data, kernel, mode='same') on the two arrays, the resulting
array's peak is shifted relative to the origin. I have attached a plot to
illustrate.
The original data is shown in blue. When I convolve it with a symmetric
kernel (black), I get an offset resulting peak (magenta). If I flip the
kernel -- even though it is perfectly symmetric -- the resulting curve is
offset in the opposite direction (yellow). However, if I offset the kernel
so it is centered exactly one pixel below the central value, the output
array gets centered correct (red), even if I flip the (now no longer
symmetric) kernel.
This is using Numpy 1.11.3, python 2.7.13, on Anaconda 4.3.0 64-bit on
Ubuntu 16.10
Using astropy.convolution, reproduces the correct red curve, so I can use
that for now, but it seems to me this is either a bug or, if it is indeed
the intended behavior, a word of caution would be merited in the docstring.
Cheers,
Emil Rivera-Thorsen
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Can you provide an example to replicate?

I haven't seen this behavior, it looks centered to me, at least for odd
window length..
AFAIR, I had to try a bit in the past for how to set the window and
location with even window length.
Post by Thøger Emil Rivera-Thorsen
np.__version__
'1.11.2'
Post by Thøger Emil Rivera-Thorsen
x = np.linspace(-1, 1, 21)
w = stats.norm.pdf(np.linspace(-3, 3, 5))
np.column_stack((x, np.convolve(x, w, mode='same')))[8:13]
array([[ -2.00000000e-01, -1.33368234e-01],
[ -1.00000000e-01, -6.66841169e-02],
[ 0.00000000e+00, 1.51788304e-17],
[ 1.00000000e-01, 6.66841169e-02],
[ 2.00000000e-01, 1.33368234e-01]])
Post by Thøger Emil Rivera-Thorsen
x = np.abs(np.linspace(-1, 1, 21))
w = stats.norm.pdf(np.linspace(-3, 3, 4))
np.column_stack((x, np.convolve(x, w, mode='same')))[8:13]
array([[ 0.2 , 0.12320129],
[ 0.1 , 0.07392077],
[ 0. , 0.02552663],
[ 0.1 , 0.02552663],
[ 0.2 , 0.07392077]])
Post by Thøger Emil Rivera-Thorsen
w = stats.norm.pdf(np.linspace(-3, 3, 5))
np.column_stack((x, np.convolve(x, w, mode='same')))[8:13]
array([[ 0.2 , 0.13336823],
[ 0.1 , 0.06757049],
[ 0. , 0.02767626],
[ 0.1 , 0.06757049],
[ 0.2 , 0.13336823]])

Josef
Thøger Emil Rivera-Thorsen
2017-03-23 02:44:04 UTC
Permalink
OK, this is embarrassing.
I had in fact made the kernel only almost symmetric, it was slightly
offset to one side.
This caused it. Only a wetware bug.
Sorry for having wasted your time!
On Wed, Mar 22, 2017 at 10:06 PM, Thøger Emil Rivera-Thorsen
Dear list;
I am honestly not certain whether this, or the SciPy list, is the
appropriate place to post this; please let me know if I got it wrong.
I am convolving a 1D data set containing a relatively narrow peak,
with a relatively narrow Gaussian kernel, in order to emulate the
effect of atmospheric seeing on astrophysical observations.
I have a 1D data array 45 pixels long, and a Gaussian kernel, and
run np.convolve(data, kernel, mode='same') on the two arrays, the
resulting array's peak is shifted relative to the origin. I have
attached a plot to illustrate.
The original data is shown in blue. When I convolve it with a
symmetric kernel (black), I get an offset resulting peak
(magenta). If I flip the kernel -- even though it is perfectly
symmetric -- the resulting curve is offset in the opposite
direction (yellow). However, if I offset the kernel so it is
centered exactly one pixel below the central value, the output
array gets centered correct (red), even if I flip the (now no
longer symmetric) kernel.
This is using Numpy 1.11.3, python 2.7.13, on Anaconda 4.3.0
64-bit on Ubuntu 16.10
Using astropy.convolution, reproduces the correct red curve, so I
can use that for now, but it seems to me this is either a bug or,
if it is indeed the intended behavior, a word of caution would be
merited in the docstring.
Cheers,
Emil Rivera-Thorsen
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
<https://mail.scipy.org/mailman/listinfo/numpy-discussion>
Can you provide an example to replicate?
I haven't seen this behavior, it looks centered to me, at least for
odd window length..
AFAIR, I had to try a bit in the past for how to set the window and
location with even window length.
np.__version__
'1.11.2'
x = np.linspace(-1, 1, 21)
w = stats.norm.pdf(np.linspace(-3, 3, 5))
np.column_stack((x, np.convolve(x, w, mode='same')))[8:13]
array([[ -2.00000000e-01, -1.33368234e-01],
[ -1.00000000e-01, -6.66841169e-02],
[ 0.00000000e+00, 1.51788304e-17],
[ 1.00000000e-01, 6.66841169e-02],
[ 2.00000000e-01, 1.33368234e-01]])
x = np.abs(np.linspace(-1, 1, 21))
w = stats.norm.pdf(np.linspace(-3, 3, 4))
np.column_stack((x, np.convolve(x, w, mode='same')))[8:13]
array([[ 0.2 , 0.12320129],
[ 0.1 , 0.07392077],
[ 0. , 0.02552663],
[ 0.1 , 0.02552663],
[ 0.2 , 0.07392077]])
w = stats.norm.pdf(np.linspace(-3, 3, 5))
np.column_stack((x, np.convolve(x, w, mode='same')))[8:13]
array([[ 0.2 , 0.13336823],
[ 0.1 , 0.06757049],
[ 0. , 0.02767626],
[ 0.1 , 0.06757049],
[ 0.2 , 0.13336823]])
Josef
_______________________________________________
NumPy-Discussion mailing list
https://mail.scipy.org/mailman/listinfo/numpy-discussion
Loading...