Skip to content

System

system

Abstract class for Dynamic Systems.

SystemABC(f=_no_step_function)

Bases: ABC

Abstract class for Dynamic Systems.

Initialize a SystemABC from a stepper function.

Parameters:

Name Type Description Default
f SystemProtocol

The stepper function for the system. Defaults to a function that raises NotImplementedError.

_no_step_function
Source code in src/flepimop2/system/abc.py
28
29
30
31
32
33
34
35
36
def __init__(self, f: SystemProtocol = _no_step_function) -> None:
    """
    Initialize a `SystemABC` from a stepper function.

    Args:
        f: The stepper function for the system. Defaults to a function that
            raises `NotImplementedError`.
    """
    self.stepper = f

step(time, state, **params)

Perform a single step of the system's dynamics.

Parameters:

Name Type Description Default
time float64

The current time.

required
state NDArray[float64]

The current state array.

required
**params Any

Additional parameters for the stepper.

{}

Returns:

Type Description
NDArray[float64]

The next state array after one step.

Source code in src/flepimop2/system/abc.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def step(
    self, time: np.float64, state: NDArray[np.float64], **params: Any
) -> NDArray[np.float64]:
    """
    Perform a single step of the system's dynamics.

    Args:
        time: The current time.
        state: The current state array.
        **params: Additional parameters for the stepper.

    Returns:
        The next state array after one step.
    """
    return self.stepper(time, state, **params)

SystemProtocol

Bases: Protocol

Type-definition (Protocol) for system stepper functions.

__call__(time, state, **kwargs)

Protocol for system stepper functions.

Parameters:

Name Type Description Default
time float64

The current time.

required
state NDArray[float64]

The current state array.

required
**kwargs Any

Additional keyword arguments for the stepper.

{}

Returns:

Type Description
NDArray[float64]

The next state array after one step.

Source code in src/flepimop2/system/protocol.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def __call__(
    self, time: np.float64, state: NDArray[np.float64], **kwargs: Any
) -> NDArray[np.float64]:
    """Protocol for system stepper functions.

    Args:
        time: The current time.
        state: The current state array.
        **kwargs: Additional keyword arguments for the stepper.

    Returns:
        The next state array after one step.
    """
    ...

build(config)

Build a SystemABC from a configuration dictionary.

Parameters:

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

Configuration dictionary or a SystemProtocol. In dict mode, it contains a 'module' key, it will be used to lookup the System module path. The module will have "flepimop2.system." prepended.

required

Returns:

Type Description
SystemABC

The constructed system.

Raises:

Type Description
TypeError

If the built system is not an instance of SystemABC.

Source code in src/flepimop2/system/abc.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def build(config: dict[str, Any] | SystemProtocol) -> SystemABC:
    """
    Build a `SystemABC` from a configuration dictionary.

    Args:
        config: Configuration dictionary or a SystemProtocol. In dict mode, it contains
            a 'module' key, it will be used to lookup the System module path. The module
            will have "flepimop2.system." prepended.

    Returns:
        The constructed system.

    Raises:
        TypeError: If the built system is not an instance of SystemABC.
    """
    if isinstance(config, SystemProtocol):
        return SystemABC(config)
    builder = _load_builder(f"flepimop2.system.{config.pop('module', 'wrapper')}")
    system = builder.build(**config)
    if not isinstance(system, SystemABC):
        msg = "The built system is not an instance of SystemABC."
        raise TypeError(msg)
    return system

abc

Abstract class for Dynamic Systems.

SystemABC(f=_no_step_function)

Bases: ABC

Abstract class for Dynamic Systems.

Initialize a SystemABC from a stepper function.

Parameters:

Name Type Description Default
f SystemProtocol

The stepper function for the system. Defaults to a function that raises NotImplementedError.

_no_step_function
Source code in src/flepimop2/system/abc.py
28
29
30
31
32
33
34
35
36
def __init__(self, f: SystemProtocol = _no_step_function) -> None:
    """
    Initialize a `SystemABC` from a stepper function.

    Args:
        f: The stepper function for the system. Defaults to a function that
            raises `NotImplementedError`.
    """
    self.stepper = f
step(time, state, **params)

Perform a single step of the system's dynamics.

Parameters:

Name Type Description Default
time float64

The current time.

