Utils
- class derrom.utils.PHELPH_ivp_integrator(model, derivs=None, dt=1.0, dt_out=1.0, method='Heun')[source]
Bases:
ivp_integrator- fit(trajectories, targets, **kwargs)[source]
fits the data driven model to the training data presented via the **kwargs. Used by the KFold_CV function
- get_error(truth, pred=None, norm='rms')[source]
Calculates the error between the ground truth values and predicted values.
- Parameters:
truth (2D numpy.ndarray) – The ground truth values.
pred (2D numpy.ndarray, optional) – The predicted values. If not provided, the function uses the predict method of the class to make predictions.
norm (str, optional) – The type of norm to be used to calculate the error. Default is ‘rms’. Possible values are: - ‘rms’: Normalized Frobenius norm - ‘fro’: Frobenius norm - ‘max’: Absolute maximum norm
- Returns:
error – The calculated error value.
- Return type:
float
- derrom.utils.get_KFold_CV_scores(model, trajectories, targets='AR', folds=5, seed=817, norms=['rms'], train_kwargs={})[source]
Conduct K-fold cross-validation on the given model using the given trajectories and targets, and return the scores for each fold.
- Parameters:
model (object) – An object that implements the fit() and score_multiple_trajectories() methods, which will be used to train and score the model.
trajectories (list) – An list of trajectories, where each trajectory is a 2D numpy.ndarray.
targets (list or str, optional) – An list of targets corresponding to the given trajectories, or ‘AR’ for autoregression mode. Defaults to ‘AR’.
folds (int, optional) – The number of folds to create for cross-validation. Defaults to 5.
seed (int, optional) – The seed value to use for the random number generator when shuffling the trajectories. Defaults to 817.
norms (list of str, optional) – A list of error norms to use for scoring the trajectories. Defaults to [‘rms’].
train_kwargs (dict, optional) – A dictionary of keyword arguments to pass to the fit() method when training the model. Defaults to {}.
- Returns:
scores – A list of n lists, where n is the number of error norms specified in norms. Each list contains the scores for each each error norm.
- Return type:
list
- derrom.utils.get_KFolds(data_list, folds=5)[source]
Split the given list of trajectories into K-folds for cross-validation.
- Parameters:
data_list (list) – An list of trajectories, where each trajectory is a 2D numyp.ndarray.
folds (int, optional) – The number of folds to create. Defaults to 5.
- Returns:
folds – A list of K folds, where each fold is a list of trajectories.
- Return type:
list
- class derrom.utils.ivp_integrator(model, derivs=None, dt=1.0, dt_out=1.0, method='Heun')[source]
Bases:
objectQuick and dirty numerical integrator for solving initial value problems (IVPs), where either all or only parts of the right-hand-side of the equations of motion are provided by a data-driven model. Implements the fit() and score_multiple() methods to work with the get_KFold_CV_scores() function.
- Parameters:
model (object) – Model object with the methods ‘predict’ and ‘fit’. ‘predict’ provides either all or parts of the derivatives. The ‘fit’ method can be invoked by the integrator during KFold_CV scoring.
derivs (function, optional) – Function that calculates the system derivatives with respect to time. Default is None, in which case the ‘predict’ method of the model object is used.
dt (float, optional) – Time step for numerical integration. Default is 1.
dt_out (float, optional) – Time step for saving output data. Default is 1.
method ({'Heun', 'Euler'}, optional) – Numerical integration method to use. Default is ‘Heun’.
- model_hist_option
Boolean indicating whether the model’s ‘full_hist’ attribute is True or False.
- Type:
bool
- fit(**kwargs)[source]
fits the data driven model to the training data presented via the **kwargs. Used by the KFold_CV function
- get_error(truth, pred=None, norm='rms')[source]
Calculates the error between the ground truth values and predicted values.
- Parameters:
truth (2D numpy.ndarray) – The ground truth values.
pred (2D numpy.ndarray, optional) – The predicted values. If not provided, the function uses the predict method of the class to make predictions.
norm (str, optional) – The type of norm to be used to calculate the error. Default is ‘rms’. Possible values are: - ‘rms’: Normalized Frobenius norm - ‘fro’: Frobenius norm - ‘max’: Absolute maximum norm
- Returns:
error – The calculated error value.
- Return type:
float
- integrate(init, n_steps, dt=None, dt_out=None)[source]
Integrate the differential equations using the specified method and time step.
- Parameters:
init (2D numpy.ndarray) – Initial state of the system.
n_steps (int) – Number of integration steps to perform.
dt (float, optional) – Time step for the integration. Defaults to self.dt if not specified.
dt_out (float, optional) – Time step for outputting results. Defaults to self.dt_out if not specified.
- Returns:
trajectory – Array containing the state of the system at each time step.
- Return type:
2D numpy.ndarray
- predict(trajectory)[source]
Use the first time steps of the given trajectory as initial conditions and integrate the system to obtain a solution with identical length. Intended to score the solutions against the input trajectories.
- Parameters:
trajectory (2D numpy.ndarray) – Test trajectory, i.e., ground truth
- Returns:
prediction – An array containing the predicted trajectory of the system.
- Return type:
2D numpy.ndarray
- score_multiple_trajectories(trajectories, targets=None, predictions=None, **kwargs)[source]
helper function to obtain error scores for multiple trajectories. Used by the derrom.utils.get.get_KFold_CV_scores function.
- Parameters:
trajectories (list) – A list of the trajectories to be scored, where each element of the list is expected to be a 2D numpy.ndarray that represents an individual trajectories. Time slices must be stored in the rows (first index) and the system state variables in the columns (second index). All trajectories must have the same number of variables (columns), the number of time slices, however, may vary.
targets (list) – A list of the targets, where each element is an numpy.ndarray that corresponds to the trajectory with the identical list index. Each element must have the same number of rows as the corresponding trajectory. If set to ‘AR’, i.e., autoregression, no targets are required.
predictions (list) – A list of the predictions, where each element is an numpy.ndarray that corresponds to the trajectory with the identical list index. Supplying predictions reduces computaion time if derrom.utils.get.get_KFold_CV_scores is to be evaluated for multiple error norms.
- Returns:
mean (float) – mean regression error from all supplied trajectories
scores (list) – list of all individual regression errors
- derrom.utils.load_model(filename='../model.obj')[source]
Load a Python object from a file using the pickle module.
- Parameters:
filename (str, optional) – The name of the file to load the object from. Defaults to ‘../model.obj’.
- Returns:
model – The loaded model object.
- Return type:
object
- derrom.utils.load_trajectories(filename='../trajectories.npz')[source]
Load a list of trajectories from a numpy .npz file.
- Parameters:
filename (str, optional) – Name of the file to load the trajectories from. Defaults to ‘../trajectories.npz’.
- Returns:
trajectories – List of loaded trajectories if file exists, otherwise None.
- Return type:
list
- derrom.utils.plot_difference(test, truth, title='difference')[source]
Plot the element-wise difference between two 2D arrays using matplotlib.
- Parameters:
test (numpy.ndarray) – The first 2D array to be compared.
truth (numpy.ndarray) – The second 2D array to be compared.
title (str, optional) – The title of the plot. Defaults to ‘difference’.
- derrom.utils.plot_trajectory(data, title='trajectory')[source]
Plot a 2D trajectory using matplotlib.
- Parameters:
data (2D numpy.ndarray) – A 2D array of the trajectory to be plotted. Each row represents a time step and each column represents a state variable.
title (str, optional) – The title of the plot. Defaults to ‘trajectory’.
- class derrom.utils.reducer_helper_class(dim_reducer=None, rdim=1)[source]
Bases:
objectA helper class for benchmarking dimensionality reducers. Implements the fit() and score_multiple() methods to work with the get_KFold_CV_scores() function.
- Parameters:
dim_reducer (object or None, optional) – The dimensionality reduction object
rdim (int, optional) – The reduced dimensionality, i.e., the number of components to retain after dimensionality reduction. Default is 1.
- rdim
The reduced dimensionality, i.e., the number of components to retain after dimensionality reduction.
- Type:
int
- dim_reducer
The dimensionality reduction object.
- Type:
object
- reg_mode
This string is set to ‘AR’ to properly work the the KFold_CV function
- Type:
str
- derrom.utils.save_model(model, filename='../model.obj')[source]
Save a model object to a file using the pickle module.
- Parameters:
model (object) – The model to be saved to a file.
filename (str, optional) – The name of the file to save the object to. Defaults to ‘../model.obj’.
- derrom.utils.save_trajectories(trajectories, filename='../trajectories')[source]
Save the given list of trajectories to a numpy .npz file.
- Parameters:
trajectories (list) – list of trajectories to save.
filename (str, optional) – Name of the file to save the trajectories to. Defaults to ‘../trajectories’.