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

scoreatpercentile() does not handle empty list inputs (Trac #1372) #1897

Closed
scipy-gitbot opened this issue Apr 25, 2013 · 2 comments · Fixed by #3186
Closed

scoreatpercentile() does not handle empty list inputs (Trac #1372) #1897

scipy-gitbot opened this issue Apr 25, 2013 · 2 comments · Fixed by #3186
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected Migrated from Trac scipy.stats
Milestone

Comments

@scipy-gitbot
Copy link

Original ticket http://projects.scipy.org/scipy/ticket/1372 on 2011-01-27 by trac user mpenning, assigned to unknown.

When iterating through slices of list structures, occasionally scoreatpercentile() may get called with an empty list, resulting in a IndexError traceback that looks similar to this:

Traceback (most recent call last):
  File "reverse.py", line 1154, in <module>
    long_stats.update_all_stats(sig,current)
  File "reverse.py", line 130, in update_all_stats
    self.stats[ii] = self.infra[ii][0](sig,current)
  File "reverse.py", line 82, in <lambda>
    fn = lambda sig,current: pct(sig['diff_600'][sig['std_diff_600']>0],1,85,current,7200)
  File "reverse.py", line 887, in pct
    return pctl(array.truncate(before=current-dt.timedelta(seconds=backdelta),after=current).valid().values,value)
  File "/usr/lib/python2.5/site-packages/scipy/stats/stats.py", line 1287, in scoreatpercentile
    return _interpolate(values[int(idx)], values[int(idx) + 1], idx % 1)
IndexError: index out of bounds
[mpenning@Bucksnort data]$

scoreatpercentile() should return numpy.nan if the input list is empty...

The following code may be useful as a local substitute while the bug gets fixed...

def scoreatpercentile(a, per):
    values = np.sort(a,axis=0)

    _interpolate = lambda a,b,fraction: a + (b - a)*fraction

    idx = per /100. * (values.shape[0] - 1)
    if (idx % 1 == 0):
        return values[idx]
    elif values.shape[0]==0:
        return np.nan
    else:
        return _interpolate(values[int(idx)], values[int(idx) + 1], idx % 1)
@scipy-gitbot
Copy link
Author

@WarrenWeckesser wrote on 2011-01-27

Add markup to the description for the traceback and code.

@rgommers
Copy link
Member

rgommers commented Jan 4, 2014

Fixed by gh-3186.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected Migrated from Trac scipy.stats
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants