示例#1
0
    def test_multiple_copies(self):
        """Check that the Mux class can be activated multiple times successfully.
        """
        ab = pescador.Streamer('ab')
        cde = pescador.Streamer('cde')
        fghi = pescador.Streamer('fghi')
        mux = pescador.mux.StochasticMux([ab, cde, fghi], n_active=5, rate=2)

        gen1 = mux.iterate(6)
        gen2 = mux.iterate(8)

        # No streamers should be active until we actually start the generators
        assert mux.active == 0

        # grab one sample each to make sure we've actually started the
        # generator
        _ = next(gen1)
        _ = next(gen2)
        assert mux.active == 2

        # the first one should die after 5 more samples
        result1 = list(gen1)
        assert len(result1) == 5
        assert mux.active == 1

        # The second should die after 7
        result2 = list(gen2)
        assert len(result2) == 7
        assert mux.active == 0
示例#2
0
    def test_chain_generator_with_multiple_copies(self):
        def stream_gen():
            things_to_generate = [
                "a", "bb", [], "ccc"
            ]
            for item in things_to_generate:
                yield pescador.Streamer(item)

        mux = pescador.mux.ChainMux(stream_gen, mode="exhaustive")

        gen1 = mux.iterate(3)
        gen2 = mux.iterate()  # n == 6

        # No streamers should be active until we actually start the generators
        assert mux.active == 0

        # grab one sample each to make sure we've actually started the
        # generator
        _ = next(gen1)
        _ = next(gen2)
        assert mux.active == 2

        # the first one should die after two more samples
        result1 = list(gen1)
        assert "".join(result1) == "bb"
        assert len(result1) == 2
        assert mux.active == 1

        # The second should die after 6
        result2 = list(gen2)
        assert "".join(result2) == "bbccc"
        assert len(result2) == 5
        assert mux.active == 0
示例#3
0
    def test_rr_multiple_copies(self):
        ab = pescador.Streamer('ab')
        cde = pescador.Streamer('cde')
        fghi = pescador.Streamer('fghi')
        mux = pescador.mux.RoundRobinMux([ab, cde, fghi], 'exhaustive')

        gen1 = mux.iterate(3)
        gen2 = mux.iterate()  # n == 9

        # No streamers should be active until we actually start the generators
        assert mux.active == 0

        # grab one sample each to make sure we've actually started the
        # generator
        _ = next(gen1)
        _ = next(gen2)
        assert mux.active == 2

        # the first one should die after two more samples
        result1 = list(gen1)
        assert "".join(result1) == "cf"
        assert len(result1) == 2
        assert mux.active == 1

        # The second should die after 6
        result2 = list(gen2)
        assert "".join(result2) == "cfbdgehi"
        assert len(result2) == 8
        assert mux.active == 0
示例#4
0
    def test_multiple_copies(self):
        """Check that the Mux class can be activated multiple times successfully.
        """
        ab = pescador.Streamer('ab')
        cde = pescador.Streamer('cde')
        fghi = pescador.Streamer('fghi')
        mux = pescador.mux.StochasticMux([ab, cde, fghi], n_active=5, rate=2)

        gen1 = mux.iterate(6)
        gen2 = mux.iterate(8)

        # No streamers should be active until we actually start the generators
        assert mux.active == 0

        # grab one sample each to make sure we've actually started the
        # generator
        _ = next(gen1)
        _ = next(gen2)
        assert mux.active == 2

        # the first one should die after 5 more samples
        result1 = list(gen1)
        assert len(result1) == 5
        assert mux.active == 1

        # The second should die after 7
        result2 = list(gen2)
        assert len(result2) == 7
        assert mux.active == 0
示例#5
0
    def test_rr_multiple_copies(self):
        ab = pescador.Streamer('ab')
        cde = pescador.Streamer('cde')
        fghi = pescador.Streamer('fghi')
        mux = pescador.mux.RoundRobinMux([ab, cde, fghi], 'exhaustive')

        gen1 = mux.iterate(3)
        gen2 = mux.iterate()  # n == 9

        # No streamers should be active until we actually start the generators
        assert mux.active == 0

        # grab one sample each to make sure we've actually started the
        # generator
        _ = next(gen1)
        _ = next(gen2)
        assert mux.active == 2

        # the first one should die after two more samples
        result1 = list(gen1)
        assert "".join(result1) == "cf"
        assert len(result1) == 2
        assert mux.active == 1

        # The second should die after 6
        result2 = list(gen2)
        assert "".join(result2) == "cfbdgehi"
        assert len(result2) == 8
        assert mux.active == 0
