Skip to content

Commit

Permalink
new matrix API (#496)
Browse files Browse the repository at this point in the history
* new matrix API

* suffixes

---------

Co-authored-by: pveigadecamargo <pveigadecamargo@anl.gov>
  • Loading branch information
pedrocamargo and pveigadecamargo authored Jan 29, 2024
1 parent 4b97e4f commit eb8800f
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 13 deletions.
1 change: 1 addition & 0 deletions aequilibrae/distribution/gravity_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
The procedures implemented in this code are some of those suggested in
Modelling Transport, 4th Edition, Ortuzar and Willumsen, Wiley 2011
"""

from time import perf_counter

import numpy as np
Expand Down
51 changes: 44 additions & 7 deletions aequilibrae/matrix/aequilibrae_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import uuid
import warnings
from functools import reduce
from pathlib import Path
from typing import List

import numpy as np
Expand Down Expand Up @@ -90,14 +91,18 @@ def __init__(self):
self.omx_file = None # type: omx.File
self.__version__ = VERSION # Writes file version

def save(self, names=()) -> None:
def save(self, names=(), file_name=None) -> None:
"""Saves matrix data back to file.
If working with AEM file, it flushes data to disk. If working with OMX, requires new names.
:Arguments:
**names** (:obj:`tuple(str)`, `Optional`): New names for the matrices. Required if working with OMX files
"""
if file_name is not None:
cores = names if len(names) else self.names
self.__save_as(file_name, cores)
return

if not self.__omx:
self.__flush(self.matrices)
Expand All @@ -122,6 +127,38 @@ def save(self, names=()) -> None:
self.names = self.omx_file.list_matrices()
self.computational_view(names)

def __save_as(self, file_name: str, cores: List[str]):

if Path(file_name).suffix.lower() == ".aem":
mat = AequilibraeMatrix()
args = {
"zones": self.zones,
"matrix_names": cores,
"index_names": self.index_names,
"memory_only": False,
"file_name": file_name,
}
mat.create_empty(**args)
mat.indices[:, :] = self.indices[:, :]
for core in cores:
mat.matrix[core][:, :] = self.matrix[core][:, :]
mat.name = self.name
mat.description = self.description
mat.close()
del mat

elif Path(file_name).suffix.lower() == ".omx":
omx_mat = omx.open_file(file_name, "w")
for core in cores:
omx_mat[core] = self.matrix[core]

for index in self.index_names:
omx_mat.create_mapping(index, self.indices[index])

omx_mat.attrs.name = self.name
omx_mat.attrs.description = self.description
omx_mat.close()

def create_empty(
self,
file_name: str = None,
Expand Down Expand Up @@ -570,9 +607,9 @@ def __write__(self):
np.memmap(self.file_path, dtype="uint8", offset=17, mode="r+", shape=1)[0] = data_size

# matrix name
np.memmap(self.file_path, dtype="S" + str(MATRIX_NAME_MAX_LENGTH), offset=18, mode="r+", shape=1)[
0
] = self.name
np.memmap(self.file_path, dtype="S" + str(MATRIX_NAME_MAX_LENGTH), offset=18, mode="r+", shape=1)[0] = (
self.name
)

# matrix description
offset = 18 + MATRIX_NAME_MAX_LENGTH
Expand Down Expand Up @@ -1095,9 +1132,9 @@ def setName(self, matrix_name: str):
if len(str(matrix_name)) > MATRIX_NAME_MAX_LENGTH:
matrix_name = str(matrix_name)[0:MATRIX_NAME_MAX_LENGTH]

np.memmap(self.file_path, dtype="S" + str(MATRIX_NAME_MAX_LENGTH), offset=18, mode="r+", shape=1)[
0
] = matrix_name
np.memmap(self.file_path, dtype="S" + str(MATRIX_NAME_MAX_LENGTH), offset=18, mode="r+", shape=1)[0] = (
matrix_name
)

def setDescription(self, matrix_description: str):
"""
Expand Down
1 change: 1 addition & 0 deletions aequilibrae/paths/results/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
STILL NEED TO ADD SOME EXPLANATIONS HERE
"""

__author__ = "Pedro Camargo ($Author: Pedro Camargo $)"
__version__ = "1.0"
__revision__ = "$Revision: 1 $"
Expand Down
1 change: 1 addition & 0 deletions aequilibrae/project/network/osm_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
For the original work, please see https://github.com/gboeing/osmnx
"""

import logging
import time
import re
Expand Down
6 changes: 3 additions & 3 deletions aequilibrae/transit/gtfs_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ def __deconflict_stop_times(self) -> None:
)

for _, rec in max_speeds.iterrows():
df.loc[
(df.dist >= rec.min_distance) & ((df.dist < rec.max_distance)), "max_speed"
] = rec.speed
df.loc[(df.dist >= rec.min_distance) & ((df.dist < rec.max_distance)), "max_speed"] = (
rec.speed
)

to_fix = df[df.max_speed < df.speed].index.values
if to_fix.shape[0] > 0:
Expand Down
8 changes: 5 additions & 3 deletions aequilibrae/transit/transit_graph_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1428,9 +1428,11 @@ def to_transit_graph(self) -> TransitGraph:
g.network["id"] = g.network.link_id
g.prepare_graph(
self.vertices[
(self.vertices.node_type == "origin")
if self.blocking_centroid_flows
else (self.vertices.node_type == "od")
(
(self.vertices.node_type == "origin")
if self.blocking_centroid_flows
else (self.vertices.node_type == "od")
)
].node_id.values
)
g.set_graph("trav_time")
Expand Down
1 change: 1 addition & 0 deletions aequilibrae/utils/worker_thread.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Original Author: UNKNOWN. COPIED FROM STACKOVERFLOW BUT CAN'T REMEMBER EXACTLY WHERE
"""

import importlib.util as iutil

spec = iutil.find_spec("PyQt5")
Expand Down

0 comments on commit eb8800f

Please sign in to comment.