コード例 #1
0
 def __init__(self, parent_thread, layer, index_field, fields, file_name):
     WorkerThread.__init__(self, parent_thread)
     self.layer = layer
     self.index_field = index_field
     self.fields = fields
     self.error = None
     self.python_version = (8 * struct.calcsize("P"))
     self.output = AequilibraEData()
     self.output_name = file_name
コード例 #2
0
 def load_from_aequilibrae_format(self):
     out_name, _ = GetOutputFileName(self, 'AequilibraE dataset',
                                     ["Aequilibrae dataset(*.aed)"], '.aed',
                                     self.path)
     try:
         self.dataset = AequilibraEData()
         self.dataset.load(out_name)
     except:
         self.error = 'Could not load file. It might be corrupted or might not be a valid AequilibraE file'
     self.exit_procedure()
コード例 #3
0
    def test_load(self):
        # re-imports the dataset
        self.ad = AequilibraEData()
        self.ad.load(file_path)

        # checks if the values were properly saved
        if self.ad.index[70] != 170:
            self.fail("Value for data index test was not as expected")

        if int(self.ad.d[70]) != 28900:
            self.fail("Value for data field test was not as expected")

        for f in self.ad.fields:
            if f not in args["field_names"]:
                self.fail("Could not retrieve all fields")
コード例 #4
0
class TestAequilibraEData(TestCase):
    def test___init__(self):
        # Generates the dataset
        dataset = AequilibraEData()
        dataset.create_empty(**args)

        dataset.index[:] = np.arange(dataset.entries) + 100
        dataset.d[:] = dataset.index[:]**2
        if dataset.index[70] != 170:
            self.fail()

        if int(dataset.d[70]) != 28900:
            self.fail()

        # removes the dataset
        del(dataset)

    def test_load(self):
        # re-imports the dataset
        self.ad = AequilibraEData()
        self.ad.load(file_path)


        # checks if the values were properly saved
        if self.ad.index[70] != 170:
            self.fail("Value for data index test was not as expected")

        if int(self.ad.d[70]) != 28900:
            self.fail("Value for data field test was not as expected")

        for f in self.ad.fields:
            if f not in args['field_names']:
                self.fail("Could not retrieve all fields")

    def test_export(self):
        self.test_load()
        temp_name = os.path.join(tempfile.gettempdir(), 'aequilibrae_data_example.csv')
        self.ad.export(temp_name)
コード例 #5
0
    def test_load(self):
        # re-imports the dataset
        self.ad = AequilibraEData()
        self.ad.load(file_path)


        # checks if the values were properly saved
        if self.ad.index[70] != 170:
            self.fail("Value for data index test was not as expected")

        if int(self.ad.d[70]) != 28900:
            self.fail("Value for data field test was not as expected")

        for f in self.ad.fields:
            if f not in args['field_names']:
                self.fail("Could not retrieve all fields")
コード例 #6
0
    def test___init__(self):
        # Generates the dataset
        dataset = AequilibraEData()
        dataset.create_empty(**args)

        dataset.index[:] = np.arange(dataset.entries) + 100
        dataset.d[:] = dataset.index[:]**2
        if dataset.index[70] != 170:
            self.fail()

        if int(dataset.d[70]) != 28900:
            self.fail()

        # removes the dataset
        del(dataset)