示例#6
0
    def test_chain_generator_with_multiple_copies(self):
        def stream_gen():
            things_to_generate = [
                "a", "bb", [], "ccc"
            ]
            for item in things_to_generate:
                yield pescador.Streamer(item)

        mux = pescador.mux.ChainMux(stream_gen, mode="exhaustive")

        gen1 = mux.iterate(3)
        gen2 = mux.iterate()  # n == 6

        # No streamers should be active until we actually start the generators
        assert mux.active == 0

        # grab one sample each to make sure we've actually started the
        # generator
        _ = next(gen1)
        _ = next(gen2)
        assert mux.active == 2

        # the first one should die after two more samples
        result1 = list(gen1)
        assert "".join(result1) == "bb"
        assert len(result1) == 2
        assert mux.active == 1

        # The second should die after 6
        result2 = list(gen2)
        assert "".join(result2) == "bbccc"
        assert len(result2) == 5
        assert mux.active == 0
示例#7
0
 def test_chain_mux_exhaustive(self):
     a = pescador.Streamer("abc")
     b = pescador.Streamer("def")
     mux = pescador.mux.ChainMux([a, b], mode="exhaustive")
     assert "".join(list(mux.iterate())) == "abcdef"
     # Make sure it's the same as itertools.chain
     assert list(mux.iterate()) == list(
         itertools.chain(a.iterate(), b.iterate()))
示例#8
0
 def test_chain_mux_exhaustive(self):
     a = pescador.Streamer("abc")
     b = pescador.Streamer("def")
     mux = pescador.mux.ChainMux([a, b],
                                 mode="exhaustive")
     assert "".join(list(mux.iterate())) == "abcdef"
     # Make sure it's the same as itertools.chain
     assert list(mux.iterate()) == list(
         itertools.chain(a.iterate(), b.iterate()))
示例#9
0
    def test_deepcopy__randomseed(self, mux_class, random_state):
        n_streams = 10
        # We use an offset to make sure each stream produces unique values.
        # That way, we can tell when the mux copies have returned
        # the same streamer or not.
        streamers = [pescador.Streamer(T.infinite_generator, offset=i * 10)
                     for i in range(n_streams)]

        mux = mux_class(streamers, random_state=random_state)

        copy_mux = copy.deepcopy(mux)
        assert mux.streamers is not copy_mux.streamers
        assert len(mux.streamers) == len(copy_mux.streamers)

        if random_state is None:
            assert mux.rng == np.random
            assert copy_mux.rng == np.random
        else:
            assert mux.rng is not copy_mux.rng

            s1 = mux.rng.get_state()
            s2 = copy_mux.rng.get_state()
            # Only the second parameter in the state tuple is useful to
            # compare.
            assert np.allclose(s1[1], s2[1])

            # Using global state (random_state=None), we can't necessarily
            # guarantee that these will be the same withour resetting the seed,
            # but here with the local random state, we can.
            sample1 = list(mux.iterate(30))
            sample2 = list(copy_mux.iterate(30))

            assert T._eq_list_of_dicts(sample1, sample2)
示例#10
0
    def test_deepcopy__randomseed(self, mux_class, random_state):
        n_streams = 10
        # We use an offset to make sure each stream produces unique values.
        # That way, we can tell when the mux copies have returned
        # the same streamer or not.
        streamers = [pescador.Streamer(T.infinite_generator, offset=i * 10)
                     for i in range(n_streams)]

        mux = mux_class(streamers, random_state=random_state)

        copy_mux = copy.deepcopy(mux)
        assert mux.streamers is not copy_mux.streamers
        assert len(mux.streamers) == len(copy_mux.streamers)

        if random_state is None:
            assert mux.rng == np.random
            assert copy_mux.rng == np.random
        else:
            assert mux.rng is not copy_mux.rng

            s1 = mux.rng.get_state()
            s2 = copy_mux.rng.get_state()
            # Only the second parameter in the state tuple is useful to
            # compare.
            assert np.allclose(s1[1], s2[1])

            # Using global state (random_state=None), we can't necessarily
            # guarantee that these will be the same withour resetting the seed,
            # but here with the local random state, we can.
            sample1 = list(mux.iterate(30))
            sample2 = list(copy_mux.iterate(30))

            assert T._eq_list_of_dicts(sample1, sample2)
