pescador.mux.ShuffledMux

class pescador.mux.ShuffledMux(streamers, weights=None, random_state=None)

A variation on a mux, which takes N streamers, and samples from them equally, guaranteeing all N streamers to be “active”.

ShuffledMux automatically restarts streams when they die.

For a more nuanced behavior, consider using StochasticMux with single_active=True.

Examples

Sample three streams equally:

>>> a = pescador.Streamer("a")
>>> b = pescador.Streamer("b")
>>> c = pescador.Streamer("c")
>>> mux = pescador.ShuffledMux([a, b, c])
>>> print("".join(mux(max_iter=9)))
'babcbcabb'
>>> print("".join(mux(max_iter=30)))
'bacbabcaabccbcaccbabccbcaaccba'

Sample stream ‘a’ twice as often as ‘b’ or ‘c’:

>>> wmux = pescador.ShuffledMux([a, b, c], weights=[0.5, 0.25, 0.25])
>>> print("".join(wmux(max_iter=9)))
'caaabaaab'
>>> print("".join(wmux(max_iter=30)))
'acacababcaabcaaacaaccabcbaaaaa'
__init__(streamers, weights=None, random_state=None)
Parameters:
streamersiterable of streamers

The collection of streamer-type objects

weightsnp.ndarray or None

Optional weighting for streamers. If None, then weights are assumed to be uniform. Otherwise, weights[i] defines the sampling proportion of streamers[i].

Must have the same length as streamers.

random_stateNone, int, or np.random.RandomState

If int, random_state is the seed used by the random number generator;

If RandomState instance, random_state is the random number generator;

If None, the random number generator is the RandomState instance used by np.random.

Methods

__init__(streamers[, weights, random_state])

Parameters:

cycle([max_iter])

Iterate from the streamer infinitely.

iterate([max_iter])

Yield items from the mux, and handle stream exhaustion and replacement.

Attributes

active

Return true if the stream is active (ie there are still open / existing streams)

is_activated_copy

is_activated_copy is true if this object is a copy of the original Streamer and has been activated.

n_streams

Return the number of streamers.