DataCollector

class Melodie.data_collector.DataCollector(target='sqlite')

Bases: object

Data Collector collects data in the model.

At the beginning of simulation, the DataCollector creates as the model creates;

User could customize which property of agents or environment should be collected.

Before the model running finished, the DataCollector dumps data to dataframe, and save to database.

Parameters:

target – A string indicating database type, currently just support “sqlite”.

setup()

Setup method, be sure to inherit it.

Returns:

time_elapsed()

Get the time spent in data collection.

Returns:

Elapsed time, a float value.

add_agent_property(container_name: str, property_name: str, as_type: Type | None = None)

This method adds a property of agents in an agent_container to the data collector.

The type which the data will be represented in the database can also be determined by as_type.

Parameters:
  • container_name – Container name, also a property name on model.

  • property_name – Property name on the agent.

  • as_type – Data type.

Returns:

add_environment_property(property_name: str, as_type: Type | None = None)

This method tells the data collector which property of environment should be collected.

Parameters:
  • property_name – Environment properties

  • as_type – Data type.

Returns:

env_property_names() List[str]

Get the environment property names to collect

Returns:

List of environment property names

agent_property_names() Dict[str, List[str]]

Get the agent property names to collect

Returns:

A dict, <agent_container_name --> agent list properties to gather>[]

agent_containers() List[Tuple[str, BaseAgentContainer]]

Get all agent containers with attributes to collect in the model.

Returns:

A list of tuples, <agent_container_name, agent_container_object>[]

collect_agent_properties(period: int)

Collect agent properties.

Parameters:

period – Current simulation step

Returns:

None

append_agent_properties_by_records(container_name: str, prop_names: List[str], container: AgentList, period: int)

Directly append properties to the properties recorder dict. If used dynamic-linked-lib as speed up extensions, directly calling this method will be necessary.

Returns:

None

append_environment_properties(period: int)
property status: bool

If data collector is enabled.

DataCollector is only enabled in the Simulator, because Trainer and Calibrator are only concerned over the properties at the end of the model-running, so recording middle status in Trainer or Calibrator is a waste of time and space.

Returns:

bool.

collect(period: int) None

The main function to collect data.

Parameters:

period – Current simulation step.

Returns:

None

property db

Create a database connection

Returns:

melodie.DB object.

static calc_time(method)

Works as a decorator.

If you would like to define a custom data-collect method, please use DataCollector.calc_time as a decorator.

get_single_agent_data(agent_container_name: str, agent_id: int)

Get time series data of one agent.

Parameters:
  • agent_container_name – Attribute name in model.

  • agent_id – Agent id

Returns:

save()

Save the collected data into database.

Returns:

None