示例#11
0
 def test_modes(self, mode):
     a = pescador.Streamer("abc")
     b = pescador.Streamer("def")
     mux = pescador.mux.ChainMux([a, b],
                                 mode="exhaustive")
     result = list(mux.iterate())
     assert len(result) > 0
示例#12
0
 def test_modes(self, mode):
     a = pescador.Streamer("abc")
     b = pescador.Streamer("def")
     mux = pescador.mux.ChainMux([a, b],
                                 mode="exhaustive")
     result = list(mux.iterate())
     assert len(result) > 0
示例#13
0
    def test_valid_modes(self, mode, n_samples):
        """Simply tests the modes to make sure they work."""
        streamers = [pescador.Streamer(T.infinite_generator) for _ in range(4)]

        mux = pescador.mux.StochasticMux(streamers, 1, rate=10, mode=mode)
        output = list(mux.iterate(n_samples))
        assert len(output) == n_samples
示例#14
0
 def test_critical_mux(self, mux_class):
     # Check on Issue #80
     chars = 'abcde'
     streamers = [pescador.Streamer(x * 5) for x in chars]
     mux = mux_class(streamers, len(chars), rate=None,
                     prune_empty_streams=False, random_state=135)
     samples = mux.iterate(max_iter=1000)
     print(collections.Counter(samples))
示例#15
0
    def test_valid_modes(self, mode, n_samples):
        """Simply tests the modes to make sure they work."""
        streamers = [pescador.Streamer(T.infinite_generator)
                     for _ in range(4)]

        mux = pescador.mux.StochasticMux(streamers, 1, rate=10, mode=mode)
        output = list(mux.iterate(n_samples))
        assert len(output) == n_samples
示例#16
0
 def test_rr_with_empty_streams(self):
     things_to_generate = [
         "aa", [], "bb", [], [], [], "cccc"
     ]
     streamers = [pescador.Streamer(x) for x in things_to_generate]
     mux = pescador.mux.RoundRobinMux(streamers, 'exhaustive')
     result = "".join(list(mux.iterate()))
     assert result == "abcabccc"
示例#17
0
 def test_critical_mux(self, mux_class):
     # Check on Issue #80
     chars = 'abcde'
     streamers = [pescador.Streamer(x * 5) for x in chars]
     mux = mux_class(streamers, len(chars), rate=None,
                     prune_empty_streams=False, random_state=135)
     samples = mux.iterate(max_iter=1000)
     print(collections.Counter(samples))
示例#18
0
 def test_rr_with_empty_streams(self):
     things_to_generate = [
         "aa", [], "bb", [], [], [], "cccc"
     ]
     streamers = [pescador.Streamer(x) for x in things_to_generate]
     mux = pescador.mux.RoundRobinMux(streamers, 'exhaustive')
     result = "".join(list(mux.iterate()))
     assert result == "abcabccc"
示例#19
0
 def test_chain_empty_streamer_of_streams(self):
     def stream_gen():
         # Yield causes it to be a generator, but the return exits first,
         # creating an 'empty' generator.
         return
         yield
     mux = pescador.mux.ChainMux(stream_gen, mode="exhaustive")
     result = "".join(list(mux.iterate()))
     assert len(result) == 0
     assert result == ''
示例#20
0
    def test_chain_generatorfn_with_empty_streams(self):
        def stream_gen():
            things_to_generate = ["aa", [], "bb", [], [], [], "cccc"]
            for item in things_to_generate:
                yield pescador.Streamer(item)

        mux = pescador.mux.ChainMux(stream_gen, mode="exhaustive")
        result = "".join(list(mux.iterate()))
        assert len(result) == 8
        assert result == "aabbcccc"
示例#21
0
    def test_mux_single_active(self, mux_class, n_streams, n_samples, k, rate):
        streamers = [pescador.Streamer(T.finite_generator, 10)
                     for _ in range(n_streams)]

        mux = mux_class(streamers, k, rate=rate)
        estimate = list(mux.iterate(n_samples))

        # Make sure we get the right number of samples
        # This is highly improbable when revive=False
        assert len(estimate) == n_samples
