Skip to content

Engine

abc

Abstract class for Engines to evolve Dynamic Systems.

EngineABC

Bases: ModuleBase

Abstract class for Engines to evolve Dynamic Systems.

model_post_init(__context)

Set _runner to the no-op sentinel if not already overridden.

Concrete subclasses should call super().model_post_init(__context) first, then assign self._runner to their runner function.

Parameters:

Name Type Description Default
__context Any

Pydantic model post-init context.

required
Source code in src/flepimop2/engine/abc/__init__.py
def model_post_init(self, __context: Any, /) -> None:  # noqa: ANN401
    """
    Set `_runner` to the no-op sentinel if not already overridden.

    Concrete subclasses should call `super().model_post_init(__context)`
    first, then assign `self._runner` to their runner function.

    Args:
        __context: Pydantic model post-init context.

    """
    super().model_post_init(__context)
    if self._runner is None:
        self._runner = _no_run_func
run(system, eval_times, initial_state, params, model_state=None, **kwargs)

Run the engine with the provided system and parameters.

Parameters:

Name Type Description Default
system SystemABC

The dynamic system to be evolved.

required
eval_times Float64NDArray

Array of time points for evaluation.

required
initial_state dict[IdentifierString, ParameterValue]

Structured initial-state entries sampled from parameters.

required
params Mapping[IdentifierString, ParameterValue]

Additional parameters for the stepper.

required
model_state ModelStateSpecification | None

Specification describing the semantic ordering of the state entries.

None
**kwargs Any

Additional keyword arguments for the engine.

{}

Returns:

Type Description
Float64NDArray

The evolved time x state array.

Source code in src/flepimop2/engine/abc/__init__.py
def run(
    self,
    system: SystemABC,
    eval_times: Float64NDArray,
    initial_state: dict[IdentifierString, ParameterValue],
    params: Mapping[IdentifierString, ParameterValue],
    model_state: ModelStateSpecification | None = None,
    **kwargs: Any,
) -> Float64NDArray:
    """
    Run the engine with the provided system and parameters.

    Args:
        system: The dynamic system to be evolved.
        eval_times: Array of time points for evaluation.
        initial_state: Structured initial-state entries sampled from
            parameters.
        params: Additional parameters for the stepper.
        model_state: Specification describing the semantic ordering of the
            state entries.
        **kwargs: Additional keyword arguments for the engine.

    Returns:
        The evolved time x state array.
    """
    return self._runner(  # type: ignore[no-any-return]
        system.bind(),
        eval_times,
        initial_state,
        params,
        model_state=model_state,
        **kwargs,
    )
validate_system(system)

Validation hook for system properties.

Parameters:

Name Type Description Default
system SystemABC

The system to validate.

required

Returns:

Type Description
list[ValidationIssue] | None

A list of validation issues, or None if not implemented.

Source code in src/flepimop2/engine/abc/__init__.py
def validate_system(  # noqa: PLR6301
    self,
    system: SystemABC,  # noqa: ARG002
) -> list[ValidationIssue] | None:
    """
    Validation hook for system properties.

    Args:
        system: The system to validate.

    Returns:
        A list of validation issues, or `None` if not implemented.
    """
    return None

EngineProtocol

Bases: Protocol

Type-definition (Protocol) for engine runner functions.

__call__(stepper, times, initial_state, params, model_state=None, **kwargs)

Protocol for engine runner functions.

Source code in src/flepimop2/engine/abc/__init__.py
def __call__(
    self,
    stepper: SystemProtocol,
    times: Float64NDArray,
    initial_state: dict[IdentifierString, ParameterValue],
    params: Mapping[IdentifierString, ParameterValue],
    model_state: ModelStateSpecification | None = None,
    **kwargs: Any,
) -> Float64NDArray:
    """Protocol for engine runner functions."""
    ...

build(config)

Build a EngineABC from a configuration dictionary.

Parameters:

Name Type Description Default
config dict[str, Any] | ModuleBase | str

Configuration dictionary or a ModuleBase instance to construct the engine from. The configuration must define a module.

required

Returns:

Type Description
EngineABC

The constructed engine instance.

Source code in src/flepimop2/engine/abc/__init__.py
def build(config: dict[str, Any] | ModuleBase | str) -> EngineABC:
    """Build a `EngineABC` from a configuration dictionary.

    Args:
        config: Configuration dictionary or a `ModuleBase` instance to construct the
            engine from. The configuration must define a `module`.

    Returns:
        The constructed engine instance.

    """
    return _build(config, "engine", EngineABC)

wrapper

A EngineABC which wraps a user-defined script file.

WrapperEngine

Bases: EngineABC

A EngineABC which wraps a user-defined script file.

validate_system(system)

Validate that the given system is compatible with this engine.

Parameters:

Name Type Description Default
system SystemABC

The system to validate.

required

Returns:

Type Description
list[ValidationIssue] | None

A list of validation issues, or None if not implemented.

Source code in src/flepimop2/engine/wrapper/__init__.py
def validate_system(self, system: SystemABC) -> list[ValidationIssue] | None:
    """
    Validate that the given system is compatible with this engine.

    Args:
        system: The system to validate.

    Returns:
        A list of validation issues, or `None` if not implemented.

    """
    if system.state_change != self.state_change:
        return [
            ValidationIssue(
                msg=(
                    f"Engine state change type, '{self.state_change}', is not "
                    "compatible with system state change type "
                    f"'{system.state_change}'."
                ),
                kind="incompatible_system",
            )
        ]
    return None