def loaded_new_graph_from_file(self): file_types = ["AequilibraE graph(*.aeg)"] new_name, file_type = GetOutputFileName(self, 'Graph file', file_types, ".aeg", self.path) self.cb_minimizing.clear() self.cb_skims.clear() self.all_centroids.setText('') self.block_paths.setChecked(False) if new_name is not None: self.graph_file_name.setText(new_name) self.graph = Graph() self.graph.load_from_disk(new_name) self.all_centroids.setText(str(self.graph.centroids)) if self.graph.block_centroid_flows: self.block_paths.setChecked(True) graph_fields = list(self.graph.graph.dtype.names) self.skimmeable_fields = [ x for x in graph_fields if x not in [ 'link_id', 'a_node', 'b_node', 'direction', 'id', ] ] for q in self.skimmeable_fields: self.cb_minimizing.addItem(q) self.cb_skims.addItem(q)
def load_graph(self): self.lbl_graphfile.setText('') file_types = ["AequilibraE graph(*.aeg)"] default_type = '.aeg' box_name = 'Traffic Assignment' graph_file, type = GetOutputFileName(self, box_name, file_types, default_type, self.path) if graph_file is not None: self.graph.load_from_disk(graph_file) not_considering_list = self.graph.required_default_fields not_considering_list.pop(-1) not_considering_list.append('id') for i in list(self.graph.graph.dtype.names): if i not in not_considering_list: self.minimizing_field.addItem(i) self.lbl_graphfile.setText(graph_file) self.results.prepare(self.graph) cores = get_parameter_chain(['system', 'cpus']) self.results.set_cores(cores) else: self.graph = Graph() self.change_status_for_path_file() self.set_behavior_special_analysis()
def test_network_skimming(self): # graph g = Graph() g.load_from_disk(test_graph) g.set_graph(cost_field='distance', skim_fields=None) # None implies that only the cost field will be skimmed # skimming results res = SkimResults() res.prepare(g) aux_res = MultiThreadedNetworkSkimming() aux_res.prepare(g, res) a = skimming_single_origin(26, g, res, aux_res, 0) skm = NetworkSkimming(g, res) skm.execute() tot = np.nanmax(res.skims.distance[:, :]) if tot > 10e10: self.fail('Skimming was not successful. At least one np.inf returned.') if skm.report: self.fail('Skimming returned an error:' + str(skm.report))
def test_prepare_graph(self): self.test_create_from_geography() self.graph.prepare_graph(centroids) reference_graph = Graph() reference_graph.load_from_disk(test_graph) if not np.array_equal(self.graph.graph, reference_graph.graph): self.fail('Reference graph and newly-prepared graph are not equal')
def test_prepare_graph(self): self.test_create_from_geography() self.graph.prepare_graph(centroids) reference_graph = Graph() reference_graph.load_from_disk(test_graph) if not np.array_equal(self.graph.graph, reference_graph.graph): self.fail("Reference graph and newly-prepared graph are not equal")
def load_graph(self): self.lbl_graphfile.setText('') file_types = ["AequilibraE graph(*.aeg)"] default_type = '.aeg' box_name = 'Traffic Assignment' graph_file, _ = GetOutputFileName(self, box_name, file_types, default_type, self.path) if graph_file is not None: self.graph.load_from_disk(graph_file) fields = list( set(self.graph.graph.dtype.names) - set(self.graph.required_default_fields)) self.minimizing_field.addItems(fields) self.update_skim_list(fields) self.lbl_graphfile.setText(graph_file) cores = get_parameter_chain(['system', 'cpus']) self.results.set_cores(cores) # show graph properties def centers_item(qt_item): cell_widget = QWidget() lay_out = QHBoxLayout(cell_widget) lay_out.addWidget(qt_item) lay_out.setAlignment(Qt.AlignCenter) lay_out.setContentsMargins(0, 0, 0, 0) cell_widget.setLayout(lay_out) return cell_widget items = [['Graph ID', self.graph.__id__], ['Number of links', self.graph.num_links], ['Number of nodes', self.graph.num_nodes], ['Number of centroids', self.graph.num_zones]] self.graph_properties_table.clearContents() self.graph_properties_table.setRowCount(5) for i, item in enumerate(items): self.graph_properties_table.setItem(i, 0, QTableWidgetItem(item[0])) self.graph_properties_table.setItem( i, 1, QTableWidgetItem(str(item[1]))) self.graph_properties_table.setItem( 4, 0, QTableWidgetItem('Block flows through centroids')) self.block_centroid_flows = QCheckBox() self.block_centroid_flows.setChecked( self.graph.block_centroid_flows) self.graph_properties_table.setCellWidget( 4, 1, centers_item(self.block_centroid_flows)) else: self.graph = Graph() self.set_behavior_special_analysis()
def test_prepare(self): # graph self.g = Graph() self.g.load_from_disk(test_graph) self.g.set_graph(cost_field='distance', skim_fields=None) self.r = PathResults() try: self.r.prepare(self.g) except: self.fail('Path result preparation failed')
def build_graphs(self) -> None: """Builds graphs for all modes currently available in the model When called, it overwrites all graphs previously created and stored in the networks' dictionary of graphs """ curr = self.conn.cursor() curr.execute('PRAGMA table_info(links);') field_names = curr.fetchall() ignore_fields = ['ogc_fid', 'geometry'] all_fields = [f[1] for f in field_names if f[1] not in ignore_fields] raw_links = curr.execute( f"select {','.join(all_fields)} from links").fetchall() links = [] for l in raw_links: lk = list(map(lambda x: np.nan if x is None else x, l)) links.append(lk) data = np.core.records.fromrecords(links, names=all_fields) valid_fields = [] removed_fields = [] for f in all_fields: if np.issubdtype(data[f].dtype, np.floating) or np.issubdtype( data[f].dtype, np.integer): valid_fields.append(f) else: removed_fields.append(f) if len(removed_fields) > 1: warn( f'Fields were removed form Graph for being non-numeric: {",".join(removed_fields)}' ) curr.execute('select node_id from nodes where is_centroid=1;') centroids = np.array([i[0] for i in curr.fetchall()], np.uint32) modes = curr.execute('select mode_id from modes;').fetchall() modes = [m[0] for m in modes] for m in modes: w = np.core.defchararray.find(data['modes'], m) net = np.array(data[valid_fields], copy=True) net['b_node'][w < 0] = net['a_node'][w < 0] g = Graph() g.mode = m g.network = net g.network_ok = True g.status = 'OK' g.prepare_graph(centroids) g.set_blocked_centroid_flows(True) self.graphs[m] = g
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_create_from_geography(self): self.graph = Graph() self.graph.create_from_geography(test_network, 'link_id', 'dir', 'distance', centroids=centroids, skim_fields=[], anode="A_NODE", bnode="B_NODE") self.graph.set_graph(cost_field='distance', block_centroid_flows=True)
def test_load_from_disk(self): self.test_save_to_disk() reference_graph = Graph() reference_graph.load_from_disk(test_graph) reference_graph.__version__ = binary_version new_graph = Graph() new_graph.load_from_disk(join(path_test, "aequilibrae_test_graph.aeg"))
def test_skimming_single_origin(self): g = Graph() g.load_from_disk(test_graph) g.set_graph(cost_field="distance") g.set_skimming("distance") origin = np.random.choice(g.centroids[:-1], 1)[0] # skimming results res = SkimResults() res.prepare(g) aux_result = MultiThreadedNetworkSkimming() aux_result.prepare(g, res) a = skimming_single_origin(origin, g, res, aux_result, 0) tot = np.sum(res.skims.distance[origin, :]) if tot > 10e10: self.fail( "Skimming was not successful. At least one np.inf returned for origin {}." .format(origin)) if a != origin: self.fail("Skimming returned an error: {} for origin {}".format( a, origin))
def test_network_skimming(self): # graph g = Graph() g.load_from_disk(test_graph) g.set_graph(cost_field="distance") g.set_skimming("distance") # skimming results res = SkimResults() res.prepare(g) aux_res = MultiThreadedNetworkSkimming() aux_res.prepare(g, res) _ = skimming_single_origin(26, g, res, aux_res, 0) skm = NetworkSkimming(g, res) skm.execute() tot = np.nanmax(res.skims.distance[:, :]) if tot > 10e10: self.fail("Skimming was not successful. At least one np.inf returned.") if skm.report: self.fail("Skimming returned an error:" + str(skm.report))
def __init__(self, iface): QDialog.__init__(self) QtGui.QDialog.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint) self.iface = iface self.setupUi(self) self.field_types = {} self.centroids = None self.node_layer = None self.line_layer = None self.index = None self.graph = Graph() self.skimmeable_fields = None self.link_features = None self.link_layer = None self.link_id = None self.node_layer = None self.node_id = None self.node_fields = None self.node_keys = None self.error = None self.graph_ok = False self.load_graph_from_file.clicked.connect( self.loaded_new_graph_from_file) self.cb_node_layer.currentIndexChanged.connect( partial(self.load_fields_to_ComboBoxes, self.cb_node_layer, self.cb_data_field, True)) self.cb_link_layer.currentIndexChanged.connect( partial(self.load_fields_to_ComboBoxes, self.cb_link_layer, self.cb_link_id_field, False)) self.cb_link_id_field.currentIndexChanged.connect( self.clear_memory_layer) self.do_load_graph.clicked.connect(self.returns_configuration) # THIRD, we load layers in the canvas to the combo-boxes for layer in qgis.utils.iface.legendInterface().layers( ): # We iterate through all layers if layer.wkbType() in point_types: self.cb_node_layer.addItem(layer.name()) if layer.wkbType() in line_types: self.cb_link_layer.addItem(layer.name()) # loads default path from parameters self.path = standard_path()
def test_create_from_geography(self): self.graph = Graph() self.graph.create_from_geography( test_network, "link_id", "dir", "distance", centroids=centroids, skim_fields=[], anode="A_NODE", bnode="B_NODE", ) self.graph.set_graph(cost_field="distance") self.graph.set_blocked_centroid_flows(block_centroid_flows=True) self.graph.set_skimming("distance")
def test_prepare(self): # graph self.g = Graph() self.g.load_from_disk(test_graph) self.g.set_graph(cost_field='distance', skim_fields=None) self.r = PathResults() try: self.r.prepare(self.g) except: self.fail('Path result preparation failed')
def test_load_from_disk(self): self.test_save_to_disk() reference_graph = Graph() reference_graph.load_from_disk(test_graph) new_graph = Graph() new_graph.load_from_disk(join(path_test, "aequilibrae_test_graph.aeg")) comparisons = [ ("Graph", new_graph.graph, reference_graph.graph), ("b_nodes", new_graph.b_node, reference_graph.b_node), ("Forward-Star", new_graph.fs, reference_graph.fs), ("cost", new_graph.cost, reference_graph.cost), ("centroids", new_graph.centroids, reference_graph.centroids), ("skims", new_graph.skims, reference_graph.skims), ("link ids", new_graph.ids, reference_graph.ids), ("Network", new_graph.network, reference_graph.network), ("All Nodes", new_graph.all_nodes, reference_graph.all_nodes), ( "Nodes to indices", new_graph.nodes_to_indices, reference_graph.nodes_to_indices, ), ] for comparison, newg, refg in comparisons: if not np.array_equal(newg, refg): self.fail( "Reference %s and %s created and saved to disk are not equal" % (comparison, comparison) ) comparisons = [ ("nodes", new_graph.num_nodes, reference_graph.num_nodes), ("links", new_graph.num_links, reference_graph.num_links), ("zones", new_graph.num_zones, reference_graph.num_zones), ( "block through centroids", new_graph.block_centroid_flows, reference_graph.block_centroid_flows, ), ("Graph ID", new_graph.__id__, self.graph_id), ("Graph Version", new_graph.__version__, self.graph_version), ] for comparison, newg, refg in comparisons: if newg != refg: self.fail( "Reference %s and %s created and saved to disk are not equal" % (comparison, comparison) )
def test_skimming_single_origin(self): origin = 1 # graph g = Graph() g.load_from_disk(test_graph) g.set_graph(cost_field="distance", skim_fields=None) # g.block_centroid_flows = False # None implies that only the cost field will be skimmed # skimming results res = SkimResults() res.prepare(g) aux_result = MultiThreadedNetworkSkimming() aux_result.prepare(g, res) a = skimming_single_origin(origin, g, res, aux_result, 0) tot = np.sum(res.skims.distance[origin, :]) if tot > 10e10: self.fail( "Skimming was not successful. At least one np.inf returned.") if a != origin: self.fail("Skimming returned an error: " + a)
def test_execute(self): # Loads and prepares the graph g = Graph() g.load_from_disk(test_graph) g.set_graph(cost_field='distance', skim_fields=None) # None implies that only the cost field will be skimmed # Prepares the matrix for assignment args = {'file_name': os.path.join(gettempdir(),'my_matrix.aem'), 'zones': g.num_zones, 'matrix_names': ['cars', 'trucks'], 'index_names': ['my indices']} matrix = AequilibraeMatrix() matrix.create_empty(**args) matrix.index[:] = g.centroids[:] matrix.cars.fill(1) matrix.trucks.fill(2) matrix.computational_view(['cars']) # Performs assignment res = AssignmentResults() res.prepare(g, matrix) assig = allOrNothing(matrix, g, res) assig.execute() res.save_to_disk(os.path.join(gettempdir(),'link_loads.aed')) res.save_to_disk(os.path.join(gettempdir(),'link_loads.csv')) matrix.computational_view() # Performs assignment res = AssignmentResults() res.prepare(g, matrix) assig = allOrNothing(matrix, g, res) assig.execute() res.save_to_disk(os.path.join(gettempdir(),'link_loads_2_classes.aed')) res.save_to_disk(os.path.join(gettempdir(),'link_loads_2_classes.csv'))
def test_load_from_disk(self): self.test_save_to_disk() reference_graph = Graph() reference_graph.load_from_disk(test_graph) new_graph = Graph() new_graph.load_from_disk(join(path_test, 'aequilibrae_test_graph.aeg')) comparisons = [('Graph', new_graph.graph, reference_graph.graph), ('b_nodes', new_graph.b_node, reference_graph.b_node), ('Forward-Star', new_graph.fs, reference_graph.fs), ('cost', new_graph.cost, reference_graph.cost), ('centroids', new_graph.centroids, reference_graph.centroids), ('skims', new_graph.skims, reference_graph.skims), ('link ids', new_graph.ids, reference_graph.ids), ('Network', new_graph.network, reference_graph.network), ('All Nodes', new_graph.all_nodes, reference_graph.all_nodes), ('Nodes to indices', new_graph.nodes_to_indices, reference_graph.nodes_to_indices)] for comparison, newg, refg in comparisons: if not np.array_equal(newg, refg): self.fail('Reference %s and %s created and saved to disk are not equal' %(comparison, comparison)) comparisons = [('nodes', new_graph.num_nodes, reference_graph.num_nodes), ('links', new_graph.num_links, reference_graph.num_links), ('zones', new_graph.num_zones, reference_graph.num_zones), ('block through centroids', new_graph.block_centroid_flows, reference_graph.block_centroid_flows), ('Graph ID', new_graph.__id__, self.graph_id), ('Graph Version', new_graph.__version__, self.graph_version)] for comparison, newg, refg in comparisons: if newg != refg: self.fail('Reference %s and %s created and saved to disk are not equal' %(comparison, comparison))
def test_set_pce(self): mat_name = AequilibraeMatrix().random_name() g = Graph() g.load_from_disk(test_graph) g.set_graph(cost_field="distance") # Creates the matrix for assignment args = { "file_name": os.path.join(gettempdir(), mat_name), "zones": g.num_zones, "matrix_names": ["cars", "trucks"], "index_names": ["my indices"], } matrix = AequilibraeMatrix() matrix.create_empty(**args) matrix.index[:] = g.centroids[:] matrix.cars.fill(1.1) matrix.trucks.fill(2.2) matrix.computational_view() tc = TrafficClass(graph=g, matrix=matrix) self.assertIsInstance(tc.results, AssignmentResults, 'Results have the wrong type') self.assertIsInstance(tc._aon_results, AssignmentResults, 'Results have the wrong type') with self.assertRaises(ValueError): tc.set_pce('not a number') tc.set_pce(1) tc.set_pce(3.9)
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_update_trace(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")
def loaded_new_graph_from_file(self): file_types = ["AequilibraE graph(*.aeg)"] new_name, file_type = GetOutputFileName(self, 'Graph file', file_types, ".aeg", self.path) self.cb_minimizing.clear() self.available_skims_table.clearContents() self.block_paths.setChecked(False) self.graph = None if new_name is not None: self.graph_file_name.setText(new_name) self.graph = Graph() self.graph.load_from_disk(new_name) self.block_paths.setChecked(self.graph.block_centroid_flows) graph_fields = list(self.graph.graph.dtype.names) self.skimmeable_fields = self.graph.available_skims() self.available_skims_table.setRowCount(len(self.skimmeable_fields)) for q in self.skimmeable_fields: self.cb_minimizing.addItem(q) self.available_skims_table.setItem(0, 0, QTableWidgetItem(q))
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', skim_fields=None) self.r = PathResults() try: self.r.prepare(self.g) except: self.fail('Path result preparation failed') def test_reset(self): self.test_prepare() try: self.r.reset() except: self.fail('Path result resetting failed') def test_update_trace(self): self.test_prepare() try: self.r.reset() except: self.fail('Path result resetting failed') 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')
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', skim_fields=None) self.r = PathResults() try: self.r.prepare(self.g) except: self.fail('Path result preparation failed') def test_reset(self): self.test_prepare() try: self.r.reset() except: self.fail('Path result resetting failed') def test_update_trace(self): self.test_prepare() try: self.r.reset() except: self.fail('Path result resetting failed') 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')
def loaded_new_graph_from_file(self): file_types = "AequilibraE graph(*.aeg)" if len(self.graph_file_name.text()) > 0: newname = QFileDialog.getOpenFileName(None, 'Result file', self.graph_file_name.text(), file_types) else: newname = QFileDialog.getOpenFileName(None, 'Result file', self.path, file_types) self.cb_minimizing.clear() self.cb_skims.clear() self.all_centroids.setText('') self.block_paths.setChecked(False) if newname is not None: self.graph_file_name.setText(newname) self.graph = Graph() self.graph.load_from_disk(newname) self.all_centroids.setText(str(self.graph.centroids)) if self.graph.block_centroid_flows: self.block_paths.setChecked(True) graph_fields = list(self.graph.graph.dtype.names) self.skimmeable_fields = [ x for x in graph_fields if x not in [ 'link_id', 'a_node', 'b_node', 'direction', 'id', ] ] for q in self.skimmeable_fields: self.cb_minimizing.addItem(q) self.cb_skims.addItem(q)
def setUp(self) -> None: self.mat_name = AequilibraeMatrix().random_name() self.g = Graph() self.g.load_from_disk(test_graph) self.g.set_graph(cost_field="distance") # Creates the matrix for assignment args = { "file_name": os.path.join(gettempdir(), self.mat_name), "zones": self.g.num_zones, "matrix_names": ["cars", "trucks"], "index_names": ["my indices"], } matrix = AequilibraeMatrix() matrix.create_empty(**args) matrix.index[:] = self.g.centroids[:] matrix.cars.fill(1.1) matrix.trucks.fill(2.2) # Exports matrix to OMX in order to have two matrices to work with matrix.export(os.path.join(gettempdir(), "my_matrix.omx")) matrix.close()
def test_load_from_disk(self): self.test_save_to_disk() reference_graph = Graph() reference_graph.load_from_disk(test_graph) new_graph = Graph() new_graph.load_from_disk(join(path_test, 'aequilibrae_test_graph.aeg')) comparisons = [ ('Graph', new_graph.graph, reference_graph.graph), ('b_nodes', new_graph.b_node, reference_graph.b_node), ('Forward-Star', new_graph.fs, reference_graph.fs), ('cost', new_graph.cost, reference_graph.cost), ('centroids', new_graph.centroids, reference_graph.centroids), ('skims', new_graph.skims, reference_graph.skims), ('link ids', new_graph.ids, reference_graph.ids), ('Network', new_graph.network, reference_graph.network), ('All Nodes', new_graph.all_nodes, reference_graph.all_nodes), ('Nodes to indices', new_graph.nodes_to_indices, reference_graph.nodes_to_indices) ] for comparison, newg, refg in comparisons: if not np.array_equal(newg, refg): self.fail( 'Reference %s and %s created and saved to disk are not equal' % (comparison, comparison)) comparisons = [ ('nodes', new_graph.num_nodes, reference_graph.num_nodes), ('links', new_graph.num_links, reference_graph.num_links), ('zones', new_graph.num_zones, reference_graph.num_zones), ('block through centroids', new_graph.block_centroid_flows, reference_graph.block_centroid_flows), ('Graph ID', new_graph.__id__, self.graph_id), ('Graph Version', new_graph.__version__, self.graph_version) ] for comparison, newg, refg in comparisons: if newg != refg: self.fail( 'Reference %s and %s created and saved to disk are not equal' % (comparison, comparison))
def assign_matrix(self, matrix: AequilibraeMatrix, result_name: str): conn = database_connection() sql = f"select link_id, direction, a_node, b_node, distance, 1 capacity from {DELAUNAY_TABLE}" df = pd.read_sql(sql, conn) centroids = np.array(np.unique(np.hstack((df.a_node.values, df.b_node.values))), int) g = Graph() g.mode = 'delaunay' g.network = df g.prepare_graph(centroids) g.set_blocked_centroid_flows(True) tc = TrafficClass('delaunay', g, matrix) ta = TrafficAssignment() ta.set_classes([tc]) ta.set_time_field('distance') ta.set_capacity_field('capacity') ta.set_vdf('BPR') ta.set_vdf_parameters({"alpha": 0, "beta": 1.0}) ta.set_algorithm('all-or-nothing') ta.execute() report = {"setup": str(ta.info())} data = [result_name, "Delaunay assignment", self.procedure_id, str(report), ta.procedure_date, ''] conn.execute("""Insert into results(table_name, procedure, procedure_id, procedure_report, timestamp, description) Values(?,?,?,?,?,?)""", data) conn.commit() conn.close() cols = [] for x in matrix.view_names: cols.extend([f'{x}_ab', f'{x}_ba', f'{x}_tot']) df = ta.results()[cols] conn = sqlite3.connect(join(environ[ENVIRON_VAR], "results_database.sqlite")) df.to_sql(result_name, conn) conn.close()
def test_execute(self): # Loads and prepares the graph g = Graph() g.load_from_disk(test_graph) g.set_graph(cost_field='distance', skim_fields=None) # None implies that only the cost field will be skimmed # Prepares the matrix for assignment args = { 'file_name': '/tmp/my_matrix.aem', 'zones': g.num_zones, 'matrix_names': ['cars', 'trucks'], 'index_names': ['my indices'] } matrix = AequilibraeMatrix() matrix.create_empty(**args) matrix.index[:] = g.centroids[:] matrix.cars.fill(1) matrix.trucks.fill(2) matrix.computational_view(['cars']) # Performs assignment res = AssignmentResults() res.prepare(g, matrix) assig = allOrNothing(matrix, g, res) assig.execute() res.save_to_disk('/tmp/link_loads.aed') res.save_to_disk('/tmp/link_loads.csv') matrix.computational_view() # Performs assignment res = AssignmentResults() res.prepare(g, matrix) assig = allOrNothing(matrix, g, res) assig.execute() res.save_to_disk('/tmp/link_loads_2_classes.aed') res.save_to_disk('/tmp/link_loads_2_classes.csv')
def test_execute(self): # Loads and prepares the graph g = Graph() g.load_from_disk(test_graph) g.set_graph(cost_field="distance", skim_fields=None) # None implies that only the cost field will be skimmed # Prepares the matrix for assignment args = { "file_name": os.path.join(gettempdir(), "my_matrix.aem"), "zones": g.num_zones, "matrix_names": ["cars", "trucks"], "index_names": ["my indices"], } matrix = AequilibraeMatrix() matrix.create_empty(**args) matrix.index[:] = g.centroids[:] matrix.cars.fill(1) matrix.trucks.fill(2) matrix.computational_view(["cars"]) # Performs assignment res = AssignmentResults() res.prepare(g, matrix) assig = allOrNothing(matrix, g, res) assig.execute() res.save_to_disk(os.path.join(gettempdir(), "link_loads.aed")) res.save_to_disk(os.path.join(gettempdir(), "link_loads.csv")) matrix.computational_view() # Performs assignment res = AssignmentResults() res.prepare(g, matrix) assig = allOrNothing(matrix, g, res) assig.execute() res.save_to_disk(os.path.join(gettempdir(), "link_loads_2_classes.aed")) res.save_to_disk(os.path.join(gettempdir(), "link_loads_2_classes.csv"))
class TestGraph(TestCase): def test_create_from_geography(self): self.graph = Graph() self.graph.create_from_geography( test_network, 'link_id', 'dir', 'distance', centroids=centroids, skim_fields = [], anode="A_NODE", bnode="B_NODE") self.graph.set_graph(cost_field='distance', block_centroid_flows=True) def test_load_network_from_csv(self): pass def test_prepare_graph(self): self.test_create_from_geography() self.graph.prepare_graph(centroids) reference_graph = Graph() reference_graph.load_from_disk(test_graph) if not np.array_equal(self.graph.graph, reference_graph.graph): self.fail('Reference graph and newly-prepared graph are not equal') def test_set_graph(self): self.test_prepare_graph() self.graph.set_graph(cost_field='distance',block_centroid_flows=True) if self.graph.num_zones != centroids.shape[0]: self.fail('Number of centroids not properly set') if self.graph.num_links != 222: self.fail('Number of links not properly set') if self.graph.num_nodes != 93: self.fail('Number of nodes not properly set - ' + str(self.graph.num_nodes)) def test_save_to_disk(self): self.test_create_from_geography() self.graph.save_to_disk(join(path_test, 'aequilibrae_test_graph.aeg')) self.graph_id = self.graph.__id__ self.graph_version = self.graph.__version__ def test_load_from_disk(self): self.test_save_to_disk() reference_graph = Graph() reference_graph.load_from_disk(test_graph) new_graph = Graph() new_graph.load_from_disk(join(path_test, 'aequilibrae_test_graph.aeg')) comparisons = [('Graph', new_graph.graph, reference_graph.graph), ('b_nodes', new_graph.b_node, reference_graph.b_node), ('Forward-Star', new_graph.fs, reference_graph.fs), ('cost', new_graph.cost, reference_graph.cost), ('centroids', new_graph.centroids, reference_graph.centroids), ('skims', new_graph.skims, reference_graph.skims), ('link ids', new_graph.ids, reference_graph.ids), ('Network', new_graph.network, reference_graph.network), ('All Nodes', new_graph.all_nodes, reference_graph.all_nodes), ('Nodes to indices', new_graph.nodes_to_indices, reference_graph.nodes_to_indices)] for comparison, newg, refg in comparisons: if not np.array_equal(newg, refg): self.fail('Reference %s and %s created and saved to disk are not equal' %(comparison, comparison)) comparisons = [('nodes', new_graph.num_nodes, reference_graph.num_nodes), ('links', new_graph.num_links, reference_graph.num_links), ('zones', new_graph.num_zones, reference_graph.num_zones), ('block through centroids', new_graph.block_centroid_flows, reference_graph.block_centroid_flows), ('Graph ID', new_graph.__id__, self.graph_id), ('Graph Version', new_graph.__version__, self.graph_version)] for comparison, newg, refg in comparisons: if newg != refg: self.fail('Reference %s and %s created and saved to disk are not equal' %(comparison, comparison)) def test_reset_single_fields(self): pass def test_add_single_field(self): pass def test_available_skims(self): self.test_set_graph() if self.graph.available_skims() != ['distance']: self.fail('Skim availability with problems')
def test_create_from_geography(self): self.graph = Graph() self.graph.create_from_geography( test_network, 'link_id', 'dir', 'distance', centroids=centroids, skim_fields = [], anode="A_NODE", bnode="B_NODE") self.graph.set_graph(cost_field='distance', block_centroid_flows=True)
class TrafficAssignmentDialog(QtWidgets.QDialog, FORM_CLASS): def __init__(self, iface): QtWidgets.QDialog.__init__(self) self.iface = iface self.setupUi(self) self.path = standard_path() self.output_path = None self.temp_path = None self.error = None self.report = None self.method = {} self.matrices = OrderedDict() self.skims = [] self.matrix = None self.graph = Graph() self.results = AssignmentResults() self.block_centroid_flows = None self.worker_thread = None # Signals for the matrix_procedures tab self.but_load_new_matrix.clicked.connect(self.find_matrices) # Signals from the Network tab self.load_graph_from_file.clicked.connect(self.load_graph) # Signals for the algorithm tab self.progressbar0.setVisible(False) self.progressbar0.setValue(0) self.progress_label0.setVisible(False) self.do_assignment.clicked.connect(self.run) self.cancel_all.clicked.connect(self.exit_procedure) self.select_output_folder.clicked.connect(self.choose_folder_for_outputs) self.cb_choose_algorithm.addItem('All-Or-Nothing') self.cb_choose_algorithm.currentIndexChanged.connect(self.changing_algorithm) # slots for skim tab self.but_build_query.clicked.connect(partial(self.build_query, 'select link')) self.changing_algorithm() # path file self.path_file = OutputType() # Queries tables = [self.select_link_list, self.list_link_extraction] for table in tables: table.setColumnWidth(0, 280) table.setColumnWidth(1, 40) table.setColumnWidth(2, 150) table.setColumnWidth(3, 40) self.graph_properties_table.setColumnWidth(0, 190) self.graph_properties_table.setColumnWidth(1, 240) # critical link self.but_build_query.clicked.connect(partial(self.build_query, 'select link')) self.do_select_link.stateChanged.connect(self.set_behavior_special_analysis) self.tot_crit_link_queries = 0 self.critical_output = OutputType() # link flow extraction self.but_build_query_extract.clicked.connect(partial(self.build_query, 'Link flow extraction')) self.do_extract_link_flows.stateChanged.connect(self.set_behavior_special_analysis) self.tot_link_flow_extract = 0 self.link_extract = OutputType() # Disabling resources not yet implemented self.do_select_link.setEnabled(False) self.but_build_query.setEnabled(False) self.select_link_list.setEnabled(False) self.skim_list_table.setEnabled(False) self.do_extract_link_flows.setEnabled(False) self.but_build_query_extract.setEnabled(False) self.list_link_extraction.setEnabled(False) self.new_matrix_to_assign() self.table_matrix_list.setColumnWidth(0, 135) self.table_matrix_list.setColumnWidth(1, 135) self.table_matrices_to_assign.setColumnWidth(0, 125) self.table_matrices_to_assign.setColumnWidth(1, 125) self.skim_list_table.setColumnWidth(0, 70) self.skim_list_table.setColumnWidth(1, 490) def choose_folder_for_outputs(self): new_name = GetOutputFolderName(self.path, 'Output folder for traffic assignment') if new_name: self.output_path = new_name self.lbl_output.setText(new_name) else: self.output_path = None self.lbl_output.setText(new_name) def load_graph(self): self.lbl_graphfile.setText('') file_types = ["AequilibraE graph(*.aeg)"] default_type = '.aeg' box_name = 'Traffic Assignment' graph_file, _ = GetOutputFileName(self, box_name, file_types, default_type, self.path) if graph_file is not None: self.graph.load_from_disk(graph_file) fields = list(set(self.graph.graph.dtype.names) - set(self.graph.required_default_fields)) self.minimizing_field.addItems(fields) self.update_skim_list(fields) self.lbl_graphfile.setText(graph_file) cores = get_parameter_chain(['system', 'cpus']) self.results.set_cores(cores) # show graph properties def centers_item(qt_item): cell_widget = QWidget() lay_out = QHBoxLayout(cell_widget) lay_out.addWidget(qt_item) lay_out.setAlignment(Qt.AlignCenter) lay_out.setContentsMargins(0, 0, 0, 0) cell_widget.setLayout(lay_out) return cell_widget items = [['Graph ID', self.graph.__id__], ['Number of links', self.graph.num_links], ['Number of nodes', self.graph.num_nodes], ['Number of centroids', self.graph.num_zones]] self.graph_properties_table.clearContents() self.graph_properties_table.setRowCount(5) for i, item in enumerate(items): self.graph_properties_table.setItem(i, 0, QTableWidgetItem(item[0])) self.graph_properties_table.setItem(i, 1, QTableWidgetItem(str(item[1]))) self.graph_properties_table.setItem(4, 0, QTableWidgetItem('Block flows through centroids')) self.block_centroid_flows = QCheckBox() self.block_centroid_flows.setChecked(self.graph.block_centroid_flows) self.graph_properties_table.setCellWidget(4, 1, centers_item(self.block_centroid_flows)) else: self.graph = Graph() self.set_behavior_special_analysis() def changing_algorithm(self): if self.cb_choose_algorithm.currentText() == 'All-Or-Nothing': self.method['algorithm'] = 'AoN' def run_thread(self): self.worker_thread.assignment.connect(self.signal_handler) # QObject.connect(self.worker_thread, SIGNAL("assignment"), self.signal_handler) self.worker_thread.start() self.exec_() def job_finished_from_thread(self): self.report = self.worker_thread.report self.produce_all_outputs() self.exit_procedure() def run(self): if self.check_data(): self.set_output_names() self.progress_label0.setVisible(True) self.progressbar0.setVisible(True) self.progressbar0.setRange(0, self.graph.num_zones) try: if self.method['algorithm'] == 'AoN': self.worker_thread = allOrNothing(self.matrix, self.graph, self.results) self.run_thread() except ValueError as error: qgis.utils.iface.messageBar().pushMessage("Input error", error.message, level=3) else: qgis.utils.iface.messageBar().pushMessage("Input error", self.error, level=3) def set_output_names(self): self.path_file.temp_file = os.path.join(self.temp_path, 'path_file.aed') self.path_file.output_name = os.path.join(self.output_path, 'path_file') self.path_file.extension = 'aed' if self.do_path_file.isChecked(): self.results.setSavePathFile(save=True, path_result=self.path_file.temp_file) self.link_extract.temp_file = os.path.join(self.temp_path, 'link_extract') self.link_extract.output_name = os.path.join(self.output_path, 'link_extract') self.link_extract.extension = 'aed' self.critical_output.temp_file = os.path.join(self.temp_path, 'critical_output') self.critical_output.output_name = os.path.join(self.output_path, 'critical_output') self.critical_output.extension = 'aed' def check_data(self): self.error = None self.change_graph_settings() if not self.graph.num_links: self.error = 'Graph was not loaded' return False self.matrix = None if not self.prepare_assignable_matrices(): return False if self.matrix is None: self.error = 'Demand matrix missing' return False if self.output_path is None: self.error = 'Parameters for output missing' return False self.temp_path = os.path.join(self.output_path, 'temp') if not os.path.exists(self.temp_path): os.makedirs(self.temp_path) self.results.prepare(self.graph, self.matrix) return True def load_assignment_queries(self): # First we load the assignment queries query_labels = [] query_elements = [] query_types = [] if self.tot_crit_link_queries: for i in range(self.tot_crit_link_queries): links = eval(self.select_link_list.item(i, 0).text()) query_type = self.select_link_list.item(i, 1).text() query_name = self.select_link_list.item(i, 2).text() for l in links: d = directions_dictionary[l[1]] lk = self.graph.ids[(self.graph.graph['link_id'] == int(l[0])) & (self.graph.graph['direction'] == d)] query_labels.append(query_name) query_elements.append(lk) query_types.append(query_type) self.critical_queries = {'labels': query_labels, 'elements': query_elements, ' type': query_types} def signal_handler(self, val): if val[0] == 'zones finalized': self.progressbar0.setValue(val[1]) elif val[0] == 'text AoN': self.progress_label0.setText(val[1]) elif val[0] == 'finished_threaded_procedure': self.job_finished_from_thread() # TODO: Write code to export skims def produce_all_outputs(self): extension = 'aed' if not self.do_output_to_aequilibrae.isChecked(): extension = 'csv' if self.do_output_to_sqlite.isChecked(): extension = 'sqlite' # Save link flows to disk self.results.save_to_disk(os.path.join(self.output_path, 'link_flows.' + extension), output='loads') # save Path file if that is the case if self.do_path_file.isChecked(): if self.method['algorithm'] == 'AoN': if self.do_output_to_sqlite.isChecked(): self.results.save_to_disk(file_name=os.path.join(self.output_path, 'path_file.' + extension), output='path_file') # Saves output skims if self.skim_list_table.rowCount() > 0: self.results.skims.copy(os.path.join(self.output_path, 'skims.aem')) # if self.do_select_link.isChecked(): # if self.method['algorithm'] == 'AoN': # del(self.results.critical_links['results']) # self.results.critical_links = None # # shutil.move(self.critical_output.temp_file + '.aep', self.critical_output.output_name) # shutil.move(self.critical_output.temp_file + '.aed', self.critical_output.output_name[:-3] + 'aed') # # if self.do_extract_link_flows.isChecked(): # if self.method['algorithm'] == 'AoN': # del(self.results.link_extraction['results']) # self.results.link_extraction = None # # shutil.move(self.link_extract.temp_file + '.aep', self.link_extract.output_name) # shutil.move(self.link_extract.temp_file + '.aed', self.link_extract.output_name[:-3] + 'aed') # Procedures related to critical analysis. Not yet fully implemented def build_query(self, purpose): if purpose == 'select link': button = self.but_build_query message = 'Select Link Analysis' table = self.select_link_list counter = self.tot_crit_link_queries else: button = self.but_build_query_extract message = 'Link flow extraction' table = self.list_link_extraction counter = self.tot_link_flow_extract button.setEnabled(False) dlg2 = LoadSelectLinkQueryBuilderDialog(self.iface, self.graph.graph, message) dlg2.exec_() if dlg2.links is not None: table.setRowCount(counter + 1) text = '' for i in dlg2.links: text = text + ', (' + only_str(i[0]) + ', "' + only_str(i[1]) + '")' text = text[2:] table.setItem(counter, 0, QTableWidgetItem(text)) table.setItem(counter, 1, QTableWidgetItem(dlg2.query_type)) table.setItem(counter, 2, QTableWidgetItem(dlg2.query_name)) del_button = QPushButton('X') del_button.clicked.connect(partial(self.click_button_inside_the_list, purpose)) table.setCellWidget(counter, 3, del_button) counter += 1 if purpose == 'select link': self.tot_crit_link_queries = counter elif purpose == 'Link flow extraction': self.tot_link_flow_extract = counter button.setEnabled(True) def click_button_inside_the_list(self, purpose): if purpose == 'select link': table = self.select_link_list else: table = self.list_link_extraction button = self.sender() index = self.select_link_list.indexAt(button.pos()) row = index.row() table.removeRow(row) if purpose == 'select link': self.tot_crit_link_queries -= 1 elif purpose == 'Link flow extraction': self.tot_link_flow_extract -= 1 def set_behavior_special_analysis(self): if self.graph.num_links < 1: behavior = False else: behavior = True self.do_path_file.setEnabled(behavior) # This line of code turns off the features of select link analysis and link flow extraction while these # features are still being developed behavior = False self.do_select_link.setEnabled(behavior) self.do_extract_link_flows.setEnabled(behavior) self.but_build_query.setEnabled(behavior * self.do_select_link.isChecked()) self.select_link_list.setEnabled(behavior * self.do_select_link.isChecked()) self.list_link_extraction.setEnabled(behavior * self.do_extract_link_flows.isChecked()) self.but_build_query_extract.setEnabled(behavior * self.do_extract_link_flows.isChecked()) def update_skim_list(self, skims): self.skim_list_table.clearContents() self.skim_list_table.setRowCount(len(skims)) for i, skm in enumerate(skims): self.skim_list_table.setItem(i, 1, QTableWidgetItem(skm)) chb = QCheckBox() my_widget = QWidget() lay_out = QHBoxLayout(my_widget) lay_out.addWidget(chb) lay_out.setAlignment(Qt.AlignCenter) lay_out.setContentsMargins(0, 0, 0, 0) my_widget.setLayout(lay_out) self.skim_list_table.setCellWidget(i, 0, my_widget) # All Matrix loading and assignables selection def update_matrix_list(self): self.table_matrix_list.clearContents() self.table_matrix_list.clearContents() self.table_matrix_list.setEditTriggers(QAbstractItemView.NoEditTriggers) self.table_matrix_list.setRowCount(len(self.matrices.keys())) for i, data_name in enumerate(self.matrices.keys()): self.table_matrix_list.setItem(i, 0, QTableWidgetItem(data_name)) cbox = QComboBox() for idx in self.matrices[data_name].index_names: cbox.addItem(str(idx)) self.table_matrix_list.setCellWidget(i, 1, cbox) def find_matrices(self): dlg2 = LoadMatrixDialog(self.iface) dlg2.show() dlg2.exec_() if dlg2.matrix is not None: matrix_name = dlg2.matrix.file_path matrix_name = os.path.splitext(os.path.basename(matrix_name))[0] matrix_name = self.find_non_conflicting_name(matrix_name, self.matrices) self.matrices[matrix_name] = dlg2.matrix self.update_matrix_list() row_count = self.table_matrices_to_assign.rowCount() new_matrix = list(self.matrices.keys())[-1] for i in range(row_count): cb = self.table_matrices_to_assign.cellWidget(i, 0) cb.insertItem(-1, new_matrix) def find_non_conflicting_name(self, data_name, dictio): if data_name in dictio: i = 1 new_data_name = data_name + '_' + str(i) while new_data_name in dictio: i += 1 new_data_name = data_name + '_' + str(i) data_name = new_data_name return data_name def changed_assignable_matrix(self, mi): chb = self.sender() mat_name = chb.currentText() table = self.table_matrices_to_assign for row in range(table.rowCount()): if table.cellWidget(row, 0) == chb: break if len(mat_name) == 0: if row + 1 < table.rowCount(): self.table_matrices_to_assign.removeRow(row) else: mat_cores = self.matrices[mat_name].names cbox2 = QComboBox() cbox2.addItems(mat_cores) self.table_matrices_to_assign.setCellWidget(row, 1, cbox2) if row + 1 == table.rowCount(): self.new_matrix_to_assign() def new_matrix_to_assign(self): # We edit ALL the combo boxes to have the current list of matrices row_count = self.table_matrices_to_assign.rowCount() self.table_matrices_to_assign.setRowCount(row_count + 1) cbox = QComboBox() cbox.addItems(list(self.matrices.keys())) cbox.addItem('') cbox.setCurrentIndex(cbox.count() - 1) cbox.currentIndexChanged.connect(self.changed_assignable_matrix) self.table_matrices_to_assign.setCellWidget(row_count, 0, cbox) def prepare_assignable_matrices(self): table = self.table_matrices_to_assign idx = self.graph.centroids mat_names = [] if table.rowCount() > 1: for row in range(table.rowCount() - 1): mat = table.cellWidget(row, 0).currentText() core = table.cellWidget(row, 1).currentText() mat_index = self.matrices[mat].index if not np.array_equal(idx, mat_index): no_zones = [item for item in mat_index if item not in idx] # We only return an error if the matrix has too many centroids if no_zones: self.error = 'Assignable matrix has centroids that do not exist in the network: {}'.format( ','.join([str(x) for x in no_zones])) return False if core in mat_names: self.error = 'Assignable matrices cannot have same names' return False mat_names.append(only_str(core)) self.matrix = AequilibraeMatrix() self.matrix.create_empty(file_name=self.matrix.random_name(), zones=idx.shape[0], matrix_names=mat_names) self.matrix.index[:] = idx[:] for row in range(table.rowCount() - 1): mat = table.cellWidget(row, 0).currentText() core = table.cellWidget(row, 1).currentText() src_mat = self.matrices[mat].matrix[core] dest_mat = self.matrix.matrix[core] rows = src_mat.shape[0] cols = src_mat.shape[1] dest_mat[:rows, :cols] = src_mat[:, :] # Inserts cols and rows that don;t exist if rows != self.matrix.zones: src_index = list(self.matrices[mat].index[:]) for i, row in enumerate(idx): if row not in src_index: dest_mat[i + 1:, :] = dest_mat[i:-1, :] dest_mat[i, :] = 0 if cols != self.matrix.zones: for j, col in enumerate(idx): if col not in src_index: dest_mat[:, j + 1:] = dest_mat[:, j:-1] dest_mat[:, j] = 0 self.matrix.computational_view() else: self.error = 'You need to have at least one matrix to assign' return False return True def change_graph_settings(self): skims = [] table = self.skim_list_table for i in range(table.rowCount()): for chb in table.cellWidget(i, 0).findChildren(QCheckBox): if chb.isChecked(): skims.append(only_str(table.item(i, 1).text())) if len(skims) == 0: skims = False self.graph.set_graph(cost_field=self.minimizing_field.currentText(), skim_fields=skims, block_centroid_flows=self.block_centroid_flows.isChecked()) def exit_procedure(self): self.close() if self.report: dlg2 = ReportDialog(self.iface, self.report) dlg2.show() dlg2.exec_()
def doWork(self): if self.selected_only: self.features = self.net_layer.selectedFeatures() self.feat_count = self.net_layer.selectedFeatureCount() else: self.features = self.net_layer.getFeatures() self.feat_count = self.net_layer.featureCount() # Checking ID uniqueness self.report.append(reporter("Checking ID uniqueness", 0)) self.emit(SIGNAL("ProgressText ( PyQt_PyObject )"),"Checking ID uniqueness. Please wait") all_ids = self.net_layer.uniqueValues(self.link_id) if NULL in all_ids: self.error = "ID field has NULL values" self.report.append(self.error) else: if len(all_ids) < self.feat_count: self.error = 'IDs are not unique.' self.report.append(self.error) if self.error is None: self.report.append(reporter('Loading data from layer', 0)) self.emit(SIGNAL("ProgressText ( PyQt_PyObject )"),"Loading data from layer") self.emit(SIGNAL("ProgressValue( PyQt_PyObject )"), 0) self.emit(SIGNAL("ProgressMaxValue( PyQt_PyObject )"), self.feat_count) self.graph = Graph() all_types = [np.int32, np.int32, np.int32, np.int8] all_titles = [reserved_fieds.link_id, reserved_fieds.a_node, reserved_fieds.b_node, reserved_fieds.direction] for name_field, values in self.fields_to_add.iteritems(): all_titles.append((name_field + '_ab').encode('ascii','ignore')) all_types.append(np.float64) all_titles.append((name_field + '_ba').encode('ascii','ignore')) all_types.append(np.float64) dt = [(t, d) for t, d in zip(all_titles, all_types)] a_node = self.net_layer.fieldNameIndex(reserved_fieds.a_node) b_node = self.net_layer.fieldNameIndex(reserved_fieds.b_node) data = [] for p, feat in enumerate(self.features): line = [] line.append(feat.attributes()[self.link_id]) line.append(feat.attributes()[a_node]) line.append(feat.attributes()[b_node]) if self.bi_directional: line.append(feat.attributes()[self.direction_field]) else: line.append(1) # We append the data fields now for k, v in self.fields_to_add.iteritems(): a, b = v line.append(feat.attributes()[a]) if self.bi_directional: line.append(feat.attributes()[b]) else: line.append(-1) for k in line: if k == NULL: t = ','.join([str(x) for x in line]) self.error = 'Field with NULL value - ID:' + str(line[0]) + " / " + t break if self.error is not None: break data.append(line) if p % 50 == 0: self.emit(SIGNAL("ProgressValue( PyQt_PyObject )"), p) if self.error is None: self.report.append(reporter('Converting data to graph', 0)) network = np.asarray(data) self.graph.network = np.zeros(network.shape[0], dtype=dt) for k, t in enumerate(dt): self.graph.network[t[0]] = network[:,k].astype(t[1]) del network self.graph.type_loaded = 'NETWORK' self.graph.status = 'OK' self.graph.network_ok = True self.graph.prepare_graph() self.graph.__source__ = None self.graph.__field_name__ = None self.graph.__layer_name__ = None self.report.append(reporter('Process finished', 0)) self.emit(SIGNAL("finished_threaded_procedure( PyQt_PyObject )"), None)
def doWork(self): # Checking ID uniqueness self.emit(SIGNAL("ProgressText ( PyQt_PyObject )"), "Checking ID uniqueness") self.emit(SIGNAL("ProgressMaxValue( PyQt_PyObject )"), self.featcount) a = [] all_ids = np.zeros(self.featcount, dtype=np.int_) if self.selected_only: self.features = self.netlayer.selectedFeatures() else: self.features = self.netlayer.getFeatures() p = 0 for feat in self.features: k = feat.attributes()[self.linkid] if k == NULL: self.error = "ID field has NULL values" break else: all_ids[p] = k p += 1 if p % 50 == 0: self.emit(SIGNAL("ProgressValue( PyQt_PyObject )"), p) self.emit(SIGNAL("ProgressValue( PyQt_PyObject )"), self.featcount) if self.error is None: # Checking uniqueness y = np.bincount(all_ids) if np.max(y) > 1: self.error = 'IDs are not unique.' if self.error is None: self.emit(SIGNAL("ProgressText ( PyQt_PyObject )"), "Loading data from layer") self.emit(SIGNAL("ProgressValue( PyQt_PyObject )"), 0) self.graph = Graph() all_types = [ np.int64, np.int64, np.int64, np.float64, np.float64, np.int64 ] all_titles = [ 'link_id', 'a_node', 'b_node', 'length_ab', 'length_ba', 'direction' ] dict_field = {} for k in self.skims: a, b, t = self.skims[k] all_types.append(np.float64) all_types.append(np.float64) all_titles.append((k + '_ab').encode('ascii', 'ignore')) all_titles.append((k + '_ba').encode('ascii', 'ignore')) dict_field[k + '_ab'] = a if self.bidirectional: dict_field[k + '_ba'] = b else: dict_field[k + '_ba'] = -1 dt = [(t, d) for t, d in zip(all_titles, all_types)] anode = self.netlayer.fieldNameIndex('A_Node') bnode = self.netlayer.fieldNameIndex('B_Node') data = [] if self.selected_only: self.features = self.netlayer.selectedFeatures() else: self.features = self.netlayer.getFeatures() p = 0 for feat in self.features: line = [] line.append(feat.attributes()[self.linkid]) line.append(feat.attributes()[anode]) line.append(feat.attributes()[bnode]) line.append(feat.attributes()[self.ablength]) if self.bidirectional: line.append(feat.attributes()[self.balength]) line.append(feat.attributes()[self.directionfield]) else: line.append(-1) line.append(1) # We append the skims now for k in all_titles: if k in dict_field: if dict_field[k] >= 0: line.append(feat.attributes()[dict_field[k]]) else: line.append(-1) for k in line: if k == NULL: t = '' for j in line: t = t + ',' + str(j) self.error = 'Field with NULL value - ID:' + str( line[0]) + " / " + t break if self.error is not None: break data.append(line) p += 1 if p % 50 == 0: self.emit(SIGNAL("ProgressValue( PyQt_PyObject )"), p) if self.error is None: network = np.asarray(data) del data self.graph.network = np.zeros(network.shape[0], dtype=dt) for k, t in enumerate(dt): self.graph.network[t[0]] = network[:, k].astype(t[1]) del network self.graph.type_loaded = 'NETWORK' self.graph.status = 'OK' self.graph.network_ok = True self.graph.prepare_graph() self.graph.__source__ = None self.graph.__field_name__ = None self.graph.__layer_name__ = None self.emit(SIGNAL("FinishedThreadedProcedure( PyQt_PyObject )"), None)
def __init__(self, iface): QtWidgets.QDialog.__init__(self) self.iface = iface self.setupUi(self) self.path = standard_path() self.output_path = None self.temp_path = None self.error = None self.report = None self.method = {} self.matrices = OrderedDict() self.skims = [] self.matrix = None self.graph = Graph() self.results = AssignmentResults() self.block_centroid_flows = None self.worker_thread = None # Signals for the matrix_procedures tab self.but_load_new_matrix.clicked.connect(self.find_matrices) # Signals from the Network tab self.load_graph_from_file.clicked.connect(self.load_graph) # Signals for the algorithm tab self.progressbar0.setVisible(False) self.progressbar0.setValue(0) self.progress_label0.setVisible(False) self.do_assignment.clicked.connect(self.run) self.cancel_all.clicked.connect(self.exit_procedure) self.select_output_folder.clicked.connect(self.choose_folder_for_outputs) self.cb_choose_algorithm.addItem('All-Or-Nothing') self.cb_choose_algorithm.currentIndexChanged.connect(self.changing_algorithm) # slots for skim tab self.but_build_query.clicked.connect(partial(self.build_query, 'select link')) self.changing_algorithm() # path file self.path_file = OutputType() # Queries tables = [self.select_link_list, self.list_link_extraction] for table in tables: table.setColumnWidth(0, 280) table.setColumnWidth(1, 40) table.setColumnWidth(2, 150) table.setColumnWidth(3, 40) self.graph_properties_table.setColumnWidth(0, 190) self.graph_properties_table.setColumnWidth(1, 240) # critical link self.but_build_query.clicked.connect(partial(self.build_query, 'select link')) self.do_select_link.stateChanged.connect(self.set_behavior_special_analysis) self.tot_crit_link_queries = 0 self.critical_output = OutputType() # link flow extraction self.but_build_query_extract.clicked.connect(partial(self.build_query, 'Link flow extraction')) self.do_extract_link_flows.stateChanged.connect(self.set_behavior_special_analysis) self.tot_link_flow_extract = 0 self.link_extract = OutputType() # Disabling resources not yet implemented self.do_select_link.setEnabled(False) self.but_build_query.setEnabled(False) self.select_link_list.setEnabled(False) self.skim_list_table.setEnabled(False) self.do_extract_link_flows.setEnabled(False) self.but_build_query_extract.setEnabled(False) self.list_link_extraction.setEnabled(False) self.new_matrix_to_assign() self.table_matrix_list.setColumnWidth(0, 135) self.table_matrix_list.setColumnWidth(1, 135) self.table_matrices_to_assign.setColumnWidth(0, 125) self.table_matrices_to_assign.setColumnWidth(1, 125) self.skim_list_table.setColumnWidth(0, 70) self.skim_list_table.setColumnWidth(1, 490)
class TestGraph(TestCase): def test_create_from_geography(self): self.graph = Graph() self.graph.create_from_geography( test_network, "link_id", "dir", "distance", centroids=centroids, skim_fields=[], anode="A_NODE", bnode="B_NODE", ) self.graph.set_graph(cost_field="distance") self.graph.set_blocked_centroid_flows(block_centroid_flows=True) self.graph.set_skimming("distance") def test_prepare_graph(self): self.test_create_from_geography() self.graph.prepare_graph(centroids) reference_graph = Graph() reference_graph.load_from_disk(test_graph) if not np.array_equal(self.graph.graph, reference_graph.graph): self.fail("Reference graph and newly-prepared graph are not equal") def test_set_graph(self): self.test_prepare_graph() self.graph.set_graph(cost_field="distance") self.graph.set_blocked_centroid_flows(block_centroid_flows=True) if self.graph.num_zones != centroids.shape[0]: self.fail("Number of centroids not properly set") if self.graph.num_links != 222: self.fail("Number of links not properly set") if self.graph.num_nodes != 93: self.fail("Number of nodes not properly set - " + str(self.graph.num_nodes)) def test_save_to_disk(self): self.test_create_from_geography() self.graph.save_to_disk(join(path_test, "aequilibrae_test_graph.aeg")) self.graph_id = self.graph.__id__ self.graph_version = self.graph.__version__ def test_load_from_disk(self): self.test_save_to_disk() reference_graph = Graph() reference_graph.load_from_disk(test_graph) new_graph = Graph() new_graph.load_from_disk(join(path_test, "aequilibrae_test_graph.aeg")) comparisons = [ ("Graph", new_graph.graph, reference_graph.graph), ("b_nodes", new_graph.b_node, reference_graph.b_node), ("Forward-Star", new_graph.fs, reference_graph.fs), ("cost", new_graph.cost, reference_graph.cost), ("centroids", new_graph.centroids, reference_graph.centroids), ("skims", new_graph.skims, reference_graph.skims), ("link ids", new_graph.ids, reference_graph.ids), ("Network", new_graph.network, reference_graph.network), ("All Nodes", new_graph.all_nodes, reference_graph.all_nodes), ("Nodes to indices", new_graph.nodes_to_indices, reference_graph.nodes_to_indices), ] for comparison, newg, refg in comparisons: if not np.array_equal(newg, refg): self.fail( "Reference %s and %s created and saved to disk are not equal" % (comparison, comparison)) comparisons = [ ("nodes", new_graph.num_nodes, reference_graph.num_nodes), ("links", new_graph.num_links, reference_graph.num_links), ("zones", new_graph.num_zones, reference_graph.num_zones), ("block through centroids", new_graph.block_centroid_flows, reference_graph.block_centroid_flows), ("Graph ID", new_graph.__id__, self.graph_id), ("Graph Version", new_graph.__version__, self.graph_version), ] for comparison, newg, refg in comparisons: if newg != refg: self.fail( "Reference %s and %s created and saved to disk are not equal" % (comparison, comparison)) def test_available_skims(self): self.test_set_graph() if self.graph.available_skims() != ["distance"]: self.fail("Skim availability with problems") 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()