コード例 #7
0
class LoadDataset(WorkerThread):
    def __init__(self, parent_thread, layer, index_field, fields, file_name):
        WorkerThread.__init__(self, parent_thread)
        self.layer = layer
        self.index_field = index_field
        self.fields = fields
        self.error = None
        self.python_version = (8 * struct.calcsize("P"))
        self.output = AequilibraEData()
        self.output_name = file_name

    def doWork(self):
        feat_count = self.layer.featureCount()
        self.ProgressMaxValue.emit(feat_count)

        # Create specification for the output file
        datafile_spec = {'entries': feat_count}
        if self.output_name is None:
            datafile_spec['memory_mode'] = True
        else:
            datafile_spec['memory_mode'] = False
        fields = []
        types = []
        idxs = []
        empties = []
        for field in self.layer.dataProvider().fields().toList():
            if field.name() in self.fields:
                if field.type() in integer_types:
                    types.append('<i8')
                    empties.append(np.iinfo(np.int64).min)
                elif field.type() in float_types:
                    types.append('<f8')
                    empties.append(np.nan)
                elif field.type() in string_types:
                    types.append('S' + str(field.length()))
                    empties.append('')
                else:
                    self.error = 'Field {} does has a type not supported.'.format(str(field.name()))
                    break
                fields.append(str(field.name()))
                idxs.append(self.layer.dataProvider().fieldNameIndex(field.name()))

        index_idx = self.layer.dataProvider().fieldNameIndex(self.index_field)
        datafile_spec['field_names'] = fields
        datafile_spec['data_types'] = types
        datafile_spec['file_path'] = self.output_name

        if self.error is None:
            self.output.create_empty(**datafile_spec)

            # Get all the data
            for p, feat in enumerate(self.layer.getFeatures()):
                for idx, field, empty in zip(idxs, fields, empties):
                    if feat.attributes()[idx] == QVariant():
                        self.output.data[field][p] = empty
                    else:
                        self.output.data[field][p] = feat.attributes()[idx]
                self.output.index[p] = feat.attributes()[index_idx]
                self.ProgressValue.emit(p)

            self.ProgressValue.emit(feat_count)
        self.finished_threaded_procedure.emit('Done')
