Пример #1
0
def test_home_education_home_removal_of_education_act(person_home_education_home):
    household = instantiate_household_with([person_home_education_home])
    assert_correct_activities(person=household.people['1'], ordered_activities_list=['home', 'education', 'home'])

    policy = modify.RemoveActivity(activities=['education'], probability=1)
    policy.apply_to(household)
    assert_correct_activities(person=household.people['1'], ordered_activities_list=['home'])
Пример #2
0
def test_evaluate_activity_policy_selects_steve_for_individual_activity_removal(mocker, Smith_Household):
    mocker.patch.object(random, 'random', side_effect=[0] + [1] * 11)
    household = Smith_Household
    steve = household.people['1']
    hilda = household.people['2']
    timmy = household.people['3']
    bobby = household.people['4']

    assert_correct_activities(person=steve, ordered_activities_list=['home', 'work', 'leisure', 'work', 'home'])
    assert_correct_activities(person=hilda, ordered_activities_list=['home', 'escort', 'shop', 'leisure', 'escort', 'home'])
    assert_correct_activities(person=timmy, ordered_activities_list=['home', 'education', 'shop', 'education', 'leisure', 'home'])
    assert_correct_activities(person=bobby, ordered_activities_list=['home', 'education', 'home'])

    # i.e. First of Steve's work activities is affected and only that activity is affected
    policy = modify.RemoveActivity(
        ['education', 'escort', 'leisure', 'shop', 'work'],
        policy_type='activity',
        probability_level='activity',
        probability=0.5)
    policy.apply_to(household)

    assert_correct_activities(person=steve, ordered_activities_list=['home', 'leisure', 'work', 'home'])
    assert_correct_activities(person=hilda, ordered_activities_list=['home', 'escort', 'shop', 'leisure', 'escort', 'home'])
    assert_correct_activities(person=timmy, ordered_activities_list=['home', 'education', 'shop', 'education', 'leisure', 'home'])
    assert_correct_activities(person=bobby, ordered_activities_list=['home', 'education', 'home'])
Пример #3
0
def test_attribute_based_remove_activity_policy_does_not_remove_matching_activities_from_non_strictly_irrelevant_people(
        home_education_home_university_student):
    household = instantiate_household_with(
        [home_education_home_university_student])

    def age_condition_under_0(attribute_value):
        return attribute_value < 0

    def job_condition_wasevrrr(attribute_value):
        return attribute_value == 'wasevrrr'

    assert_correct_activities(
        person=household.people['1'],
        ordered_activities_list=['home', 'education', 'home'])
    assert not age_condition_under_0(household.people['1'].attributes['age'])
    assert not job_condition_wasevrrr(household.people['1'].attributes['job'])

    policy_remove_higher_education = modify.ActivityPolicy(
        modify.RemoveActivity(['education']),
        modify.ActivityProbability(['education'], 1),
        modify.PersonAttributeFilter(conditions={
            'age': age_condition_under_0,
            'job': job_condition_wasevrrr
        },
                                     how='any'))

    policy_remove_higher_education.apply_to(household)

    assert_correct_activities(
        person=household.people['1'],
        ordered_activities_list=['home', 'education', 'home'])
Пример #4
0
def test_ActivityPolicy_verifies_for_appropriate_probabilities(mocker):
    mocker.patch.object(modify, 'verify_probability')
    modify.ActivityPolicy(modify.RemoveActivity(['']), 0.5)

    modify.verify_probability.assert_called_once_with(
        0.5,
        (float, list, modify.SimpleProbability, modify.ActivityProbability)
    )
Пример #5
0
def test_remove_household_activities_delegates_to_remove_person_activities_for_persons_in_household(
        mocker, SmithHousehold):
    mocker.patch.object(modify.RemoveActivity, 'remove_person_activities')

    policy = modify.RemoveActivity([''])
    policy.remove_household_activities(SmithHousehold)

    assert modify.RemoveActivity.remove_person_activities.call_count == 4
Пример #6
0
def test_remove_person_activities_delegates_to_remove_activities_for_Bobbys_activities(
        mocker, Bobby):
    mocker.patch.object(modify.RemoveActivity, 'remove_activities')

    policy = modify.RemoveActivity([''])
    policy.remove_person_activities(Bobby)

    modify.RemoveActivity.remove_activities.assert_called_once()
Пример #7
0
def test_RemoveActivity_apply_to_delegates_policy_type_activity_to_evaluate_activity_policy(mocker):
    mocker.patch.object(modify.RemoveActivity, 'evaluate_activity_policy')
    policy = modify.RemoveActivity(
        ['work'],
        policy_type='activity',
        probability=0.5)

    policy.apply_to(Household(1))
    modify.RemoveActivity.evaluate_activity_policy.assert_called_once()
