← API Reference

elwood_spatial.bins

Value discretization. BinSpec maps continuous measurements into discrete bins for entropy-based analysis. Bins are inclusive on both ends: a value matches when low ≤ value ≤ high.

BinSpec

@dataclass(frozen=True)
class BinSpec:
    edges: tuple[tuple[float, float], ...]
    labels: tuple[str, ...] | None = None
FieldTypeDescription
edgestuple[tuple[float, float], ...]Sequence of (low, high) bin boundaries
labelstuple[str, ...] | NoneOptional human-readable label per bin

Properties

PropertyReturnsDescription
num_binsintNumber of bins

Class Methods

BinSpec.from_tuples(bins)

ParameterTypeDescription
binsSequence[tuple[float, float]]List of (low, high) pairs
bins = BinSpec.from_tuples([(0, 50), (51, 100), (101, 150)])

BinSpec.from_dicts(bins, range_key="range", label_key="cat")

ParameterTypeDefaultDescription
binsSequence[dict]List of dicts with range and label keys
range_keystr"range"Key for the [low, high] range
label_keystr"cat"Key for the category label
bins = BinSpec.from_dicts([
    {"cat": "Good", "range": [0, 50]},
    {"cat": "Moderate", "range": [51, 100]},
])

Instance Methods

bin_index(value) → int

Returns the bin index for a scalar value, or -1 if out of range, NaN, or None.

bin_values(values) → np.ndarray

Vectorized — returns an array of bin indices for a NumPy array of values.

Convenience Functions

compute_bin_index(value, bins)

ParameterTypeDescription
valuefloatScalar value
binsBinSpecBin specification

Returns int — bin index or -1.

compute_bin_indices(values, bins)

ParameterTypeDescription
valuesnp.ndarrayArray of values
binsBinSpecBin specification

Returns np.ndarray of integers.