コード例 #8
0
class LoadDatasetDialog(QtWidgets.QDialog, FORM_CLASS):
    def __init__(self, iface, single_use=True):
        QtWidgets.QDialog.__init__(self)
        self.iface = iface
        self.setupUi(self)
        self.path = standard_path()

        self.output_name = None
        self.layer = None
        self.zones = None
        self.cells = None
        self.error = None
        self.selected_fields = None
        self.worker_thread = None
        self.dataset = None
        self.ignore_fields = []
        self.single_use = single_use

        self.radio_layer_matrix.clicked.connect(
            partial(self.size_it_accordingly, False))
        self.radio_aequilibrae.clicked.connect(
            partial(self.size_it_accordingly, False))
        self.chb_all_fields.clicked.connect(self.set_tables_with_fields)
        self.but_adds_to_links.clicked.connect(self.append_to_list)

        # For changing the network layer
        self.cob_data_layer.currentIndexChanged.connect(
            self.load_fields_to_combo_boxes)
        self.but_removes_from_links.clicked.connect(self.removes_fields)
        # For adding skims
        self.but_load.clicked.connect(self.load_from_aequilibrae_format)
        self.but_save_and_use.clicked.connect(self.load_the_vector)
        self.but_import_and_use.clicked.connect(self.load_just_to_use)

        # THIRD, we load layers in the canvas to the combo-boxes
        for layer in all_layers_from_toc():  # We iterate through all layers
            if 'wkbType' in dir(layer):
                if layer.wkbType() in [100] + point_types + poly_types:
                    self.cob_data_layer.addItem(layer.name())

        if not self.single_use:
            self.radio_layer_matrix.setChecked(True)
            self.radio_aequilibrae.setEnabled(False)
            self.but_import_and_use.setEnabled(False)
            self.but_load.setEnabled(False)
            self.but_save_and_use.setText('Import')

        self.size_it_accordingly(partial(self.size_it_accordingly, False))

    def set_tables_with_fields(self):
        self.size_it_accordingly(False)

        if self.chb_all_fields.isChecked() and self.layer is not None:
            self.ignore_fields = []
            self.selected_fields = [
                x.name() for x in self.layer.dataProvider().fields().toList()
            ]

        for table in [self.table_all_fields, self.table_fields_to_import]:
            table.setRowCount(0)
            table.clearContents()
        if self.layer is not None:
            comb = [(self.table_fields_to_import, self.selected_fields),
                    (self.table_all_fields, self.ignore_fields)]
            for table, fields in comb:
                for field in fields:
                    table.setRowCount(table.rowCount() + 1)
                    item1 = QTableWidgetItem(field)
                    item1.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable)
                    table.setItem(table.rowCount() - 1, 0, item1)

    def size_it_accordingly(self, final=False):
        def set_size(w, h):
            self.setMaximumSize(QtCore.QSize(w, h))
            self.resize(w, h)

        if self.radio_aequilibrae.isChecked():
            set_size(154, 100)
        else:
            if final:
                if self.radio_layer_matrix.isChecked():
                    if self.chb_all_fields.isChecked():
                        set_size(498, 120)
                    self.progressbar.setMinimumHeight(100)
                else:
                    set_size(498, 410)
                    self.progressbar.setMinimumHeight(390)
            else:
                if self.chb_all_fields.isChecked():
                    set_size(449, 120)
                else:
                    set_size(449, 410)

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

            self.ignore_fields.extend(old_fields)
            self.selected_fields = [
                x for x in self.selected_fields if x not in old_fields
            ]

        self.set_tables_with_fields()

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

            self.selected_fields.extend(new_fields)
            self.ignore_fields = [
                x for x in self.ignore_fields if x not in new_fields
            ]

        self.set_tables_with_fields()

    def load_fields_to_combo_boxes(self):
        self.cob_index_field.clear()

        all_fields = []
        if self.cob_data_layer.currentIndex() >= 0:
            self.ignore_fields = []
            self.layer = get_vector_layer_by_name(
                self.cob_data_layer.currentText())
            self.selected_fields = [
                x.name() for x in self.layer.dataProvider().fields().toList()
            ]
            for field in self.layer.dataProvider().fields().toList():
                if field.type() in integer_types:
                    self.cob_index_field.addItem(field.name())
                    all_fields.append(field.name())
                if field.type() in float_types:
                    all_fields.append(field.name())
        self.set_tables_with_fields()

    def run_thread(self):
        self.worker_thread.ProgressValue.connect(
            self.progress_value_from_thread)
        self.worker_thread.ProgressMaxValue.connect(
            self.progress_range_from_thread)
        self.worker_thread.finished_threaded_procedure.connect(
            self.finished_threaded_procedure)

        self.chb_all_fields.setEnabled(False)
        self.but_load.setEnabled(False)
        self.but_save_and_use.setEnabled(False)
        self.worker_thread.start()
        self.exec_()

    # VAL and VALUE have the following structure: (bar/text ID, value)
    def progress_range_from_thread(self, val):
        self.progressbar.setRange(0, val)

    def progress_value_from_thread(self, val):
        self.progressbar.setValue(val)

    def finished_threaded_procedure(self, param):
        self.but_load.setEnabled(True)
        self.but_save_and_use.setEnabled(True)
        self.chb_all_fields.setEnabled(True)
        if self.worker_thread.error is not None:
            qgis.utils.iface.messageBar().pushMessage(
                "Error while loading vector:",
                self.worker_thread.error,
                level=1)
        else:
            self.dataset = self.worker_thread.output
        self.exit_procedure()

    def load_from_aequilibrae_format(self):
        out_name, _ = GetOutputFileName(self, 'AequilibraE dataset',
                                        ["Aequilibrae dataset(*.aed)"], '.aed',
                                        self.path)
        try:
            self.dataset = AequilibraEData()
            self.dataset.load(out_name)
        except:
            self.error = 'Could not load file. It might be corrupted or might not be a valid AequilibraE file'
        self.exit_procedure()

    def load_the_vector(self):
        if self.single_use:
            self.output_name = None
        else:
            self.error = None
            self.output_name, _ = GetOutputFileName(
                self, 'AequilibraE dataset', ["Aequilibrae dataset(*.aed)"],
                '.aed', self.path)
            if self.output_name is None:
                self.error = 'No name provided for the output file'

        if self.radio_layer_matrix.isChecked() and self.error is None:
            if self.cob_data_layer.currentIndex(
            ) < 0 or self.cob_index_field.currentIndex() < 0:
                self.error = 'Invalid field chosen'

            index_field = self.cob_index_field.currentText()
            if index_field in self.selected_fields:
                self.selected_fields.remove(index_field)

            if len(self.selected_fields) > 0:
                self.worker_thread = LoadDataset(qgis.utils.iface.mainWindow(),
                                                 layer=self.layer,
                                                 index_field=index_field,
                                                 fields=self.selected_fields,
                                                 file_name=self.output_name)
                self.size_it_accordingly(True)
                self.run_thread()
            else:
                qgis.utils.iface.messageBar().pushMessage(
                    "Error:",
                    "One cannot load a dataset with indices only",
                    level=1)
        if self.error is not None:
            qgis.utils.iface.messageBar().pushMessage("Error:",
                                                      self.error,
                                                      level=1)

    def load_just_to_use(self):
        self.single_use = True
        self.load_the_vector()

    def exit_procedure(self):
        self.close()
