Пример #1
0
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')
Пример #2
0
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()
Пример #3
0
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')
Пример #4
0
class ImpedanceMatrixDialog(QtGui.QDialog, FORM_CLASS):
    def __init__(self, iface):
        QDialog.__init__(self)
        self.iface = iface
        self.setupUi(self)

        self.result = SkimResults()
        self.validtypes = integer_types + float_types
        self.tot_skims = 0
        self.name_skims = 0
        self.graph = None
        self.skimmeable_fields = []
        self.skim_fields = []
        self.error = None
        # FIRST, we connect slot signals

        # For loading a new graph
        self.load_graph_from_file.clicked.connect(
            self.loaded_new_graph_from_file)

        # For adding skims
        # self.bt_add_skim.clicked.connect(self.add_to_skim_list)
        self.but_adds_to_links.clicked.connect(self.append_to_list)
        self.but_removes_from_links.clicked.connect(self.removes_fields)

        self.do_dist_matrix.clicked.connect(self.run_skimming)

        # SECOND, we set visibility for sections that should not be shown when the form opens (overlapping items)
        #        and re-dimension the items that need re-dimensioning
        self.hide_all_progress_bars()
        self.available_skims_table.setColumnWidth(0, 245)
        self.skim_list.setColumnWidth(0, 245)
        self.available_skims_table.setEditTriggers(
            QtGui.QAbstractItemView.NoEditTriggers)
        self.skim_list.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)

        # loads default path from parameters
        self.path = standard_path()

    def removes_fields(self):
        table = self.available_skims_table
        final_table = self.skim_list

        for i in final_table.selectedRanges():
            old_fields = [
                final_table.item(row, 0).text()
                for row in xrange(i.topRow(),
                                  i.bottomRow() + 1)
            ]

            for row in xrange(i.bottomRow(), i.topRow() - 1, -1):
                final_table.removeRow(row)

            counter = table.rowCount()
            for field in old_fields:
                table.setRowCount(counter + 1)
                item1 = QTableWidgetItem(field)
                item1.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable)
                table.setItem(counter, 0, item1)
                counter += 1

    def append_to_list(self):
        table = self.available_skims_table
        final_table = self.skim_list

        for i in table.selectedRanges():
            new_fields = [
                table.item(row, 0).text()
                for row in xrange(i.topRow(),
                                  i.bottomRow() + 1)
            ]

            for f in new_fields:
                self.skim_fields.append(f.encode('utf-8'))
            for row in xrange(i.bottomRow(), i.topRow() - 1, -1):
                table.removeRow(row)

            counter = final_table.rowCount()
            for field in new_fields:
                final_table.setRowCount(counter + 1)
                item1 = QTableWidgetItem(field)
                item1.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable)
                final_table.setItem(counter, 0, item1)
                counter += 1

    def hide_all_progress_bars(self):
        self.progressbar.setVisible(False)
        self.progress_label.setVisible(False)
        self.progressbar.setValue(0)
        self.progress_label.setText('')

    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))

    def browse_outfile(self):
        self.imped_results = None
        new_name, extension = GetOutputFileName(
            self, 'AequilibraE impedance computation', matrix_export_types,
            '.aem', self.path)
        if new_name is not None:
            self.imped_results = new_name.encode('utf-8')

    def run_thread(self):
        self.do_dist_matrix.setVisible(False)
        self.progressbar.setRange(0, self.graph.num_zones)
        QObject.connect(self.worker_thread, SIGNAL("skimming"),
                        self.signal_handler)
        self.worker_thread.start()
        self.exec_()

    def signal_handler(self, val):
        if val[0] == 'zones finalized':
            self.progressbar.setValue(val[1])
        elif val[0] == 'text skimming':
            self.progress_label.setText(val[1])
        elif val[0] == 'finished_threaded_procedure':
            self.finished_threaded_procedure()

    def finished_threaded_procedure(self):
        self.report = self.worker_thread.report
        self.result.skims.export(self.imped_results)
        self.exit_procedure()

    def run_skimming(self):  # Saving results

        if self.error is None:
            self.browse_outfile()
            cost_field = self.cb_minimizing.currentText().encode('utf-8')

            # We prepare the graph to set all nodes as centroids
            if self.rdo_all_nodes.isChecked():
                self.graph.prepare_graph(self.graph.all_nodes)

            self.graph.set_graph(
                cost_field=cost_field,
                skim_fields=self.skim_fields,
                block_centroid_flows=self.block_paths.isChecked())

            self.result.prepare(self.graph)

            self.funding1.setVisible(False)
            self.funding2.setVisible(False)
            self.progressbar.setVisible(True)
            self.progress_label.setVisible(True)
            self.worker_thread = NetworkSkimming(self.graph, self.result)
            try:
                self.run_thread()
            except ValueError as error:
                qgis.utils.iface.messageBar().pushMessage("Input error",
                                                          error.message,
                                                          level=3)
        else:
            qgis.utils.iface.messageBar().pushMessage("Error:",
                                                      self.error,
                                                      level=3)

    def check_inputs(self):
        self.error = None
        if self.rdo_all_nodes.isChecked() and self.block_paths.isChecked():
            self.error = 'It is not possible to trace paths between all nodes while blocking flows through centroids'

        if self.graph is None:
            self.error = 'No graph loaded'

        if len(self.skim_fields) < 1:
            self.error = 'No skim fields provided'

    def exit_procedure(self):
        self.close()
        if self.report:
            dlg2 = ReportDialog(self.iface, self.report)
            dlg2.show()
            dlg2.exec_()
Пример #5
0
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")