9.4.1. aequilibrae.AequilibraeMatrix

class aequilibrae.AequilibraeMatrix

Bases: object

Matrix class

__init__()

Creates a memory instance for a matrix, that can be used to load an existing matrix or to create an empty one

Methods

__init__()

Creates a memory instance for a matrix, that can be used to load an existing matrix or to create an empty one

close()

Removes matrix from memory and flushes all data to disk, or closes the OMX file if that is the case

columns()

Returns column vector for the matrix in the computational view

computational_view([core_list])

Creates a memory view for a list of matrices that is compatible with Cython memory buffers

copy([output_name, cores, names, compress])

Copies a list of cores (or all cores) from one matrix file to another one

create_empty([file_name, zones, …])

Creates an empty matrix in the AequilibraE format

create_from_omx(file_path, omx_path[, …])

Creates an AequilibraeMatrix from an original OpenMatrix

export(output_name[, cores])

Exports the matrix to other formats.

get_matrix(core[, copy])

Returns the data for a matrix core

load(file_path)

Loads matrix from disk.

nan_to_num()

Converts all NaN values in all cores in the computational view to zeros

random_name()

Returns a random name for a matrix with root in the temp directory of the user

rows()

Returns row vector for the matrix in the computational view

setDescription(matrix_description)

Sets description for the matrix

setName(matrix_name)

Sets the name for the matrix itself

set_index(index_to_set)

Sets the standard index to be the one the user wants to have be the one being used in all operations during run time.

__init__()

Creates a memory instance for a matrix, that can be used to load an existing matrix or to create an empty one

create_empty(file_name: str = None, zones: int = None, matrix_names: List[str] = None, data_type: numpy.dtype = <class 'numpy.float64'>, index_names: List[str] = None, compressed: bool = False)

Creates an empty matrix in the AequilibraE format

Args:

file_name (str): Local path to the matrix file

zones (int): Number of zones in the model (Integer). Maximum number of zones in a matrix is 4,294,967,296

matrix_names (list): A regular Python list of names of the matrix. Limit is 50 characters each. Maximum number of cores per matrix is 256

file_name (str): Local path to the matrix file

data_type (np.dtype, optional): Data type of the matrix as NUMPY data types (NP.int32, np.int64, np.float32, np.float64). Defaults to np.float64

index_names (list, optional): A regular Python list of names for indices. Limit is 20 characters each). Maximum number of indices per matrix is 256

compressed (bool, optional): Whether it is a flat matrix or a compressed one (Boolean - Not yet implemented)

  zones_in_the_model = 3317
  names_list = ['Car trips', 'pt trips', 'DRT trips', 'bike trips', 'walk trips']

  mat = AequilibraeMatrix()
  mat.create_empty(file_name='my/path/to/file',
                   zones=zones_in_the_model,
                   matrix_names= names_list)
  mat.num_indices
1
  mat.zones
3317
  np.sum(mat[trips])
0.0
get_matrix(core: str, copy=False) → numpy.ndarray

Returns the data for a matrix core

Args:

core (str): name of the matrix core to be returned

copy (bool, optional): return a copy of the data. Defaults to False

Returns

object (np.ndarray): NumPy array

create_from_omx(file_path: str, omx_path: str, cores: List[str] = None, mappings: List[str] = None, robust: bool = True, compressed: bool = False) → None

Creates an AequilibraeMatrix from an original OpenMatrix

Args:

file_path (str): Path for the output AequilibraEMatrix

omx_path (str): Path to the OMX file one wants to import

cores (list): List of matrix cores to be imported

mappings (list): List of the matrix mappings (i.e. indices, centroid numbers) to be imported

robust (bool, optional): Boolean for whether AequilibraE should try to adjust the names for cores and indices in case they are too long. Defaults to True

compressed (bool, optional): Boolean for whether we should compress the output matrix. Not yet implemented

set_index(index_to_set: str) → None

Sets the standard index to be the one the user wants to have be the one being used in all operations during run time. The first index is ALWAYS the default one every time the matrix is instantiated

Args:

index_to_set (str): Name of the index to be used. The default index name is ‘main_index’

  zones_in_the_model = 3317
  names_list = ['Car trips', 'pt trips', 'DRT trips', 'bike trips', 'walk trips']
  index_list = ['tazs',  'census']

  mat = AequilibraeMatrix()
  mat.create_empty(file_name='my/path/to/file',
                   zones=zones_in_the_model,
                   matrix_names=names_list,
                   index_names =index_list )
  mat.num_indices
2
  mat.current_index
'tazs'
  mat.set_index('census')
  mat.current_index
'census'
close()

Removes matrix from memory and flushes all data to disk, or closes the OMX file if that is the case

export(output_name: str, cores: List[str] = None)

Exports the matrix to other formats. Formats currently supported: CSV, OMX

When exporting to AEM or OMX, the user can chose to export only a set of cores, but all indices are exported