コード例 #9
0
from aequilibrae.distribution import SyntheticGravityModel, GravityApplication
import numpy as np
import tempfile
import os

zones = 10

# row vector
args = {
    "entries": zones,
    "field_names": [u"rows"],
    "data_types": [np.float64],
    "memory_mode": True,
}

row_vector = AequilibraEData()
row_vector.create_empty(**args)
row_vector.index[:] = np.arange(row_vector.entries) + 100
row_vector.rows[:] = row_vector.index[:] + np.random.rand(zones)[:]

# column vector
args["field_names"] = ["columns"]
column_vector = AequilibraEData()
column_vector.create_empty(**args)
column_vector.index[:] = np.arange(column_vector.entries) + 100
column_vector.columns[:] = column_vector.index[:] + np.random.rand(zones)[:]

# balance vectors
column_vector.columns[:] = column_vector.columns[:] * (
    row_vector.rows.sum() / column_vector.columns.sum()
)
コード例 #10
0
from unittest import TestCase
from aequilibrae.matrix import AequilibraEData, AequilibraeMatrix
from aequilibrae.distribution import SyntheticGravityModel, GravityApplication
import numpy as np
import tempfile
import os

zones = 10

# row vector
args = {'entries': zones,
        'field_names': [u'rows'],
        'data_types': [np.float64],
        'memory_mode': True}

row_vector = AequilibraEData()
row_vector.create_empty(**args)
row_vector.index[:] = np.arange(row_vector.entries) + 100
row_vector.rows[:] = row_vector.index[:] + np.random.rand(zones)[:]

# column vector
args['field_names'] = ['columns']
column_vector = AequilibraEData()
column_vector.create_empty(**args)
column_vector.index[:] = np.arange(column_vector.entries) + 100
column_vector.columns[:] = column_vector.index[:] + np.random.rand(zones)[:]

# balance vectors
column_vector.columns[:] = column_vector.columns[:] * (row_vector.rows.sum() / column_vector.columns.sum())

