Component
A base Component class to be used to create components for use in vivarium
simulations.
- vivarium.component.DEFAULT_EVENT_PRIORITY = 5
The default priority at which events will be triggered.
- class vivarium.component.Component[source]
The base class for all components used in a Vivarium simulation.
A
Componentin a Vivarium simulation represents a distinct feature or aspect of the model. It encapsulates the logic and data needed for that feature. Components commonly interact with the rest of the simulation by creating and updating columns in the state table, registering pipelines, and registering modifiers on pipelines created by other components. Observer components might also register observations. All components within a simulation must have a unique name, which is generated by default from the component’s class and the argument passed to its constructor.The
setup_component()method is run by Vivarium during the setup phase and performs a series of operations to prepare the component for the simulation. These operations include setting the logger for the component, calling the component’s customsetup()method, setting the population view if the component needs one, and registering listeners for each lifecycle event if the component has defined a method to be triggered on that event.Subclasses of
Componentshould override these properties as needed:Subclasses of
Componentshould override these methods in order to have operations occur during the appropriate lifecycle phase of a simulation:- CONFIGURATION_DEFAULTS: dict[str, Any] = {}
A dictionary containing the defaults for any configurations managed by this component. An empty dictionary indicates no managed configurations. Components will look for a
data_sourcesblock in this dictionary to build lookup tables automatically.
- property name: str
The name of the component.
By convention, these are in snake case with arguments of the
__init__()appended and separated by..Names must be unique within a simulation.
The name is created by first converting the name of the class to snake case. Then, the names of the initialization parameters are appended, separated by
.. If a parameter is an instance ofComponent, itsnameproperty is used; otherwise, the string representation of the parameter is used. The resulting string is stored in the_nameattribute and returned.IMPORTANT: this property must not be accessed within the
__init__()functions of this component or its subclasses or its value may not be initialized correctly.
- property logger: loguru.Logger
The logger for this component.
- Raises:
LifeCycleError – If the logger has not been initialized.
- property population_view: PopulationView
The
PopulationViewfor this component.- Raises:
PopulationError – If the component does not have access to the state table.
- property configuration_defaults: dict[str, Any]
The dictionary containing the defaults for any configurations managed by this component.
These default values will be stored at the
component_configslayer of the simulation’sLayeredConfigTree.
- property lookup_table_value_columns: dict[str, str | list[str]]
A mapping of lookup table names to their value columns.
- property time_step_prepare_priority: int
The priority of this component’s
time_step__preparelistener.
- property time_step_cleanup_priority: int
The priority of this component’s
time_step__cleanuplistener.
- setup_component(builder)[source]
Sets up the component for a Vivarium simulation.
This method is run by Vivarium during the setup phase. It performs a series of operations to prepare the component for the simulation.
It sets the
loggerfor the component, sets up the component, sets the population view, and registers various listeners includingpost_setup,simulant_initializer,time_step__prepare,time_step,time_step__cleanup,collect_metrics, andsimulation_endlisteners.
- setup(builder)[source]
Defines custom actions this component needs to run during the setup lifecycle phase.
This method is intended to be overridden by subclasses to perform any necessary setup operations specific to the component. By default, it does nothing.
- on_post_setup(event)[source]
Method that vivarium will run during the
post_setupevent.This method is intended to be overridden by subclasses if there are operations they need to perform specifically during the
post_setupevent.Notes
This method is not commonly used functionality.
- Parameters:
event (Event) – The event object associated with the
post_setupevent.- Return type:
None
- on_time_step_prepare(event)[source]
Method that vivarium will run during the
time_step__prepareevent.This method is intended to be overridden by subclasses if there are operations they need to perform specifically during the
time_step__prepareevent.
- on_time_step(event)[source]
Method that vivarium will run during the
time_stepevent.This method is intended to be overridden by subclasses if there are operations they need to perform specifically during the
time_stepevent.
- on_time_step_cleanup(event)[source]
Method that vivarium will run during the
time_step__cleanupevent.This method is intended to be overridden by subclasses if there are operations they need to perform specifically during the
time_step__cleanupevent.
- on_collect_metrics(event)[source]
Method that vivarium will run during the
collect_metricsevent.This method is intended to be overridden by subclasses if there are operations they need to perform specifically during the
collect_metricsevent.
- on_simulation_end(event)[source]
Method that vivarium will run during the
simulation_endevent.This method is intended to be overridden by subclasses if there are operations they need to perform specifically during the
simulation_endevent.
- get_initialization_parameters()[source]
Retrieves the values of all parameters specified in the
__init__()that have an attribute with the same name.Notes
This retrieves the value of the attribute at the time of calling, which is not guaranteed to be the same as the original value.
- get_configuration(builder)[source]
Retrieves the configuration for this component from the builder.
This method retrieves the configuration for this component from the simulation’s overall configuration. The configuration is retrieved using the name of the component as the key.
- Return type:
- Parameters:
builder (Builder) – The simulation’s builder object.
- Returns:
The configuration for this component, or a default empty configuration.
- build_lookup_table(builder, name, data_source=None, value_columns=None)[source]
Builds a LookupTable.
If a data_source is not provided, the method will look for a data source in the component’s configuration under the key “data_sources” with the provided name.
If value_columns provided is a list or tuple, a LookupTable returning a DataFrame will be built. If it is a string or None, a LookupTable returning a Series will be built. If value_columns is None, the name of the returned Series will be “value”.
- Parameters:
builder (Builder) – The builder object used to set up the component.
data_source (DataInput | None) – The data source to build the LookupTable from. If None, the data source will be retrieved from the component’s configuration.
name (str) – The name of the lookup table, used to retrieve the data source from the configuration if data_source is None.
value_columns (list[str] | tuple[str, ...] | str | None) – The columns to include in the LookupTable.
- Returns:
The LookupTable built from the data source.
- Raises:
layered_config_tree.exceptions.ConfigurationError – If the data source is invalid.
- Return type:
LookupTable[pd.Series[Any]] | LookupTable[pd.DataFrame]
- get_data(builder, data_source)[source]
Retrieves data from a data source.
If the data source is a float or a DataFrame, it is treated as the data itself. If the data source is a string, containing the substring ‘::’, it is treated as a function to call to retrieve the data. The string to the left of ‘::’ is the module to import, and the string to the right is the function to call. ‘self’ can be provided to the left of ‘::’ to call a method on the component itself. If the data source is a string without the substring ‘::’, it is treated as a key in the artifact.
- Return type:
SupportsFloat|Timedelta|timedelta|Timestamp|datetime|DataFrame|list[SupportsFloat|Timedelta|timedelta|Timestamp|datetime] |tuple[SupportsFloat|Timedelta|timedelta|Timestamp|datetime,...] |Mapping[str,list[SupportsFloat|Timedelta|timedelta|Timestamp|datetime] |list[str]]- Parameters:
builder (Builder) – The builder object used to set up the component.
data_source (SupportsFloat | Timedelta | timedelta | Timestamp | datetime | DataFrame | list[SupportsFloat | Timedelta | timedelta | Timestamp | datetime] | tuple[SupportsFloat | Timedelta | timedelta | Timestamp | datetime, ...] | Mapping[str, list[SupportsFloat | Timedelta | timedelta | Timestamp | datetime] | list[str]] | str | Callable[[Builder], SupportsFloat | Timedelta | timedelta | Timestamp | datetime | DataFrame | list[SupportsFloat | Timedelta | timedelta | Timestamp | datetime] | tuple[SupportsFloat | Timedelta | timedelta | Timestamp | datetime, ...] | Mapping[str, list[SupportsFloat | Timedelta | timedelta | Timestamp | datetime] | list[str]]]) – The data source to retrieve data from.
- Returns:
The data retrieved from the data source.
- Raises:
layered_config_tree.exceptions.ConfigurationError – If the data source is invalid.