Compute the integral and error over a range of bins
507956
def integral(self,
                 xbin1=1, xbin2=-2,
                 ybin1=1, ybin2=-2,
                 zbin1=1, zbin2=-2,
                 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
        if ybin1 is None:
            ybin1 = 0 if overflow else 1
        if ybin2 is None:
            ybin2 = -1 if overflow else -2
        if zbin1 is None:
            zbin1 = 0 if overflow else 1
        if zbin2 is None:
            zbin2 = -1 if overflow else -2
        nbinsx = self.nbins(axis=0, overflow=True)
        xbin1 %= nbinsx
        xbin2 %= nbinsx
        nbinsy = self.nbins(axis=1, overflow=True)
        ybin1 %= nbinsy
        ybin2 %= nbinsy
        nbinsz = self.nbins(axis=2, overflow=True)
        zbin1 %= nbinsz
        zbin2 %= nbinsz
        options = 'width' if width else ''
        if error:
            error = ROOT.Double()
            integral = super(_Hist3D, self).IntegralAndError(
                xbin1, xbin2, ybin1, ybin2, zbin1, zbin2, error, options)
            return integral, error
        return super(_Hist3D, self).Integral(
            xbin1, xbin2, ybin1, ybin2, zbin1, zbin2, options)
569398
def get_bin_index(self, value):
        """"""
        if value == self.max_value:
            return self.num_bins - 1
        return int((value - self.min_value) / self.bin_size)
1239252
def containment_angle_bin(self, egy_bins, fraction=0.68, scale_fn=None):
        """"""

        vals = self.interp_bin(egy_bins, self.dtheta, scale_fn=scale_fn)
        dtheta = np.radians(self.dtheta[:, np.newaxis] * np.ones(vals.shape))
        return self._calc_containment(dtheta, vals, fraction)
398912
def val_to_bin_bounded(edges, x):
    """"""
    nbins = len(edges) - 1
    ibin = val_to_bin(edges, x)
    ibin[ibin < 0] = 0
    ibin[ibin > nbins - 1] = nbins - 1
    return ibin
991755
def _locate_bin(bins, n):
    
    while bins[n] != n:
        n = bins[n]
    return n
994695
def _compute_error(self):
        
        # err_i = sum_j R_{k,ij} A_{k,ji} - d_i
        self._err = np.sum(np.multiply(self._R_k, self._C_k.T), axis=0) - self._d
433807
def _optimize_binning(x, range, method='Freedman'):
    
    N = x.shape[0]
    if method.lower() == 'freedman':
        s = sort(x)
        IQR = s[int(N * .75)] - s[int(
            N * .25)]  # Interquantile range (75% -25%)
        width = 2 * IQR * N ** (-1. / 3)

    elif method.lower() == 'scott':
        width = 3.49 * x.std() * N ** (-1. / 3)
    else:
        raise ValueError('Method must be Scott or Freedman', method)
    return int(diff(range) / width)
510382
def total(self, xbin1=1, xbin2=-2):
        
        integral, stat_error = self.hist.integral(
            xbin1=xbin1, xbin2=xbin2, error=True)
        # sum systematics in quadrature
        ups = [0]
        dns = [0]
        for sys_name in self.sys_names():
            sys_low, sys_high = self.sys_hist(sys_name)
            up = sys_high.integral(xbin1=xbin1, xbin2=xbin2) - integral
            dn = sys_low.integral(xbin1=xbin1, xbin2=xbin2) - integral
            if up > 0:
                ups.append(up**2)
            else:
                dns.append(up**2)
            if dn > 0:
                ups.append(dn**2)
            else:
                dns.append(dn**2)
        syst_error = (sqrt(sum(ups)), sqrt(sum(dns)))
        return integral, stat_error, syst_error
1107131
def bincount(dig, weight, minlength):
    
    if numpy.isscalar(weight):
        return numpy.bincount(dig, minlength=minlength) * weight
    else:
        return numpy.bincount(dig, weight, minlength)
810326
def get_missing_bins(original, trimmed):
    

    original_diag = np.diag(original)
    trimmed_diag = np.diag(trimmed)
    index = []
    m = min(original.shape)
    for j in range(min(trimmed.shape)):
        k = 0
        while original_diag[j + k] != trimmed_diag[j] and k < 2 * m:
            k += 1
        index.append(k + j)
    return np.array(index)
1229314
def calc_bin(self, _bin=None):
        
        if _bin is None:
            try:
                _bin = bins.bins(self.start, self.end, one=True)
            except TypeError:
                _bin = None
        return _bin
1116638
def compute_histogram(values, edges, use_orig_distr=False):
    """"""

    if use_orig_distr:
        return values

    # ignoring invalid values: Inf and Nan
    values = check_array(values).compressed()

    hist, bin_edges = np.histogram(values, bins=edges, density=True)
    hist = preprocess_histogram(hist, values, edges)

    return hist
706212
def _infer_precision(base_precision, bins):
    
    for precision in range(base_precision, 20):
        levels = [_round_frac(b, precision) for b in bins]
        if algos.unique(levels).size == bins.size:
            return precision
    return base_precision
17997
def get_bin_indices(self, values):
        """"""
        return tuple([self.get_axis_bin_index(values[ax_i], ax_i)
                      for ax_i in range(self.dimensions)])
1109436
def std_err(self):
        

        return np.sqrt(np.sum(np.square(self.resids), axis=0) / self.df_err)
880242
def _std_err(self):
        
        return np.sqrt(np.sum(np.square(self._resids), axis=1) / self._df_err)
1210673
def dist_points(bin_edges, d):
    
    bc = bin_centers(bin_edges)
    if d is not None:
        d = DISTS[d['type']](d, bc)
    return d, bc