required
state NDArray[float64]

The current state array.

required
**params Any

Additional parameters for the stepper.

{}

Returns:

Type Description
NDArray[float64]

The next state array after one step.

Source code in src/flepimop2/system/abc.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def step(
    self, time: np.float64, state: NDArray[np.float64], **params: Any
) -> NDArray[np.float64]:
    """
    Perform a single step of the system's dynamics.

    Args:
        time: The current time.
        state: The current state array.
        **params: Additional parameters for the stepper.

    Returns:
        The next state array after one step.
    """
    return self.stepper(time, state, **params)

build(config)

Build a SystemABC from a configuration dictionary.

Parameters:

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

Configuration dictionary or a SystemProtocol. In dict mode, it contains a 'module' key, it will be used to lookup the System module path. The module will have "flepimop2.system." prepended.

required

Returns:

Type Description
SystemABC

The constructed system.

Raises:

Type Description
TypeError

If the built system is not an instance of SystemABC.

Source code in src/flepimop2/system/abc.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def build(config: dict[str, Any] | SystemProtocol) -> SystemABC:
    """
    Build a `SystemABC` from a configuration dictionary.

    Args:
        config: Configuration dictionary or a SystemProtocol. In dict mode, it contains
            a 'module' key, it will be used to lookup the System module path. The module
            will have "flepimop2.system." prepended.

    Returns:
        The constructed system.

    Raises:
        TypeError: If the built system is not an instance of SystemABC.
    """
    if isinstance(config, SystemProtocol):
        return SystemABC(config)
    builder = _load_builder(f"flepimop2.system.{config.pop('module', 'wrapper')}")
    system = builder.build(**config)
    if not isinstance(system, SystemABC):
        msg = "The built system is not an instance of SystemABC."
        raise TypeError(msg)
    return system

protocol

Type-definition (Protocol) for system stepper functions.

SystemProtocol

Bases: Protocol

Type-definition (Protocol) for system stepper functions.

__call__(time, state, **kwargs)

Protocol for system stepper functions.

Parameters:

Name Type Description Default
time float64

The current time.

required
state NDArray[float64]

The current state array.

required
**kwargs Any

Additional keyword arguments for the stepper.

{}

Returns:

Type Description
NDArray[float64]

The next state array after one step.

Source code in src/flepimop2/system/protocol.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def __call__(
    self, time: np.float64, state: NDArray[np.float64], **kwargs: Any
) -> NDArray[np.float64]:
    """Protocol for system stepper functions.

    Args:
        time: The current time.
        state: The current state array.
        **kwargs: Additional keyword arguments for the stepper.

    Returns:
        The next state array after one step.
    """
    ...

wrapper

A SystemABC which wraps a user-defined script file.

WrapperSystem(script)

Bases: SystemABC

A SystemABC which wraps a user-defined script file.

Initialize a WrapperSystem from a script file.

Parameters:

Name Type Description Default
script PathLike[str]

Path to a script file which defines a 'stepper' function.

required

Raises:

Type Description
AttributeError

If the module does not have a valid 'stepper' function.

Source code in src/flepimop2/system/wrapper.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
def __init__(self, script: PathLike[str]) -> None:
    """
    Initialize a `WrapperSystem` from a script file.

    Args:
        script: Path to a script file which defines a 'stepper' function.

    Raises:
        AttributeError: If the module does not have a valid 'stepper' function.
    """
    mod = _load_module(script, "flepimop2.system.wrapped")
    if not _validate_function(mod, "stepper"):
        msg = f"Module at {script} does not have a valid 'stepper' function."
        raise AttributeError(msg)
    super().__init__(mod.stepper)

build(script)

Build a WrapperSystem from a configuration arguments.

Parameters:

Name Type Description Default
script PathLike[str]

Path to a script file which defines a 'stepper' function.

required

Returns:

Type Description
WrapperSystem

The constructed wrapper system instance.

Source code in src/flepimop2/system/wrapper.py
29
30
31
32
33
34
35
36
37
38
39
def build(script: PathLike[str]) -> WrapperSystem:
    """
    Build a `WrapperSystem` from a configuration arguments.

    Args:
        script: Path to a script file which defines a 'stepper' function.

    Returns:
        The constructed wrapper system instance.
    """
    return WrapperSystem(script)