Пример #8
0
def test_RemoveActivity_apply_to_delegates_to_remove_individual_activities_when_given_person_and_activities(
        mocker, SmithHousehold):
    mocker.patch.object(modify.RemoveActivity, 'remove_individual_activities')

    policy = modify.RemoveActivity([''])
    policy.apply_to(SmithHousehold, SmithHousehold['4'], [Activity])

    modify.RemoveActivity.remove_individual_activities.assert_called_once_with(
        SmithHousehold['4'], [Activity])
Пример #9
0
def test_remove_activities_removes_Bobbys_education(Bobby):
    policy = modify.RemoveActivity(['education'])

    def fnc(act):
        return True

    policy.remove_activities(Bobby, fnc)

    assert_correct_activities(Bobby, ['home'])
Пример #10
0
def test_RemoveActivity_apply_to_delegates_to_remove_household_activities_when_given_household(
        mocker, SmithHousehold):
    mocker.patch.object(modify.RemoveActivity, 'remove_household_activities')

    policy = modify.RemoveActivity([''])
    policy.apply_to(SmithHousehold)

    modify.RemoveActivity.remove_household_activities.assert_called_once_with(
        SmithHousehold)
Пример #11
0
def test_HouseholdPolicy_apply_to_delegates_to_modifier_policy_apply_to_for_single_probability(mocker, SmithHousehold):
    mocker.patch.object(modify.RemoveActivity, 'apply_to')
    mocker.patch.object(modify.SimpleProbability, 'sample', return_value=True)

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

    policy.apply_to(household)

    modify.RemoveActivity.apply_to.assert_called_once_with(household)
Пример #12
0
def test_PersonPolicy_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.PersonPolicy(modify.RemoveActivity(['']), [1., modify.SimpleProbability(1.)])
    household = SmithHousehold

    policy.apply_to(household)

    assert modify.RemoveActivity.apply_to.call_count == 4
Пример #13
0
def test_remove_activity_policy_only_removes_individual_activities(mocker, home_education_shop_education_home):
    mocker.patch.object(modify.RemoveActivity, 'is_selected', side_effect=[False, True])

    person = home_education_shop_education_home
    assert_correct_activities(person=person, ordered_activities_list=['home', 'education', 'shop', 'education', 'home'])

    policy_remove_education = modify.RemoveActivity(['education'], probability=1)
    policy_remove_education.remove_individual_activities(person)

    assert_correct_activities(person=person, ordered_activities_list=['home', 'education', 'shop', 'home'])
Пример #14
0
def test_household_policy_with_activity_based_probability(Smith_Household, mocker):
    mocker.patch.object(modify.RemoveActivity, 'remove_household_activities')
    mocker.patch.object(random, 'random', side_effect=[1] * 11 + [0])
    household = Smith_Household
    # i.e. Bobby's education activity is affected and affects activities on household level
    policy = modify.RemoveActivity(
        ['education', 'escort', 'leisure', 'shop', 'work'],
        policy_type='household',
        probability_level='activity',
        probability=0.5)
    policy.apply_to(household)

    modify.RemoveActivity.remove_household_activities.assert_called_once_with(household)
Пример #15
0
def test_person_policy_with_activity_based_probability_with_a_satisfied_person_attribute(
        SmithHousehold, mocker):
    mocker.patch.object(modify.RemoveActivity, 'remove_person_activities')
    mocker.patch.object(random, 'random', side_effect=[0] + [1] * 11)
    household = SmithHousehold

    # i.e. First of Steve's work activities is affected and affects all listed activities for just Steve
    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_20 = [i for i in range(21)]
    above_20 = [i for i in range(21, 101)]
    age_distribution = {
        **dict(zip(below_20, [0] * len(below_20))),
        **dict(zip(above_20, [1] * len(above_20)))
    }

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

    policy = modify.PersonPolicy(
        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)
    steve = household.people['1']

    modify.RemoveActivity.remove_person_activities.assert_called_once_with(
        steve)
Пример #16
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)
Пример #17
0
def test_person_policy_with_activity_based_probability(Smith_Household, mocker):
    mocker.patch.object(modify.RemoveActivity, 'remove_person_activities')
    mocker.patch.object(random, 'random', side_effect=[0] + [1] * 11)
    household = Smith_Household
    # i.e. First of Steve's work activities is affected and affects all listed activities for just Steve
    policy = modify.RemoveActivity(
        ['education', 'escort', 'leisure', 'shop', 'work'],
        policy_type='person',
        probability_level='activity',
        probability=0.5)
    policy.apply_to(household)
    steve = household.people['1']

    modify.RemoveActivity.remove_person_activities.assert_called_once_with(steve)