示例#22
0
    def test_mux_single_active(self, mux_class, n_streams, n_samples, k, rate):
        streamers = [pescador.Streamer(T.finite_generator, 10)
                     for _ in range(n_streams)]

        mux = mux_class(streamers, k, rate=rate)
        estimate = list(mux.iterate(n_samples))

        # Make sure we get the right number of samples
        # This is highly improbable when revive=False
        assert len(estimate) == n_samples
示例#23
0
 def test_chain_empty_streamer_of_streams(self):
     def stream_gen():
         # Yield causes it to be a generator, but the return exits first,
         # creating an 'empty' generator.
         return
         yield
     mux = pescador.mux.ChainMux(stream_gen, mode="exhaustive")
     result = "".join(list(mux.iterate()))
     assert len(result) == 0
     assert result == ''
示例#24
0
    def test_mux_replacement(self, mux_class, n_streams, n_samples, n_active,
                             rate, random_state):
        streamers = [pescador.Streamer(T.infinite_generator)
                     for _ in range(n_streams)]

        mux = mux_class(streamers, n_active, rate=rate,
                        random_state=random_state)
        estimate = list(mux.iterate(n_samples))

        # Make sure we get the right number of samples
        assert len(estimate) == n_samples
示例#25
0
    def test_chain_mux_exhaustive_many(self):
        a = pescador.Streamer("a")
        b = pescador.Streamer("b")
        c = pescador.Streamer("c")
        d = pescador.Streamer("d")
        e = pescador.Streamer("e")
        f = pescador.Streamer("f")
        g = pescador.Streamer("g")

        mux = pescador.mux.ChainMux([a, b, c, d, e, f, g], mode="exhaustive")
        assert "".join(list(mux.iterate())) == "abcdefg"
示例#26
0
    def test_mux_replacement(self, mux_class, n_streams, n_samples, n_active,
                             rate, random_state):
        streamers = [pescador.Streamer(T.infinite_generator)
                     for _ in range(n_streams)]

        mux = mux_class(streamers, n_active, rate=rate,
                        random_state=random_state)
        estimate = list(mux.iterate(n_samples))

        # Make sure we get the right number of samples
        assert len(estimate) == n_samples
示例#27
0
    def test_chain_mux_exhaustive_many(self):
        a = pescador.Streamer("a")
        b = pescador.Streamer("b")
        c = pescador.Streamer("c")
        d = pescador.Streamer("d")
        e = pescador.Streamer("e")
        f = pescador.Streamer("f")
        g = pescador.Streamer("g")

        mux = pescador.mux.ChainMux([a, b, c, d, e, f, g],
                                    mode="exhaustive")
        assert "".join(list(mux.iterate())) == "abcdefg"
示例#28
0
    def test_chain_generatorfn_with_empty_streams(self):
        def stream_gen():
            things_to_generate = [
                "aa", [], "bb", [], [], [], "cccc"
            ]
            for item in things_to_generate:
                yield pescador.Streamer(item)

        mux = pescador.mux.ChainMux(stream_gen, mode="exhaustive")
        result = "".join(list(mux.iterate()))
        assert len(result) == 8
        assert result == "aabbcccc"
示例#29
0
    def test_rr_permuted_cycle(self):
        a = pescador.Streamer('a')
        b = pescador.Streamer('bb')
        empty = pescador.Streamer([])
        c = pescador.Streamer('c')
        mux = pescador.mux.RoundRobinMux([a, b, empty, c], 'permuted_cycle')

        result = list(mux.iterate(12))
        counts = collections.Counter(result)
        assert len(counts) == 3
        assert counts['a'] == 3
        assert counts['b'] == 6
        assert counts['c'] == 3
示例#30
0
    def test_rr_permuted_cycle(self):
        a = pescador.Streamer('a')
        b = pescador.Streamer('bb')
        empty = pescador.Streamer([])
        c = pescador.Streamer('c')
        mux = pescador.mux.RoundRobinMux([a, b, empty, c], 'permuted_cycle')

        result = list(mux.iterate(12))
        counts = collections.Counter(result)
        assert len(counts) == 3
        assert counts['a'] == 3
        assert counts['b'] == 6
        assert counts['c'] == 3
