Generates a histogram of values from a string field. Output is:

        [[low, high, count, query],    ... ] Bin edges is determined by equal division
        of the fields
550845
def fieldAlphaHistogram(
        self, name, q='*:*', fq=None, nbins=10, includequeries=True
    ):
        
        oldpersist = self.persistent
        self.persistent = True
        bins = []
        qbin = []
        fvals = []
        try:
            # get total number of values for the field
            # TODO: this is a slow mechanism to retrieve the number of distinct values
            # Need to replace this with something more efficient.
            ## Can probably replace with a range of alpha chars - need to check on
            ## case sensitivity
            fvals = self.fieldValues(name, q, fq, maxvalues=-1)
            nvalues = len(fvals[name]) / 2
            if nvalues < nbins:
                nbins = nvalues
            if nvalues == nbins:
                # Use equivalence instead of range queries to retrieve the values
                for i in range(0, nbins):
                    bin = [fvals[name][i * 2], fvals[name][i * 2], 0]
                    binq = '%s:%s' % (name, self.prepareQueryTerm(name, bin[0]))
                    qbin.append(binq)
                    bins.append(bin)
            else:
                delta = nvalues / nbins
                if delta == 1:
                    # Use equivalence queries, except the last one which includes the
                    # remainder of terms
                    for i in range(0, nbins - 2):
                        bin = [fvals[name][i * 2], fvals[name][i * 2], 0]
                        binq = '%s:%s' % (name, self.prepareQueryTerm(name, bin[0]))
                        qbin.append(binq)
                        bins.append(bin)
                    term = fvals[name][(nbins - 1) * 2]
                    bin = [term, fvals[name][((nvalues - 1) * 2)], 0]
                    binq = '%s:[%s TO *]' % (name, self.prepareQueryTerm(name, term))
                    qbin.append(binq)
                    bins.append(bin)
                else:
                    # Use range for all terms
                    # now need to page through all the values and get those at the edges
                    coffset = 0.0
                    delta = float(nvalues) / float(nbins)
                    for i in range(0, nbins):
                        idxl = int(coffset) * 2
                        idxu = (int(coffset + delta) * 2) - 2
                        bin = [fvals[name][idxl], fvals[name][idxu], 0]
                        # logging.info(str(bin))
                        binq = ''
                        try:
                            if i == 0:
                                binq = '%s:[* TO %s]' % (
                                    name,
                                    self.prepareQueryTerm(name, bin[1]),
                                )
                            elif i == nbins - 1:
                                binq = '%s:[%s TO *]' % (
                                    name,
                                    self.prepareQueryTerm(name, bin[0]),
                                )
                            else:
                                binq = '%s:[%s TO %s]' % (
                                    name,
                                    self.prepareQueryTerm(name, bin[0]),
                                    self.prepareQueryTerm(name, bin[1]),
                                )
                        except Exception:
                            self.logger.exception('Exception 1 in fieldAlphaHistogram:')
                        qbin.append(binq)
                        bins.append(bin)
                        coffset = coffset + delta
            # now execute the facet query request
            params = {
                'q': q,
                'rows': '0',
                'facet': 'true',
                'facet.field': name,
                'facet.limit': '1',
                'facet.mincount': 1,
                'wt': 'python',
            }
            request = urllib.parse.urlencode(params, doseq=True)
            for sq in qbin:
                try:
                    request = request + '&%s' % urllib.parse.urlencode(
                        {'facet.query': self.encoder(sq)[0]}
                    )
                except Exception:
                    self.logger.exception('Exception 2 in fieldAlphaHistogram')
            rsp = self.doPost(self.solrBase + '', request, self.formheaders)
            data = eval(rsp.read())
            for i in range(0, len(bins)):
                v = data['facet_counts']['facet_queries'][qbin[i]]
                bins[i][2] = v
                if includequeries:
                    bins[i].append(qbin[i])
        finally:
            self.persistent = oldpersist
            if not self.persistent:
                self.conn.close()
        return bins
681936
def cdf(arr, pos=None):
    '''
    
    '''

    r = (arr.min(), arr.max())
    hist, bin_edges = np.histogram(arr, bins=2 * int(r[1] - r[0]), range=r)
    hist = np.asfarray(hist) / hist.sum()
    cdf = np.cumsum(hist)
    if pos is None:
        return cdf
    i = np.argmax(cdf > pos)
    return bin_edges[i]
