Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Instantaneous rate estimation fails for inconsistent kernel selection #288

Closed
JuliaSprenger opened this issue Jan 14, 2020 · 2 comments · Fixed by #313
Closed

[Bug] Instantaneous rate estimation fails for inconsistent kernel selection #288

JuliaSprenger opened this issue Jan 14, 2020 · 2 comments · Fixed by #313

Comments

@JuliaSprenger
Copy link
Member

When providing a kernel on a much smaller time scale than sampling rate requested the instantaneous rate estimation will fail on numpy level with
ValueError: zero-size array to reduction operation minimum which has no identity

Here's a minimal failing example

from elephant.statistics import instantaneous_rate
from elephant import kernels
spiketrain = neo.spiketrain_generation.homogeneous_poisson_process(10*Hz, t_start=array(0.) * ms, t_stop=array(10000.) * ms)
kernel = kernels.AlphaKernel(sigma = 5*ms, invert = True)
rate = instantaneous_rate(spiketrain, sampling_period = 200*ms, kernel=kernel)

I would expect this to be caught by the instantaneous rate and providing a more telling error message instead of this failing on a numpy level.

The full traceback is

ValueError                                Traceback (most recent call last)
<ipython-input-167-5ac422ce9801> in <module>
      1 from elephant import kernels
      2 kernel = kernels.AlphaKernel(sigma = 5*ms, invert = True)
----> 3 rate = instantaneous_rate(spiketrain1, sampling_period = 200*ms, kernel=kernel)

~/anaconda3/envs/t/lib/python3.7/site-packages/elephant/statistics.py in instantaneous_rate(spiketrain, sampling_period, kernel, cutoff, t_start, t_stop, trim)
    743 
    744     if not trim:
--> 745         r = r[kernel.median_index(t_arr):-(kernel(t_arr).size -
    746                                            kernel.median_index(t_arr))]
    747     elif trim:

~/anaconda3/envs/t/lib/python3.7/site-packages/elephant/kernels.py in median_index(self, t)
    215         """
    216         return np.nonzero(self(t).cumsum() *
--> 217                           (t[len(t) - 1] - t[0]) / (len(t) - 1) >= 0.5)[0].min()
    218 
    219     def is_symmetric(self):

~/anaconda3/envs/t/lib/python3.7/site-packages/numpy/core/_methods.py in _amin(a, axis, out, keepdims, initial)
     30 def _amin(a, axis=None, out=None, keepdims=False,
     31           initial=_NoValue):
---> 32     return umr_minimum(a, axis, None, out, keepdims, initial)
     33 
     34 def _sum(a, axis=None, dtype=None, out=None, keepdims=False,

ValueError: zero-size array to reduction operation minimum which has no identity
@dizcza
Copy link
Member

dizcza commented Jan 31, 2020

  1. Are you sure we should raise an error in this case?
  2. Is t in median_index() supposed to be sorted?
  3. A median index is something undefined unless t is sorted. Even if it's sorted, it's still undefined for arrays with an even number of values. Can we rewrite the logic such that a median, not its index, is returned? Then we don't need to tackle this issue.

@JuliaSprenger
Copy link
Member Author

Regarding 1. Of course raising a warning instead and providing Nan as return value is also an option. I would argue again introducing an automacigal adjustment of the input parameters as this makes the function less transparent.
Regarding 2 & 3 I don't know / have a strong opinion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants