binny.utils.broadcasting module#

Utilities to broadcast parameters to per-bin arrays.

binny.utils.broadcasting.as_per_bin(x: float | int | Sequence | None, n_bins: int, name: str) ndarray#

Broadcasts a scalar/None/sequence to a per-bin array of length n_bins.

Rules: - If x is a float or int -> repeat for all bins. - If x is None -> return an object array of None with length n_bins. - If x is a sequence -> validate length n_bins and broadcast elementwise. - Raise ValueError if lengths mismatch.

Parameters:
  • x – A scalar, None, or sequence.

  • n_bins – Number of bins.

  • name – Name of the parameter (for error messages).

Returns:

A NumPy array of length n_bins. Uses dtype=float when possible, otherwise dtype=object (e.g. when values include None).

Raises:
  • ValueError – If n_bins < 1 or if x is a sequence of incorrect length.

  • TypeError – If x is not a scalar, None, or sequence.