Example #1
0
def longest_ones(x):
    """
    return the indicies of the longest stretch of contiguous ones in x,
    assuming x is a vector of zeros and ones.

    If there are two equally long stretches, pick the first
    """
    x = asarray(x)
    if len(x)==0: return array([])

    #print 'x', x
    ind = find(x==0)
    if len(ind)==0:  return arange(len(x))
    if len(ind)==len(x): return array([])

    y = zeros( (len(x)+2,), Int)
    y[1:-1] = x
    d = diff(y)
    #print 'd', d
    up = find(d ==  1);
    dn = find(d == -1);

    #print 'dn', dn, 'up', up, 
    ind = find( dn-up == max(dn - up))
    # pick the first
    if iterable(ind): ind = ind[0]
    ind = arange(up[ind], dn[ind])

    return ind
Example #2
0
def hist(y, bins=10, normed=0):
    """
    Return the histogram of y with bins equally sized bins.  If bins
    is an array, use the bins.  Return value is
    (n,x) where n is the count for each bin in x

    If normed is False, return the counts in the first element of the
    return tuple.  If normed is True, return the probability density
    n/(len(y)*dbin)

    If y has rank>1, it will be raveled
    Credits: the Numeric 22 documentation

    

    """
    y = asarray(y)
    if len(y.shape)>1: y = ravel(y)

    if not iterable(bins):       
        ymin, ymax = min(y), max(y)
        if ymin==ymax:
            ymin -= 0.5
            ymax += 0.5
        bins = linspace(ymin, ymax, bins)

    n = searchsorted(sort(y), bins)
    n = diff(concatenate([n, [len(y)]]))
    if normed:
       db = bins[1]-bins[0]
       return 1/(len(y)*db)*n, bins
    else:
       return n, bins
Example #3
0
def longest_ones(x):
    """
    return the indicies of the longest stretch of contiguous ones in x,
    assuming x is a vector of zeros and ones.

    If there are two equally long stretches, pick the first
    """
    x = asarray(x)
    if len(x) == 0: return array([])

    #print 'x', x
    ind = find(x == 0)
    if len(ind) == 0: return arange(len(x))
    if len(ind) == len(x): return array([])

    y = zeros((len(x) + 2, ), Int)
    y[1:-1] = x
    d = diff(y)
    #print 'd', d
    up = find(d == 1)
    dn = find(d == -1)

    #print 'dn', dn, 'up', up,
    ind = find(dn - up == max(dn - up))
    # pick the first
    if iterable(ind): ind = ind[0]
    ind = arange(up[ind], dn[ind])

    return ind
Example #4
0
def hist(y, bins=10, normed=0):
    """
    Return the histogram of y with bins equally sized bins.  If bins
    is an array, use the bins.  Return value is
    (n,x) where n is the count for each bin in x

    If normed is False, return the counts in the first element of the
    return tuple.  If normed is True, return the probability density
    n/(len(y)*dbin)

    If y has rank>1, it will be raveled
    Credits: the Numeric 22 documentation

    

    """
    y = asarray(y)
    if len(y.shape) > 1: y = ravel(y)

    if not iterable(bins):
        ymin, ymax = min(y), max(y)
        if ymin == ymax:
            ymin -= 0.5
            ymax += 0.5
        bins = linspace(ymin, ymax, bins)

    n = searchsorted(sort(y), bins)
    n = diff(concatenate([n, [len(y)]]))
    if normed:
        db = bins[1] - bins[0]
        return 1 / (len(y) * db) * n, bins
    else:
        return n, bins
Example #5
0
def longest_contiguous_ones(x):
    """
    return the indicies of the longest stretch of contiguous ones in x,
    assuming x is a vector of zeros and ones.
    """
    if len(x)==0: return array([])

    ind = find(x==0)
    if len(ind)==0:  return arange(len(x))
    if len(ind)==len(x): return array([])

    y = zeros( (len(x)+2,),  x.typecode())
    y[1:-1] = x
    dif = diff(y)
    up = find(dif ==  1);
    dn = find(dif == -1);
    ind = find( dn-up == max(dn - up))
    ind = arange(take(up, ind), take(dn, ind))

    return ind
Example #6
0
def longest_contiguous_ones(x):
    """
    return the indicies of the longest stretch of contiguous ones in x,
    assuming x is a vector of zeros and ones.
    """
    if len(x) == 0: return array([])

    ind = find(x == 0)
    if len(ind) == 0: return arange(len(x))
    if len(ind) == len(x): return array([])

    y = zeros((len(x) + 2, ), typecode(x))
    y[1:-1] = x
    dif = diff(y)
    up = find(dif == 1)
    dn = find(dif == -1)
    ind = find(dn - up == max(dn - up))
    ind = arange(take(up, ind), take(dn, ind))

    return ind
Example #7
0
def trapz(x, y):
   if len(x)!=len(y):
      raise ValueError, 'x and y must have the same length'
   if len(x)<2:
      raise ValueError, 'x and y must have > 1 element'
   return asum(0.5*diff(x)*(y[1:]+y[:-1]))
Example #8
0
def trapz(x, y):
    if len(x) != len(y):
        raise ValueError, 'x and y must have the same length'
    if len(x) < 2:
        raise ValueError, 'x and y must have > 1 element'
    return asum(0.5 * diff(x) * (y[1:] + y[:-1]))