Compute the integral and error over a range of bins
190443
def integral(self, xbin1=None, xbin2=None,
                 width=False, error=False, overflow=False):
        
        if xbin1 is None:
            xbin1 = 0 if overflow else 1
        if xbin2 is None:
            xbin2 = -1 if overflow else -2
        nbinsx = self.nbins(axis=0, overflow=True)
        xbin1 %= nbinsx
        xbin2 %= nbinsx
        options = 'width' if width else ''
        if error:
            error = ROOT.Double()
            integral = super(_Hist, self).IntegralAndError(
                xbin1, xbin2, error, options)
            return integral, error
        return super(_Hist, self).Integral(xbin1, xbin2, options)
1199665
def mean_absolute_error(seq, correct):
    
    assert len(seq) == len(correct)
    diffs = [abs(a-b) for a, b in zip(seq, correct)]
    return sum(diffs)/float(len(diffs))
425920
def _get_bins(self, kind, order, reverse=False):
        

        if kind == "radius":
            start = self.max_radius * self.radius_inner
            end = self.max_radius

        elif kind == "angle":
            start = self.start_angle
            end = self.start_angle + 2 * np.pi

        bounds = np.linspace(start, end, len(order) + 1)
        bins = np.vstack([bounds[:-1], bounds[1:]]).T

        if reverse:
            bins = bins[::-1]

        return dict(zip(order, bins))
442839
def bin_data(data, dim = 40, num_bins = 10):
  
  intervals = generate_RF_bins(data, dim, num_bins)
  binned_data = [numpy.concatenate([bin_number(data[x][i], intervals[i])
      for i in range(len(data[x]))]) for x in range(len(data))]
  return binned_data
232753
def randints(s, e, n=1):
        
        assert e >= s, "Wrong range: [{}, {})".format(s, e)
        n = max(1, n)
        arr = [s + a % (e - s) for a in struct.unpack('<%dL' % n, os.urandom(4 * n))]
        return arr
874565
def errtrapz(x, yerr):
    
    x = np.array(x)
    assert isinstance(x, np.ndarray)
    yerr = np.array(yerr)
    return 0.5 * np.sqrt((x[1] - x[0]) ** 2 * yerr[0] ** 2 +
                         np.sum((x[2:] - x[:-2]) ** 2 * yerr[1:-1] ** 2) +
                         (x[-1] - x[-2]) ** 2 * yerr[-1] ** 2)
529328
def generate_RF_bins(data, dim = 40, num_bins = 10):
  
  intervals = []
  for i in range(dim):
    current_dim_data = [data[x][i] for x in range(len(data))]
    current_dim_data = numpy.sort(current_dim_data)
    intervals.append([current_dim_data[int(len(current_dim_data)*x/num_bins)]
        for x in range(1, num_bins)])
  return intervals
137413
def distLinf(x1,y1,x2,y2):
    """"""
    return int(max(abs(x2-x1),abs(y2-y1)))
849364
def right(self, f, n=1):
        
        intervals = self.intervals[f.chrom]
        ilen = len(intervals)
        iright = binsearch_right_end(intervals, f.end, 0, ilen)
        results = []

        while iright < ilen:
            i = len(results)
            if i > n:
                if distance(f, results[i - 1]) != distance(f, results[i - 2]):
                    return results[:i - 1]
            other = intervals[iright]
            iright += 1
            if distance(other, f) == 0: continue
            results.append(other)
        return results
944822
def numpy_bins(self) -> List[np.ndarray]:
        """"""
        return [binning.numpy_bins for binning in self._binnings]
233461
def knuth(data):
        
        import scipy.optimize as optimize

        def f(data):
            from scipy.special import gammaln

            m, M = np.min(data), np.max(data)
            n = len(data)

            def fff(x):
                k = x[0]  # number of bins
                if k <= 0:
                    return float("+inf")
                binning = np.linspace(m, M, k + 1)
                histo, bincenters = np.histogram(data, binning)

                return -(n * np.log(k) + gammaln(k / 2.) - gammaln(n + k / 2.) +
                         k * gammaln(1. / 2.) + np.sum(gammaln(histo + 0.5)))
            return fff

        k0 = np.sqrt(len(data))
        return optimize.fmin(f(data), np.array([k0]), disp=False)[0]
598980
def _heuristic_bin_width(obs):
    """"""
    IQR = sp.percentile(obs, 75) - sp.percentile(obs, 25)
    N = len(obs)
    return 2*IQR*N**(-1/3)
1153751
def errors(self):
        

        n = self.n
        dt = self.dt
        order = self._order
        sigma = self.sigma

        error = np.zeros(order + 1)
        std = np.zeros(order + 1)

        if n == 0:
            return (error, std)

        if order == 0:
            error[0] = sigma/sqrt(n)
            std[0] = sigma/sqrt(n)

        elif order == 1:
            if n > 1:
                error[0] = sigma * sqrt(2*(2*n-1) / (n*(n+1)))
                error[1] = sigma * sqrt(12. / (n*(n*n-1)*dt*dt))
            std[0] = sigma * sqrt((2*(2*n-1)) / (n*(n+1)))
            std[1] = (sigma/dt) * sqrt(12. / (n*(n*n-1)))

        elif order == 2:
            dt2 = dt * dt

            if n >= 3:
                error[0] = sigma * sqrt(3*(3*n*n-3*n+2) / (n*(n+1)*(n+2)))
                error[1] = sigma * sqrt(12*(16*n*n-30*n+11) /
                                        (n*(n*n-1)*(n*n-4)*dt2))
                error[2] = sigma * sqrt(720/(n*(n*n-1)*(n*n-4)*dt2*dt2))

            std[0] = sigma * sqrt((3*(3*n*n - 3*n + 2)) / (n*(n+1)*(n+2)))
            std[1] = (sigma/dt) * sqrt((12*(16*n*n - 30*n + 11)) /
                                       (n*(n*n - 1)*(n*n - 4)))
            std[2] = (sigma/dt2) * sqrt(720 / (n*(n*n-1)*(n*n-4)))

        return error, std
545188
def bins(self) -> List[np.ndarray]:
        """"""
        return [binning.bins for binning in self._binnings]
809736
def check_param_ranges(num_bins, num_groups, num_values, trim_outliers, trim_percentile):
    """"""

    if num_bins < minimum_num_bins:
        raise ValueError('Too few bins! The number of bins must be >= 5')

    if num_values < num_groups:
        raise ValueError('Insufficient number of values in features (< number of nodes), or invalid membership!')

    if trim_outliers:
        if trim_percentile < 0 or trim_percentile >= 100:
            raise ValueError('percentile of tail values to trim must be in the semi-open interval [0,1).')
    elif num_values < 2:
        raise ValueError('too few features to compute minimum and maximum')

    return
1078217
def solveBinPacking(s,B):
    
    n = len(s)
    U = len(FFD(s,B)) # upper bound of the number of bins
    model = bpp(s,B)
    x,y = model.data

    model.optimize()

    bins = [[] for i in range(U)]
    for (i,j) in x:
        if model.getVal(x[i,j]) > .5:
            bins[j].append(s[i])
    for i in range(bins.count([])):
        bins.remove([])
    for b in bins:
        b.sort()
    bins.sort()
    return bins
641241
def argresample(a,**kwargs):
    '''
    
    '''
    
    dt=kwargs.get('dt',np.median(deriv(a)))
    
    h,_=np.histogram(a, bins=(a.max()-a.min())/dt + 1, range=[a.min()-dt/2.,a.max()+dt/2.])
    
    return h.astype(bool)