Skip to content

Configuration classes

The configuration parameters provided in the configuration files are used to create the Configs class object. This object is used to store the configuration parameters and to provide the configuration parameters to the other classes.

Configs class

Class for storing all configurations given in the configuration file.

This class organises the configuration parameters into those releated to ODE, solver, simulator, emulator and plotter.

Parameters:

Name Type Description Default
config_file PathLike

The path to the configuration file

required
ode ODEConfig

The ODE configurations

field(init=False)
solver SolverConfig

The solver configurations

field(init=False)
simulator SimulatorConfig

The simulator configurations

field(init=False)
emulator EmulatorConfig

The emulator configurations

field(init=False)
plotter PlotterConfig

The plotter configurations

field(init=False)

Each of these configurations are instances of the respective configuration classes.

Source code in emulode/config.py
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
@dataclass
class Configs:
    """
    Class for storing all configurations given in the configuration file.

    This class organises the configuration parameters into those releated to
    ODE, solver, simulator, emulator and plotter.

    Args:
        config_file: The path to the configuration file

        ode: The ODE configurations
        solver: The solver configurations
        simulator: The simulator configurations
        emulator: The emulator configurations
        plotter: The plotter configurations

    Each of these configurations are instances of the respective configuration
    classes.
    """

    config_file: os.PathLike

    ode: ODEConfig = field(init=False)
    solver: SolverConfig = field(init=False)
    simulator: SimulatorConfig = field(init=False)
    emulator: EmulatorConfig = field(init=False)
    plotter: PlotterConfig = field(init=False)

    def __post_init__(self) -> None:

        self.load_config()
        self.individual_validate()

    def load_config(self) -> None:
        """Load the configuration file."""

        with open(self.config_file, "r", encoding="utf-8") as file:
            config_dict = yaml.safe_load(file)

        self.ode = ODEConfig(config_dict["ode"])
        self.solver = SolverConfig(config_dict["solver"])
        self.simulator = SimulatorConfig(config_dict["simulator"])
        self.emulator = EmulatorConfig(config_dict["emulator"])
        self.plotter = PlotterConfig(config_dict["plotter"])

    def individual_validate(self) -> None:
        """Validate the data."""

        self.ode.validate()
        self.solver.validate()
        self.simulator.validate()
        self.emulator.validate()
        self.plotter.validate()

    def __repr__(self) -> str:
        """Return a string representation of the configuration file."""

        return (
            f"{self.ode}\n"
            f"{self.solver}\n"
            f"{self.simulator}\n"
            f"{self.emulator}\n"
            f"{self.plotter}\n"
        )

The ode, solver, simulation, emulation and plotter members are instances of the Config abstract base class.

Config abstract base class

Bases: ABC

Abstract class for the configuration file. This class should be inherited by other configuration classes.

This defines the basic structure of all the congiguration classes. It contains the config_dict which is the dictionary representation of the configuration file and the required_keys which is a list of keys that must be present in the configuration file. These checks are performed in the __init__ method.

Every configuration class should have a validate method which validates the data in the configuration class and a __repr__ method which returns a string representation of the configuration class.

Parameters:

Name Type Description Default
config_dict dict

The dictionary representation of the configuration file

required
required_keys list[str]

A list of keys that must be present in the configuration file

required
Source code in emulode/config.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class Config(ABC):
    """
    Abstract class for the configuration file. This class should be inherited
    by other configuration classes.

    This defines the basic structure of all the congiguration classes. It
    contains the `config_dict` which is the dictionary representation of the
    configuration file and the `required_keys` which is a list of keys that
    must be present in the configuration file. These checks are performed in
    the `__init__` method.

    Every configuration class should have a `validate` method which validates
    the data in the configuration class and a `__repr__` method which returns
    a string representation of the configuration class.

    Args:
        config_dict: The dictionary representation of the configuration file
        required_keys: A list of keys that must be present in the configuration
            file
    """

    def __init__(self, config_dict: dict, required_keys: list[str]) -> None:

        self.check_keys(config_dict, required_keys)

        for key, value in config_dict.items():
            if key in required_keys:
                setattr(self, key, value)

    @abstractmethod
    def validate(self) -> None:
        """Validate the data in the configuration file."""

    @abstractmethod
    def __repr__(self) -> str:
        """Return a string representation of the configuration class."""

    def check_keys(self, config, required_keys: list[str]) -> None:
        """
        Checks that the given keys are present in the config file. If not, raises
        a KeyError. This method is used to check the required keys in the
        initialization of the configuration base class.
        """

        for key in required_keys:

            if getattr(self, key) is not None:
                continue

            if key not in config:
                raise KeyError(f"Key '{key}' not found in config file")

