def test_read_write_read_continuity_complex_xml(tmp_path): test_trips_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "test_data/test_matsim_plans.xml")) test_attributes_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "test_data/test_matsim_attributes.xml")) population_in = read_matsim(test_trips_path, test_attributes_path) complex_plan_in = population_in['census_1']['census_1'].plan plans_location = str(tmp_path / "test_plans.xml") write_matsim_plans(population_in, path=plans_location, comment="test") attributes_location = str(tmp_path / "test_attributes.xml") write_matsim_attributes(population_in, location=attributes_location, comment="test") population_out = read_matsim( plans_path=plans_location, attributes_path=attributes_location, household_key='hid' ) complex_plan_out = population_out['census_1']['census_1'].plan assert complex_plan_in == complex_plan_out assert population_in == population_out
def test_write_benchmarks_multiple(tmpdir): test_trips_path = os.path.abspath( os.path.join(os.path.dirname(__file__), "test_data/test_matsim_plans.xml")) test_attributes_path = os.path.abspath( os.path.join(os.path.dirname(__file__), "test_data/test_matsim_attributes.xml")) population = read_matsim(test_trips_path, test_attributes_path, weight=1000) assert list(write_distance_benchmark(population).trips) == [ 2000, 3000, 1000, 5000, 0, 0, 0, 0 ] assert list(write_mode_distance_benchmark(population).trips) == [ 2000.0, 3000.0, 1000.0, 5000.0, 0.0, 0.0, 0.0, 0.0 ] assert list(write_mode_duration_benchmark(population).trips) == [ 2000.0, 0.0, 0.0, 3000.0, 2000.0, 1000.0, 0.0, 0.0, 1000.0 ] assert list(write_duration_benchmark(population).trips) == [ 2000, 0, 0, 3000, 2000, 1000, 0, 0, 1000 ] assert list(write_departure_time_benchmark(population).trips) == [ 1000, 2000, 2000, 2000, 5000 ]
def test_read_write_v12_consistent(tmp_path): test_tripsv12_path = os.path.abspath( os.path.join(os.path.dirname(__file__), "test_data/test_matsim_plansv12.xml")) population = read_matsim(test_tripsv12_path, version=12) location = str(tmp_path / "test.xml.gz") write_matsim( population=population, version=12, plans_path=location, comment="test", household_key=None, ) expected_file = "{}/test.xml.gz".format(tmp_path) population2 = read_matsim(expected_file, version=12) assert population == population2
def test_benchmark_trips_hour(tmp_path): test_trips_path = os.path.abspath( os.path.join(os.path.dirname(__file__), "test_data/test_matsim_plans.xml")) test_attributes_path = os.path.abspath( os.path.join(os.path.dirname(__file__), "test_data/test_matsim_attributes.xml")) population = read_matsim(test_trips_path, test_attributes_path, weight=1000) benchmark = write_benchmarks(population, dimensions=['departure_hour'], data_fields=['freq'], aggfunc=[sum], path=None) expected_benchmark = pd.DataFrame({ 'departure_hour': { 0: 6, 1: 7, 2: 14, 3: 20, 4: 21 }, 'freq_sum': { 0: 1000, 1: 2000, 2: 2000, 3: 2000, 4: 5000 } }) assert benchmark.equals(expected_benchmark)
def test_parsing_person_with_network_route(): population = read_matsim(test_trips_path) person = population['census_2']['census_2'] bike_trip = person.plan.day[1] assert bike_trip.mode == 'bike' assert bike_trip.network_route == ['link_1', 'link_2', 'link_3'] assert bike_trip.route_id is None
def test_write_read_continuity_gzip(tmp_path, population_heh): plans_location = str(tmp_path / "test_plans.xml.gz") write_matsim_plans(population_heh, path=plans_location, comment="test") attributes_location = str(tmp_path / "test_attributes.xml.gz") write_matsim_attributes(population_heh, location=attributes_location, comment="test") population = read_matsim( plans_path=plans_location, attributes_path=attributes_location, household_key='hid' ) assert population_heh['0']['1'].plan == population['0']['1'].plan assert population_heh == population
def test_parsing_complex_person_results_in_valid_pt_leg(): population = read_matsim(test_trips_path, test_attributes_path) person = population['census_1']['census_1'] pt_leg = person.plan.day[5] assert pt_leg.mode == 'pt' assert pt_leg.service_id == '25239' assert pt_leg.route_id == 'VJ307b99b535bf55bc9d62b5475e5edf0d37176bcf' assert pt_leg.o_stop == '9100ROMFORD.link:25821' assert pt_leg.d_stop == '9100UPMNSP6.link:302438' assert pt_leg.network_route == None
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_parse_v12_matsim(): population = read_matsim(test_tripsv12_path, version=12) person = population['chris']['chris'] assert person.has_valid_plan assert person.attributes == {'subpopulation': 'rich', 'age': 'yes'} legs = list(person.plan.legs) assert legs[0].mode == "car" assert legs[1].mode == "car" assert legs[1].distance == 10300 assert legs[1].service_id == None assert legs[1].route_id == None assert legs[1].o_stop == None assert legs[1].d_stop == None assert legs[1].network_route == ['3-4', '4-3', '3-2', '2-1', '1-2']
def test_parse_transit_v12_matsim(): population = read_matsim(test_tripsv12_path, version=12) person = population['fred']['fred'] assert person.has_valid_plan assert person.attributes == {'subpopulation': 'poor', 'age': 'no'} legs = list(person.plan.legs) assert legs[0].mode == "walk" assert legs[1].mode == "bus" assert legs[1].distance == 10100 assert legs[1].service_id == 'city_line' assert legs[1].route_id == 'work_bound' assert legs[1].o_stop == 'home_stop_out' assert legs[1].d_stop == 'work_stop_in' assert legs[1].network_route == None
import argparse import logging import pandas as pd from tabulate import tabulate from pam import read logging.basicConfig(level=logging.ERROR) parser = argparse.ArgumentParser() parser.add_argument("plans_path", help="path to plans.xml") args = parser.parse_args() print(f"Loading population plans from {args.plans_path}...") population = read.read_matsim(args.plans_path, crop=False) print("Building pre-crop stats...") stats = population.stats print("Cropping activity plan components that end after 24 hours...") for hid, pid, person in population.people(): person.plan.crop() person.plan.autocomplete_matsim() print("Building post-crop stats...") clean_stats = population.stats data = pd.DataFrame({'original plans': stats, 'plans <24 hours': clean_stats}) data['% of original'] = 100 * data['plans <24 hours'] / data['original plans'] data.style.format({ '% of original': '{:.2f}'.format,
def test_read_plan_with_negative_durations(): population = read_matsim(test_bad_trips_path, test_bad_attributes_path) population['test']['test'].print()
def test_fail_bad_version(): with pytest.raises(UserWarning): population = read_matsim(test_tripsv12_path, version=1)
def cyclist(): return read.read_matsim(test_trips_path)['census_2']['census_2']
def pt_person(): return read.read_matsim(test_trips_path)['census_1']['census_1']
def test_parse_complex_matsim(): population = read_matsim(test_trips_path, test_attributes_path) person = population['census_1']['census_1'] assert person.has_valid_plan
def test_remove_pt_interactions(): population = read_matsim(test_trips_path, test_attributes_path) person = population['census_1']['census_1'] person.plan.simplify_pt_trips() assert 'pt interaction' not in [a.act for a in person.activities] assert person.has_valid_plan
def test_fail_v12_plus_attributes(): with pytest.raises(UserWarning): population = read_matsim(test_tripsv12_path, attributes_path='fake', version=12)