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

Pedro/messaging #555

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
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: 2 additions & 0 deletions aequilibrae/paths/all_or_nothing.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def _build_signal(self):
if self.assignment is None:
self.assignment = SIGNAL(object)
self.assignment.emit(["start", self.matrix.zones, self.class_name])
else:
self.assignment.emit(["set_text", f"All-or-Nothing: {self.class_name}"])

def doWork(self):
self.execute()
Expand Down
10 changes: 5 additions & 5 deletions aequilibrae/paths/linear_approximation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
from aequilibrae.paths.traffic_assignment import TrafficAssignment

from aequilibrae.utils.signal import SIGNAL
from aequilibrae.utils.interface.worker_thread import WorkerThread
from aequilibrae.utils.python_signal import PythonSignal


class LinearApproximation:
class LinearApproximation(WorkerThread):
def __init__(self, assig_spec, algorithm, project=None) -> None:
WorkerThread.__init__(self, None)
self.equilibration = SIGNAL(object)
self.assignment = SIGNAL(object)
if isinstance(self.assignment, PythonSignal):
self.assignment.pos = 1

self.assignment.emit(["set_position", 1])
self.logger = project.logger if project else logging.getLogger("aequilibrae")

self.project_path = project.project_base_path if project else gettempdir()
Expand Down Expand Up @@ -471,6 +471,7 @@ def execute(self): # noqa: C901
self.equilibration.emit(["start", self.max_iter, "Equilibrium Assignment"])
self.logger.info(f"{self.algorithm} Assignment STATS")
self.logger.info("Iteration, RelativeGap, stepsize")
self.assignment.emit(["start", c.matrix.zones, "All-or-Nothing"])
for self.iter in range(1, self.max_iter + 1): # noqa: B020
self.iteration_issue = []
self.equilibration.emit(["key_value", "rgap", self.rgap])
Expand All @@ -481,7 +482,6 @@ def execute(self): # noqa: C901
self.__maybe_create_path_file_directories()

for c in self.traffic_classes: # type: TrafficClass
self.assignment.emit(["start", c.matrix.zones, "All-or-Nothing"])
# cost = c.fixed_cost / c.vot + self.congested_time # now only once
cost = c.fixed_cost + self.congested_time
aggregate_link_costs(cost, c.graph.compact_cost, c.results.crosswalk)
Expand Down
Empty file.
28 changes: 28 additions & 0 deletions aequilibrae/utils/interface/worker_thread.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from aequilibrae.utils.qgis_utils import inside_qgis

if inside_qgis:
from PyQt5.QtCore import QThread
from PyQt5.QtCore import pyqtSignal
else:

class QThread: # type: ignore
def __init__(self, *arg):
pass


class WorkerThread(QThread):
if inside_qgis:
jobFinished = pyqtSignal(object)

def __init__(self, parentThread):
QThread.__init__(self, parentThread)

def run(self):
self.running = True
success = self.doWork()
if inside_qgis:
self.jobFinished.emit(success)

def stop(self):
self.running = False
pass
22 changes: 16 additions & 6 deletions aequilibrae/utils/python_signal.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import importlib.util as iutil
import os
import warnings
from random import choice

Expand All @@ -13,8 +14,7 @@

qgis = iutil.find_spec("qgis") is not None

if missing_tqdm and not qgis:
warnings.warn("No progress bars will be shown. Please install tqdm to see them")
show_status = os.environ.get("AEQ_SHOW_PROGRESS", "FALSE") == "TRUE"


class PythonSignal: # type: ignore
Expand All @@ -28,23 +28,26 @@ class PythonSignal: # type: ignore
['action', 'bar hierarchy', 'value', 'text', 'master']

'action': 'start', 'update', or 'finished_*_processing' (the last one applies in QGIS)
'bar hierarchy': 'master' or 'secondary'
'position': Position (0 for top, 1 for bottom)
'value': Numerical value for the action (total or current)
'text': Whatever label to be updated
'master': The corresponding master bar for this task
"""

deactivate = missing_tqdm # by default don't use progress bars in tests
deactivate = not show_status # by default don't use progress bars in tests

def __init__(self, object):
self.color = choice(["green", "magenta", "cyan", "blue", "red", "yellow"])
self.pbar = None # type: tqdm
self.keydata = {}
self.pos = 0
self.position = 0

def emit(self, val):
if self.deactivate:
return
if val[0] == "set_position":
self.position = val[1]

if val[0] == "finished":
if self.pbar is not None:
self.pbar.close()
Expand All @@ -61,14 +64,21 @@ def emit(self, val):
self.keydata[val[1]] = val[2]

elif val[0] == "start":
if missing_tqdm and not qgis:
warnings.warn("No progress bars will be shown. Please install tqdm to see them")
if self.pbar is not None:
self.pbar.close()
desc = str(val[2]).rjust(50)
self.pbar = tqdm(total=val[1], colour=self.color, leave=False, desc=desc, position=self.pos)
self.pbar = tqdm(total=val[1], colour=self.color, leave=False, desc=desc, position=self.position)

elif val[0] == "update":
self.pbar.update(val[1] - self.pbar.n)
if len(val) > 2:
desc = str(val[2]).rjust(50)
if self.pbar.desc != desc:
self.pbar.set_description(desc, refresh=True)

elif val[0] == "set_text":
desc = str(val[1]).rjust(50)
if self.pbar.desc != desc:
self.pbar.set_description(desc, refresh=True)
4 changes: 2 additions & 2 deletions aequilibrae/utils/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ def noop(_):
pass


if iutil.find_spec("PyQt5") is not None:
from PyQt5.QtCore import pyqtSignal as SIGNAL # type: ignore
if iutil.find_spec("qgis") is not None:
from PyQt5.QtCore import pyqtSignal as SIGNAL

noop(SIGNAL.__class__) # This should be no-op but it stops PyCharm from "optimising" the above import
else:
Expand Down
4 changes: 0 additions & 4 deletions tests/aequilibrae/project/test_osm_downloader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import importlib.util as iutil
import os
from random import random
from tempfile import gettempdir
Expand All @@ -8,9 +7,6 @@

from aequilibrae.project.network.osm.osm_downloader import OSMDownloader

spec = iutil.find_spec("PyQt5")
pyqt = spec is not None


class TestOSMDownloader(TestCase):
def setUp(self) -> None:
Expand Down