def test_extract_household_mode_classes(): household = Household(hid='1') for i, (act, mode) in enumerate(zip(['work', 'school'], ['car', 'pt'])): person = Person(pid=str(i)) person.add( Activity(seq=1, act='home', area='A', start_time=mtdt(0), end_time=mtdt(600))) person.add( Leg(seq=2, mode=mode, start_area='A', end_area='B', start_time=mtdt(600), end_time=mtdt(620))) person.add( Activity(seq=3, act=act, area='B', start_time=mtdt(620), end_time=mtdt(1200))) household.add(person) assert household.mode_classes == set(['car', 'pt'])
def test_population_num_households(): population = Population() population.add(Household('1')) population['1'].add(Person('0', freq=1)) population['1'].add(Person('1', freq=3)) population.add(Household('2')) assert population.num_households == 2
def test_extract_population_mode_classes(): population = Population() for hid, (_act, _mode) in enumerate(zip(['work', 'shop'], ['pt', 'walk'])): household = Household(hid=str(hid)) population.add(household) for i, (act, mode) in enumerate(zip(['work', _act], ['car', _mode])): person = Person(pid=str(i)) person.add( Activity(seq=1, act='home', area='A', start_time=mtdt(0), end_time=mtdt(600))) person.add( Leg(seq=2, mode=mode, start_area='A', end_area='B', start_time=mtdt(600), end_time=mtdt(620))) person.add( Activity(seq=3, act=act, area='B', start_time=mtdt(620), end_time=mtdt(1200))) household.add(person) assert population.mode_classes == set(['car', 'pt', 'walk'])
def test_household_add_person(): household = Household(1) person = Person(1) person.add(Activity(1, 'home', 1, start_time=0)) household.add(person) assert len(household.people) == 1 assert list(household.people) == [1]
def test_population_size(): population = Population() population.add(Household('1')) population['1'].add(Person('0', freq=1)) population['1'].add(Person('1', freq=3)) population.add(Household('2')) assert population.size == 4
def test_population_stats(): population = Population() population.add(Household('1')) population['1'].add(Person('0', freq=1)) population['1'].add(Person('1', freq=3)) population.add(Household('2')) population['2'].add(Person('2', freq=3)) assert isinstance(population.stats, dict)
def test_hhs_not_equal_with_missing_persons(): hh1 = Household('1', attributes={1: 1}) p1 = Person('1', attributes={1: 1}) hh1.add(p1) hh4 = Household('2', attributes={1: 2}) assert not hh1 == hh4
def test_plot_act_time_bins(Steve, Hilda): population = Population() for i, person in enumerate([Steve, Hilda]): hh = Household(i) hh.add(person) population.add(hh) fig = plot_activity_times(population) assert isinstance(fig, Figure)
def test_populations_equal_given_same_population(): pop1 = Population() hh1 = Household('1', attributes={1: 1}) p1 = Person('1', attributes={1: 1}) hh1.add(p1) pop1.add(hh1) assert pop1 == pop1
def test_count_population(): population = Population() for i in range(1, 5): hh = Household(str(i)) for ii in range(i): hh.add(Person(f"{i}_{ii}")) population.add(hh) assert population.population == 10
def test_pickle_population(person_crop_last_act, tmpdir): population = Population() hh = Household('1') hh.add(person_crop_last_act) population.add(hh) path = os.path.join(tmpdir, 'test.pkl') population.pickle(path) assert os.path.exists(path)
def test_populations_not_equal_given_diff_type(): pop1 = Population() hh1 = Household('1', attributes={1: 1}) p1 = Person('1', attributes={1: 1}) hh1.add(p1) pop1.add(hh1) assert not pop1 == None
def test_reindex_population_duplicate_key(): pop = Population() for i in ['1', 'test_1']: hh = Household(i) pop.add(hh) for j in ['1', 'test_1']: hh.add(Person(j)) with pytest.raises(KeyError): pop.reindex("test_")
def test_build_leg_log(person_heh): population = Population() for i in range(5): hh = Household(i) hh.add(person_heh) population.add(hh) log = extract_leg_log(population) assert len(log) == 10 assert list(log.columns) == ['mode', 'start', 'end', 'duration']
def test_household_iadd_person(): hh1 = Household('1') p1 = Person('1') hh1.add(p1) p2 = Person('2') hh1 += p2 assert set(hh1.people) == {'1', '2'} assert isinstance(hh1.people['2'], Person)
def test_load_pickle_population(person_crop_last_act, tmpdir): population = Population() hh = Household('1') hh.add(person_crop_last_act) population.add(hh) path = os.path.join(tmpdir, 'test.pkl') population.pickle(path) loaded = load_pickle(path) assert loaded.households assert list(loaded.households['1'].people) == list( population.households['1'].people)
def test_population_combine_person_duplicate_key(): pop1 = Population() hh1 = Household('1') p1 = Person('1') hh1.add(p1) pop1.add(hh1) p2 = Person('1') with pytest.raises(KeyError): pop1.combine(p2, '')
def test_time_binner(person_heh): population = Population() for i in range(5): hh = Household(i) hh.add(person_heh) population.add(hh) log = extract_activity_log(population) binned = time_binner(log) assert len(binned) == 96 for h in ['start', 'end', 'duration']: assert binned[h].sum() == 3
def test_populations_not_equal_given_missing_person(): pop1 = Population() hh1 = Household('1', attributes={1: 1}) p1 = Person('1', attributes={1: 1}) hh1.add(p1) pop1.add(hh1) pop4 = Population() hh4 = Household('2', attributes={1: 2}) pop4.add(hh4) assert not pop1 == pop4
def test_write_plans_xml_v12_assert_contents(tmp_path): population = Population() hh = Household('a') p = Person('a', attributes={'1':'1'}) p.add(Activity( act="home", loc=Point((0,0)), start_time=datetime(1900,1,1,0,0,0), end_time=datetime(1900,1,1,8,0,0) )) p.add(Leg( mode='car', start_loc=Point((0,0)), end_loc=Point((0,1000)), start_time=datetime(1900,1,1,8,0,0), end_time=datetime(1900,1,1,9,0,0) )) p.add(Activity( act="work", loc=Point((0,1000)), start_time=datetime(1900,1,1,9,0,0), end_time=datetime(1900,1,1,18,0,0) )) p.add(Leg( mode='car', start_loc=Point((0,1000)), end_loc=Point((0,0)), start_time=datetime(1900,1,1,18,0,0), end_time=datetime(1900,1,1,19,0,0) )) p.add(Activity( act="home", loc=Point((0,0)), start_time=datetime(1900,1,1,19,0,0), end_time=END_OF_DAY )) hh.add(p) population.add(hh) plans_location = str(tmp_path / "test_plans.xml") write_matsim( population, plans_path=plans_location, comment="test", version=12, household_key=None ) new = read_matsim( plans_location, version=12 ) assert new == population assert new['a']['a'].attributes == {'1':'1'} assert new['a']['a'].plan.day[1].distance == 1000
def test_population_combine_person(): pop1 = Population() hh1 = Household('1') p1 = Person('1') hh1.add(p1) pop1.add(hh1) p2 = Person('1') pop1.combine(p2, 'test_') assert set(pop1.households) == {'1', 'test_1'} assert set(pop1.households['test_1'].people) == {'test_1'} assert isinstance(pop1.households['test_1'], Household) assert isinstance(pop1.households['test_1'].people['test_1'], Person)
def test_home_education_home_removal_of_education_act( person_home_education_home): household = Household(1) person = person_home_education_home household.add(person) policy = policies.RemoveEducationActivity(1) policy.apply_to(household) assert [p.act for p in household.people[1].activities] == ['home'] assert household.people[1].plan[0].start_time == minutes_to_datetime(0) assert household.people[1].plan[0].end_time == minutes_to_datetime(24 * 60 - 1)
def test_population_iadd_person(): pop1 = Population() hh1 = Household('1') p1 = Person('1') hh1.add(p1) pop1.add(hh1) p2 = Person('2') pop1 += p2 assert set(pop1.households) == {'1', '2'} assert set(pop1.households['2'].people) == {'2'} assert isinstance(pop1.households['2'], Household) assert isinstance(pop1.households['2'].people['2'], Person)
def population(): population = Population() for hid in range(1, 11): household = Household(hid) for pid in range(2): pid = f"{hid}-{pid}" person = Person(pid) person.add(Activity(1, 'home', 'a')) person.add(Leg(1, 'car', 'a', 'b')) person.add(Activity(2, 'work', 'b')) person.add(Leg(2, 'car', 'b', 'a')) person.add(Activity(3, 'home', 'a')) household.add(person) population.add(household) for hid in range(10, 21): household = Household(hid) for pid in range(2): pid = f"{hid}-{pid}" person = Person(pid) person.add(Activity(1, 'home', 'a')) person.add(Leg(1, 'bus', 'a', 'b')) person.add(Activity(2, 'education', 'b')) person.add(Leg(2, 'bus', 'b', 'a')) person.add(Activity(3, 'home', 'a')) household.add(person) population.add(household) return population
def test_reindex_population(): pop = Population() for i in ['1', '2']: hh = Household(i) pop.add(hh) for j in ['1', '2']: hh.add(Person(j)) pop.reindex("test_") assert set(pop.households) == {'test_1', 'test_2'} assert {hh.hid for hh in pop.households.values()} == {'test_1', 'test_2'} assert set(pop.households['test_1'].people) == {'test_1', 'test_2'} assert {p.pid for p in pop.households['test_1'].people.values() } == {'test_1', 'test_2'}
def test_population_print(capfd, person_heh): population = Population() population.add(Household('1')) population['1'].add(person_heh) population.print() out, _ = capfd.readouterr() assert (out)
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() population.add(household) return population
def test_hhs_with_persons_equal_with_same_persons(): hh1 = Household('1', attributes={1: 1}) p1 = Person('1', attributes={1: 1}) hh1.add(p1) hh2 = Household('1', attributes={1: 1}) p2 = Person('1', attributes={1: 1}) hh2.add(p2) assert hh1 == hh2
def test_hhs_with_persons_equal_with_same_persons_diff_pid(): hh1 = Household('1', attributes={1: 1}) p1 = Person('1', attributes={1: 1}) hh1.add(p1) hh3 = Household('2', attributes={1: 1}) p3 = Person('2', attributes={1: 1}) hh3.add(p3) assert hh1 == hh3
def test_hhs_not_equal_with_diff_persons_attributes(): hh1 = Household('1', attributes={1: 1}) p1 = Person('1', attributes={1: 1}) hh1.add(p1) hh5 = Household('2', attributes={1: 1}) p5 = Person('1', attributes={1: 2}) hh5.add(p5) assert not hh1 == hh5