peakutils.peak.interpolate() error

Issue #15 resolved
Tom Runia created an issue

When running the following simple test code, I encounter the error that is given below. This is a simple test example, but I encounter the same problem in actual research code. Is this a bug or am I doing something wrong?

N = 100
x = np.arange(N)
y = np.random.randn(N)

y_scaled, _ = peakutils.prepare.scale(y)
indices = peakutils.peak.indexes(y_scaled)

print(x.shape, y_scaled.shape, indices.shape)
ind_inter = peakutils.peak.interpolate(x, y_scaled, ind=indices)

plt.plot(x,y_scaled, lw=1)

for ind in indices:
    plt.scatter(ind, y_scaled[ind], marker='o')

plt.show()

Stacktrace:

Traceback (most recent call last):
((100,), (100,), (32,))
  File "/Users/tomrunia/Development/python/QuasiPeriodicFiltering/pogalin/plot_degree_of_periodicity.py", line 25, in <module>
    ind_inter = peakutils.peak.interpolate(x, y_scaled, ind=indices)
  File "/Users/tomrunia/Development/virtualenv/tensorflow/lib/python2.7/site-packages/peakutils/peak.py", line 181, in interpolate
    fit = func(x[slice_], y[slice_])
  File "/Users/tomrunia/Development/virtualenv/tensorflow/lib/python2.7/site-packages/peakutils/peak.py", line 142, in gaussian_fit
    initial = [np.max(y), x[0], (x[1] - x[0]) * 5]
  File "/Users/tomrunia/Development/virtualenv/tensorflow/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 2297, in amax
    out=out, **kwargs)
  File "/Users/tomrunia/Development/virtualenv/tensorflow/lib/python2.7/site-packages/numpy/core/_methods.py", line 26, in _amax
    return umr_maximum(a, axis, None, out, keepdims)
ValueError: zero-size array to reduction operation maximum which has no identity

Comments (5)

  1. Tom Runia reporter

    And this question is not longer a bug, but I was wondering why the interpolate() results are so far off in this example. The dot markers are the original indices and the crosses are interpolated peak locations. I experimented with several width settings, but it isn't getting much better than this. Any comments on how I can improve the results?

    peaks.png

    N = 30
    x = np.arange(N)
    y = np.random.randn(N)
    y_scaled, _ = peakutils.prepare.scale(y)
    
    plt.plot(x,y_scaled, lw=1)
    
    # Peakutils detection
    indices_pu = peakutils.peak.indexes(y_scaled)
    ind_inter = peakutils.peak.interpolate(x, y_scaled, ind=indices_pu, width=2)
    
    for i, ind in enumerate(indices_pu):
        print("%.2f => %.2f" % (indices_pu[i], ind_inter[i]))
        color_idx = i % len(colors)
        plt.scatter(ind, y_scaled[ind], color=colors[color_idx], marker='o')
        plt.scatter(ind_inter[i], y_scaled[ind], color=colors[color_idx], marker='x')
    
    # SciPy detection
    # indices_cwt = scipy.signal.find_peaks_cwt(y_scaled, np.arange(1,10))
    #
    # for i, ind in enumerate(indices_cwt):
    #     color_idx = i % len(colors)
    #     plt.scatter(ind, y_scaled[ind], color=colors[color_idx], marker='d')
    
    plt.show()
    
  2. Lucas Hermann Negri repo owner

    Interpolation will not help here since the data is just random noise (there is no peak structure beyond a single point here, no useful information). If you reduce the width of the Gaussians when calling interpolate (width=1), you will see that the interpolation gets better.

  3. Log in to comment