bin_edges

mdtools.check.bin_edges(bins, amin=0, amax=1, right=False, tol=1e-6, verbose=True)[source]

Check if bin edges are chosen properly for binning a given interval.

The bin edges must meet the following requirements:

  • There must be at least one bin edge.

  • The first bin edge must not be less than amin.

  • The last bin edge must not be greater than amax.

Parameters:
  • bins (array_like) – Array containing the bin edges. The bin edges are sorted in ascending order and duplicates are removed.

  • amin (scalar, optional) – A minimum value that the first bin edge must not undermine.

  • amax (scalar, optional) – A maximum value that the last bin edge must not exceed.

  • right (bool, optional) – Indicating whether the bin intervals include the right or the left bin edge. If True, the bin intervals include the right bin edge, i.e. the bin intervals are left-open and right-closed: (a, b] -> a < x <= b. If False (default), the bin intervals include the left bin edge, i.e. the bin intervals are left-closed and right-open: [a, b) -> a <= x < b.

  • tol (scalar, optional) – A tolerance value subtracted from amin or added to amax. If right is True, the first bin edge will be set to amin - tol to account for the left-open bin interval. If right is False, the last bin edge will be set to amax + tol to account for the right-open bin interval. In this way, values of amin or amax will be properly binned into the first or last bin, respectively, when using numpy.digitize(). Set tol to zero if you do not want to account for the first/last half-open bin interval.

  • verbose (bool, optional) – If True (default), any changes of the bin edges are printed to standard output.

Returns:

bins (numpy.ndarray) – Unique and sorted copy of the input bin edges. If bins did not already contain amin and/or amax these values are inserted with tol subtracted or added according to the value of right. If bins already contained amin and/or amax, tol will be subtracted or added according to the value of right.

Raises:

ValueError – If len(bins) is zero; bins[0] is less than amin or amin - tol (if right is False or True); bins[-1] is greater than amax + tol or amax (if right is False or True).

See also

mdtools.check.bins()

Check if the input is appropriate for creating bin edges

mdtools.check.distance_bins()

Check if the input is appropriate for binning distances that obey the minimum image convention

numpy.digitize()

Return the indices of the bins to which each value in the input array belongs

Notes

The bin edges returned by this function can for instance be used to discretize the positions of compounds that are wrapped into the primary unit cell with numpy.digitize().