mixle.enumeration.streams module

Generic stream primitives for smart enumeration.

The building blocks shared by the best-first algorithms and the combinator enumerators: freeze (canonical hashable keys for de-duplicating support values), BufferedStream (random access by rank into a lazy descending-probability stream), merge_enumerators (k-way merge of sorted streams with disjoint supports), and supports_enumeration (the capability probe). See mixle.stats.compute.pdist.DistributionEnumerator for the enumeration contract.

freeze(x)[source]

Return a canonical hashable key for x, for de-duplicating support values.

Lists/tuples freeze element-wise to tuples, dicts to frozensets of (key, value) pairs, sets to frozensets, numpy arrays to (shape, bytes), numpy scalars to their python equivalents, and NaN to a shared sentinel (so nan == nan for dedup purposes). Raises TypeError for values that cannot be canonicalized.

Parameters:

x (Any)

Return type:

Hashable

supports_enumeration(dist)[source]

Return True if dist.enumerator() can be constructed.

Return type:

bool

class BufferedStream(it)[source]

Bases: object

Random access by rank into a lazy stream of (value, log_prob) pairs.

get(i) extends an internal buffer as needed and returns the i-th item, or None if the stream has fewer than i+1 items. The underlying stream is consumed at most once regardless of how many consumers share this object.

Parameters:

it (Iterator[tuple[Any, float]])

get(i)[source]
Parameters:

i (int)

Return type:

tuple[Any, float] | None

merge_enumerators(streams, offsets)[source]

Lazy k-way merge of sorted (value, log_prob) streams with per-stream offsets.

Stream k’s log probs are shifted by offsets[k]. Correct only when the streams have pairwise disjoint supports (no de-duplication or re-scoring is performed).

Parameters:
Return type:

Iterator[tuple[Any, float]]