Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes distribution procedures #244

Merged
merged 5 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion qaequilibrae/modules/common_tools/database_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def __init__(self, aeq_dataset, separator, decimals, parent=None):
if np.issubdtype(t, np.integer):
self.types.append(0)
self.empties.append(np.iinfo(t).min)
elif np.issubdtype(t, np.float):
elif np.issubdtype(t, np.floating):
self.types.append(1)
self.empties.append(np.nan)
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from qgis.PyQt.QtCore import *
from aequilibrae.utils.worker_thread import WorkerThread
from aequilibrae.distribution import GravityApplication
from aequilibrae.utils.worker_thread import WorkerThread
from qgis.PyQt.QtCore import *


class ApplyGravityProcedure(WorkerThread):
Expand All @@ -18,4 +18,4 @@ def doWork(self):
self.report = self.gravity.report
except ValueError as e:
self.error = e
self.finished_threaded_procedure.emit("apply_gravity")
self.jobFinished.emit("apply_gravity")
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from aequilibrae.distribution import GravityCalibration
from aequilibrae.utils.worker_thread import WorkerThread
from qgis.PyQt.QtCore import *

from aequilibrae.utils.worker_thread import WorkerThread

class CalibrateGravityProcedure(WorkerThread):
def __init__(self, parentThread, **kwargs):
Expand All @@ -11,10 +12,7 @@ def __init__(self, parentThread, **kwargs):
self.model = None

def doWork(self):
try:
self.gravity.calibrate()
self.report = self.gravity.report
self.model = self.gravity.model
except ValueError as e:
self.error = e
self.finished_threaded_procedure.emit("calibrate")
self.gravity.calibrate()
self.report = self.gravity.report
self.model = self.gravity.model
self.jobFinished.emit("calibrate")
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
import os
from collections import OrderedDict
from functools import partial
from os.path import join

import numpy as np
from aequilibrae.distribution import SyntheticGravityModel
from aequilibrae.distribution.synthetic_gravity_model import valid_functions
from aequilibrae.matrix import AequilibraeData, AequilibraeMatrix

from qgis.PyQt.QtWidgets import QAbstractItemView
from qaequilibrae.modules.matrix_procedures.matrix_lister import list_matrices
from qaequilibrae.modules.common_tools import PandasModel
import qgis
from aequilibrae.context import get_logger
from qgis.PyQt import QtWidgets, uic
Expand All @@ -18,7 +22,7 @@
from qaequilibrae.modules.common_tools import GetOutputFileName
from qaequilibrae.modules.common_tools import ReportDialog
from qaequilibrae.modules.common_tools.auxiliary_functions import standard_path
from qaequilibrae.modules.matrix_procedures import LoadMatrixDialog, LoadDatasetDialog, DisplayAequilibraEFormatsDialog
from qaequilibrae.modules.matrix_procedures import LoadDatasetDialog, DisplayAequilibraEFormatsDialog

FORM_CLASS, _ = uic.loadUiType(os.path.join(os.path.dirname(__file__), "forms/ui_distribution.ui"))
spec = iutil.find_spec("openmatrix")
Expand All @@ -31,12 +35,15 @@


class DistributionModelsDialog(QtWidgets.QDialog, FORM_CLASS):
def __init__(self, iface, mode=None):
def __init__(self, qgs_proj, mode=None):
QtWidgets.QDialog.__init__(self)
self.iface = iface
self.iface = qgs_proj.iface
self.setupUi(self)
self.path = standard_path()

self.qgs_proj = qgs_proj
self.project = qgs_proj.project

self.error = None
self.report = []
self.job_queue = OrderedDict()
Expand All @@ -55,7 +62,6 @@ def __init__(self, iface, mode=None):
self.rdo_calibrate_gravity.clicked.connect(self.configure_inputs)

self.but_load_data.clicked.connect(self.load_datasets)
self.but_load_mat.clicked.connect(self.load_matrices)
self.but_load_model.clicked.connect(self.load_model)

