Exemplo n.º 1
0
def test_writes_od_matrix_to_expected_file(tmpdir):
    population = Population()

    household = Household(hid='0')
    person = Person(pid='0', home_area='Barnet', attributes={'occ': 'white'})
    person.add(Activity(1, 'home', 'Barnet', start_time=mtdt(0)))
    person.add(
        Leg(1,
            mode='car',
            start_area='Barnet',
            end_area='Southwark',
            start_time=mtdt(400),
            purp='work'))
    person.add(Activity(2, 'work', 'Southwark', start_time=mtdt(420)))
    person.add(
        Leg(2,
            'car',
            start_area='Southwark',
            end_area='Barnet',
            start_time=mtdt(1020),
            purp='work'))
    person.add(
        Activity(3,
                 'home',
                 'Barnet',
                 start_time=mtdt(1040),
                 end_time=mtdt(1439)))
    household.add(person)
    population.add(household)

    household = Household(hid='1')
    person = Person(pid='1', home_area='Ealing', attributes={'occ': 'white'})
    person.add(Activity(1, 'home', 'Ealing', start_time=mtdt(0)))
    person.add(
        Leg(1,
            mode='cycle',
            start_area='Ealing',
            end_area='Westminster,City of London',
            start_time=mtdt(500),
            purp='education'))
    person.add(
        Activity(2,
                 'education',
                 'Westminster,City of London',
                 start_time=mtdt(550)))
    person.add(
        Leg(2,
            'cycle',
            start_area='Westminster,City of London',
            end_area='Ealing',
            start_time=mtdt(700),
            purp='education'))
    person.add(
        Activity(3,
                 'home',
                 'Ealing',
                 start_time=mtdt(750),
                 end_time=mtdt(1439)))
    household.add(person)
    population.add(household)

    household = Household(hid='2')
    person = Person(pid='2', home_area='Ealing', attributes={'occ': 'white'})
    person.add(Activity(1, 'home', 'Ealing', start_time=mtdt(0)))
    person.add(
        Leg(1,
            mode='car',
            start_area='Ealing',
            end_area='Westminster,City of London',
            start_time=mtdt(450),
            purp='work'))
    person.add(
        Activity(2, 'work', 'Westminster,City of London',
                 start_time=mtdt(480)))
    person.add(
        Leg(2,
            'car',
            start_area='Westminster,City of London',
            end_area='Ealing',
            start_time=mtdt(1050),
            purp='work'))
    person.add(
        Activity(3,
                 'home',
                 'Ealing',
                 start_time=mtdt(1080),
                 end_time=mtdt(1439)))
    household.add(person)
    population.add(household)

    household = Household(hid='3')
    person = Person(pid='3', home_area='Barnet', attributes={'occ': 'blue'})
    person.add(Activity(1, 'home', 'Barnet', start_time=mtdt(0)))
    person.add(
        Leg(1,
            mode='walk',
            start_area='Barnet',
            end_area='Barnet',
            start_time=mtdt(450),
            purp='shop'))
    person.add(Activity(2, 'shop', 'Barnet', start_time=mtdt(470)))
    person.add(
        Leg(2,
            'walk',
            start_area='Barnet',
            end_area='Barnet',
            start_time=mtdt(600),
            purp='shop'))
    person.add(
        Activity(3,
                 'home',
                 'Barnet',
                 start_time=mtdt(620),
                 end_time=mtdt(1439)))
    household.add(person)
    population.add(household)

    household = Household(hid='4')
    person = Person(pid='4', home_area='Ealing', attributes={'occ': 'blue'})
    person.add(Activity(1, 'home', 'Ealing', start_time=mtdt(0)))
    person.add(
        Leg(1,
            mode='cycle',
            start_area='Ealing',
            end_area='Ealing',
            start_time=mtdt(400),
            purp='work'))
    person.add(Activity(2, 'work', 'Ealing', start_time=mtdt(420)))
    person.add(
        Leg(2,
            'cycle',
            start_area='Ealing',
            end_area='Ealing',
            start_time=mtdt(1030),
            purp='work'))
    person.add(
        Activity(3,
                 'home',
                 'Ealing',
                 start_time=mtdt(1050),
                 end_time=mtdt(1439)))
    household.add(person)
    population.add(household)

    attribute_list = ['white', 'blue', 'total']
    mode_list = ['car', 'cycle', 'walk', 'total']
    time_slice = [(400, 500), (1020, 1060)]

    write_od_matrices(population, tmpdir, leg_filter='Mode')
    for m in mode_list:
        od_matrix_file = os.path.join(tmpdir, m + "_od.csv")
        od_matrix_csv_string = open(od_matrix_file).read()
        if m == 'car':
            expected_od_matrix = \
                    'Origin,Barnet,Ealing,Southwark,"Westminster,City of London"\n' \
                    'Barnet,0,0,1,0\n' \
                    'Ealing,0,0,0,1\n' \
                    'Southwark,1,0,0,0\n' \
                    '"Westminster,City of London",0,1,0,0\n'
            assert od_matrix_csv_string == expected_od_matrix
        if m == 'cycle':
            expected_od_matrix = \
                    'Origin,Ealing,"Westminster,City of London"\n' \
                    'Ealing,2,1\n' \
                    '"Westminster,City of London",1,0\n'
            assert od_matrix_csv_string == expected_od_matrix
        if m == 'walk':
            expected_od_matrix = \
                    'Origin,Barnet\n' \
                    'Barnet,2\n'
            assert od_matrix_csv_string == expected_od_matrix
        if m == 'total':
            expected_od_matrix = \
                    'Origin,Barnet,Ealing,Southwark,"Westminster,City of London"\n' \
                    'Barnet,2,0,1,0\n' \
                    'Ealing,0,2,0,2\n' \
                    'Southwark,1,0,0,0\n' \
                    '"Westminster,City of London",0,2,0,0\n'
            assert od_matrix_csv_string == expected_od_matrix

    write_od_matrices(population, tmpdir, person_filter='occ')
    for a in attribute_list:
        od_matrix_file = os.path.join(tmpdir, a + "_od.csv")
        od_matrix_csv_string = open(od_matrix_file).read()
        if a == 'white':
            expected_od_matrix = \
                    'Origin,Barnet,Ealing,Southwark,"Westminster,City of London"\n' \
                    'Barnet,0,0,1,0\n' \
                    'Ealing,0,0,0,2\n' \
                    'Southwark,1,0,0,0\n' \
                    '"Westminster,City of London",0,2,0,0\n'
            assert od_matrix_csv_string == expected_od_matrix
        if a == 'blue':
            expected_od_matrix = \
                    'Origin,Barnet,Ealing\n' \
                    'Barnet,2,0\n' \
                    'Ealing,0,2\n'
            assert od_matrix_csv_string == expected_od_matrix
        if a == 'total':
            expected_od_matrix = \
                    'Origin,Barnet,Ealing,Southwark,"Westminster,City of London"\n' \
                    'Barnet,2,0,1,0\n' \
                    'Ealing,0,2,0,2\n' \
                    'Southwark,1,0,0,0\n' \
                    '"Westminster,City of London",0,2,0,0\n'
            assert od_matrix_csv_string == expected_od_matrix

    write_od_matrices(population,
                      tmpdir,
                      time_minutes_filter=[(400, 500), (1020, 1060)])
    for start_time, end_time in time_slice:
        file_name = str(start_time) + '_to_' + str(end_time)
        od_matrix_file = os.path.join(tmpdir, 'time_' + file_name + '_od.csv')
        od_matrix_csv_string = open(od_matrix_file).read()
        if (start_time, end_time) == (400, 500):
            expected_od_matrix = \
                    'Origin,Barnet,Ealing,Southwark,"Westminster,City of London"\n' \
                    'Barnet,1,0,1,0\n' \
                    'Ealing,0,1,0,1\n'
            assert od_matrix_csv_string == expected_od_matrix
        if (start_time, end_time) == (1020, 1060):
            expected_od_matrix = \
                    'Origin,Barnet,Ealing\n' \
                    'Ealing,0,1\n' \
                    'Southwark,1,0\n' \
                    '"Westminster,City of London",0,1\n'
            assert od_matrix_csv_string == expected_od_matrix