When exporting to CSV, the active index will be used, and all cores will be exported as separate columns in the output file

Args:

output_name (str): Path to the output file

cores (list): Names of the cores to be exported.

  zones_in_the_model = 3317
  names_list = ['Car trips', 'pt trips', 'DRT trips', 'bike trips', 'walk trips']

  mat = AequilibraeMatrix()
  mat.create_empty(file_name='my/path/to/file', zones=zones_in_the_model, matrix_names= names_list)
  mat.cores
['Car trips', 'pt trips', 'DRT trips', 'bike trips', 'walk trips']

  mat.export('my_new_path', ['Car trips', 'bike trips'])

  mat2 = AequilibraeMatrix()
  mat2.load('my_new_path')
  mat2.cores
['Car trips', 'bike trips']
load(file_path: str)

Loads matrix from disk. All cores and indices are load. First index is default

Args:

file_path (str): Path to AEM or OMX file on disk

  zones_in_the_model = 3317
  names_list = ['Car trips', 'pt trips', 'DRT trips', 'bike trips', 'walk trips']

  mat = AequilibraeMatrix()
  mat.create_empty(file_name='my/path/to/file', zones=zones_in_the_model, matrix_names= names_list)
  mat.close()

  mat2 = AequilibraeMatrix()
  mat2.load('my/path/to/file.omx')
  mat2.zones
3317
computational_view(core_list: List[str] = None)

Creates a memory view for a list of matrices that is compatible with Cython memory buffers

It allows for AequilibraE matrices to be used in all parallelized algorithms within AequilibraE

In case of OMX matrices, the computational view is held only in memory

Args:

core_list (list): List with the names of all matrices that need to be in the buffer

  zones_in_the_model = 3317
  names_list = ['Car trips', 'pt trips', 'DRT trips', 'bike trips', 'walk trips']

  mat = AequilibraeMatrix()
  mat.create_empty(file_name='my/path/to/file', zones=zones_in_the_model, matrix_names= names_list)
  mat.computational_view(['bike trips', 'walk trips'])
  mat.view_names
['bike trips', 'walk trips']
copy(output_name: str = None, cores: List[str] = None, names: List[str] = None, compress: bool = None) → None

Copies a list of cores (or all cores) from one matrix file to another one

Args:

output_name (str): Name of the new matrix file

cores (list):List of the matrix cores to be copied

names (list, optional): List with the new names for the cores. Defaults to current names

compress (bool, optional): Whether you want to compress the matrix or not. Defaults to False Not yet implemented

  zones_in_the_model = 3317
  names_list = ['Car trips', 'pt trips', 'DRT trips', 'bike trips', 'walk trips']

  mat = AequilibraeMatrix()
  mat.create_empty(file_name='my/path/to/file', zones=zones_in_the_model, matrix_names= names_list)
  mat.copy('my/new/path/to/file', cores=['bike trips', 'walk trips'], names=['bicycle', 'walking'])

  mat2 = AequilibraeMatrix()
  mat2.load('my/new/path/to/file')
  mat.cores
['bicycle', 'walking']
rows() → numpy.ndarray

Returns row vector for the matrix in the computational view

Computational view needs to be set to a single matrix core

Returns

object (np.ndarray): the row totals for the matrix currently on the computational view

  mat = AequilibraeMatrix()
  mat.load('my/path/to/file')
  mat.computational_view(mat.cores[0])
  mat.rows()
array([0.,...,0.])
columns() → numpy.ndarray

Returns column vector for the matrix in the computational view

Computational view needs to be set to a single matrix core

Returns

object (np.ndarray): the column totals for the matrix currently on the computational view

  mat = AequilibraeMatrix()
  mat.load('my/path/to/file')
  mat.computational_view(mat.cores[0])
  mat.columns()
array([0.34,.0.,...,14.03])
nan_to_num()

Converts all NaN values in all cores in the computational view to zeros

mat = AequilibraeMatrix()
mat.load('my/path/to/file')
mat.computational_view(mat.cores[0])
mat.nan_to_num()
setName(matrix_name: str)

Sets the name for the matrix itself

Args:

matrix_name (str): matrix name. Maximum length is 50 characters

  mat = AequilibraeMatrix()
  mat.load('my/path/to/file')
  mat.setName('This is my example')
  mat.name
'This is my example'
setDescription(matrix_description: str)

Sets description for the matrix

Args:

matrix_description (str): Text with matrix description . Maximum length is 144 characters

  mat = AequilibraeMatrix()
  mat.load('my/path/to/file')
  mat.setDescription('This is some text about this matrix of mine')
  mat.description
'This is some text about this matrix of mine'
_AequilibraeMatrix__define_data_class()
_AequilibraeMatrix__vector(axis: int)
static random_name() → str

Returns a random name for a matrix with root in the temp directory of the user

  name = AequilibraeMatrix().random_name()
'/tmp/Aequilibrae_matrix_54625f36-bf41-4c85-80fb-7fc2e3f3d76e.aem'