self.cob_prod_data.currentIndexChanged.connect(
Expand All @@ -74,9 +80,8 @@ def __init__(self, iface, mode=None):

self.but_run.clicked.connect(self.run)
self.but_queue.clicked.connect(self.add_job_to_queue)
self.but_cancel.clicked.connect(self.exit_procedure)
self.table_datasets.doubleClicked.connect(self.matrix_and_data_double_clicked)
self.table_matrices.doubleClicked.connect(self.matrix_and_data_double_clicked)
self.but_cancel.clicked.connect(self.close)
self.table_datasets.doubleClicked.connect(self.data_double_clicked)

self.table_jobs.setColumnWidth(0, 50)
self.table_jobs.setColumnWidth(1, 295)
Expand All @@ -95,18 +100,24 @@ def __init__(self, iface, mode=None):
self.rdo_calibrate_gravity.setChecked(True)
self.configure_inputs()

self.load_matrices()
self.user_chosen_model = None
self.update_model_parameters()

def matrix_and_data_double_clicked(self, mi):
def load_matrices(self):
self.matrices = list_matrices(self.project.matrices.fldr)

self.matrices_model = PandasModel(self.matrices)
self.list_matrices.setModel(self.matrices_model)
self.cob_imped_mat.addItems(self.matrices["name"].tolist())
self.cob_seed_mat.addItems(self.matrices["name"].tolist())


def data_double_clicked(self, mi):
row = mi.row()
if row > -1:
if self.sender().objectName() == "table_matrices":
obj_to_view = self.table_matrices.item(row, 0).text()
dlg2 = DisplayAequilibraEFormatsDialog(self.iface, self.matrices[obj_to_view])
else:
obj_to_view = self.table_datasets.item(row, 0).text()
dlg2 = DisplayAequilibraEFormatsDialog(self.iface, self.datasets[obj_to_view])
obj_to_view = self.table_datasets.item(row, 0).text()
dlg2 = DisplayAequilibraEFormatsDialog(self.iface, self.datasets[obj_to_view])
dlg2.show()
dlg2.exec_()

Expand All @@ -118,7 +129,6 @@ def configure_inputs(self):
self.resize(511, 334)
self.model_tabs.setEnabled(True)
self.model_tabs.setVisible(True)
self.progressbar.setVisible(False)
to_remove = []
if self.rdo_ipf.isChecked():
self.job = "ipf"
Expand Down Expand Up @@ -206,20 +216,6 @@ def load_datasets(self):
self.load_comboboxes(self.datasets.keys(), self.cob_prod_data)
self.load_comboboxes(self.datasets.keys(), self.cob_atra_data)

def load_matrices(self):
dlg2 = LoadMatrixDialog(self.iface)
dlg2.show()
dlg2.exec_()
if isinstance(dlg2.matrix, AequilibraeMatrix):
matrix_name = dlg2.matrix.file_path
if matrix_name is not None:
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.add_to_table(self.matrices, self.table_matrices)
self.load_comboboxes(self.matrices.keys(), self.cob_imped_mat)
self.load_comboboxes(self.matrices.keys(), self.cob_seed_mat)

def load_model(self):
file_name = self.browse_outfile("mod")
try:
Expand All @@ -235,12 +231,14 @@ def change_vector_field(self, cob_orig, cob_dest, dt):
if dt == "data":
for f in self.datasets[d].fields:
if np.issubdtype(self.datasets[d].data[f].dtype, np.integer) or np.issubdtype(
self.datasets[d].data[f].dtype, np.float
self.datasets[d].data[f].dtype, np.float64
):
cob_dest.addItem(f)
else:
for f in self.matrices[d].names:
cob_dest.addItem(f)
file_name = self.matrices.at[cob_orig.currentIndex(), "file_name"]
mat = AequilibraeMatrix()
mat.load(join(self.project.matrices.fldr, file_name))
cob_dest.addItems(mat.names)

def load_comboboxes(self, list_to_load, data_cob):
data_cob.clear()
Expand Down Expand Up @@ -290,11 +288,15 @@ def add_job_to_queue(self):
worker_thread = None
if self.check_data():
if self.job != "ipf":
imped_matrix = self.matrices[self.cob_imped_mat.currentText()]
imped_name = self.matrices.at[self.cob_imped_mat.currentIndex(), "file_name"]
imped_matrix = AequilibraeMatrix()
imped_matrix.load(join(self.project.matrices.fldr, imped_name))
imped_matrix.computational_view([self.cob_imped_field.currentText()])

if self.job != "apply":
seed_matrix = self.matrices[self.cob_seed_mat.currentText()]
seed_name = self.matrices.at[self.cob_seed_mat.currentIndex(), "file_name"]
seed_matrix = AequilibraeMatrix()
seed_matrix.load(join(self.project.matrices.fldr, seed_name))
seed_matrix.computational_view([self.cob_seed_field.currentText()])

if self.job != "calibrate":
Expand Down Expand Up @@ -377,7 +379,6 @@ def add_job_to_list(self, job, out_name):
self.table_jobs.setItem(i, 2, QTableWidgetItem(self.tr("Queued")))

def run(self):
self.progressbar.setVisible(True)
self.chb_empty_as_zero.setVisible(False)
try:
for out_name in self.job_queue.keys():
Expand Down Expand Up @@ -412,22 +413,10 @@ def check_data(self):
return True

def run_thread(self):
self.worker_thread.ProgressValue.connect(self.progress_value_from_thread)
self.worker_thread.ProgressText.connect(self.progress_text_from_thread)
self.worker_thread.ProgressMaxValue.connect(self.progress_range_from_thread)
self.worker_thread.finished_threaded_procedure.connect(self.job_finished_from_thread)
self.worker_thread.start()
self.worker_thread.jobFinished.connect(self.job_finished_from_thread)
self.worker_thread.doWork()
self.show()

def progress_range_from_thread(self, val):
self.progressbar.setRange(0, val[1])

def progress_value_from_thread(self, value):
self.progressbar.setValue(value[1])

def progress_text_from_thread(self, value):
self.progress_label.setText(value[1])

def job_finished_from_thread(self, success):
error = self.worker_thread.error
if error is not None:
Expand All @@ -442,7 +431,6 @@ def job_finished_from_thread(self, success):
self.exit_procedure()

def exit_procedure(self):
self.close()
if self.report is not None:
dlg2 = ReportDialog(self.iface, self.report)
dlg2.show()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>511</width>
<height>330</height>
<height>334</height>
</rect>
</property>
<property name="sizePolicy">
Expand Down Expand Up @@ -103,62 +103,8 @@
<string>Load matrices</string>
</attribute>
<layout class="QGridLayout" name="gridLayout_5">
<item row="0" column="2" rowspan="6">
<widget class="QTableWidget" name="table_matrices">
<column>
<property name="text">
<string>Matrix name</string>
</property>
</column>
<column>
<property name="text">
<string># of cores</string>
</property>
</column>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Matrices</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item row="1" column="1">
<spacer name="verticalSpacer_3">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="4" column="1">
<spacer name="verticalSpacer_4">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="3" column="1">
<widget class="QPushButton" name="but_load_mat">
<property name="text">
<string>Load</string>
</property>
</widget>
<item row="0" column="1">
<widget class="QTableView" name="list_matrices"/>
</item>
</layout>
</widget>
Expand Down Expand Up @@ -466,10 +412,13 @@
<string>jobs</string>
</attribute>
<layout class="QGridLayout" name="gridLayout_2">
<item row="2" column="0">
<widget class="QProgressBar" name="progressbar">
<property name="value">
<number>0</number>
<item row="1" column="0">
<widget class="QCheckBox" name="chb_empty_as_zero">
<property name="text">
<string>Treat empties as zeros</string>
</property>
<property name="checked">
<bool>false</bool>
</property>
</widget>
</item>
Expand All @@ -492,20 +441,7 @@
</column>
</widget>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="chb_empty_as_zero">
<property name="text">
<string>Treat empties as zeros</string>
</property>
<property name="checked">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
<zorder>progressbar</zorder>
<zorder>table_jobs</zorder>
<zorder>chb_empty_as_zero</zorder>
</widget>
</widget>
</item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class IpfProcedure(WorkerThread):
finished_threaded_procedure = pyqtSignal(object)

def __init__(self, parentThread, **kwargs):
WorkerThread.__init__(self, parentThread)
Expand All @@ -21,4 +20,4 @@ def doWork(self):
self.output = self.ipf.output
except ValueError as e:
self.error = e
self.finished_threaded_procedure.emit("finishedIPF")
self.jobFinished.emit("finishedIPF")
5 changes: 4 additions & 1 deletion qaequilibrae/modules/menu_actions/action_distribution.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
def run_distribution_models(qgis_project):
from qaequilibrae.modules.distribution_procedures import DistributionModelsDialog
dlg2 = DistributionModelsDialog(qgis_project.iface)
if qgis_project.project is None:
qgis_project.iface.messageBar().pushMessage("Error", "You need to load a project first", level=3, duration=10)
return
dlg2 = DistributionModelsDialog(qgis_project)
dlg2.show()
dlg2.exec_()
Loading