Exemplo n.º 2
0
def instantiate_household_with(persons: list):
    household = Household(1)
    for person in persons:
        household.add(person)
    return household
Exemplo n.º 3
0
def test_hhs_equal_with_persons_same_hh():
    hh1 = Household('1', attributes={1: 1})
    p1 = Person('1', attributes={1: 1})
    hh1.add(p1)

    assert hh1 == hh1
Exemplo n.º 4
0
def test_pickle_household(person_crop_last_act, tmpdir):
    hh = Household('1')
    hh.add(person_crop_last_act)
    path = os.path.join(tmpdir, 'test.pkl')
    hh.pickle(path)
    assert os.path.exists(path)
Exemplo n.º 5
0
def hh():
    hh = Household('A', attributes={1: 1})
    p = Person('person_A', attributes={1: 1})
    hh.add(p)
    return hh
Exemplo n.º 6
0
def test_population_not_contains_hh_same_hh_and_diff_num_persons(population):
    hh = Household('A', attributes={1: 1})
    p5 = Person('person_A', attributes={1: 1})
    hh.add(p5)
    assert hh not in population
Exemplo n.º 7
0
def test_reindex_hh_dupliate_key_error():
    hh = Household('1')
    for j in ['1', 'test_1']:
        hh.add(Person(j))
    with pytest.raises(KeyError):
        hh.reindex("test_")
Exemplo n.º 8
0
def test_reindex_hh():
    hh = Household('1')
    for j in ['1', '2']:
        hh.add(Person(j))
    hh.reindex("test_")
    assert set(hh.people) == {'test_1', 'test_2'}
Exemplo n.º 9
0
def population_heh():
    home_loc = Point(0,0)
    education_loc = Point(110,110)
    attributes = {
        'hid': '0',
        'hh_size': '3',
        'inc': "high"
    }
    person = Person('1', attributes=attributes)
    person.add(
        Activity(
            seq=1,
            act='home',
            area='a',
            loc = home_loc,
            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='education',
            area='b',
            loc=education_loc,
            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',
            loc=home_loc,
            start_time=mtdt(180),
            end_time=END_OF_DAY
        )
    )
    person.plan.autocomplete_matsim()
    household = Household('0')
    household.add(person)
    population = Population('populus')
    population.add(household)
    return population