#NEWLINE# def mux( seed_pool , n_samples , k , lam = 256.0 , pool_weights = None , with_replacement = True , prune_empty_seeds = True , revive = False ) : #NEWLINE# #INDENT# 'Stochastic multiplexor for generator seeds.\n\n    Given an array of Streamer objects, do the following:\n\n        1. Select ``k`` seeds at random to activate\n        2. Assign each activated seed a sample count ~ Poisson(lam)\n        3. Yield samples from the streams by randomly multiplexing\n           from the active set.\n        4. When a stream is exhausted, select a new one from the pool.\n\n    Parameters\n    ----------\n    seed_pool : iterable of Streamer\n        The collection of Streamer objects\n\n    n_samples : int > 0 or None\n        The number of samples to generate.\n        If ``None``, sample indefinitely.\n\n    k : int > 0\n        The number of streams to keep active at any time.\n\n    lam : float > 0 or None\n        Rate parameter for the Poisson distribution governing sample counts\n        for individual streams.\n        If ``None``, sample infinitely from each stream.\n\n    pool_weights : np.ndarray or None\n        Optional weighting for ``seed_pool``.\n        If ``None``, then weights are assumed to be uniform.\n        Otherwise, ``pool_weights[i]`` defines the sampling proportion\n        of ``seed_pool[i]``.\n\n        Must have the same length as ``seed_pool``.\n\n    with_replacement : bool\n        Sample Streamers with replacement.  This allows a single stream to be\n        used multiple times (even simultaneously).\n        If ``False``, then each Streamer is consumed at most once and never\n        revisited.\n\n    prune_empty_seeds : bool\n        Disable seeds from the pool that produced no data.\n        If ``True``, Streamers that previously produced no data are never\n        revisited.\n        Note that this may be undesireable for streams where past emptiness\n        may not imply future emptiness.\n\n    revive: bool\n        If ``with_replacement`` is ``False``, setting ``revive=True``\n        will re-insert previously exhausted seeds into the candidate set.\n\n        This configuration allows a seed to be active at most once at any\n        time.\n    ' #NEWLINE# n_seeds = len ( seed_pool ) #NEWLINE# if ( not n_seeds ) : #NEWLINE# #INDENT# raise RuntimeError ( 'Cannot mux an empty seed-pool' ) #NEWLINE# #UNINDENT# seed_distribution = ( ( 1.0 / n_seeds ) * np . ones ( n_seeds ) ) #NEWLINE# if ( pool_weights is None ) : #NEWLINE# #INDENT# pool_weights = seed_distribution . copy ( ) #NEWLINE# #UNINDENT# pool_weights = np . atleast_1d ( pool_weights ) #NEWLINE# assert ( len ( pool_weights ) == len ( seed_pool ) ) #NEWLINE# assert ( pool_weights > 0.0 ) . any ( ) #NEWLINE# pool_weights /= np . sum ( pool_weights ) #NEWLINE# streams = ( [ None ] * k ) #NEWLINE# stream_weights = np . zeros ( k ) #NEWLINE# stream_counts = np . zeros ( k , dtype = int ) #NEWLINE# stream_idxs = np . zeros ( k , dtype = int ) #NEWLINE# for idx in range ( k ) : #NEWLINE# #INDENT# if ( not ( seed_distribution > 0 ) . any ( ) ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# stream_idxs [ idx ] = np . random . choice ( n_seeds , p = seed_distribution ) #NEWLINE# ( streams [ idx ] , stream_weights [ idx ] ) = generate_new_seed ( stream_idxs [ idx ] , seed_pool , pool_weights , seed_distribution , lam , with_replacement ) #NEWLINE# #UNINDENT# weight_norm = np . sum ( stream_weights ) #NEWLINE# n = 0 #NEWLINE# if ( n_samples is None ) : #NEWLINE# #INDENT# n_samples = np . inf #NEWLINE# #UNINDENT# while ( ( n < n_samples ) and ( weight_norm > 0.0 ) ) : #NEWLINE# #INDENT# idx = np . random . choice ( k , p = ( stream_weights / weight_norm ) ) #NEWLINE# try : #NEWLINE# #INDENT# ( yield six . advance_iterator ( streams [ idx ] ) ) #NEWLINE# n += 1 #NEWLINE# stream_counts [ idx ] += 1 #NEWLINE# #UNINDENT# except StopIteration : #NEWLINE# #INDENT# if ( prune_empty_seeds and ( stream_counts [ idx ] == 0 ) ) : #NEWLINE# #INDENT# seed_distribution [ stream_idxs [ idx ] ] = 0.0 #NEWLINE# #UNINDENT# if ( revive and ( not with_replacement ) ) : #NEWLINE# #INDENT# if seed_distribution . any ( ) : #NEWLINE# #INDENT# seed_distribution [ stream_idxs [ idx ] ] = np . max ( seed_distribution ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# seed_distribution [ stream_idxs [ idx ] ] = 1.0 #NEWLINE# #UNINDENT# #UNINDENT# if ( seed_distribution > 0 ) . any ( ) : #NEWLINE# #INDENT# seed_distribution [ : ] /= np . sum ( seed_distribution ) #NEWLINE# stream_idxs [ idx ] = np . random . choice ( n_seeds , p = seed_distribution ) #NEWLINE# ( streams [ idx ] , stream_weights [ idx ] ) = generate_new_seed ( stream_idxs [ idx ] , seed_pool , pool_weights , seed_distribution , lam , with_replacement ) #NEWLINE# stream_counts [ idx ] = 0 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# stream_weights [ idx ] = 0.0 #NEWLINE# #UNINDENT# weight_norm = np . sum ( stream_weights )