Skip to content

Handle cases in EventTable.cluster() where table is empty or clustered multiple times

Created by: myNameIsPatrick

This PR handles a few edge cases more gracefully when calling EventTable.cluster():

  1. The table is empty.
  2. The table is already clustered.

In both cases, the error encountered looks like:

>>> table.cluster('time', 'snr', 0.1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/patrick.godwin/workspace/projects/gwpy/gwpy/table/table.py", line 782, in cluster
    padded_sublists = [numpy.append(s, numpy.array([s[-1]+1]))
  File "/home/patrick.godwin/workspace/projects/gwpy/gwpy/table/table.py", line 782, in <listcomp>
    padded_sublists = [numpy.append(s, numpy.array([s[-1]+1]))
IndexError: index -1 is out of bounds for axis 0 with size 0

In both cases, the original table is now returned as a result. I have also added a test which checks that running table.cluster() with the same parameters twice returns the same table.

Merge request reports