380793
function binten(bin){
  var len = bin.length;
  var result = new Array(len + 4);
  result[len + 3] = false;
  result[len + 2] = bin[len - 1];
  result[len + 1] = bin[len - 2];
  var cnum = 0;
  var c = false
  for(var i=len - 1; i>=2; i--){
    var a = bin[i];
    var b = bin[i-2];
    result[i + 1] = a ^ b ^ c;
//    c = (a & b) | (a & c) | (b & c);
    cnum = a + b + c;
    c = (cnum>>1) ? true : false;
  }
  result[2] = bin[1] ^ c;
  c = bin[1] & c;
  result[1] = bin[0] ^ c;
  c = bin[0] & c;
  if(c){
    result[0] = true;
    return result;
  }else{
    return result.slice(1);
  }
}
961407
def histogram(numberfile, vmin, vmax, xlabel, title, outfmt="pdf",
              bins=50, skip=0, col=0, ascii=False, base=0, fill="white"):
    
    if ascii:
        return texthistogram([numberfile], vmin, vmax, title=title,
                bins=bins, skip=skip, col=col, base=base)

    data, vmin, vmax = get_data(numberfile, vmin, vmax, skip=skip, col=col)
    outfile = numberfile + ".base{0}.{1}".format(base, outfmt) \
                if base else numberfile + ".pdf"
    template = histogram_log_template if base else histogram_template
    rtemplate = RTemplate(template, locals())
    rtemplate.run()
769470
function _hist2c( data, xEdges, yEdges ) {

		var id1, id2, counts = [];

		// Initialize our counts array: (all zeros):
		for ( var i = -1; i < xEdges.length; i++ ) {
			counts[ i+1 ] = [];
			for ( var j = -1; j < yEdges.length; j++ ) {
				counts[ i+1 ][ j+1 ] = 0;
			} // end FOR i
		} // end FOR j

		// For each value in the data array, find where the value resides along the edge vector in each dimension:
		for ( var k = 0; k < data.length; k++ ) {

			for ( var n = 0; n < data[ k ].length; n++ ) {

				// Perform a binary search along each dimension to find the index where the value equals or exceeds the corresponding value in the edge vector:
				id1 = binarysearch( xEdges, data[ k ][ n ][ 0 ] );
				id2 = binarysearch( yEdges, data[ k ][ n ][ 1 ] );

				// Update the counts for the bin:
				counts[ id1+1 ][ id2+1 ] += 1;

			} // end FOR n

		} // end FOR k

		// Return the counts:
		return counts;

	}
359298
private int[] getRange(String field, int min, int max) {
        int[] result = new int[2];
        if (field.contains("*")) {
            result[0] = min;
            result[1] = max - 1;
            return result;
        }
        if (!field.contains("-")) {
            result[0] = result[1] = Integer.valueOf(field);
        } else {
            String[] split = StringUtils.delimitedListToStringArray(field, "-");
            if (split.length > 2) {
                throw new IllegalArgumentException("Range has more than two fields: '" + field + "' in expression \""
                        + this.expression + "\"");
            }
            result[0] = Integer.valueOf(split[0]);
            result[1] = Integer.valueOf(split[1]);
        }
        if (result[0] >= max || result[1] >= max) {
            throw new IllegalArgumentException("Range exceeds maximum (" + max + "): '" + field + "' in expression \""
                    + this.expression + "\"");
        }
        if (result[0] < min || result[1] < min) {
            throw new IllegalArgumentException("Range less than minimum (" + min + "): '" + field
                    + "' in expression \"" + this.expression + "\"");
        }
        return result;
    }