Пример #18
0
def test_person_policy_with_person_based_probability(mocker, Smith_Household):
    mocker.patch.object(modify.RemoveActivity, 'remove_person_activities')
    mocker.patch.object(random, 'random', side_effect=[1, 1, 1, 0])
    household = Smith_Household
    # i.e. Bobby is affected and his activities are the only one affected in household
    policy = modify.RemoveActivity(
        ['education', 'escort', 'leisure', 'shop', 'work'],
        policy_type='person',
        probability_level='person',
        probability=0.5)
    bobby = household.people['4']
    policy.apply_to(household)

    modify.RemoveActivity.remove_person_activities.assert_called_once_with(bobby)
Пример #19
0
def test_person_policy_with_person_based_probability_with_a_satisfied_person_attribute(
        mocker, SmithHousehold):
    mocker.patch.object(modify.RemoveActivity, 'remove_person_activities')
    mocker.patch.object(random, 'random', side_effect=[1, 1, 1, 0])
    household = SmithHousehold

    # i.e. Bobby is affected and his activities are the only one affected in household
    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.PersonPolicy(
        modify.RemoveActivity(
            ['education', 'escort', 'leisure', 'shop', 'work']),
        modify.PersonProbability(discrete_sampler, {
            'mapping': age_mapping,
            'distribution': age_distribution
        }))
    bobby = household.people['4']
    policy.apply_to(household)

    modify.RemoveActivity.remove_person_activities.assert_called_once_with(
        bobby)
Пример #20
0
def test_household_policy_with_activity_based_probability_with_a_satisfied_person_attribute(Smith_Household, mocker):
    mocker.patch.object(modify.RemoveActivity, 'remove_household_activities')
    mocker.patch.object(random, 'random', side_effect=[0])
    household = Smith_Household
    # i.e. Bobby's education activity is affected and affects activities on household level
    def age_condition_under_10(attribute_value):
        return attribute_value < 10
    policy = modify.RemoveActivity(
        ['education', 'escort', 'leisure', 'shop', 'work'],
        policy_type='household',
        probability_level='activity',
        probability=0.5,
        attribute_conditions={'age': age_condition_under_10},
        attribute_strict_conditions=True)
    policy.apply_to(household)

    modify.RemoveActivity.remove_household_activities.assert_called_once_with(household)
Пример #21
0
def test_remove_activity_policy_only_removes_individual_activities(
        mocker, home_education_shop_education_home):
    person = home_education_shop_education_home
    assert_correct_activities(person=person,
                              ordered_activities_list=[
                                  'home', 'education', 'shop', 'education',
                                  'home'
                              ])
    act_to_remove = list(person.activities)[3]

    policy_remove_education = modify.RemoveActivity(['education'])
    policy_remove_education.remove_individual_activities(
        person, [act_to_remove])

    assert_correct_activities(
        person=person,
        ordered_activities_list=['home', 'education', 'shop', 'home'])
Пример #22
0
def test_person_policy_with_activity_based_probability_with_a_satisfied_person_attribute(Smith_Household, mocker):
    mocker.patch.object(modify.RemoveActivity, 'remove_person_activities')
    mocker.patch.object(random, 'random', side_effect=[0] + [1] * 11)
    household = Smith_Household
    # i.e. First of Steve's work activities is affected and affects all listed activities for just Steve
    def age_condition_over_20(attribute_value):
        return attribute_value > 20
    policy = modify.RemoveActivity(
        ['education', 'escort', 'leisure', 'shop', 'work'],
        policy_type='person',
        probability_level='activity',
        probability=0.5,
        attribute_conditions={'age': age_condition_over_20},
        attribute_strict_conditions=True)
    policy.apply_to(household)
    steve = household.people['1']

    modify.RemoveActivity.remove_person_activities.assert_called_once_with(steve)
Пример #23
0
def test_person_policy_with_person_based_probability_with_a_satisfied_person_attribute(mocker, Smith_Household):
    mocker.patch.object(modify.RemoveActivity, 'remove_person_activities')
    mocker.patch.object(random, 'random', side_effect=[1, 1, 1, 0])
    household = Smith_Household
    # i.e. Bobby is affected and his activities are the only one affected in household
    def age_condition_under_10(attribute_value):
        return attribute_value < 10
    policy = modify.RemoveActivity(
        ['education', 'escort', 'leisure', 'shop', 'work'],
        policy_type='person',
        probability_level='person',
        probability=0.5,
        attribute_conditions={'age': age_condition_under_10},
        attribute_strict_conditions=True)
    bobby = household.people['4']
    policy.apply_to(household)

    modify.RemoveActivity.remove_person_activities.assert_called_once_with(bobby)
