Skip to main content
Emergent ships two module-level helper functions as reference implementations. They are intentionally simple and are intended to illustrate the expected function signatures for initial_data_function and timestep_function. They are not designed for production use.

genInitialData

genInitialData(model: AgentModel) -> dict
A sample initial data function. Returns a dictionary assigning each node a random integer id between 1 and 100 (inclusive). This function matches the signature required by set_initial_data_function: it accepts the model instance and returns a dictionary that is merged into the node’s attribute store.
model
AgentModel
required
The model instance. Passed automatically by initialize_graph when this function is registered as the initial data function. The function does not read any data from the model, but the signature is required by the framework.
return
dict
A dictionary of the form {"id": int} where id is a random integer between 1 and 100.
This is a reference implementation. For real simulations, write your own initial data function that returns the attributes relevant to your model.
from emergent.main import AgentModel, genInitialData

model = AgentModel()
model.set_initial_data_function(genInitialData)
model.initialize_graph()

# Each node now has an "id" attribute set to a random integer 1–100
graph = model.get_graph()
for node, data in graph.nodes(data=True):
    print(node, data["id"])

genTimestepData

genTimestepData(model: AgentModel, nodeData: dict) -> dict
A sample per-node update function. Increments nodeData["id"] by 1 and returns the updated dictionary.
This function is not automatically called by the framework. It does not match the signature expected by set_timestep_function, which requires a function of the form (model: AgentModel) -> None. To use it, you must wrap it inside a timestep function that iterates over the graph’s nodes manually.
model
AgentModel
required
The model instance. Not used by this function, but included to reflect a common pattern where per-node helpers receive the model for access to parameters.
nodeData
dict
required
The current attribute dictionary for a single node. Must contain an "id" key with a numeric value.
return
dict
The same nodeData dictionary with nodeData["id"] incremented by 1.
from emergent.main import AgentModel, genInitialData, genTimestepData

model = AgentModel()
model.set_initial_data_function(genInitialData)

# Wire genTimestepData into a compliant timestep function
def my_timestep(model):
    graph = model.get_graph()
    for node, data in graph.nodes(data=True):
        updated = genTimestepData(model, data)
        graph.nodes[node].update(updated)

model.set_timestep_function(my_timestep)
model.initialize_graph()
model.timestep()

Build docs developers (and LLMs) love