udf_model

compiler

class BinOp(val: 'str', left: 't.Optional[Node]' = None, right: 't.Optional[Node]' = None)

Bases: Node

accept_children(visitor)

Branch nodes should override this to let the visitor visit the node’s children

left: Node | None = None
right: Node | None = None
class Func(val: 'str', args: 't.Tuple[Node, ...]' = ())

Bases: Node

accept_children(visitor)

Branch nodes should override this to let the visitor visit the node’s children

args: Tuple[Node, ...] = ()
class Node(val: 'str')

Bases: object

accept(visitor, top_down=False)

Accept a visitor and traverse the tree. Branch nodes must override Node.accept_children

Parameters:
  • visitor – the Visitor

  • top_down – whether to first the branch nodes and then the children (top_down=True) or first the children and then the branch nodes (top_down=False)

accept_children(visitor)

Branch nodes should override this to let the visitor visit the node’s children

accept_node(visitor)

Accept a visitor but do not traverse any children. The visitor is responsible for traversing the tree.

val: str
class NodeVisitor

Bases: object

visit(node: Node)
class Num(val: 'str')

Bases: Node

class Parser(tokenizer: Iterable)

Bases: object

A simple recursive descent parser

add_expr()

add_expr : [“+”|”-”] mul_expr ((“+” | “-”) mul_expr)*

atom()

factor : num | function_or_name | “(” expr “)”

comp_expr()

comp_expr: add_expr ((“==” | “!=”) add_expr)*

current_token: Token
error()
expect(*token_type: str)
expr()
function_or_name()

function_or_name : name “(” expr? (“,” expr)* “)” | name

ignore = ('ws',)
mul_expr()

mul_expr : atom ((MUL | DIV) atom)*

next_valid_token()
parse()
peek(*token_type)
class Token(type, text)

Bases: NamedTuple

text: str

Alias for field number 1

type: str

Alias for field number 0

class UDFCompiler

Bases: NodeVisitor

visit(node: Node)
visit(node: Var)
visit(node: Num)
visit(node: BinOp)
visit(node: Func)
class Var(val: 'str')

Bases: Node

class VariableNameCollector

Bases: NodeVisitor

visit(node: Node)
visit(node: Var)
compile(string)
compile_func(node: Node)
get_vars(node: Node)
parse(tokens: Iterable[Token])
tokenize(string: str, patterns: dict | None = None) Iterator[Token]

functions

default_func(arr: TrackedCSRArray | ndarray, default_val: float | TrackedCSRArray | ndarray)
func(name: str)
if_func(*arrays_or_values)
max_func(*arrays_or_values)

calculate row-wise maximum value of n arrays or values. Every array must have the same length in the first dimension. Values are broadcasted along the first axis

min_func(*arrays_or_values)

calculate row-wise minimum value of n arrays or values. Every array must have the same length in the first dimension. Values are broadcasted along the first axis

sum_func(arr)

udf_model

class UDF(func, output_attr)

Bases: object

run(inputs: Dict[str, UniformAttribute | CSRAttribute])
class UDFInfo(dataset: 'str', entity_group: 'str', expression: 'str', output_attribute: 'str')

Bases: object

dataset: str
entity_group: str
expression: str
get_output_attribute(state: TrackedState, schema: AttributeSchema, replace_sub=True)
make_udf(state: TrackedState, schema: AttributeSchema, replace_sub=True)
output_attribute: str
class UDFModel(model_config: dict)

Bases: TrackedModel

inputs: Dict[str, UniformAttribute | CSRAttribute]
run_udfs()
setup(state: TrackedState, schema: AttributeSchema, **_)

In setup, a model receives a state object, it’s config and other parameters. The goal of setup is to prepare the state by giving it information of the attributes it needs to track (by subscribing (INIT/SUB/OPT) or publishing (PUB) attributes) from which datasets. These attributes may be grouped together in EntityGroup classes or created directly. The main entry points for registering are:

  • state.add_dataset() for registering a bunch of EntityGroup classes for a certain dataset name at once

  • state.add_entity_group() for registering a single EntityGroup class (or instance) for a dataset name

  • state.register_attribute() for registering a single attribute in a dataset/entity_group combination

During setup there is no data available in the state. These will be downloaded automatically by the TrackedModelAdapter. However, additional datasets may be requested directly through the init_data_handler parameter.

Parameters:
  • state – The model’s TrackedState object, managed by the TrackedModelAdapter

  • settings – global settings

  • schema – The AttributeSchema with all registered attributes

  • init_data_handler – an InitDataHandler that may be used to retrieve additional datasets

  • logger – a logging.Logger instance

udfs: List[UDF]
update(**_)

The update method is called for every update coming from the model engine. However it is only called the first time once all PUB attributes have their arrays filled with data. When the simulation progresses to t>0 before the model’s SUB attributes have been filled, an Exception is raised, indicating that the model was not ready yet.

Parameters:
  • state – The model’s TrackedState object, managed by the TrackedModelAdapter

  • moment – The current simulation Moment

Returns:

an optional Moment indicating the next time a model want to be woken up, as per the model engine’s protocol

convert_v1_v2(config)
get_input_attributes(config: dict, schema: AttributeSchema, state: TrackedState)
get_udf_infos(config)
prepare_optional_attributes(config, inputs: Dict[str, UniformAttribute | CSRAttribute])

Module contents

class UDFModel(model_config: dict)

Bases: TrackedModel

inputs: Dict[str, UniformAttribute | CSRAttribute]
run_udfs()
setup(state: TrackedState, schema: AttributeSchema, **_)

In setup, a model receives a state object, it’s config and other parameters. The goal of setup is to prepare the state by giving it information of the attributes it needs to track (by subscribing (INIT/SUB/OPT) or publishing (PUB) attributes) from which datasets. These attributes may be grouped together in EntityGroup classes or created directly. The main entry points for registering are:

  • state.add_dataset() for registering a bunch of EntityGroup classes for a certain dataset name at once

  • state.add_entity_group() for registering a single EntityGroup class (or instance) for a dataset name

  • state.register_attribute() for registering a single attribute in a dataset/entity_group combination

During setup there is no data available in the state. These will be downloaded automatically by the TrackedModelAdapter. However, additional datasets may be requested directly through the init_data_handler parameter.

Parameters:
  • state – The model’s TrackedState object, managed by the TrackedModelAdapter

  • settings – global settings

  • schema – The AttributeSchema with all registered attributes

  • init_data_handler – an InitDataHandler that may be used to retrieve additional datasets

  • logger – a logging.Logger instance

udfs: List[UDF]
update(**_)

The update method is called for every update coming from the model engine. However it is only called the first time once all PUB attributes have their arrays filled with data. When the simulation progresses to t>0 before the model’s SUB attributes have been filled, an Exception is raised, indicating that the model was not ready yet.

Parameters:
  • state – The model’s TrackedState object, managed by the TrackedModelAdapter

  • moment – The current simulation Moment

Returns:

an optional Moment indicating the next time a model want to be woken up, as per the model engine’s protocol