Skip to content

Base

Define the abstract bases of every engine class and of every spec.

BaseConfig

Bases: _BaseEstimator[ContextT]

An abstract engine class, such as a solver, a backend, or an observer.

It stores every argument unchanged under its own name, as a scikit-learn estimator does, so get_params, set_params, and clone work on it, such as RK4Solver(dt=0.01).set_params(dt=0.1). Its method prepare(context) checks the arguments, raising a named error for one it cannot take, and sets the attributes that end in _. Its method run(inputs, final=False) does the one job of the prepared object, such as a step of a solver, and keeps what it produces in attributes that end in _. A last call with final=True also frees what prepare took, such as an open file. A subclass writes the private methods _prepare and _run. It is the base of the solvers that step a chemical reactor, the backends that run a forest fire, and the observers that record an epidemic.

Read more in the user guide, under Extend the library.

See Also

BaseComponent: The base of the components of a system, which have no run. BaseSpec: The base of frozen values that classes read.

Examples:

>>> from skcomplex.core.base import BaseConfig
>>> from skcomplex.core.engine.solvers import RK4Solver
>>> solver = RK4Solver(dt=0.01).set_params(dt=0.1)
>>> isinstance(solver, BaseConfig), solver.dt
(True, 0.1)

run(inputs=None, final=False)

Perform the one action of the prepared object on its one input.

It keeps its results in attributes.

Parameters:

Name Type Description Default
inputs InputT | None

The input of the action, such as the state a solver advances or the values a law reads.

None
final bool

Whether this is the last call, after which the object releases what preparing acquired.

False

Returns:

Name Type Description
self Self

The object, with the results of the action in its attributes that end in _.

Raises:

Type Description
NotPreparedError

The object was not prepared with prepare.

Source code in src/skcomplex/core/base.py
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
def run(self, inputs: InputT | None = None, final: bool = False) -> Self:
    """Perform the one action of the prepared object on its one input.

    It keeps its results in attributes.

    Args:
        inputs:
            The input of the action, such as the state a solver advances or the values a law reads.

        final:
            Whether this is the last call, after which the object releases what preparing acquired.

    Returns:
        self:
            The object, with the results of the action in its attributes that end in `_`.

    Raises:
        NotPreparedError:
            The object was not prepared with `prepare`.
    """
    self._is_prepared()
    self._run(inputs)
    if final:
        self._release()
    return self

BaseSpec() dataclass

An abstract spec, a frozen value that a class reads, records, or builds.

A spec is a frozen dataclass whose fields are what it is, such as the visual VisualSpec('points'). It says what a class reads, records what a run produced, or holds what a compiler built. It never changes, so it is changed with dataclasses.replace, and it has neither prepare nor run. A subclass that checks its fields writes the private method _check, which the class that reads the spec calls. Specs mark the conserved money of an account, the axis of velocities of a gas, and the frames a run of a flock recorded.

Read more in the user guide, under Extend the library.

See Also

BaseConfig: The base of the engine classes.

Examples:

>>> import dataclasses
>>> from skcomplex.core.base import BaseSpec
>>> from skcomplex.core.components.elements import VisualSpec
>>> isinstance(VisualSpec('points'), BaseSpec), dataclasses.replace(VisualSpec('points'), kind='line').kind
(True, 'line')