The Config abstract base class is used to define the configuration parameters for the ode, solver, simulation, emulation and plotter members of the Configs class.

Note: In the following classes, all parameters other than config_dict are optional. The config_dict parameter is a dictionary containing the default configuration parameters (as typically read from a configuration file). The optional parameters if provided will override the parameters in the config_dict dictionary.


ODEConfig class

Bases: Config

Class for the ODE configuration file.

This class contains the chosen ODE and the parameters for the chosen ODE. The required_keys for this class are "chosen_ode" and "parameters".

Parameters:

Name Type Description Default
config_dict dict

The dictionary representation of the 'ode' part of the configuration file

required
chosen_ode str

The chosen ODE

None
parameters dict

The parameters for the chosen ODE

None
Source code in emulode/config.py
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
class ODEConfig(Config):
    """
    Class for the ODE configuration file.

    This class contains the chosen ODE and the parameters for the chosen ODE. The
    `required_keys` for this class are "chosen_ode" and "parameters".

    Args:
        config_dict: The dictionary representation of the 'ode' part of the configuration file

        chosen_ode: The chosen ODE
        parameters: The parameters for the chosen ODE
    """

    def __init__(
        self, config_dict: dict, chosen_ode: str = None, parameters: dict = None
    ) -> None:

        required_keys = ["chosen_ode", "parameters"]
        self.chosen_ode: str = chosen_ode
        self.parameters: dict = parameters

        super().__init__(config_dict, required_keys)

        if self.chosen_ode is None:
            raise ValueError("Chosen ODE not found")

        self.parameters = config_dict["parameters"][self.chosen_ode]

    def validate(self) -> None:
        """Validate the data."""

        if not isinstance(self.parameters, dict):
            raise ValueError("Parameters must be a dictionary")

        for key, value in self.parameters.items():
            if not isinstance(value, (int, float)):
                raise ValueError(f"Parameter {key} must be a number")

    def __repr__(self) -> str:
        """Return a string representation of the configuration file."""

        return (
            f"ODE System\n===========\n"
            f"Chosen ODE: {self.chosen_ode}\n"
            f"Parameters: {self.parameters}\n"
        )

SolverConfig class

Bases: Config

Class for the solver configuration file.

This class contains parameters related to the solver. The required_keys for this class are "initial_conditions", "t_range", "n_steps" and "transience".

Parameters:

Name Type Description Default
config_dict dict

The dictionary representation of the 'solver' part of the configuration file

required
initial_conditions list[float]

The initial conditions for the ODE

None
t_range list[float]

The time range for the simulation in the form [t_initial, t_final]

None
n_steps int

The number of steps for the simulation

None
transience float

The transience for the simulation (as a fraction between 0 and 1)

