Skip to content

Commit

Permalink
Addresses issue 158 (#159)
Browse files Browse the repository at this point in the history
* Addresses issue 158

* Style changes
  • Loading branch information
pedrocamargo authored Jul 7, 2020
1 parent de382aa commit 2cc9292
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 37 deletions.
22 changes: 14 additions & 8 deletions aequilibrae/paths/AoN.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ include 'basic_path_finding.pyx'
include 'bpr.pyx'
include 'parallel_numpy.pyx'

from libc.stdlib cimport abort, malloc, free
from .__version__ import binary_version as VERSION_COMPILED

def one_to_all(origin, matrix, graph, result, aux_result, curr_thread):
Expand Down Expand Up @@ -209,14 +208,14 @@ def path_computation(origin, destination, graph, results):
origin_index = graph.nodes_to_indices[orig]
dest_index = graph.nodes_to_indices[dest]
if results.__graph_id__ != graph.__id__:
return "Results object not prepared. Use --> results.prepare(graph)"
raise ValueError("Results object not prepared. Use --> results.prepare(graph)")

# Consistency checks
# if origin >= graph.fs.shape[0]:
# raise ValueError ("Node " + str(origin) + " is outside the range of nodes in the graph")

if VERSION_COMPILED != graph.__version__:
return 'This graph was created for a different version of AequilibraE. Please re-create it'
raise ValueError('This graph was created for a different version of AequilibraE. Please re-create it')

#We transform the python variables in Cython variables
nodes = graph.num_nodes
Expand Down Expand Up @@ -286,6 +285,7 @@ def path_computation(origin, destination, graph, results):

if predecessors_view[dest_index] > 0:
all_connectors = []
link_directions = []
all_nodes = [dest_index]
mileposts = []
p = dest_index
Expand All @@ -294,11 +294,13 @@ def path_computation(origin, destination, graph, results):
p = predecessors_view[p]
connector = conn_view[dest_index]
all_connectors.append(graph.graph['link_id'][connector])
link_directions.append(graph.graph['direction'][connector])
mileposts.append(g_view[connector])
all_nodes.append(p)
dest_index = p
results.path = np.asarray(all_connectors, graph.default_types('int'))[::-1]
results.path_nodes = graph.all_nodes[np.asarray(all_nodes, graph.default_types('int'))][::-1]
results.path_link_directions = np.asarray(link_directions, graph.default_types('int'))[::-1]
mileposts.append(0)
results.milepost = np.cumsum(mileposts[::-1])

Expand Down Expand Up @@ -328,6 +330,7 @@ def update_path_trace(results, destination, graph):
results.path_nodes = None
if results.predecessors[dest_index] > 0:
all_connectors = []
link_directions = []
all_nodes = [dest_index]
mileposts = []
p = dest_index
Expand All @@ -336,10 +339,12 @@ def update_path_trace(results, destination, graph):
p = results.predecessors[p]
connector = results.connectors[dest_index]
all_connectors.append(graph.graph['link_id'][connector])
link_directions.append(graph.graph['direction'][connector])
mileposts.append(graph.cost[connector])
all_nodes.append(p)
dest_index = p
results.path = np.asarray(all_connectors, graph.default_types('int'))[::-1]
results.path_link_directions = np.asarray(link_directions, graph.default_types('int'))[::-1]
results.path_nodes = graph.all_nodes[np.asarray(all_nodes, graph.default_types('int'))][::-1]
mileposts.append(0)
results.milepost = np.cumsum(mileposts[::-1])
Expand All @@ -359,19 +364,20 @@ def skimming_single_origin(origin, graph, result, aux_result, curr_thread):
graph_fs = graph.fs

if result.__graph_id__ != graph.__id__:
return "Results object not prepared. Use --> results.prepare(graph)"

raise ValueError("Results object not prepared. Use --> results.prepare(graph)")

if orig not in graph.centroids:
return "Centroid " + str(orig) + " is outside the range of zones in the graph"
raise ValueError("Centroid " + str(orig) + " is outside the range of zones in the graph")

if origin_index > graph.num_nodes:
return "Centroid " + str(orig) + " does not exist in the graph"
raise ValueError("Centroid " + str(orig) + " does not exist in the graph")

if graph_fs[origin_index] == graph_fs[origin_index + 1]:
return "Centroid " + str(orig) + " does not exist in the graph"
raise ValueError("Centroid " + str(orig) + " does not exist in the graph")

if VERSION_COMPILED != graph.__version__:
return 'This graph was created for a different version of AequilibraE. Please re-create it'
raise ValueError('This graph was created for a different version of AequilibraE. Please re-create it')

nodes = graph.num_nodes + 1
zones = graph.num_zones
Expand Down
6 changes: 3 additions & 3 deletions aequilibrae/paths/all_or_nothing.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ def execute(self):
if pyqt:
self.assignment.emit(["finished_threaded_procedure", None])

def func_assig_thread(self, O, all_threads):
def func_assig_thread(self, origin, all_threads):
thread_id = threading.get_ident()
th = all_threads.get(thread_id, all_threads["count"])
if th == all_threads["count"]:
all_threads[thread_id] = all_threads["count"]
all_threads["count"] += 1

x = one_to_all(O, self.matrix, self.graph, self.results, self.aux_res, th)
x = one_to_all(origin, self.matrix, self.graph, self.results, self.aux_res, th)
self.cumulative += 1
if x != O:
if x != origin:
self.report.append(x)
if pyqt:
self.assignment.emit(["zones finalized", self.cumulative])
Expand Down
2 changes: 1 addition & 1 deletion aequilibrae/paths/linear_approximation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from scipy.optimize import root as root_scalar

recent_scipy = False
logger.warning(f"Using older version of Scipy. For better performance, use Scipy >= 1.4")
logger.warning("Using older version of Scipy. For better performance, use Scipy >= 1.4")

if False:
from aequilibrae.paths.traffic_assignment import TrafficAssignment
Expand Down
6 changes: 3 additions & 3 deletions aequilibrae/paths/network_skimming.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ def execute(self):
self.skimming.emit(["text skimming", "Saving Outputs"])
self.skimming.emit(["finished_threaded_procedure", None])

def func_skim_thread(self, O, all_threads):
def func_skim_thread(self, origin, all_threads):
if threading.get_ident() in all_threads:
th = all_threads[threading.get_ident()]
else:
all_threads[threading.get_ident()] = all_threads["count"]
th = all_threads["count"]
all_threads["count"] += 1
x = skimming_single_origin(O, self.graph, self.results, self.aux_res, th)
x = skimming_single_origin(origin, self.graph, self.results, self.aux_res, th)
self.cumulative += 1
if x != O:
if x != origin:
self.report.append(x)
if pyqt:
self.skimming.emit(["zones finalized", self.cumulative])
Expand Down
2 changes: 1 addition & 1 deletion aequilibrae/paths/parameters.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ cdef double INFINITE = 1.79769313e+308

VERSION = 0.6
MINOR_VRSN = 5
binary_version = "0.6.1"
binary_version = "0.6.2"
release_name = "Rio de Janeiro"
8 changes: 7 additions & 1 deletion aequilibrae/paths/results/path_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ class PathResults:
res.prepare(car_graph)
res.compute_path(17, 13199)
# res.milepost, res.path_nodes, res.path, res.skims contain all results
# res.milepost contains the milepost corresponding to each node along the path
# res.path_nodes contains the sequence of nodes that form the path
# res.path contains the sequence of links that form the path
# res.path_link_directions contains the link directions corresponding to the above links
# res.skims contain all skims requested when preparing the graph
# Update all the outputs mentioned above for destination 1265. Same origin: 17
res.update_trace(1265)
Expand All @@ -45,6 +49,7 @@ def __init__(self) -> None:
self.skims = None
self.path = None
self.path_nodes = None
self.path_link_directions = None
self.milepost = None
self.reached_first = None
self.origin = None
Expand Down Expand Up @@ -105,6 +110,7 @@ def reset(self) -> None:
self.skims.fill(np.inf)
self.path = None
self.path_nodes = None
self.path_link_directions = None
self.milepost = None

else:
Expand Down
4 changes: 2 additions & 2 deletions aequilibrae/project/network/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ def build_graphs(self) -> None:

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))
for link in raw_links:
lk = list(map(lambda x: np.nan if x is None else x, link))
links.append(lk)

data = np.core.records.fromrecords(links, names=all_fields)
Expand Down
2 changes: 1 addition & 1 deletion aequilibrae/project/network/osm_utils/place_getter.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ def placegetter(place: str) -> Tuple[Union[None, List[float]], list]:
if attempts == max_attempts - 1 and bbox is None:
report.append("Reached maximum download attempts. Please wait a few minutes and try again")
else:
report.append(f"We got an error for place query.")
report.append("We got an error for place query.")

return (bbox, report)
46 changes: 29 additions & 17 deletions tests/aequilibrae/paths/test_pathResults.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from aequilibrae.paths import path_computation, Graph
from aequilibrae.paths.results import PathResults
from ...data import test_graph
import numpy as np

# Adds the folder with the data to the path and collects the paths to the files
lib_path = os.path.abspath(os.path.join("..", "../tests"))
Expand All @@ -15,7 +16,7 @@


class TestPathResults(TestCase):
def test_prepare(self):
def setUp(self) -> None:
# graph
self.g = Graph()
self.g.load_from_disk(test_graph)
Expand All @@ -28,18 +29,25 @@ def test_prepare(self):
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__()))
self.r.compute_path(dest, origin)
self.r.reset()

self.assertEqual(self.r.path, None, 'Fail to reset the Path computation object')
self.assertEqual(self.r.path_nodes, None, 'Fail to reset the Path computation object')
self.assertEqual(self.r.path_link_directions, None, 'Fail to reset the Path computation object')
self.assertEqual(self.r.milepost, None, 'Fail to reset the Path computation object')
self.assertEqual(self.r.predecessors.max(), -1, 'Fail to reset the Path computation object')
self.assertEqual(self.r.predecessors.min(), -1, 'Fail to reset the Path computation object')
self.assertEqual(self.r.connectors.max(), -1, 'Fail to reset the Path computation object')
self.assertEqual(self.r.connectors.min(), -1, 'Fail to reset the Path computation object')
self.assertEqual(self.r.skims.max(), np.inf, 'Fail to reset the Path computation object')
self.assertEqual(self.r.skims.min(), np.inf, 'Fail to reset the Path computation object')

new_r = PathResults()
with self.assertRaises(ValueError):
new_r.reset()

def test_compute_paths(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)

Expand All @@ -63,13 +71,14 @@ def test_compute_paths(self):
if list(self.r.milepost) != [0, 341, 1398, 2162]:
self.fail("Path computation failed. Wrong milepost results")

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__()))
if list(self.r.path_link_directions) != [-1, -1, -1]:
self.fail("Path computation failed. Wrong link directions")

self.r.compute_path(dest, origin)
if list(self.r.path_link_directions) != [1, 1, 1]:
self.fail("Path computation failed. Wrong link directions")

def test_update_trace(self):
self.r.compute_path(origin, dest - 1)

self.r.update_trace(dest)
Expand All @@ -82,3 +91,6 @@ def test_update_trace(self):

if list(self.r.milepost) != [0, 341, 1398, 2162]:
self.fail("Path computation failed. Wrong milepost results")

if list(self.r.path_link_directions) != [-1, -1, -1]:
self.fail("Path computation failed. Wrong link directions")
Binary file modified tests/data/test_graph.aeg
Binary file not shown.

0 comments on commit 2cc9292

Please sign in to comment.