Skip to content

Backend

backend

Backend module for handling file IO in flepimop2.

BackendABC(backend_model)

Bases: ABC

Abstract base class for flepimop2 file IO backends.

Initialize the backend with the given configuration.

Parameters:

Name Type Description Default
backend_model dict[str, Any]

The configuration dictionary for the backend.

required
Source code in src/flepimop2/backend/abc.py
16
17
18
19
20
21
22
def __init__(self, backend_model: dict[str, Any]) -> None:  # noqa: B027
    """
    Initialize the backend with the given configuration.

    Args:
        backend_model: The configuration dictionary for the backend.
    """

read(run_meta)

Read a numpy array from storage.

Parameters:

Name Type Description Default
run_meta RunMeta

Metadata about the current run.

required

Returns:

Type Description
NDArray[float64]

The numpy array read from storage.

Source code in src/flepimop2/backend/abc.py
34
35
36
37
38
39
40
41
42
43
44
def read(self, run_meta: RunMeta) -> NDArray[np.float64]:
    """
    Read a numpy array from storage.

    Args:
        run_meta: Metadata about the current run.

    Returns:
        The numpy array read from storage.
    """
    return self._read(run_meta)

save(data, run_meta)

Save a numpy array to storage.

Parameters:

Name Type Description Default
data NDArray[float64]

The numpy array to save.

required
run_meta RunMeta

Metadata about the current run.

required
Source code in src/flepimop2/backend/abc.py
24
25
26
27
28
29
30
31
32
def save(self, data: NDArray[np.float64], run_meta: RunMeta) -> None:
    """
    Save a numpy array to storage.

    Args:
        data: The numpy array to save.
        run_meta: Metadata about the current run.
    """
    return self._save(data, run_meta)

build(config)

Build a BackendABC from a configuration dictionary.

Parameters:

Name Type Description Default
config dict[str, Any]

Configuration dictionary. The dict must contains a 'module' key, which will be used to lookup the Backend module path. The module will have "flepimop2.backend." prepended.

required

Returns:

Type Description
BackendABC

The constructed backend instance.

Raises:

Type Description
TypeError

If the built backend is not an instance of BackendABC.

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

    Args:
        config: Configuration dictionary. The dict must contains a 'module' key, which
            will be used to lookup the Backend module path. The module will have
            "flepimop2.backend." prepended.

    Returns:
        The constructed backend instance.

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

abc

Abstract base class for flepimop2 file IO backends.

BackendABC(backend_model)

Bases: ABC

Abstract base class for flepimop2 file IO backends.

Initialize the backend with the given configuration.

Parameters:

Name Type Description Default
backend_model dict[str, Any]

The configuration dictionary for the backend.

required
Source code in src/flepimop2/backend/abc.py
16
17
18
19
20
21
22
def __init__(self, backend_model: dict[str, Any]) -> None:  # noqa: B027
    """
    Initialize the backend with the given configuration.

    Args:
        backend_model: The configuration dictionary for the backend.
    """
read(run_meta)

Read a numpy array from storage.

Parameters:

Name Type Description Default
run_meta RunMeta

Metadata about the current run.

required

Returns:

Type Description
NDArray[float64]

The numpy array read from storage.

Source code in src/flepimop2/backend/abc.py
34
35
36
37
38
39
40
41
42
43
44
def read(self, run_meta: RunMeta) -> NDArray[np.float64]:
    """
    Read a numpy array from storage.

    Args:
        run_meta: Metadata about the current run.

    Returns:
        The numpy array read from storage.
    """
    return self._read(run_meta)
save(data, run_meta)

Save a numpy array to storage.

Parameters:

Name Type Description Default
data NDArray[float64]

The numpy array to save.

required
run_meta RunMeta

Metadata about the current run.

required
Source code in src/flepimop2/backend/abc.py
24
25
26
27
28
29
30
31
32
def save(self, data: NDArray[np.float64], run_meta: RunMeta) -> None:
    """
    Save a numpy array to storage.

    Args:
        data: The numpy array to save.
        run_meta: Metadata about the current run.
    """
    return self._save(data, run_meta)

build(config)

Build a BackendABC from a configuration dictionary.

Parameters:

Name Type Description Default
config dict[str, Any]

Configuration dictionary. The dict must contains a 'module' key, which will be used to lookup the Backend module path. The module will have "flepimop2.backend." prepended.

required

Returns:

Type Description
BackendABC

The constructed backend instance.

Raises:

Type Description
TypeError

If the built backend is not an instance of BackendABC.

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

    Args:
        config: Configuration dictionary. The dict must contains a 'module' key, which
            will be used to lookup the Backend module path. The module will have
            "flepimop2.backend." prepended.

    Returns:
        The constructed backend instance.

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

csv

CSV backend for flepimop2.

CsvBackend(root)

Bases: BackendABC

CSV backend for saving numpy arrays to CSV files.

Initialize the CSV backend with configuration.

Parameters:

Name Type Description Default
root PathLike[str] | None

Base output directory for CSV files.

required

Raises:

Type Description
TypeError

If the 'root' is not a string or Path.

Source code in src/flepimop2/backend/csv.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def __init__(self, root: PathLike[str] | None) -> None:
    """
    Initialize the CSV backend with configuration.

    Args:
        root: Base output directory for CSV files.

    Raises:
        TypeError: If the 'root' is not a string or Path.
    """
    base_path = Path(root) if root is not None else Path.cwd() / "model_output"
    if base_path.is_file():
        msg = "The 'path' in backend configuration must be a directory."
        raise TypeError(msg)
    if not (base_path.exists() and os.access(base_path, os.W_OK)):
        msg = f"The specified 'path' does not exist or is not writable: {base_path}"
        raise TypeError(msg)
    self.base_path = base_path

build(root=None)

Build a CsvBackend from a configuration dictionary.

Parameters:

Name Type Description Default
root PathLike[str] | None

Base output directory for CSV files. 'module' key, which will be used to lookup the Backend module path. The module will have "flepimop2.backends." prepended.

None

Returns:

Type Description
BackendABC

The constructed csv backend.

Source code in src/flepimop2/backend/csv.py
77
78
79
80
81
82
83
84
85
86
87
88
89
def build(root: PathLike[str] | None = None) -> BackendABC:
    """
    Build a `CsvBackend` from a configuration dictionary.

    Args:
        root: Base output directory for CSV files. 'module' key, which will be used to
            lookup the Backend module path. The module will have "flepimop2.backends."
            prepended.

    Returns:
        The constructed csv backend.
    """
    return CsvBackend(root)