import bisect


def _get_spikes_sub(data, idx=0, window=None):
    """
    Get the spikes from the cell with this idx within window window.
    
    :param data: is the spike data. This should be a 2D array or
        a set or dict where the spikes from a given cell can be
        accessed as data[idx]. The spikes also need to be sorted
        if window is specified.
        
    :param idx: is the id from data to extract.
    
    :param window: a tuple, (low, high), specifying the window range of 
        values to return.
        
    :return: A list of spike times or an empty list if no spikes are valid. 
    """
    try :
        if type(window) is tuple and len(window)==2:
            spikes = data[idx]
            low = bisect.bisect_left(spikes, window[0])
            high = bisect.bisect_right(spikes, window[1])
            return spikes[low:high]
        else:
            if idx in data:
                return data[idx]
            else:
                return [] # Empty array
    except:
        raise