def _get_weighted_choices(rng, sub_rate, indel_rate): choices_by_nt = {} for src_nt in "ACGT": choices = "ACGTID" probs = [sub_rate / 4] * 4 # ACGT probs += [indel_rate / 2] * 2 # ID probs[choices.index(src_nt)] = 1 - sum(probs) + sub_rate / 4 choices_by_nt[src_nt] = weighted_sampling(choices, probs, rng) return choices_by_nt
def test_weighted_sampling__non_numerical_weight_raises_type_error(): choices = range(3) weights = [1, "foo", 3] iterator = sampling.weighted_sampling(choices, weights) assert_raises(TypeError, iterator.next)
def test_weighted_sampling__zero_weight_raises_value_error(): choices = range(3) weights = [1, 0, 3] iterator = sampling.weighted_sampling(choices, weights) assert_raises(ValueError, iterator.next)
def _do_different_length_input_raises(choices, weights): iterator = sampling.weighted_sampling(choices, weights) assert_raises(ValueError, iterator.next)
def _do_empty_input_raises(choices, weights): iterator = sampling.weighted_sampling(choices, weights) assert_raises(ValueError, iterator.next)
def _do_select_by_weight(value, expectation): choices = "abc" weights = (1, 2, 3) rng = flexmock(random=lambda: value) iterator = sampling.weighted_sampling(choices, weights, rng) assert_equal(iterator.next(), expectation)
def _do_select_by_weight(value, expectation): choices = "abc" weights = (1, 2, 3) rng = flexmock(random = lambda: value) iterator = sampling.weighted_sampling(choices, weights, rng) assert_equal(iterator.next(), expectation)