Esempio n. 1
0
def test_HouseholdPolicy_verifies_for_appropriate_probabilities(mocker):
    mocker.patch.object(modify, 'verify_probability')
    modify.HouseholdPolicy(modify.RemoveActivity(['']), 0.5)

    modify.verify_probability.assert_called_once_with(
        0.5,
        (float, list, modify.SimpleProbability, modify.ActivityProbability, modify.PersonProbability,
         modify.HouseholdProbability)
    )
Esempio n. 2
0
def test_HouseholdPolicy_apply_to_delegates_to_modifier_policy_apply_to_for_list_of_probabilities(mocker, SmithHousehold):
    mocker.patch.object(modify.RemoveActivity, 'apply_to')
    mocker.patch.object(modify.SimpleProbability, 'p', return_value=1)

    policy = modify.HouseholdPolicy(modify.RemoveActivity(['']), [1., modify.SimpleProbability(1.)])
    household = SmithHousehold

    policy.apply_to(household)

    modify.RemoveActivity.apply_to.assert_called_once_with(household)
Esempio n. 3
0
def test_household_policy_with_activity_based_probability_with_a_satisfied_person_attribute(
        SmithHousehold, mocker):
    mocker.patch.object(modify.RemoveActivity, 'remove_household_activities')
    mocker.patch.object(random, 'random', side_effect=[0])
    household = SmithHousehold

    # i.e. Bobby's education activity is affected and affects activities on household level
    def discrete_sampler(obj, mapping, distribution):
        p = distribution
        for key in mapping:
            value = obj.attributes.get(key)
            if value is None:
                raise KeyError(
                    f"Cannot find mapping: {key} in sampling features: {obj.attributes}"
                )
            p = p.get(value)
            if p is None:
                raise KeyError(
                    f"Cannot find feature for {key}: {value} in distribution: {p}"
                )
        return p

    age_mapping = ['age']
    below_10 = [i for i in range(11)]
    above_10 = [i for i in range(11, 101)]
    age_distribution = {
        **dict(zip(below_10, [1] * len(below_10))),
        **dict(zip(above_10, [0] * len(above_10)))
    }

    people_satisfying_age_condition_under_10 = 0
    for pid, person in household.people.items():
        people_satisfying_age_condition_under_10 += discrete_sampler(
            person, age_mapping, age_distribution)
    assert people_satisfying_age_condition_under_10 == 1

    policy = modify.HouseholdPolicy(
        modify.RemoveActivity(
            ['education', 'escort', 'leisure', 'shop', 'work']), [
                modify.ActivityProbability(
                    ['education', 'escort', 'leisure', 'shop', 'work'], 0.5),
                modify.PersonProbability(discrete_sampler, {
                    'mapping': age_mapping,
                    'distribution': age_distribution
                })
            ])
    policy.apply_to(household)

    modify.RemoveActivity.remove_household_activities.assert_called_once_with(
        household)
Esempio n. 4
0
def test_household_policy_with_person_based_probability(
        SmithHousehold, mocker):
    mocker.patch.object(modify.RemoveActivity, 'remove_household_activities')
    mocker.patch.object(random, 'random', side_effect=[0.06249])
    household = SmithHousehold
    # i.e. Bobby is affected and affects activities on household level
    policy = modify.HouseholdPolicy(
        modify.RemoveActivity(
            ['education', 'escort', 'leisure', 'shop', 'work']),
        modify.PersonProbability(0.5))
    policy.apply_to(household)

    modify.RemoveActivity.remove_household_activities.assert_called_once_with(
        household)