aequilibrae.distribution#

Classes

GravityApplication([project])

Applies a synthetic gravity model.

GravityCalibration([project])

Calibrate a traditional gravity model

Ipf([project])

Iterative proportional fitting procedure

SyntheticGravityModel()

Simple class object to represent synthetic gravity models

class aequilibrae.distribution.GravityApplication(project=None, **kwargs)#

Applies a synthetic gravity model.

Model is an instance of SyntheticGravityModel class.

Impedance is an instance of AequilibraeMatrix.

Vectors are a pandas DataFrame.

>>> import pandas as pd
>>> from aequilibrae.distribution import SyntheticGravityModel, GravityApplication

>>> project = create_example(project_path)

# We define the model we will use
>>> model = SyntheticGravityModel()

# Before adding a parameter to the model, you need to define the model functional form
# You can select one of GAMMA, EXPO or POWER.
>>> model.function = "GAMMA"

# Only the parameter(s) applicable to the chosen functional form will have any effect
>>> model.alpha = 0.1
>>> model.beta = 0.0001

# We load the impedance matrix
>>> matrix = project.matrices.get_matrix("skims.omx", subset=["distance_blended"])

# We create the vectors we will use
>>> query = "SELECT zone_id, population, employment FROM zones;"
>>> with project.db_connection as conn:
...     df = pd.read_sql(query, conn)
>>> df.sort_values(by="zone_id", inplace=True)
>>> df.set_index("zone_id", inplace=True)

# You create the vectors you would have
>>> df = df.assign(productions=df.population * 3.0)
>>> df = df.assign(attractions=df.employment * 4.0)
>>> vectors = df[["productions", "attractions"]]

# Balance the vectors
>>> vectors["attractions"] *= vectors["productions"].sum() / vectors["attractions"].sum()

# Create the problem object
>>> args = {"impedance": matrix,
...         "vectors": vectors,
...         "row_field": "productions",
...         "model": model,
...         "column_field": "attractions",
...         "nan_as_zero":True
...         }
>>> gravity = GravityApplication(**args)

# Solve and save the outputs
>>> gravity.apply()
>>> gravity.output.save_as_omx(project_path / 'matrices' / 'gravity_omx.omx')

>>> project.close()
apply()#

Runs the Gravity Application instance as instantiated

Resulting matrix is the output class member

save_to_project(file_name: str, project=None) → None#

Saves the matrix output to the project file

Arguments:

file_name (str): Name for the OMX matrix file

project (Project, Optional): Project we want to save the results to. Defaults to the explicitly supplied constructor project

class aequilibrae.distribution.GravityCalibration(project=None, **kwargs)[source]#

Calibrate a traditional gravity model

Available deterrence function forms are: ‘EXPO’, ‘POWER’ or ‘GAMMA’.

>>> from aequilibrae.distribution import GravityCalibration

>>> project = create_example(project_path)

# We load the demand matrix
>>> demand = project.matrices.get_matrix("demand.omx", subset=["matrix"])

# We load the skim matrix
>>> skim = project.matrices.get_matrix("skims.omx", subset=["time_final"])

>>> args = {"matrix": demand,
...         "impedance": skim,
...         "row_field": "productions",
...         "function": 'expo',
...         "nan_as_zero": True}
>>> gravity = GravityCalibration(**args)

# Solve and save outputs
>>> gravity.calibrate()
>>> gravity.model.save(project_path / 'dist_expo_model.mod')

>>> project.close()
calibrate()[source]#

Calibrate the model

Resulting model is in output class member

class aequilibrae.distribution.Ipf(project=None, **kwargs)[source]#

Iterative proportional fitting procedure

>>> from aequilibrae.distribution import Ipf

>>> project = create_example(project_path)

>>> matrix = project.matrices.get_matrix("demand.omx", subset=["matrix"])

>>> vectors = pd.DataFrame(
...     {"productions": np.zeros(len(matrix.index)), "attractions": np.zeros(len(matrix.index))},
...     index=matrix.index,
... )

>>> core = next(iter(matrix))
>>> vectors["productions"] = matrix[core].sum(axis=1)
>>> vectors["attractions"] = matrix[core].sum(axis=0)

>>> ipf_args = {"matrix": matrix,
...             "vectors": vectors,
...             "row_field": "productions",
...             "column_field": "attractions",
...             "nan_as_zero": False}

>>> fratar = Ipf(**ipf_args)
>>> fratar.fit()

# We can get back to our OMX matrix in the end
>>> fratar.output.save_as_omx(Path(my_folder_path) / "to_omx_output.omx")

>>> project.close()
fit()[source]#

Runs the IPF instance problem to adjust the matrix

Resulting matrix is the output class member

save_to_project(file_name: str, project=None) → None[source]#

Saves the matrix output to the project file

Arguments:

file_name (str): Name for the OMX matrix file

project (Project, Optional): Project we want to save the results to. Defaults to the explicitly supplied constructor project

class aequilibrae.distribution.SyntheticGravityModel[source]#

Simple class object to represent synthetic gravity models

load(file_name)[source]#

Loads model from disk. Extension is *.mod

save(file_name)[source]#

Saves model to disk in yaml format. Extension is *.mod