def test_transit_read_write(): base_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) examples_dir = os.path.join(base_path,'example') tests_temp_dir = os.path.join(base_path,'scratch') if not os.path.exists(tests_temp_dir): os.mkdir(tests_temp_dir) transit_net_dir = os.path.join(examples_dir, "stpaul") transit_net = TransitNetwork(feed_path = transit_net_dir) print('Transit Net Directory:', transit_net_dir) transit_net.write(outpath=tests_temp_dir) print('Transit Write Directory:', tests_temp_dir)
def test_invalid_optional_selection_variable(request): print("\n--Starting:", request.node.name) net = TransitNetwork.read(STPAUL_DIR) with pytest.raises(Exception): # `wheelchair` rather than `wheelchair_accessible` net.select_transit_features({ "trip_id": "14940701-JUN19-MVS-BUS-Weekday-01", "wheelchair": "0" }) with pytest.raises(Exception): # Missing trip_id, route_id, route_short_name, or route_long_name net.select_transit_features({"wheelchair_accessible": "0"}) # Correct trip variable sel = net.select_transit_features({ "trip_id": "14940701-JUN19-MVS-BUS-Weekday-01", "wheelchair_accessible": "1" }) assert set(sel) == set(["14940701-JUN19-MVS-BUS-Weekday-01"]) # Correct route variable sel = net.select_transit_features({ "route_long_name": "Express", "agency_id": "2" }) assert set(sel) == set(["14978409-JUN19-MVS-BUS-Weekday-01"]) print("--Finished:", request.node.name)
def test_read_write_cube_transit_standard_from_wrangler_object(request): print("\n--Starting:", request.node.name) from network_wrangler import TransitNetwork cube_transit_net = StandardTransit.fromTransitNetwork( TransitNetwork.read(feed_path=BASE_TRANSIT_DIR)) cube_transit_net.write_as_cube_lin( os.path.join(SCRATCH_DIR, "t_transit_test.lin"))
def test_select_transit_features(request): print("\n--Starting:", request.node.name) net = TransitNetwork.read(STPAUL_DIR) test_selections = { "1. simple trip_id": { "trip_id": "14940701-JUN19-MVS-BUS-Weekday-01", "answer": ["14940701-JUN19-MVS-BUS-Weekday-01"], }, "2. multiple trip_id": { "trip_id": [ "14969841-JUN19-RAIL-Weekday-01", # unordered "14940701-JUN19-MVS-BUS-Weekday-01", ], "answer": [ "14940701-JUN19-MVS-BUS-Weekday-01", "14969841-JUN19-RAIL-Weekday-01", ], }, "3. route_id": { "route_id": "365-111", "answer": ["14947182-JUN19-MVS-BUS-Weekday-01"], }, } for i, sel in test_selections.items(): print("--->", i, "\n", sel) answer = sel.pop("answer") selected_trips = net.select_transit_features(sel) assert set(selected_trips) == set(answer) print("--Finished:", request.node.name)
def test_transit_read_write(request): print("\n--Starting:", request.node.name) transit_net = TransitNetwork.read(feed_path=STPAUL_DIR) print("Transit Net Directory:", STPAUL_DIR) transit_net.write(path=SCRATCH_DIR) print("Transit Write Directory:", SCRATCH_DIR) print("--Finished:", request.node.name)
def test_invalid_selection_key(request): print("\n--Starting:", request.node.name) net = TransitNetwork.read(STPAUL_DIR) with pytest.raises(Exception): # trip_ids rather than trip_id should fail net.select_transit_features({"trip_ids": ["14941433-JUN19-MVS-BUS-Weekday-01"]}) print("--Finished:", request.node.name)
def test_zero_valid_facilities(request): print("\n--Starting:", request.node.name) net = TransitNetwork.read(STPAUL_DIR) with pytest.raises(Exception): net.select_transit_features({ "trip_id": ["14941433-JUN19-MVS-BUS-Weekday-01"], "time": ["06:00:00", "09:00:00"], }) print("--Finished:", request.node.name)
def test_set_roadnet(request): print("\n--Starting:", request.node.name) road_net = RoadwayNetwork.read( link_file=os.path.join(STPAUL_DIR, "link.json"), node_file=os.path.join(STPAUL_DIR, "node.geojson"), shape_file=os.path.join(STPAUL_DIR, "shape.geojson"), fast=True ) transit_net = TransitNetwork.read(STPAUL_DIR) transit_net.set_roadnet(road_net) print("--Finished:", request.node.name)
def test_project_card(request): print("\n--Starting:", request.node.name) transit_net = TransitNetwork.read(STPAUL_DIR) project_card_path = os.path.join( STPAUL_DIR, "project_cards", "12_transit_shape_change.yml" ) project_card = ProjectCard.read(project_card_path) transit_net.apply_transit_feature_change( transit_net.select_transit_features(project_card.facility), project_card.properties ) # Shapes result = transit_net.feed.shapes[transit_net.feed.shapes["shape_id"] == "2940002"]["shape_model_node_id"].tolist() answer = ["45983", "126312", "126316", "46663", "46665", "150855", "11188", "84899", "46666", "77077", "68609", "39425", "62146", "41991", "70841", "45691", "69793", "45683", "45685", "7688", "45687", "100784", "100782", "45688", "37609", "19077", "38696", "91685", "38698", "138152", "91825", "71086", "133190", "133187", "133188", "133183", "133179", "133177", "44298", "67125", "68417", "72311", "75802", "46083", "75783", "71964", "71456", "44190", "61464", "75786", "74898", "73817", "51814", "75787", "75122", "75788", "123002", "123003", "39803", "75789", "70300", "68072", "68039", "68041", "68043", "68044", "40878", "32484", "32474", "32485", "32499", "32532", "32544", "32479", "32491", "32502", "10658", "10655", "10651", "10652", "10649", "111619", "10643", "10642", "10640", "74056", "70553", "12145", "12147", "56079", "12142", "79492", "76595", "51578", "74179", "82208", "41799", "123353", "123351", "51651", "67557", "12136", "12135", "67419", "56842", "80494", "128663", "52195", "12137", "12146", "25816", "25809", "115180", "9910", "115159", "115154", "162573", "10409", "164501", "170453", "158052", "145473", "25658", "91126", "90985", "90993", "145453", "112931", "157281", "163689", "163713", "117979", "116895", "138571", "87210", "97813", "165113", "133948", "101458", "105285", "100603", "165025", "127797", "7753", "127801", "28889", "28890", "127804", "7754", "127802", "48699", "125802", "125237", "96386", "96366", "96367", "145532", "94231", "6578", "7278", "9831", "10910", "10242", "112268", "110085", "114928", "114941", "11694", "11654", "11705", "154299", "11708", "11710", "11704", "11674", "11668", "11677", "9483", "161536", "88542", "22645", "19792", "126666", "170579", "145513", "155886", "101864", "100562", "51955", "23270", "23263", "23271", "139930"] assert result == answer # Stops result = transit_net.feed.stop_times[transit_net.feed.stop_times["trip_id"] == "14944022-JUN19-MVS-BUS-Weekday-01"]["stop_id"].tolist() result_tail = result[-5:] answer_tail = ["16842", "16837", "16836", "16833", "17040"] assert result_tail == answer_tail print("--Finished:", request.node.name)
def test_wo_existing(request): print("\n--Starting:", request.node.name) transit_net = TransitNetwork.read(STPAUL_DIR) # A new node ID (not in stops.txt) should fail right now with pytest.raises(Exception): transit_net.apply_transit_feature_change( trip_ids=transit_net.select_transit_features( {"trip_id": ["14944022-JUN19-MVS-BUS-Weekday-01"]} ), properties=[ { "property": "routing", "set": [1] } ] ) transit_net.apply_transit_feature_change( trip_ids=transit_net.select_transit_features( {"trip_id": ["14986385-JUN19-MVS-BUS-Weekday-01"]} ), properties=[ { "property": "routing", "set": [75318] } ] ) # Shapes result = transit_net.feed.shapes[ transit_net.feed.shapes["shape_id"] == "210005" ]["shape_model_node_id"].tolist() answer = ["1"] assert result == answer # Stops result = transit_net.feed.stop_times[ transit_net.feed.stop_times["trip_id"] == "14986385-JUN19-MVS-BUS-Weekday-01" ]["stop_id"].tolist() answer = ["2609"] # first matching stop_id in stops.txt assert result == answer print("--Finished:", request.node.name)
def test_wrong_existing(request): print("\n--Starting:", request.node.name) net = TransitNetwork.read(STPAUL_DIR) selected_trips = net.select_transit_features({ "trip_id": [ "14944018-JUN19-MVS-BUS-Weekday-01", "14944012-JUN19-MVS-BUS-Weekday-01", ] }) with pytest.raises(Exception): net.apply_transit_feature_change(selected_trips, [{ "property": "headway_secs", "existing": 553, "set": 900 }]) print("--Finished:", request.node.name)
def test_transit_road_consistencies(request): print("\n--Starting:", request.node.name) net = TransitNetwork.read(STPAUL_DIR) STPAUL_SHAPE_FILE = os.path.join(STPAUL_DIR, "shape.geojson") STPAUL_LINK_FILE = os.path.join(STPAUL_DIR, "link.json") STPAUL_NODE_FILE = os.path.join(STPAUL_DIR, "node.geojson") road_net = RoadwayNetwork.read( link_file=STPAUL_LINK_FILE, node_file=STPAUL_NODE_FILE, shape_file=STPAUL_SHAPE_FILE, fast=True, ) net.set_roadnet(road_net=road_net) net.validate_road_network_consistencies() print(net.validated_road_network_consistency) print("--Finished:", request.node.name)
def test_select_transit_features_by_nodes(request): print("\n--Starting:", request.node.name) transit_net = TransitNetwork.read(STPAUL_DIR) # Any nodes trip_ids = transit_net.select_transit_features_by_nodes( node_ids=["29636", "29666"] ) assert set(trip_ids) == set([ "14940701-JUN19-MVS-BUS-Weekday-01", "14942968-JUN19-MVS-BUS-Weekday-01" ]) # All nodes trip_ids = transit_net.select_transit_features_by_nodes( node_ids=["29636", "29666"], require_all=True ) assert set(trip_ids) == set([ "14940701-JUN19-MVS-BUS-Weekday-01" ]) print("--Finished:", request.node.name)
def test_apply_summary_wrappers(request): print("\n--Starting:", request.node.name) card_filenames = [ "3_multiple_roadway_attribute_change.yml", "multiple_changes.yml", "4_simple_managed_lane.yml", ] project_card_directory = os.path.join(STPAUL_DIR, "project_cards") project_cards_list = [ ProjectCard.read(os.path.join(project_card_directory, filename), validate=False) for filename in card_filenames ] base_scenario = { "road_net": RoadwayNetwork.read( link_file=STPAUL_LINK_FILE, node_file=STPAUL_NODE_FILE, shape_file=STPAUL_SHAPE_FILE, fast=True, ), "transit_net": TransitNetwork.read(STPAUL_DIR), } my_scenario = Scenario.create_scenario( base_scenario=base_scenario, project_cards_list=project_cards_list) my_scenario.apply_all_projects() my_scenario.scenario_summary() print("--Finished:", request.node.name)
def test_apply_transit_feature_change_from_projectcard(request): print("\n--Starting:", request.node.name) net = TransitNetwork.read(STPAUL_DIR) test_selections = [ { "file": "7_simple_transit_attribute_change.yml", "answer": { "trip_ids": ["14940701-JUN19-MVS-BUS-Weekday-01"], "headway_secs": [900], }, }, { "file": "8_simple_transit_attribute_change.yml", "answer": { "trip_ids": [ "14944012-JUN19-MVS-BUS-Weekday-01", "14944019-JUN19-MVS-BUS-Weekday-01", ], "headway_secs": [253, 226], }, }, { "file": "9_simple_transit_attribute_change.yml", "answer": { "trip_ids": [ "14940701-JUN19-MVS-BUS-Weekday-01", "14943414-JUN19-MVS-BUS-Weekday-01", "14943415-JUN19-MVS-BUS-Weekday-01", "14946111-JUN19-MVS-BUS-Weekday-01", "14946257-JUN19-MVS-BUS-Weekday-01", "14946470-JUN19-MVS-BUS-Weekday-01", "14946471-JUN19-MVS-BUS-Weekday-01", "14946480-JUN19-MVS-BUS-Weekday-01", "14946521-JUN19-MVS-BUS-Weekday-01", "14947182-JUN19-MVS-BUS-Weekday-01", "14947504-JUN19-MVS-BUS-Weekday-01", "14947734-JUN19-MVS-BUS-Weekday-01", "14947755-JUN19-MVS-BUS-Weekday-01", "14978409-JUN19-MVS-BUS-Weekday-01", "14981028-JUN19-MVS-BUS-Weekday-01", "14981029-JUN19-MVS-BUS-Weekday-01", "14986383-JUN19-MVS-BUS-Weekday-01", "14986385-JUN19-MVS-BUS-Weekday-01", ], "headway_secs": [1800], }, }, ] for i, test in enumerate(test_selections): print("--->", i) print("Reading project card", test["file"], "...") project_card_path = os.path.join(STPAUL_DIR, "project_cards", test["file"]) project_card = ProjectCard.read(project_card_path) net.apply_transit_feature_change( net.select_transit_features(project_card.facility), project_card.properties) freq = net.feed.frequencies answers = test["answer"]["headway_secs"] if len(answers) > 1: for i, answer in enumerate(answers): match = freq.trip_id == test["answer"]["trip_ids"][i] result = freq[match]["headway_secs"] assert set(result) == set([answer]) else: matches = freq.trip_id.isin(test["answer"]["trip_ids"]) results = freq[matches]["headway_secs"].tolist() assert set(results) == set(answers) print("--Finished:", request.node.name)
def test_read_cube_transit_standard_from_wrangler_object(request): print("\n--Starting:", request.node.name) from network_wrangler import TransitNetwork tnet = TransitNetwork.read(feed_path=BASE_TRANSIT_DIR) cube_transit_net = StandardTransit.fromTransitNetwork(tnet)
def test_select_transit_features_from_projectcard(request): print("\n--Starting:", request.node.name) net = TransitNetwork.read(STPAUL_DIR) test_selections = [ { "file": "7_simple_transit_attribute_change.yml", "answer": ["14940701-JUN19-MVS-BUS-Weekday-01"], }, { "file": "7a_multi_transit_attribute_change.yml", "answer": [ "14940701-JUN19-MVS-BUS-Weekday-01", "14948032-JUN19-MVS-BUS-Weekday-01", ], }, { "file": "8_simple_transit_attribute_change.yml", "answer": [ "14944012-JUN19-MVS-BUS-Weekday-01", "14944018-JUN19-MVS-BUS-Weekday-01", "14944019-JUN19-MVS-BUS-Weekday-01", "14944022-JUN19-MVS-BUS-Weekday-01", ], }, { "file": "8a_multi_transit_attribute_change.yml", "answer": [ "14944012-JUN19-MVS-BUS-Weekday-01", "14944018-JUN19-MVS-BUS-Weekday-01", "14944019-JUN19-MVS-BUS-Weekday-01", "14944022-JUN19-MVS-BUS-Weekday-01", "14948211-JUN19-MVS-BUS-Weekday-01", # additional for 53-111 "14948218-JUN19-MVS-BUS-Weekday-01", ], }, { "file": "9_simple_transit_attribute_change.yml", "answer": [ "14940701-JUN19-MVS-BUS-Weekday-01", "14943414-JUN19-MVS-BUS-Weekday-01", "14943415-JUN19-MVS-BUS-Weekday-01", "14946111-JUN19-MVS-BUS-Weekday-01", "14946257-JUN19-MVS-BUS-Weekday-01", "14946470-JUN19-MVS-BUS-Weekday-01", "14946471-JUN19-MVS-BUS-Weekday-01", "14946480-JUN19-MVS-BUS-Weekday-01", "14946521-JUN19-MVS-BUS-Weekday-01", "14947182-JUN19-MVS-BUS-Weekday-01", "14947504-JUN19-MVS-BUS-Weekday-01", "14947734-JUN19-MVS-BUS-Weekday-01", "14947755-JUN19-MVS-BUS-Weekday-01", "14978409-JUN19-MVS-BUS-Weekday-01", "14981028-JUN19-MVS-BUS-Weekday-01", "14981029-JUN19-MVS-BUS-Weekday-01", "14986383-JUN19-MVS-BUS-Weekday-01", "14986385-JUN19-MVS-BUS-Weekday-01", ], }, { "file": "9a_multi_transit_attribute_change.yml", "answer": [ "14940701-JUN19-MVS-BUS-Weekday-01", "14943414-JUN19-MVS-BUS-Weekday-01", "14943415-JUN19-MVS-BUS-Weekday-01", "14946111-JUN19-MVS-BUS-Weekday-01", "14946257-JUN19-MVS-BUS-Weekday-01", "14946470-JUN19-MVS-BUS-Weekday-01", "14946471-JUN19-MVS-BUS-Weekday-01", "14946480-JUN19-MVS-BUS-Weekday-01", "14946521-JUN19-MVS-BUS-Weekday-01", "14947182-JUN19-MVS-BUS-Weekday-01", "14947504-JUN19-MVS-BUS-Weekday-01", "14947734-JUN19-MVS-BUS-Weekday-01", "14947755-JUN19-MVS-BUS-Weekday-01", "14978409-JUN19-MVS-BUS-Weekday-01", "14981028-JUN19-MVS-BUS-Weekday-01", "14981029-JUN19-MVS-BUS-Weekday-01", "14986383-JUN19-MVS-BUS-Weekday-01", "14986385-JUN19-MVS-BUS-Weekday-01", "14946199-JUN19-MVS-BUS-Weekday-01", # add below for Ltd Stop "14947598-JUN19-MVS-BUS-Weekday-01", "14948211-JUN19-MVS-BUS-Weekday-01", "14948218-JUN19-MVS-BUS-Weekday-01", ], }, ] for i, test in enumerate(test_selections): print("--->", i) print("Reading project card", test["file"], "...") project_card_path = os.path.join(STPAUL_DIR, "project_cards", test["file"]) project_card = ProjectCard.read(project_card_path) sel = project_card.facility selected_trips = net.select_transit_features(sel) assert set(selected_trips) == set(test["answer"]) print("--Finished:", request.node.name)