def to_slice(idxs):
    """Convert an index array to a slice if possible. Otherwise,
    return the index array. Indices are assumed to be sorted in
    ascending order.
    """
    if len(idxs) == 1:
        return slice(idxs[0], idxs[0]+1)
    elif len(idxs) == 0:
        return idxs

    stride = idxs[1]-idxs[0]

    if stride <= 0:
        return idxs

    #make sure stride is consistent throughout the array
    if any(idxs[1:]-idxs[:-1] != stride):
        return idxs

    # set the upper bound to idxs[-1]+stride instead of idxs[-1]+1 because
    # later, we compare upper and lower bounds when collapsing slices
    return slice(idxs[0], idxs[-1]+stride, stride)