def test_compute_with_skimming(self): r = PathResults() self.g.set_skimming("free_flow_time") r.prepare(self.g) r.compute_path(origin, dest) self.assertEqual(r.milepost[-1], r.skims[dest], "Skims computed wrong when computing path")
def test_path_disconnected_penalize_link_in_memory(self): links = [2, 4, 5, 14] self.project.network.build_graphs() g = self.project.network.graphs["c"] # type: Graph g.exclude_links(links) g.set_graph("free_flow_time") g.set_blocked_centroid_flows(False) r = PathResults() r.prepare(g) r.compute_path(1, 5) self.assertEqual(None, r.path, 'Failed to return None for disconnected') r.compute_path(1, 2) self.assertEqual(len(r.path), 1, 'Returned the wrong thing for existing path on disconnected network')
def test_exclude_links(self): # excludes a link before any setting or preparation self.graph.set_blocked_centroid_flows(False) self.graph.set_graph("distance") r1 = PathResults() r1.prepare(self.graph) r1.compute_path(20, 21) self.assertEqual(list(r1.path), [62]) r1 = PathResults() self.graph.exclude_links([62]) r1.prepare(self.graph) r1.compute_path(20, 21) self.assertEqual(list(r1.path), [63, 69])
def test_exclude_links(self): p = Project() p.load(siouxfalls_project) p.network.build_graphs() g = p.network.graphs['c'] # type: Graph # excludes a link before any setting or preparation g.exclude_links([12]) g.set_graph('distance') r1 = PathResults() r1.prepare(g) r1.compute_path(1, 14) self.assertEqual(list(r1.path), [2, 6, 10, 34]) # We exclude one link that we know was part of the last shortest path g.exclude_links([10]) r2 = PathResults() r2.prepare(g) r2.compute_path(1, 14) self.assertEqual(list(r2.path), [2, 7, 36, 34]) p.conn.close()
class TestPathResultsDisconnected(TestCase): def setUp(self) -> None: self.project = create_example(join(gettempdir(), "test_path_disconnected" + uuid4().hex)) def tearDown(self) -> None: self.project.close() def test_path_disconnected_delete_link(self): self.project.conn.executemany('delete from Links where link_id=?', [[2], [4], [5], [14]]) self.project.conn.commit() self.project.network.build_graphs() self.g = self.project.network.graphs["c"] # type: Graph self.g.set_graph("free_flow_time") self.g.set_blocked_centroid_flows(False) self.r = PathResults() self.r.prepare(self.g) self.r.compute_path(1, 5) self.assertEqual(None, self.r.path, 'Failed to return None for disconnected') self.r.compute_path(1, 2) self.assertEqual(len(self.r.path), 1, 'Returned the wrong thing for existing path on disconnected network') def test_path_disconnected_penalize_link_in_memory(self): links = [2, 4, 5, 14] self.project.network.build_graphs() g = self.project.network.graphs["c"] # type: Graph g.exclude_links(links) g.set_graph("free_flow_time") g.set_blocked_centroid_flows(False) r = PathResults() r.prepare(g) r.compute_path(1, 5) self.assertEqual(None, r.path, 'Failed to return None for disconnected') r.compute_path(1, 2) self.assertEqual(len(r.path), 1, 'Returned the wrong thing for existing path on disconnected network')
# And prepare the path computation structure res = PathResults() res.prepare(graph) # %% # Now we can compute all the path islands we have islands = [] idx_islands = 0 # while missing_nodes.shape[0] >= 2: print(datetime.now().strftime("%H:%M:%S"), f' - Computing island: {idx_islands}') res.reset() res.compute_path(missing_nodes[0], missing_nodes[1]) res.predecessors[graph.nodes_to_indices[missing_nodes[0]]] = 0 connected = graph.all_nodes[np.where(res.predecessors >= 0)] connected = np.intersect1d(missing_nodes, connected) missing_nodes = np.setdiff1d(missing_nodes, connected) print(f' Nodes to find: {missing_nodes.shape[0]:,}') df = pd.DataFrame({'node_id': connected, 'island': idx_islands}) islands.append(df) idx_islands += 1 print(f'\nWe found {idx_islands} islands') # %% # consolidate everything into a single DataFrame islands = pd.concat(islands) # And save to disk alongside our model
class TestPathResults(TestCase): def setUp(self) -> None: self.project = create_example( join(gettempdir(), "test_set_pce_" + uuid4().hex)) self.project.network.build_graphs() self.g = self.project.network.graphs["c"] # type: Graph self.g.set_graph("free_flow_time") self.g.set_blocked_centroid_flows(False) self.matrix = self.project.matrices.get_matrix("demand_omx") self.matrix.computational_view() self.r = PathResults() self.r.prepare(self.g) def tearDown(self) -> None: self.project.close() self.matrix.close() del self.r def test_reset(self): self.r.compute_path(dest, origin) self.r.reset() self.assertEqual(self.r.path, None, "Fail to reset the Path computation object") self.assertEqual(self.r.path_nodes, None, "Fail to reset the Path computation object") self.assertEqual(self.r.path_link_directions, None, "Fail to reset the Path computation object") self.assertEqual(self.r.milepost, None, "Fail to reset the Path computation object") self.assertEqual(self.r.predecessors.max(), -1, "Fail to reset the Path computation object") self.assertEqual(self.r.predecessors.min(), -1, "Fail to reset the Path computation object") self.assertEqual(self.r.connectors.max(), -1, "Fail to reset the Path computation object") self.assertEqual(self.r.connectors.min(), -1, "Fail to reset the Path computation object") if self.r.skims is not None: self.assertEqual(self.r.skims.max(), np.inf, "Fail to reset the Path computation object") self.assertEqual(self.r.skims.min(), np.inf, "Fail to reset the Path computation object") new_r = PathResults() with self.assertRaises(ValueError): new_r.reset() def test_compute_paths(self): path_computation(5, 2, self.g, self.r) self.assertEqual(list(self.r.path), [12, 14], "Path computation failed. Wrong sequence of links") self.assertEqual(list(self.r.path_link_directions), [1, 1], "Path computation failed. Wrong link directions") self.assertEqual( list(self.r.path_nodes), [5, 6, 2], "Path computation failed. Wrong sequence of path nodes") self.assertEqual(list(self.r.milepost), [0, 4, 9], "Path computation failed. Wrong milepost results") def test_compute_with_skimming(self): r = PathResults() self.g.set_skimming("free_flow_time") r.prepare(self.g) r.compute_path(origin, dest) self.assertEqual(r.milepost[-1], r.skims[dest], "Skims computed wrong when computing path") def test_update_trace(self): self.r.compute_path(origin, 2) self.r.update_trace(10) self.assertEqual(list(self.r.path), [13, 25], "Path update failed. Wrong sequence of links") self.assertEqual(list(self.r.path_link_directions), [1, 1], "Path update failed. Wrong link directions") self.assertEqual(list(self.r.path_nodes), [5, 9, 10], "Path update failed. Wrong sequence of path nodes") self.assertEqual(list(self.r.milepost), [0, 5, 8], "Path update failed. Wrong milepost results")
class TestBlockingTrianglePathResults(TestCase): def setUp(self) -> None: os.environ['PATH'] = os.path.join( gettempdir(), 'temp_data') + ';' + os.environ['PATH'] self.proj_dir = os.path.join(gettempdir(), uuid.uuid4().hex) copytree(triangle_graph_blocking, self.proj_dir) self.project = Project() self.project.open(self.proj_dir) self.project.network.build_graphs(modes=["c"]) self.g = self.project.network.graphs["c"] # type: Graph self.g.set_graph("free_flow_time") self.g.set_blocked_centroid_flows(True) self.r = PathResults() self.r.prepare(self.g) def tearDown(self) -> None: self.project.close() del self.r def test_compute_paths(self): self.r.compute_path(1, 2) self.assertEqual(list(self.r.path_nodes), [1, 3, 2]) self.assertEqual(list(self.r.path), [1, 2]) self.r.compute_path(2, 1) self.assertEqual(list(self.r.path_nodes), [2, 1]) self.assertEqual(list(self.r.path), [3]) self.r.compute_path(3, 1) self.assertEqual(list(self.r.path_nodes), [3, 2, 1]) self.assertEqual(list(self.r.path), [2, 3]) self.r.compute_path(3, 2) self.assertEqual(list(self.r.path_nodes), [3, 2]) self.assertEqual(list(self.r.path), [2]) self.r.compute_path(1, 3) self.assertEqual(list(self.r.path_nodes), [1, 3]) self.assertEqual(list(self.r.path), [1]) self.r.compute_path(2, 3) self.assertEqual(list(self.r.path_nodes), [2, 1, 3]) self.assertEqual(list(self.r.path), [3, 1]) def test_compute_blocking_paths(self): self.r.compute_path(4, 5) self.assertEqual(list(self.r.path_nodes), [4, 1, 3, 2, 5]) self.assertEqual(list(self.r.path), [4, 1, 2, 5]) self.r.compute_path(5, 4) self.assertEqual(list(self.r.path_nodes), [5, 2, 1, 4]) self.assertEqual(list(self.r.path), [5, 3, 4]) self.r.compute_path(6, 4) self.assertEqual(list(self.r.path_nodes), [6, 3, 2, 1, 4]) self.assertEqual(list(self.r.path), [6, 2, 3, 4]) self.r.compute_path(6, 5) self.assertEqual(list(self.r.path_nodes), [6, 3, 2, 5]) self.assertEqual(list(self.r.path), [6, 2, 5]) self.r.compute_path(4, 6) self.assertEqual(list(self.r.path_nodes), [4, 1, 3, 6]) self.assertEqual(list(self.r.path), [4, 1, 6]) self.r.compute_path(5, 6) self.assertEqual(list(self.r.path_nodes), [5, 2, 1, 3, 6]) self.assertEqual(list(self.r.path), [5, 3, 1, 6]) def test_update_trace(self): self.r.compute_path(1, 2) self.assertEqual(list(self.r.path_nodes), [1, 3, 2]) self.assertEqual(list(self.r.path), [1, 2]) self.r.update_trace(3) self.assertEqual(list(self.r.path_nodes), [1, 3]) self.assertEqual(list(self.r.path), [1]) def test_update_blocking_trace(self): self.r.compute_path(4, 5) self.assertEqual(list(self.r.path_nodes), [4, 1, 3, 2, 5]) self.assertEqual(list(self.r.path), [4, 1, 2, 5]) self.r.update_trace(6) self.assertEqual(list(self.r.path_nodes), [4, 1, 3, 6]) self.assertEqual(list(self.r.path), [4, 1, 6])
class TestPathResults(TestCase): def setUp(self) -> None: # graph self.g = Graph() self.g.load_from_disk(test_graph) self.g.set_graph(cost_field="distance") self.r = PathResults() try: self.r.prepare(self.g) except Exception as err: self.fail("Path result preparation failed - {}".format(err.__str__())) def test_reset(self): self.r.compute_path(dest, origin) self.r.reset() self.assertEqual(self.r.path, None, 'Fail to reset the Path computation object') self.assertEqual(self.r.path_nodes, None, 'Fail to reset the Path computation object') self.assertEqual(self.r.path_link_directions, None, 'Fail to reset the Path computation object') self.assertEqual(self.r.milepost, None, 'Fail to reset the Path computation object') self.assertEqual(self.r.predecessors.max(), -1, 'Fail to reset the Path computation object') self.assertEqual(self.r.predecessors.min(), -1, 'Fail to reset the Path computation object') self.assertEqual(self.r.connectors.max(), -1, 'Fail to reset the Path computation object') self.assertEqual(self.r.connectors.min(), -1, 'Fail to reset the Path computation object') self.assertEqual(self.r.skims.max(), np.inf, 'Fail to reset the Path computation object') self.assertEqual(self.r.skims.min(), np.inf, 'Fail to reset the Path computation object') new_r = PathResults() with self.assertRaises(ValueError): new_r.reset() def test_compute_paths(self): path_computation(origin, dest, self.g, self.r) if list(self.r.path) != [53, 52, 13]: self.fail("Path computation failed. Wrong sequence of links") if list(self.r.path_nodes) != [5, 168, 166, 27]: self.fail("Path computation failed. Wrong sequence of path nodes") if list(self.r.milepost) != [0, 341, 1398, 2162]: self.fail("Path computation failed. Wrong milepost results") self.r.compute_path(origin, dest) if list(self.r.path) != [53, 52, 13]: self.fail("Path computation failed. Wrong sequence of links") if list(self.r.path_nodes) != [5, 168, 166, 27]: self.fail("Path computation failed. Wrong sequence of path nodes") if list(self.r.milepost) != [0, 341, 1398, 2162]: self.fail("Path computation failed. Wrong milepost results") if list(self.r.path_link_directions) != [-1, -1, -1]: self.fail("Path computation failed. Wrong link directions") self.r.compute_path(dest, origin) if list(self.r.path_link_directions) != [1, 1, 1]: self.fail("Path computation failed. Wrong link directions") def test_update_trace(self): self.r.compute_path(origin, dest - 1) self.r.update_trace(dest) if list(self.r.path) != [53, 52, 13]: self.fail("Path computation failed. Wrong sequence of links") if list(self.r.path_nodes) != [5, 168, 166, 27]: self.fail("Path computation failed. Wrong sequence of path nodes") if list(self.r.milepost) != [0, 341, 1398, 2162]: self.fail("Path computation failed. Wrong milepost results") if list(self.r.path_link_directions) != [-1, -1, -1]: self.fail("Path computation failed. Wrong link directions")
class TestPathResults(TestCase): def test_prepare(self): # graph self.g = Graph() self.g.load_from_disk(test_graph) self.g.set_graph(cost_field="distance") self.r = PathResults() try: self.r.prepare(self.g) except Exception as err: self.fail("Path result preparation failed - {}".format(err.__str__())) def test_reset(self): self.test_prepare() try: self.r.reset() except Exception as err: self.fail("Path result resetting failed - {}".format(err.__str__())) def test_compute_paths(self): self.test_prepare() try: self.r.reset() except Exception as err: self.fail("Path result resetting failed - {}".format(err.__str__())) path_computation(origin, dest, self.g, self.r) if list(self.r.path) != [53, 52, 13]: self.fail("Path computation failed. Wrong sequence of links") if list(self.r.path_nodes) != [5, 168, 166, 27]: self.fail("Path computation failed. Wrong sequence of path nodes") if list(self.r.milepost) != [0, 341, 1398, 2162]: self.fail("Path computation failed. Wrong milepost results") self.r.compute_path(origin, dest) if list(self.r.path) != [53, 52, 13]: self.fail("Path computation failed. Wrong sequence of links") if list(self.r.path_nodes) != [5, 168, 166, 27]: self.fail("Path computation failed. Wrong sequence of path nodes") if list(self.r.milepost) != [0, 341, 1398, 2162]: self.fail("Path computation failed. Wrong milepost results") def test_update_trace(self): self.test_prepare() try: self.r.reset() except Exception as err: self.fail("Path result resetting failed - {}".format(err.__str__())) self.r.compute_path(origin, dest - 1) self.r.update_trace(dest) if list(self.r.path) != [53, 52, 13]: self.fail("Path computation failed. Wrong sequence of links") if list(self.r.path_nodes) != [5, 168, 166, 27]: self.fail("Path computation failed. Wrong sequence of path nodes") if list(self.r.milepost) != [0, 341, 1398, 2162]: self.fail("Path computation failed. Wrong milepost results")