CSVDataCollector

BPTK API Documentation for the CSVDataCollector class
Keywords

agent-based modeling, abm, csv, data collection, bptk, bptk-py, python, business prototyping

CSVDataCollector

CSVDataCollector Constructor

CSVDataCollector(prefix=‘csv/’)

A data collector that writes simulation results to CSV files instead of keeping them in memory.

Use it for runs whose results are larger than you want in memory, or when the analysis happens somewhere else — a spreadsheet, R, a BI tool. Each agent type gets its own file below prefix, <agent type>.csv, with one row per agent and timestep: id, time and one column per property. The recorded events go to events.csv. Columns are separated by ;.

The directory is created if it does not exist. Each run starts the files afresh, because the scheduler calls reset before it.

from BPTK_Py import Model, CSVDataCollector, SimultaneousScheduler

model = Model(
    starttime=1, stoptime=60, dt=1, name="Customer Acquisition",
    scheduler=SimultaneousScheduler(),
    data_collector=CSVDataCollector(prefix="results/"),
)
  • Parameters

    • prefix – String (Default=‘csv/’). The directory the files are written to, including the trailing slash.

CSVDataCollector.collect_agent_statistics

collect_agent_statistics(sim_time, agents)

Called by the scheduler once per timestep. Appends one row per agent to the file of its agent type. The first row written to a file fixes its columns; a property that a later agent has and the first one did not is left out, with a warning in the log.

  • Parameters

    • sim_time – Float. The timestep being recorded.

    • agents – List. The agents to record.

CSVDataCollector.record_event

record_event(time, event)

Appends one event to events.csv: time, event (its name), sender_id and receiver_id.

  • Parameters

    • time – Float. The timestep at which the event was sent.

    • event – Event. The event to record.

CSVDataCollector.statistics

statistics()

The results are in the files rather than in memory, so there is nothing to return here - and nothing for plot_scenarios to draw. Read the CSV files instead.

  • Returns

    An empty dictionary.

CSVDataCollector.reset

reset()

Starts every file afresh at the next row written to it. The scheduler calls it at the start of each run, so there is no need to call it yourself.

To learn how to write a collector of your own, see Custom Data Collectors.