None
Source code in emulode/config.py
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
class SolverConfig(Config):
    """
    Class for the solver configuration file.

    This class contains parameters related to the solver. The `required_keys` for
    this class are "initial_conditions", "t_range", "n_steps" and "transience".

    Args:
        config_dict: The dictionary representation of the 'solver' part of the configuration file

        initial_conditions: The initial conditions for the ODE
        t_range: The time range for the simulation in the form [t_initial, t_final]
        n_steps: The number of steps for the simulation
        transience: The transience for the simulation (as a fraction between 0 and 1)
    """

    def __init__(
        self,
        config_dict: dict,
        initial_conditions: list[float] = None,
        t_range: list[float] = None,
        n_steps: int = None,
        transience: float = None,
    ) -> None:

        required_keys = ["initial_conditions", "t_range", "n_steps", "transience"]
        self.initial_conditions: list[float] = initial_conditions
        self.t_range: list[float] = t_range
        self.n_steps: int = n_steps
        self.transience: float = transience

        super().__init__(config_dict, required_keys)

    @property
    def t_initial(self) -> float:
        """Return the initial time."""

        return self.t_range[0]

    @property
    def t_final(self) -> float:
        """Return the final time."""

        return self.t_range[1]

    def validate(self) -> None:

        if not isinstance(self.initial_conditions, list):
            raise ValueError("Initial conditions must be a list")

        if not isinstance(self.t_range, list):
            raise ValueError("Time range must be a tuple")

        if not isinstance(self.n_steps, int):
            raise ValueError("Number of steps must be an integer")

        if not isinstance(self.transience, float):
            raise ValueError("Transience must be a float")

        if len(self.t_range) != 2:
            raise ValueError("Time range must have two elements")

        if self.t_initial >= self.t_final:
            raise ValueError("Initial time must be less than final time")

        if self.n_steps <= 0:
            raise ValueError("Number of steps must be positive")

        if self.transience < 0 or self.transience > 1:
            raise ValueError("Transience must be between 0 and 1")

    def __repr__(self) -> str:

        return (
            f"Solver\n======\n"
            f"Initial conditions: {self.initial_conditions}\n"
            f"Time range: {self.t_range}\n"
            f"Number of steps: {self.n_steps}\n"
            f"Transience: {self.transience}\n"
        )

SimulatorConfig class

Bases: Config

Class for the simulator configuration file.

This class contains parameters related to the simulator. The required_keys for this class are "parameter_of_interest", "range" and "n_simulation_points".

Parameters:

Name Type Description Default
config_dict dict

The dictionary representation of the 'simulator' part of the configuration file

required
parameter_of_interest str

The parameter of interest (Must be one of parameters in ODE)

None
parameter_range tuple[float]

The range for the simulation in the form (min, max)

None
n_simulation_points int

The number of simulation points

None
Source code in emulode/config.py
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
class SimulatorConfig(Config):
    """
    Class for the simulator configuration file.

    This class contains parameters related to the simulator. The `required_keys` for
    this class are "parameter_of_interest", "range" and
    "n_simulation_points".

    Args:
        config_dict: The dictionary representation of the 'simulator' part of the configuration file

        parameter_of_interest: The parameter of interest (Must be one of parameters in ODE)
        parameter_range: The range for the simulation in the form (min, max)
        n_simulation_points: The number of simulation points
    """

    def __init__(
        self,
        config_dict: dict,
        parameter_of_interest: str = None,
        parameter_range: tuple[float] = None,
        n_simulation_points: int = None,
    ) -> None:

        required_keys = [
            "parameter_of_interest",
            "range",
            "n_simulation_points",
        ]
        self.parameter_of_interest = parameter_of_interest
        self.range = parameter_range
        self.n_simulation_points = n_simulation_points

        super().__init__(config_dict, required_keys)

    def validate(self) -> None:

        if not isinstance(self.parameter_of_interest, str):
            print(self.parameter_of_interest)
            raise ValueError("Parameter of interest must be a string")

        if not isinstance(self.range, list):
            raise ValueError("Range must be a tuple")

        if not isinstance(self.n_simulation_points, int):
            raise ValueError("Number of simulation points must be an integer")

        if len(self.range) != 2:
            raise ValueError("Range must have two elements")

        if self.n_simulation_points <= 0:
            raise ValueError("Number of simulation points must be positive")

    def __repr__(self) -> str:

        return (
            f"Simulator\n=========\n"
            f"Parameter of interest: {self.parameter_of_interest}\n"
            f"Range: {self.range}\n"
            f"Number of simulation points: {self.n_simulation_points}\n"
        )

EmulatorConfig class

Bases: Config

Class for the emulator configuration file.

