biogeme.model_elements.database_adapter module¶
Database adapters (strategy objects) for ModelElements.
This module provides a small adapter interface that encapsulates the
preparation and exposure of the working database used to evaluate model
expressions, together with a factory for the corresponding
ExpressionRegistry.
Two concrete adapters are provided:
RegularAdapter– uses the database as-is (no transformation).FlatPanelAdapter– flattens a panel database and updates expressions with the maximum number of observations per individual.
These adapters allow the ModelElements container to remain generic
and free of conditional logic about database variants.
Michel Bierlaire Tue Nov 11 2025, 17:40:56
- class biogeme.model_elements.database_adapter.FlatPanelAdapter(database)[source]¶
Bases:
objectAdapter that flattens a panel database for expression evaluation.
This adapter converts an input panel database into a flat database using
PanelDatabase. It also updates each expression with the maximum number of observations per individual.- Note:
After
prepare(), :pyattr:`database` points to the flat database.sample_sizeis the number of rows in the flat database.number_of_observationsis the number of rows in the original database.
- Parameters:
database (Database | None)
- build_registry(expressions)[source]¶
Build an
ExpressionRegistrybound to the flat database.- Parameters:
expressions (
dict[str,Expression]) – Mapping of expression names to expressions.- Returns:
Registry bound to :pyattr:`database`.
- Return type:
- property database: Database¶
Working database, i.e., the flattened database.
- Returns:
The flat database created in
prepare().- Return type:
- Raises:
BiogemeError – If
prepare()has not been called yet.
- property number_of_observations: int¶
Number of observations in the original (panel) database.
- Returns:
Number of observations.
- Return type:
int
- prepare(expressions)[source]¶
Flatten the panel database and update expressions.
- Parameters:
expressions (
dict[str,Expression]) – Mapping of expression names to expressions. Each expression is informed of the maximum number of observations per individual viaset_maximum_number_of_observations_per_individual.- Return type:
None
- property sample_size: int¶
Number of rows in the working (flat) database.
- Returns:
Number of rows.
- Return type:
int
- class biogeme.model_elements.database_adapter.ModelElementsAdapter(*args, **kwargs)[source]¶
Bases:
ProtocolStrategy interface for providing a working database and registry.
Any implementation must be able to prepare its internal state given the model expressions, expose the database on which expressions will be evaluated, and build an
ExpressionRegistrybound to that database.Methods¶
- prepare(expressions)
Perform any one-time preparation needed before evaluation (e.g., flatten a panel DB, set expression metadata).
- database
The
Databaseagainst which expressions are to be evaluated.- build_registry(expressions)
Construct an
ExpressionRegistryconnected to the working database.- sample_size
Number of data rows in the working database.
- number_of_observations
Number of original observations in the source database (this may differ from
sample_sizefor flattened panel data).
- param expressions:
Mapping of expression names to
Expressioninstances. Implementations may use this to attach metadata.
- build_registry(expressions)[source]¶
- Return type:
- Parameters:
expressions (dict[str, Expression])
- property number_of_observations: int¶
- prepare(expressions)[source]¶
- Return type:
None- Parameters:
expressions (dict[str, Expression])
- property sample_size: int¶
- class biogeme.model_elements.database_adapter.RegularAdapter(database)[source]¶
Bases:
objectAdapter for a regular (non-panel or non-flattened) database.
This adapter is a thin wrapper around a
Databaseinstance and performs no transformation.- Note:
sample_sizeequalsnumber_of_observations.prepare()is a no-op.
- Parameters:
database (Database | None)
- build_registry(expressions)[source]¶
Build an
ExpressionRegistrybound to the working database.- Parameters:
expressions (
dict[str,Expression]) – Mapping of expression names to expressions.- Returns:
Registry bound to :pyattr:`database`.
- Return type:
- property number_of_observations: int¶
Number of observations in the source database.
- prepare(expressions)[source]¶
No-op preparation for regular databases.
- Parameters:
expressions (
dict[str,Expression]) – Mapping of expression names to expressions (unused).- Return type:
None
- property sample_size: int¶
Number of rows in the working database.