Пример #24
0
def test_attribute_based_remove_activity_policy_removes_all_matching_activities_from_non_strictly_relevant_people(
        home_education_home_university_student):
    household = instantiate_household_with([home_education_home_university_student])

    def age_condition_over_17(attribute_value):
        return attribute_value > 17

    def job_condition_wasevrrr(attribute_value):
        return attribute_value == 'wasevrrr'

    assert_correct_activities(person=household.people['1'], ordered_activities_list=['home', 'education', 'home'])
    assert age_condition_over_17(household.people['1'].attributes['age'])
    assert not job_condition_wasevrrr(household.people['1'].attributes['job'])

    policy_remove_higher_education = modify.RemoveActivity(
        ['education'],
        probability=1,
        attribute_conditions={'age': age_condition_over_17, 'job': job_condition_wasevrrr},
        attribute_strict_conditions=False
    )

    policy_remove_higher_education.apply_to(household)

    assert_correct_activities(person=household.people['1'], ordered_activities_list=['home'])
Пример #25
0
def test_activity_is_not_selected_when_condition_is_not_satisfied(mocker):
    mocker.patch.object(random, 'random', return_value=0.75)
    policy_remove_activity = modify.RemoveActivity(['some_activity'], probability=0.5)

    assert not policy_remove_activity.is_selected()
Пример #26
0
def test_RemoveActivity_throws_exception_if_apply_to_given_wrong_input(Bobby):
    policy = modify.RemoveActivity([''])
    with pytest.raises(NotImplementedError) as e:
        policy.apply_to(Bobby)
    assert 'Types passed incorrectly: <class \'pam.core.Person\'>, <class \'NoneType\'>, <class \'NoneType\'>. You need <class \'type\'> at the very least.' \
           in str(e.value)
Пример #27
0
def test_is_activity_for_removal_activity_matches_RemoveActivity_activities():
    activity = Activity(act='some_activity')
    policy_remove_activity = modify.RemoveActivity(['some_activity'])

    assert policy_remove_activity.is_activity_for_removal(activity)
Пример #28
0
def test_home_work_home_education_home_removal_of_education_act():
    person = Person(1)
    person.add(
        Activity(
            seq=1,
            act='home',
            area='a',
            start_time=mtdt(0),
            end_time=mtdt(60)
        )
    )
    person.add(
        Leg(
            seq=1,
            mode='car',
            start_area='a',
            end_area='b',
            start_time=mtdt(60),
            end_time=mtdt(90)
        )
    )
    person.add(
        Activity(
            seq=2,
            act='work',
            area='b',
            start_time=mtdt(90),
            end_time=mtdt(120)
        )
    )
    person.add(
        Leg(
            seq=2,
            mode='car',
            start_area='b',
            end_area='a',
            start_time=mtdt(120),
            end_time=mtdt(180)
        )
    )
    person.add(
        Activity(
            seq=3,
            act='home',
            area='a',
            start_time=mtdt(180),
            end_time=mtdt(300)
        )
    )
    person.add(
        Leg(
            seq=3,
            mode='car',
            start_area='a',
            end_area='b',
            start_time=mtdt(300),
            end_time=mtdt(390)
        )
    )
    person.add(
        Activity(
            seq=2,
            act='education',
            area='b',
            start_time=mtdt(390),
            end_time=mtdt(520)
        )
    )
    person.add(
        Leg(
            seq=2,
            mode='car',
            start_area='b',
            end_area='a',
            start_time=mtdt(520),
            end_time=mtdt(580)
        )
    )
    person.add(
        Activity(
            seq=3,
            act='home',
            area='a',
            start_time=mtdt(680),
            end_time=END_OF_DAY
        )
    )
    household = instantiate_household_with([person])
    assert_correct_activities(person=household.people['1'], ordered_activities_list=['home', 'work', 'home', 'education', 'home'])

    policy = modify.RemoveActivity(activities=['education'], probability=1)
    policy.apply_to(household)
    assert_correct_activities(person=household.people['1'], ordered_activities_list=['home', 'work', 'home'])
Пример #29
0
def test_is_activity_for_removal_activity_does_not_match_RemoveActivity_activities():
    activity = Activity(act = 'other_activity')
    policy_remove_activity = modify.RemoveActivity(['some_activity'], probability=0.5)

    assert not policy_remove_activity.is_activity_for_removal(activity)
Пример #30
0
def test_activity_is_selected_if_probability_1(mocker):
    mocker.patch.object(random, 'random', side_effect=[0]+[i/10 for i in range(1,10)])

    policy_remove_activity = modify.RemoveActivity(['some_activity'], probability=1)
    for i in range(10):
        assert policy_remove_activity.is_selected()