Пример #1
0
def test_OptionValueField_raises_error_when_sum_of_probabilities_is_not_one():
    probs = {"A": 0.8, "B": 0.02}

    with pytest.raises(ValueError):
        field.OptionValueField(probabilities=probs)

    probs = {"A": 0.8, "B": 2}

    with pytest.raises(ValueError):
        field.OptionValueField(probabilities=probs)
Пример #2
0
def test_OptionValueField_chooses_value_basedon_probability_from_distribution(
        monkeypatch):
    n = mock.Mock()
    mock_choices = {"M": 0.2, "F": 0.8}
    n.return_value = "M"
    monkeypatch.setattr("random.choice", n)

    dob = field.OptionValueField(probabilities=mock_choices)

    assert dob.next_value(row) == "M"
    n.assert_called_with(dob._option_picks)
Пример #3
0
def test_OptionValueField_handles_small_probability_when_choosing_options():
    probs = {"A": 0.8, "B": 0.02, "C": 0.0001, "D": 0.1799}

    ovf = field.OptionValueField(probabilities=probs)
    HeadFake.set_seed(1234)

    assert len(ovf._option_picks) == 10000
    assert len(list(filter(lambda x: x == "A", ovf._option_picks))) == 8000
    assert len(list(filter(lambda x: x == "B", ovf._option_picks))) == 200
    assert len(list(filter(lambda x: x == "C", ovf._option_picks))) == 1
    assert len(list(filter(lambda x: x == "D", ovf._option_picks))) == 1799
    assert ["A", "A", "A", "A",
            "D"] == [ovf.next_value(row) for i in range(1, 6)]
Пример #4
0
def test_IfElseField_handles_condition_as_dictionary():
    fset = Fieldset(fields={"marital_status": "M"})
    HeadFake.set_seed(5)
    gender_cond = {"field": "gender", "operator": operator.eq, "value": "M"}

    ms_test_cond = {
        "field": "marital_status",
        "operator": operator.eq,
        "value": "M"
    }

    female_ov = field.OptionValueField(probabilities={
        "MISS": 0.7,
        "MS": 0.1,
        "DR": 0.1,
        "PROF": 0.1
    })

    ms_if_else = field.IfElseField(condition=ms_test_cond,
                                   true_value="MRS",
                                   false_value=female_ov)

    gender_if_else = field.IfElseField(condition=gender_cond,
                                       true_value="MR",
                                       false_value=ms_if_else)

    gender_if_else.init_from_fieldset(fset)

    assert gender_if_else.next_value({
        "gender": "M",
        "marital_status": "M"
    }) == "MR"
    assert gender_if_else.next_value({
        "gender": "M",
        "marital_status": "S"
    }) == "MR"
    assert gender_if_else.next_value({
        "gender": "F",
        "marital_status": "S"
    }) == "PROF"
    assert gender_if_else.next_value({
        "gender": "F",
        "marital_status": "S"
    }) == "MISS"
    assert gender_if_else.next_value({
        "gender": "F",
        "marital_status": "S"
    }) == "MISS"
Пример #5
0
def test_OptionValueField_handles_very_small_probability_with_warning_when_choosing_options(
        monkeypatch):
    from unittest.mock import Mock
    warn = Mock()
    monkeypatch.setattr("warnings.warn", warn)
    probs = {"A": 0.8, "B": 0.02, "C": 0.000001, "D": 0.179999}

    ovf = field.OptionValueField(probabilities=probs)
    HeadFake.set_seed(1234)

    assert len(ovf._option_picks) == 1000000
    assert len(list(filter(lambda x: x == "A", ovf._option_picks))) == 800000
    assert len(list(filter(lambda x: x == "B", ovf._option_picks))) == 20000
    assert len(list(filter(lambda x: x == "C", ovf._option_picks))) == 1
    assert len(list(filter(lambda x: x == "D", ovf._option_picks))) == 179999
    assert ["B", "A", "A", "A",
            "A"] == [ovf.next_value(row) for i in range(1, 6)]
    warn.assert_called_with(
        "Options include probabilities of 1e-6. This requires the creation of 1e6 possible options"
    )