Python: from function to attribute

Technical Note #17

August 2016
By Grant MacKinnon

Most users are familiar with creating their own attributes within objects within an Aimsun model. These will typically be numbers or strings but it is also possible to add a column to an object where a value is returned based on some Python Code, allowing a value to be calculated on the fly.


A simple example is one that places a label letter on a turn to say whether it is Left, Through, Right or U-Turn.



Aimsun Python function attribute Showing the attribute in the network

Click to enlarge: Showing the attribute in the network


Firstly we could create this column using the Types Editor (by selecting the relevant object type from the Types window), then adding a new column of Column Type Function, in this case one that returns a string as a result.



Aimsu Python function attribute Showing the attribute in the network

Click to enlarge: Create the column using the Types Editor


The function needs to contain a Python function with signature of the form:

def eval_attr(object):

which returns a value with the result.


Here is the code we store inside the column

def determineTurningLetterForAngle(angle):
	dirString = ""
	if abs(angle)<30.0: dirString+="T" elif abs(angle)>150.0:< dirString+="U" elif angle>0.0:
		dirString+="L"
	else:
		dirString+="R"
	return dirString

def retrieveTurningLetterForTurning(turning):
	angle = turning.calcAngleSections()
	directionString = determineTurningLetterForAngle(angle)
	return directionString
	
def eval_attr(object):
	res = retrieveTurningLetterForTurning(object)
	return res


However we can create all this from a script which creates the column, and populates the code.
The code to set up the column directly from a Python script is as follows:


def main():
	turnLetterAttr = model.getColumn("GKTurning::TurnLetter")
	if turnLetterAttr == None:
		turningType = model.getType("GKTurning")
		turnLetterAttr = 
turningType.addColumn("GKTurning::TurnLetter","TurnLetter", 
GKColumn.String, GKColumn.ePython)
	turnLetterAttr.setPythonCode(PYTHON_CODE_STRING)

main()

PYTHON_CODE_STRING contains the definition of the function.


Download Python script for creating a turning classification label

More technical notes

Componentes de función macro

Marzo 2016: Marga Delgado explica cómo usar los componentes de función macro para crear elementos de salida adicionales a la hora de llevar a cabo una asignación.

  • ¿Tienes alguna pregunta? Ponte en contacto.

    ¡Estamos aquí para ayudarte!

  • ¿Tienes alguna pregunta? Ponte en contacto.

    ¡Estamos aquí para ayudarte!

SHARE

Citar Aimsun Next

Aimsun Next 23

Aimsun (2023). Aimsun Next 23 Manual del usuario, Aimsun Next Versión 23.0.0, Barcelona, España. Acceso: 19, 2023. [Online].
Disponible en: https://docs.aimsun.com/next/23.0.0/


Aimsun Next 20.0.5

Aimsun (2021). Aimsun Next 20.0.5 Manual del usuario, Aimsun Next Versión 20.0.3, Barcelona, España. Acceso: May. 1, 2021. [En software].
Disponible: qthelp://aimsun.com.aimsun.20.0/doc/UsersManual/Intro.html

Aimsun Next 23

@manual {​​​​​​​​AimsunManual,

título = {​​​​​​​​Aimsun Next 23 User's Manual}​​​​​,
autor = {​​​​​​​​Aimsun}​​​​​​​​,
edición = {​​​​​​​​​​​​​​​Aimsun Next 23.0.0}​​​​​​​​​​​​​​​,
domicilio = {​​​​​​​​​​​​​​​Barcelona, Spain}​​​​​​​​​​​​​​​,
año = {​​​​​​​​​​​​​​​2023. [Online]}​​​​​​​​​​​​​​​,
mes = {​​​​​​​​​​​​​​​Accessed on: Month, Day, Year}​​​​​​​​​​​​​​​,
url = {​​​​​​​​​​​​​​​https://docs.aimsun.com/next/23.0.0/}​​​​​​​​​​​​​​​,
}​​​​​​​​​​​​​​​


Aimsun Next 20.0.5

@manual {​​​​​​​​AimsunManual,

título = {​​​​​​​​Aimsun Next 20.0.5 User's Manual}​​​​​​​​,
autor = {​​​​​​​​Aimsun}​​​​​​​​,
edición = {​​​​​​​​​​​​​​​Aimsun Next 20.0.5}​​​​​​​​​​​​​​​,
domicilio = {​​​​​​​​​​​​​​​Barcelona, Spain}​​​​​​​​​​​​​​​,
año = {​​​​​​​​​​​​​​​2021. [En software]}​​​​​​​​​​​​​​​,
mes = {​​​​​​​​​​​​​​​Accessed on: Month, Day, Year}​​​​​​​​​​​​​​​,
url = {​​​​​​​​​​​​​​​qthelp://aimsun.com.aimsun.20.0/doc/UsersManual/Intro.html}​​​​​​​​​​​​​​​,
}​​​​​​​​​​​​​​​

Aimsun Next 23

TY - COMP
T1 - Manual del usuario de Aimsun Next 23
A1 - Aimsun
ET - Aimsun Next Version 23.0.0
Y1 - 2023
Y2 - Acceso: Mes, Día, Año
CY - Barcelona, España
PB - Aimsun
UR - [En software]. Disponible en: https://docs.aimsun.com/next/23.0.0/


Aimsun Next 20.0.5

TY - COMP
T1 - Manual del usuario de Aimsun Next 20.0.5
A1 - Aimsun
ET - Aimsun Next Version 20.0.5
Y1 - 2021
Y2 - Acceso: Mes, Día, Año
CY - Barcelona, España
PB - Aimsun
UR - [In software]. Available: qthelp://aimsun.com.aimsun.20.0/doc/UsersManual/Intro.html