示例#31
0
def test_critical_mux():
    # Check on Issue #80
    chars = 'abcde'
    streamers = [pescador.Streamer(x * 5) for x in chars]
    mux = pescador.Mux(streamers,
                       k=len(chars),
                       rate=None,
                       with_replacement=False,
                       revive=True,
                       prune_empty_streams=False,
                       random_state=135)
    samples = mux.iterate(max_iter=1000)
    print(collections.Counter(samples))
示例#32
0
    def test_weighted_empty_streams(self, mux_class):
        def __empty():
            if False:
                yield 1

        reference = pescador.Streamer(T.finite_generator, 10)
        empty = pescador.Streamer(__empty)

        mux = mux_class([reference, empty], weights=[1e-10, 1e10])
        estimate = list(mux.iterate(10))

        ref = list(reference)
        assert len(ref) == len(estimate)
        for b1, b2 in zip(ref, estimate):
            T._eq_batch(b1, b2)
示例#33
0
    def test_mux_k_greater_n(self, mux_class, n_samples, rate, random_state):
        """Test that replacement works correctly. See #112:
        https://github.com/pescadores/pescador/issues/112

        When streamers are activated, they should make copies of their
        underlying streamers, and this should work. Before the bug
        was fixed, this would fail. Note; this doesn't test underlying
        state at all, however.
        """
        a = pescador.Streamer('a')
        b = pescador.Streamer('b')

        mux = mux_class([a, b], 6, rate, random_state=random_state)
        result = list(mux.iterate(n_samples))
        assert len(result) == n_samples
示例#34
0
    def test_mux_k_greater_n(self, mux_class, n_samples, rate, random_state):
        """Test that replacement works correctly. See #112:
        https://github.com/pescadores/pescador/issues/112

        When streamers are activated, they should make copies of their
        underlying streamers, and this should work. Before the bug
        was fixed, this would fail. Note; this doesn't test underlying
        state at all, however.
        """
        a = pescador.Streamer('a')
        b = pescador.Streamer('b')

        mux = mux_class([a, b], 6, rate, random_state=random_state)
        result = list(mux.iterate(n_samples))
        assert len(result) == n_samples
示例#35
0
def test_mux_single_infinite(mux_class):
    """Test a single finite streamer for each mux class which can revive it's
    streamers.
    """
    reference = list(T.finite_generator(50))
    stream = pescador.Streamer(reference)

    mux = mux_class([stream])
    estimate = list(mux.iterate(max_iter=100))

    assert len(estimate) == 2 * len(reference)
    reference = (reference + reference)
    for i in range(len(reference)):
        assert set(reference[i].keys()) == set(estimate[i].keys())
        for key in reference[i].keys():
            assert np.all(reference[i][key] == estimate[i][key])
示例#36
0
def test_mux_revive(n_streams, n_samples, k, rate):
    streamers = [
        pescador.Streamer(T.finite_generator, 10) for _ in range(n_streams)
    ]

    mux = pescador.mux.Mux(streamers,
                           k,
                           rate=rate,
                           with_replacement=False,
                           revive=True)

    estimate = list(mux.iterate(n_samples))

    # Make sure we get the right number of samples
    # This is highly improbable when revive=False
    assert len(estimate) == n_samples
示例#37
0
def test_mux_single_infinite(mux_class):
    """Test a single finite streamer for each mux class which can revive it's
    streamers.
    """
    reference = list(T.finite_generator(50))
    stream = pescador.Streamer(reference)

    mux = mux_class([stream])
    estimate = list(mux.iterate(max_iter=100))

    assert len(estimate) == 2 * len(reference)
    reference = (reference + reference)
    for i in range(len(reference)):
        assert set(reference[i].keys()) == set(estimate[i].keys())
        for key in reference[i].keys():
            assert np.all(reference[i][key] == estimate[i][key])
示例#38
0
    def test_weighted_empty_streams(self, mux_class):

        def __empty():
            if False:
                yield 1

        reference = pescador.Streamer(T.finite_generator, 10)
        empty = pescador.Streamer(__empty)

        mux = mux_class([reference, empty],
                        weights=[1e-10, 1e10])
        estimate = list(mux.iterate(10))

        ref = list(reference)
        assert len(ref) == len(estimate)
        for b1, b2 in zip(ref, estimate):
            T._eq_batch(b1, b2)