# Impedance matrix_procedures
コード例 #11
0
    def __init__(self, iface):
        QtWidgets.QDialog.__init__(self)
        self.iface = iface
        self.setupUi(self)

        self.error = None

        self.error = None
        self.data_path, self.data_type = GetOutputFileName(
            self, 'AequilibraE custom formats',
            ["Aequilibrae dataset(*.aed)", "Aequilibrae matrix(*.aem)"],
            '.aed', standard_path())

        if self.data_type is None:
            self.error = 'Path provided is not a valid dataset'
            self.exit_with_error()

        self.data_type = self.data_type.upper()

        if self.data_type == 'AED':
            self.data_to_show = AequilibraEData()
        elif self.data_type == 'AEM':
            self.data_to_show = AequilibraeMatrix()

        try:
            self.data_to_show.load(self.data_path)
        except:
            self.error = 'Could not load dataset'
            self.exit_with_error()

        # Elements that will be used during the displaying
        self._layout = QVBoxLayout()
        self.table = QTableView()
        self._layout.addWidget(self.table)

        # Settings for displaying
        self.show_layout = QHBoxLayout()

        # Thousand separator
        self.thousand_separator = QCheckBox()
        self.thousand_separator.setChecked(True)
        self.thousand_separator.setText('Thousands separator')
        self.thousand_separator.toggled.connect(self.format_showing)
        self.show_layout.addWidget(self.thousand_separator)

        self.spacer = QSpacerItem(5, 0, QtWidgets.QSizePolicy.Expanding,
                                  QtWidgets.QSizePolicy.Minimum)
        self.show_layout.addItem(self.spacer)

        # Decimals
        txt = QLabel()
        txt.setText('Decimal places')
        self.show_layout.addWidget(txt)
        self.decimals = QSpinBox()
        self.decimals.valueChanged.connect(self.format_showing)
        self.decimals.setMinimum(0)
        self.decimals.setValue(4)
        self.decimals.setMaximum(10)

        self.show_layout.addWidget(self.decimals)
        self._layout.addItem(self.show_layout)

        # differentiates between matrix and dataset
        if self.data_type == 'AEM':
            self.data_to_show.computational_view([self.data_to_show.names[0]])
            # Matrices need cores and indices to be set as well
            self.mat_layout = QHBoxLayout()
            self.mat_list = QComboBox()
            for n in self.data_to_show.names:
                self.mat_list.addItem(n)

            self.mat_list.currentIndexChanged.connect(self.change_matrix_cores)
            self.mat_layout.addWidget(self.mat_list)

            self.idx_list = QComboBox()
            for i in self.data_to_show.index_names:
                self.idx_list.addItem(i)
            self.idx_list.currentIndexChanged.connect(self.change_matrix_cores)
            self.mat_layout.addWidget(self.idx_list)
            self._layout.addItem(self.mat_layout)
            self.change_matrix_cores()

        self.but_export = QPushButton()
        self.but_export.setText('Export')
        self.but_export.clicked.connect(self.export)

        self.but_close = QPushButton()
        self.but_close.clicked.connect(self.exit_procedure)
        self.but_close.setText('Close')

        self.but_layout = QHBoxLayout()
        self.but_layout.addWidget(self.but_export)
        self.but_layout.addWidget(self.but_close)

        self._layout.addItem(self.but_layout)

        # We chose to use QTableView. However, if we want to allow the user to edit the dataset
        # The we need to allow them to switch to the slower QTableWidget
        # Code below

        # self.table = QTableWidget(self.data_to_show.entries, self.data_to_show.num_fields)
        # self.table.setHorizontalHeaderLabels(self.data_to_show.fields)
        # self.table.setObjectName('data_viewer')
        #
        # self.table.setVerticalHeaderLabels([str(x) for x in self.data_to_show.index[:]])
        # self.table.clearContents()
        #
        # for i in range(self.data_to_show.entries):
        #     for j, f in enumerate(self.data_to_show.fields):
        #         item1 = QTableWidgetItem(str(self.data_to_show.data[f][i]))
        #         item1.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable)
        #         self.table.setItem(i, j, item1)

        self.resize(700, 500)
        self.setLayout(self._layout)
        self.format_showing()
コード例 #12
0
import os
import tempfile
import uuid
from unittest import TestCase
import tempfile
import numpy as np

from aequilibrae.matrix import AequilibraEData

file_path = AequilibraEData().random_name()
args = {'file_path': file_path,
        'entries': 100,
        'field_names': ['d', 'data2', 'data3'],
        'data_types': [np.float64, np.float32, np.int8]}

class TestAequilibraEData(TestCase):
    def test___init__(self):
        # Generates the dataset
        dataset = AequilibraEData()
        dataset.create_empty(**args)

        dataset.index[:] = np.arange(dataset.entries) + 100
        dataset.d[:] = dataset.index[:]**2
        if dataset.index[70] != 170:
            self.fail()

        if int(dataset.d[70]) != 28900:
            self.fail()

        # removes the dataset
        del(dataset)