727071
def histogram_bin_edges_minwidth(min_width, bins):
  
  '''

  # escape
  if min_width is None : return bins
  if min_width is False: return bins

  # keep removing where needed
  while True:

    idx = np.where(np.diff(bins) < min_width)[0]

    if len(idx) == 0: return bins

    idx = idx[0]

    if idx+1 == len(bins)-1: bins = np.hstack(( bins[:(idx)  ], bins[-1]       ))
    else                   : bins = np.hstack(( bins[:(idx+1)], bins[(idx+2):] ))
1059852
def _weave_dohist(data, s, binsize, hist, rev, dorev=False, verbose=0):

    if dorev:
        dorev=1
    else:
        dorev=0
    
    code = """

    int64_t nbin = hist.size();
    int64_t binnum_old = -1;

    // index of minimum value
    int64_t imin = s(0);
    for (int64_t i=0; i<s.size(); i++) {

        int64_t offset = i+nbin+1;
        int64_t data_index = s(i);


        if (dorev) {
            rev(offset) = data_index;
        }

        int64_t binnum = (int64_t) ( (data(data_index)-data(imin))/binsize);

        if (binnum >= 0 && binnum < nbin) {
            if (dorev && (binnum > binnum_old) ) {
                int64_t tbin = binnum_old + 1;
                while (tbin <= binnum) {
                    rev(tbin) = offset;
                    tbin++;
                }
            }
            hist(binnum) = hist(binnum) + 1;
            binnum_old = binnum;
        }
    }

    int64_t tbin = binnum_old + 1;
    while (tbin <= nbin) {
        if (dorev) {
            rev(tbin) = rev.size();
        }
        tbin++;
    }

    """

    scipy.weave.inline(code, ['data','s','binsize','hist','rev','dorev'],
                       type_converters = scipy.weave.converters.blitz, verbose=verbose)
    return
1257559
def histogram(self, cycle_data, bins=10):
        
        values, edges = np.histogram(cycle_data['cycle_time'].astype('timedelta64[D]').dropna(), bins=bins)

        index = []
        for i, v in enumerate(edges):
            if i == 0:
                continue
            index.append("%.01f to %.01f" % (edges[i - 1], edges[i],))

        return pd.Series(values, name="Items", index=index)
1225394
def _process_hist(self, hist):
        
        edges, hvals, widths, lims, isdatetime = super(SideHistogramPlot, self)._process_hist(hist)
        offset = self.offset * lims[3]
        hvals *= 1-self.offset
        hvals += offset
        lims = lims[0:3] + (lims[3] + offset,)
        return edges, hvals, widths, lims, isdatetime
443418
private void getHistogramRecursively(TableCountProbingContext probingContext, Histogram histogram, StrSubstitutor sub,
      Map<String, String> values, int count, long startEpoch, long endEpoch) {
    long midpointEpoch = startEpoch + (endEpoch - startEpoch) / 2;

    // don't split further if small, above the probe limit, or less than 1 second difference between the midpoint and start
    if (count <= probingContext.bucketSizeLimit
        || probingContext.probeCount > probingContext.probeLimit
        || (midpointEpoch - startEpoch < MIN_SPLIT_TIME_MILLIS)) {
      histogram.add(new HistogramGroup(Utils.epochToDate(startEpoch, SECONDS_FORMAT), count));
      return;
    }

    int countLeft = getCountForRange(probingContext, sub, values, startEpoch, midpointEpoch);

    getHistogramRecursively(probingContext, histogram, sub, values, countLeft, startEpoch, midpointEpoch);
    log.debug("Count {} for left partition {} to {}", countLeft, startEpoch, midpointEpoch);

    int countRight = count - countLeft;

    getHistogramRecursively(probingContext, histogram, sub, values, countRight, midpointEpoch, endEpoch);
    log.debug("Count {} for right partition {} to {}", countRight, midpointEpoch, endEpoch);
  }
698909
def _draw_ascii_graph(self, field):
        ''''''
        row_strings = []
        for y in range(len(field[0])):
            row = ''
            for x in range(len(field)):
                row += field[x][y]
            row_strings.insert(0, row)
        graph_string = '\n'.join(row_strings)
        return graph_string
1040356
def _make_histogram(values, bins):
    """"""
    values = values.reshape(-1)
    counts, limits = np.histogram(values, bins=bins)
    limits = limits[1:]

    sum_sq = values.dot(values)
    return HistogramProto(min=values.min(),
                          max=values.max(),
                          num=len(values),
                          sum=values.sum(),
                          sum_squares=sum_sq,
                          bucket_limit=limits,
                          bucket=counts)
358346
def loghistogram(data, base=2, ascii=True, title="Counts", summary=False):
    
    from jcvi.utils.cbook import percentage

    if summary:
        unique = len(data)
        total = sum(data)

        # Print out a distribution
        print("Unique: {0}".format(percentage(unique, total)), file=sys.stderr)

    bins = defaultdict(int)
    for d in data:
        logd = int(log(d, base))
        bins[logd] += 1

    x, y = [], []
    for size, number in sorted(bins.items()):
        lb, ub = base ** size, base ** (size + 1)
        x.append((lb, ub))
        y.append(number)

    asciiplot(x, y, title=title)
92065
def multi_h1(cls, a_dict: Dict[str, Any], bins=None, **kwargs) -> "HistogramCollection":
        """"""
        from physt.binnings import calculate_bins
        mega_values = np.concatenate(list(a_dict.values()))
        binning = calculate_bins(mega_values, bins, **kwargs)

        title = kwargs.pop("title", None)
        name = kwargs.pop("name", None)

        collection = HistogramCollection(binning=binning, title=title, name=name)
        for key, value in a_dict.items():
            collection.create(key, value)
        return collection
65607
def val_to_bin(edges, x):
    """"""
    ibin = np.digitize(np.array(x, ndmin=1), edges) - 1
    return ibin
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