示例#39
0
    def test_shuffled_mux_simple(self):
        "Test that `ShuffledMux` samples from all provided streams"
        to_generate = ['a', 'b', 'c', 'd', 'e']
        streams = [pescador.Streamer(_cycle, x) for x in to_generate]
        mux = pescador.ShuffledMux(streams, random_state=10)

        samples = list(mux.iterate(max_iter=1000))
        counter = collections.Counter(samples)

        # Test that there is [a, b, c] in the set
        assert set(counter.keys()) == set(to_generate)

        # Test that the statistics line up with expected.
        for i, key in enumerate(to_generate):
            np.testing.assert_approx_equal(counter[key] / len(samples),
                                           mux.weights[i],
                                           significant=1)
示例#40
0
    def test_shuffled_mux_simple(self):
        "Test that `ShuffledMux` samples from all provided streams"
        to_generate = ['a', 'b', 'c', 'd', 'e']
        streams = [pescador.Streamer(_cycle, x) for x in to_generate]
        mux = pescador.ShuffledMux(streams, random_state=10)

        samples = list(mux.iterate(max_iter=1000))
        counter = collections.Counter(samples)

        # Test that there is [a, b, c] in the set
        assert set(counter.keys()) == set(to_generate)

        # Test that the statistics line up with expected.
        for i, key in enumerate(to_generate):
            np.testing.assert_approx_equal(counter[key] / len(samples),
                                           mux.weights[i],
                                           significant=1)
示例#41
0
def test_empty_streams():
    def __empty():
        if False:
            yield 1

    reference = pescador.Streamer(T.finite_generator, 10)
    empty = pescador.Streamer(__empty)

    mux = pescador.mux.Mux([reference, empty],
                           2,
                           rate=None,
                           with_replacement=False,
                           weights=[1e-10, 1e10])
    estimate = list(mux.iterate(10))

    ref = list(reference)
    assert len(ref) == len(estimate)
    for b1, b2 in zip(ref, estimate):
        T.__eq_batch(b1, b2)
示例#42
0
    def test_chain_streamer_of_streams(self):
        """If you want to pass parameters to your generator function,
        you have to do it with a streamer.
        """
        def stream_gen(n, source_letters):
            """
            Parameters
            ----------
            n : how many to stream.

            source_letters : list of things to stream.
            """
            for char in source_letters:
                yield pescador.Streamer(char * n)

        streamers = pescador.Streamer(stream_gen, 10, "abcde")
        mux = pescador.mux.ChainMux(streamers, mode="exhaustive")
        result = "".join(list(mux.iterate()))
        assert len(result) == 50
        assert result == "{}{}{}{}{}".format(
            'a' * 10, 'b' * 10, 'c' * 10, 'd' * 10, 'e' * 10)
示例#43
0
    def test_critical_mux(self, mux_class):
        """This test checks the following:

        When `max_iter` is specified, the `Mux` should return / complete
        when the input generators are complete, and should not cycle
        infinitely.

        # Check on Issue #80
        https://github.com/pescadores/pescador/issues/80
        """
        chars = 'abcde'
        n_reps = 7
        streamers = [pescador.Streamer(x * n_reps) for x in chars]
        mux = mux_class(streamers, len(chars), rate=None,
                        prune_empty_streams=False, random_state=135)
        samples = list(mux.iterate(max_iter=1000))
        sample_counts = collections.Counter(samples)
        assert len(sample_counts) == len(chars)
        for k, v in sample_counts.items():
            assert v == n_reps
        assert len(samples) == len(chars) * n_reps
示例#44
0
    def test_chain_streamer_of_streams(self):
        """If you want to pass parameters to your generator function,
        you have to do it with a streamer.
        """
        def stream_gen(n, source_letters):
            """
            Parameters
            ----------
            n : how many to stream.

            source_letters : list of things to stream.
            """
            for char in source_letters:
                yield pescador.Streamer(char * n)

        streamers = pescador.Streamer(stream_gen, 10, "abcde")
        mux = pescador.mux.ChainMux(streamers, mode="exhaustive")
        result = "".join(list(mux.iterate()))
        assert len(result) == 50
        assert result == "{}{}{}{}{}".format(
            'a' * 10, 'b' * 10, 'c' * 10, 'd' * 10, 'e' * 10)
