Skip to content

About

Source code for the Emergent ABM Python package.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Emergent

A Python module for building and running agent-based models on networks. Emergent provides an AgentModel that combines a configurable graph topology (via NetworkX) with pluggable initial-data and timestep logic, so you can define custom agent behavior while the framework handles parameters, graph creation, and iteration.

Requirements

  • Python >= 3.10.7
  • NetworkX >= 3.3
  • NumPy >= 2.0.0
  • Matplotlib >= 3.9.2
  • SciPy >= 1.15.1

Installation

Install from PyPI:

python3 -m pip install emergent

Or install from source in development mode:

git clone https://github.com/toni-akintola/modelpy-module.git
cd modelpy-module
pip install -e .

Quick Start

from emergent.main import AgentModel, genInitialData, genTimestepData

# Create a model and set parameters
model = AgentModel()
model.update_parameters({
    "num_nodes": 5,
    "graph_type": "complete",
})

# Plug in your initial data and timestep logic
model.set_initial_data_function(genInitialData)
model.set_timestep_function(genTimestepData)

# Build the graph and run
model.initialize_graph()
model.timestep()  # run one step
# or
model.run_to_convergence()  # run until MAX_TIMESTEPS

Core API

AgentModel

The main class that holds parameters, the graph, and the functions used to initialize and update nodes.

Parameters (defaults)

Parameter Default Description
num_nodes 3 Number of nodes in the graph
graph_type "complete" Topology: see Graph types
model_variation "base" Model variant label

Use update_parameters(dict) to set these and any custom parameters. Use list_parameters() to see current keys and delete_parameters(list) to remove optional ones.

Graph types

  • complete — complete graph (every pair of nodes connected)
  • cycle — cycle graph
  • wheel — wheel graph
  • Other — empty graph with num_nodes nodes (custom topologies can be set via set_graph())

Key methods

  • set_initial_data_function(callable)
    Sets the function used to generate initial node data. It will be called as f(model) once per node; it should return a dict of attributes for that node.

  • set_timestep_function(callable)
    Sets the function that runs each timestep. It is called as f(model) and should read/update the graph via model.get_graph() and model.set_graph().

  • initialize_graph()
    Creates the graph from num_nodes and graph_type, then assigns initial data to each node using the initial-data function.

  • timestep()
    Runs one timestep by calling the timestep function.

  • run_to_convergence()
    Calls timestep() repeatedly up to MAX_TIMESTEPS (1000) and returns the number of steps run.

  • get_graph()
    Returns the current NetworkX graph (e.g. for inspection or custom layout).

  • set_graph(graph)
    Replaces the model’s graph with a NetworkX graph (e.g. for custom or external topologies).

Helpers in main.py

  • genInitialData(model)
    Example initial-data function: returns a dict with a single "id" key (random int 1–100).

  • genTimestepData(model, nodeData)
    Example timestep logic: increments nodeData["id"] by 1 and returns it.

These are minimal examples; in practice you replace them with your own initial data and timestep logic.

Extending the model

  1. Initial data
    Implement a function (model: AgentModel) -> dict that returns the attribute dict for one node. The framework calls it once per node during initialize_graph().

  2. Timestep
    Implement a function (model: AgentModel) -> None that:

    • Gets the graph with model.get_graph()
    • Reads/updates nodes (and optionally edges)
    • Calls model.set_graph(graph) if you replace the graph
  3. Custom parameters
    Add any keys you need via update_parameters(). Read them in your functions with model["param_name"].

  4. Custom graph
    Build a NetworkX graph yourself and pass it to set_graph() instead of (or after) using initialize_graph().

Running tests

From the project root:

python -m pytest tests/
# or
python -m unittest tests/test.py

Building and publishing

Build the package:

pip install build twine
python -m build

Upload to TestPyPI or PyPI:

twine upload --repository testpypi dist/*
# or
twine upload dist/*

Links

License

See the repository for license information.

About

Source code for the Emergent ABM Python package.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages