def slice_for_range(data, fromVal, toVal):
    """
    Returns a slice indicating the region [fromVal, toVal] in a 1D ndarray data

    Example:
    >>> data = linspace(1, 50, 50)
    >>> s = slice_for_range(data, 4, 10)
    >>> data[s]
    array([  4.,   5.,   6.,   7.,   8.,   9.,  10.])
    """
    if (data[-1] - data[0]) / (toVal - fromVal) < 0:
        # Order of fromVal and toVal is reversed
        temp = toVal
        toVal = fromVal
        fromVal = temp

    foundFromVal = False
    start = None
    stop = None
    if toVal > fromVal:
        # ascending order
        for idx, val in enumerate(data):
            if foundFromVal:
                if val > toVal:
                    stop = idx
                    break
            else:
                if val >= fromVal:
                    start = idx
                    foundFromVal = True
    else:
        # descending order
        for idx, val in enumerate(data):
            if foundFromVal:
                if val < toVal:
                    stop = idx
                    break
            else:
                if val <= fromVal:
                    start = idx
                    foundFromVal = True

    return slice(start, stop)