This class contains parameters related to the emulator. The required_keys for this class are "n_layers", "n_prediction_points" and "n_iterations".

Parameters:

Name Type Description Default
config_dict dict

The dictionary representation of the 'emulator' part of the configuration file

required
n_layers int

The number of layers in the emulator

None
n_prediction_points int

The number of prediction points

None
n_iterations int

The number of iterations for the emulator

None
Source code in emulode/config.py
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
class EmulatorConfig(Config):
    """
    Class for the emulator configuration file.

    This class contains parameters related to the emulator. The `required_keys` for
    this class are "n_layers", "n_prediction_points" and "n_iterations".

    Args:
        config_dict: The dictionary representation of the 'emulator' part of the configuration file

        n_layers: The number of layers in the emulator
        n_prediction_points: The number of prediction points
        n_iterations: The number of iterations for the emulator
    """

    def __init__(
        self,
        config_dict: dict,
        n_layers: int = None,
        n_prediction_points: int = None,
        n_iterations: int = None,
    ) -> None:

        required_keys = ["n_layers", "n_prediction_points", "n_iterations"]
        self.n_layers: int = n_layers
        self.n_prediction_points: int = n_prediction_points
        self.n_iterations: int = n_iterations

        super().__init__(config_dict, required_keys)

    def validate(self) -> None:

        if not isinstance(self.n_layers, int):
            raise ValueError("Number of layers must be an integer")

        if not isinstance(self.n_prediction_points, int):
            raise ValueError("Number of prediction points must be an integer")

        if not isinstance(self.n_iterations, int):
            raise ValueError("Number of iterations must be an integer")

        if self.n_layers <= 0:
            raise ValueError("Number of layers must be positive")

        if self.n_prediction_points <= 0:
            raise ValueError("Number of prediction points must be positive")

        if self.n_iterations <= 0:
            raise ValueError("Number of iterations must be positive")

    def __repr__(self) -> str:

        return (
            f"Emulator\n========\n"
            f"Number of layers: {self.n_layers}\n"
            f"Number of prediction points: {self.n_prediction_points}\n"
            f"Number of iterations: {self.n_iterations}\n"
        )

PlotterConfig class

Bases: Config

Class for the plotter configuration file.

This class contains parameters related to the plotter. The required_keys for this class are "directory", "filename", "x_label" and "y_label".

Parameters:

Name Type Description Default
config_dict dict

The dictionary representation of the 'plotter' part of the configuration file

required
directory str

The directory to save the plot

None
filename str

The filename for the plot

None
x_label str

The x label for the plot

None
y_label str

The y label for the plot

None
Source code in emulode/config.py
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
class PlotterConfig(Config):
    """
    Class for the plotter configuration file.

    This class contains parameters related to the plotter. The `required_keys` for
    this class are "directory", "filename", "x_label" and "y_label".

    Args:
        config_dict: The dictionary representation of the 'plotter' part of the configuration file

        directory: The directory to save the plot
        filename: The filename for the plot
        x_label: The x label for the plot
        y_label: The y label for the plot
    """

    def __init__(
        self,
        config_dict: dict,
        directory: str = None,
        filename: str = None,
        x_label: str = None,
        y_label: str = None,
    ) -> None:

        required_keys = ["directory", "filename", "x_label", "y_label"]
        self.directory: str = directory
        self.filename: str = filename
        self.x_label: str = x_label
        self.y_label: str = y_label

        super().__init__(config_dict, required_keys)

    def validate(self) -> None:

        if not isinstance(self.directory, str):
            raise ValueError("Directory must be a string")

        if not isinstance(self.filename, str):
            raise ValueError("Filename must be a string")

        if not isinstance(self.x_label, str):
            raise ValueError("X label must be a string")

        if not isinstance(self.y_label, str):
            raise ValueError("Y label must be a string")

    def __repr__(self) -> str:

        return (
            f"Plotter\n=======\n"
            f"Directory: {self.directory}\n"
            f"Filename: {self.filename}\n"
            f"X label: {self.x_label}\n"
            f"Y label: {self.y_label}\n"
        )