示例#45
0
    def test_shuffled_mux_weights(self):
        "When sampling with weights, do the statistics line up?"
        a = pescador.Streamer(_cycle, 'a')
        b = pescador.Streamer(_cycle, 'b') 
        c = pescador.Streamer(_cycle, 'c')

        weights = [.6, .3, .1]
        mux = pescador.ShuffledMux([a, b, c], weights=weights, random_state=10)

        samples = list(mux.iterate(max_iter=1000))
        counter = collections.Counter(samples)

        # Test that there is [a, b, c] in the set
        assert set(counter.keys()) == set(['a', 'b', 'c'])

        # Test the statistics on the counts.
        # Does the sampling approximately match the weights?
        for i, key in enumerate(['a', 'b', 'c']):
            np.testing.assert_approx_equal(counter[key] / len(samples),
                                           weights[i],
                                           significant=1)
示例#46
0
    def test_shuffled_mux_weights(self):
        "When sampling with weights, do the statistics line up?"
        a = pescador.Streamer(_cycle, 'a')
        b = pescador.Streamer(_cycle, 'b') 
        c = pescador.Streamer(_cycle, 'c')

        weights = [.6, .3, .1]
        mux = pescador.ShuffledMux([a, b, c], weights=weights, random_state=10)

        samples = list(mux.iterate(max_iter=1000))
        counter = collections.Counter(samples)

        # Test that there is [a, b, c] in the set
        assert set(counter.keys()) == {'a', 'b', 'c'}

        # Test the statistics on the counts.
        # Does the sampling approximately match the weights?
        for i, key in enumerate(['a', 'b', 'c']):
            np.testing.assert_approx_equal(counter[key] / len(samples),
                                           weights[i],
                                           significant=1)
示例#47
0
    def test_critical_mux(self, mux_class):
        """This test checks the following:

        When `max_iter` is specified, the `Mux` should return / complete
        when the input generators are complete, and should not cycle
        infinitely.

        # Check on Issue #80
        https://github.com/pescadores/pescador/issues/80
        """
        chars = 'abcde'
        n_reps = 7
        streamers = [pescador.Streamer(x * n_reps) for x in chars]
        mux = mux_class(streamers, len(chars), rate=None,
                        prune_empty_streams=False, random_state=135)
        samples = list(mux.iterate(max_iter=1000))
        sample_counts = collections.Counter(samples)
        assert len(sample_counts) == len(chars)
        for k, v in sample_counts.items():
            assert v == n_reps
        assert len(samples) == len(chars) * n_reps
示例#48
0
 def test_roundrobin_mux_simple(self):
     ab = pescador.Streamer('ab')
     cde = pescador.Streamer('cde')
     fghi = pescador.Streamer('fghi')
     mux = pescador.mux.RoundRobinMux([ab, cde, fghi], 'exhaustive')
     assert "".join(list(mux.iterate())) == "acfbdgehi"
示例#49
0
 def test_chain_mux_cycle(self):
     a = pescador.Streamer("abc")
     b = pescador.Streamer("def")
     mux = pescador.mux.ChainMux([a, b],
                                 mode="cycle")
     assert "".join(list(mux.iterate(max_iter=12))) == "abcdefabcdef"
示例#50
0
 def test_rr_basic_cycle(self):
     a = pescador.Streamer('a')
     b = pescador.Streamer('bb')
     mux = pescador.mux.RoundRobinMux([a, b], 'cycle')
     assert "".join(list(mux.iterate(7))) == "abbabba"
示例#51
0
 def test_chain_mux_cycle(self):
     a = pescador.Streamer("abc")
     b = pescador.Streamer("def")
     mux = pescador.mux.ChainMux([a, b],
                                 mode="cycle")
     assert "".join(list(mux.iterate(max_iter=12))) == "abcdefabcdef"
示例#52
0
 def test_rr_basic_cycle(self):
     a = pescador.Streamer('a')
     b = pescador.Streamer('bb')
     mux = pescador.mux.RoundRobinMux([a, b], 'cycle')
     assert "".join(list(mux.iterate(7))) == "abbabba"
示例#53
0
 def test_roundrobin_mux_simple(self):
     ab = pescador.Streamer('ab')
     cde = pescador.Streamer('cde')
     fghi = pescador.Streamer('fghi')
     mux = pescador.mux.RoundRobinMux([ab, cde, fghi], 'exhaustive')
     assert "".join(list(mux.iterate())) == "acfbdgehi"