r'''
Merge bins with right-neighbour until each bin has a minimum number of data-points.

:arguments:

  **data** (``<array_like>``)
    Input data. The histogram is computed over the flattened array.

  **bins** (``<array_like>`` | ``<int>``)
    The bin-edges (or the number of bins, automatically converted to equal-sized bins).

  **min_count** (``<int>``)
    The minimum number of data-points per bin.

def histogram_bin_edges_mincount(data, min_count, bins):
  r'''
Merge bins with right-neighbour until each bin has a minimum number of data-points.

:arguments:

  **data** (``<array_like>``)
    Input data. The histogram is computed over the flattened array.

  **bins** (``<array_like>`` | ``<int>``)
    The bin-edges (or the number of bins, automatically converted to equal-sized bins).

  **min_count** (``<int>``)
    The minimum number of data-points per bin.
  '''

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

  # check
  if type(min_count) != int: raise IOError('"min_count" must be an integer number')

  # keep removing where needed
  while True:

    P, _ = np.histogram(data, bins=bins, density=False)

    idx = np.where(P < min_count)[0]

    if len(idx) == 0: return bins

    idx = idx[0]

    if idx+1 == len(P): bins = np.hstack(( bins[:(idx)  ], bins[-1]       ))
    else              : bins = np.hstack(( bins[:(idx+1)], bins[(idx+2):] ))