Data loadersο
Module for handling datasets and computing prediction errors in population dynamics.
This module provides several dataset classes designed for loading and accessing different formats of the population trajectory data, including trajectory data and coupling data, and testing fit and prediction errors.
Classesο
PopulationDatasetHandles loading and batching of particle trajectory data. The single unit if a particle trajectory.
CouplingsDatasetLoads coupling data for trajectory models, including weights, features, and densities. The single unit is a coupling.
LinearParametrizationDatasetLoads data for the linear parametrization. The single unit is the entire dataset.
PopulationEvalDatasetFacilitates evaluation of model predictions using particle trajectories and computes prediction errors such as the Wasserstein distance.
- class dataset.CouplingsDataset(dataset_name: str)[source]ο
Bases:
DatasetDataset class for loading and accessing couplings data.
The dataset is expected to be located in a directory named βdata/{dataset_name}β and consist of multiple .npy files. It provides access to input features, target features, time labels, weights, density values, and density gradients.
- weightο
Array of weights extracted from the couplings data.
- Type:
np.ndarray
- xο
Array of input features extracted from the couplings data.
- Type:
np.ndarray
- yο
Array of target features extracted from the couplings data.
- Type:
np.ndarray
- timeο
Array of time labels extracted from the couplings data.
- Type:
np.ndarray
- densitiesο
Array of density values extracted from the densities files.
- Type:
np.ndarray
- densities_gradsο
Array of gradients of densities extracted from the densities files.
- Type:
np.ndarray
- __getitem__(idx: int) Tuple[Array, Array, Array, Array, Array, Array][source]ο
Retrieve a sample (x, y, t, w, rho, rho_grad) from the dataset at the given index.
- Parameters:
idx (int) β The index of the sample to retrieve.
- Returns:
A tuple containing:
Input features (jnp.ndarray): Initial particle distribution.
Target features (jnp.ndarray): Target particle distribution.
Time label (jnp.ndarray): Time label.
Weight of the coupling (jnp.ndarray): Weight of the coupling.
Density value (jnp.ndarray): Density value.
Gradient of densities (jnp.ndarray): Gradient of densities.
- Return type:
Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray, jnp.ndarray, jnp.ndarray, jnp.ndarray]
- class dataset.LinearParametrizationDataset(dataset_name: str)[source]ο
Bases:
DatasetThis dataset class loads and organizes data necessary for linear parametrization solver tasks, for which all data is analyzed together.
- __getitem__(_) List[Tuple[ndarray, ndarray, ndarray, ndarray, ndarray, ndarray]][source]ο
Retrieve the entire dataset.
Since for the linear parametrization all data is used together, this method returns all data at once and the index parameter _ is ignored.
- Parameters:
_ (any) β This parameter is ignored.
- Returns:
List[Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray, jnp.ndarray, jnp.ndarray, jnp.ndarray]] β A list of tuples, where each tuple contains:
- Input features (jnp.ndarray) (Initial particle distribution.)
- Target features (jnp.ndarray) (Target particle distribution.)
- Time label (jnp.ndarray) (Time label associated with each sample.)
- Weight of the coupling (jnp.ndarray) (Weight assigned to the coupling.)
- Density values (jnp.ndarray) (Density values.)
- Gradient of densities (jnp.ndarray) (Gradient of the density values.)
- class dataset.PopulationDataset(dataset_name: str, batch_size: int)[source]ο
Bases:
DatasetDataset class for loading and accessing particle trajectory data.
The dataset is expected to be located in a directory named βdata/{dataset_name}β and consist of a single .npy file named βdata.npyβ. The data contains particle trajectories over time, where each timestep has a set of particles.
If the number of particles in a timestep is less than the maximum number of particles in any timestep, the dataset wraps around to handle the imbalance.
- trajectoryο
Array of shape (num_timesteps, num_particles, num_features) containing the particle trajectories. Each entry in the array represents a particleβs state at a given timestep.
- Type:
np.ndarray
- __getitem__(idx: int) list[source]ο
Retrieve particle data for each timestep at the given index.
- Parameters:
idx (int) β The index of the particle to retrieve.
- Returns:
A list where each element is an array representing the state of a particle at each timestep. The length of the list corresponds to the number of timesteps, and each array represents the particle state at a specific timestep.
- Return type:
list of np.ndarray
- __init__(dataset_name: str, batch_size: int) None[source]ο
Initialize the PopulationDataset by loading data from βdata.npyβ.
- Parameters:
dataset_name (str) β The name of the dataset to load. The dataset should be located in βdata/{dataset_name}β and should contain a .npy file named βdata.npyβ.
- class dataset.PopulationEvalDataset(key, dataset_name: str, solver: str, wasserstein_metric: int, label='test_data')[source]ο
Bases:
DatasetThis dataset class loads and organizes population trajectory data for evaluation.
- trajectoryο
A dictionary where each key corresponds to a unique timestep in the dataset, and the value is an array of trajectory data associated with that timestep.
- Type:
dict
- label_mappingο
A dictionary mapping the original sample labels to consecutive integer indices.
- Type:
dict
- Tο
The number of timesteps in the trajectories.
- Type:
int
- data_dimο
The dimensionality of the data at each timestep.
- Type:
int
- no_ground_truthο
Flag indicating if the dataset lacks a ground truth file.
- Type:
bool
- potentialο
The potential function used in the predictions.
- Type:
str
- internalο
The internal dynamics setting used.
- Type:
str
- betaο
The beta parameter used in the simulations.
- Type:
float
- interactionο
The interaction function used in the predictions.
- Type:
str
- dtο
The timestep size used in the simulation.
- Type:
float
- trajectory_only_potentialο
Trajectory predictions considering only the potential term.
- Type:
np.ndarray
- trajectory_only_interactionο
Trajectory predictions considering only the interaction term.
- Type:
np.ndarray
- __getitem__(idx: int) ndarray[source]ο
Retrieves a particleβs features at the first timestep.
- Parameters:
idx (int) β The index of the particle to retrieve.
- Returns:
The features of the specified particle at the first timestep.
- Return type:
np.ndarray
- __init__(key, dataset_name: str, solver: str, wasserstein_metric: int, label='test_data')[source]ο
Initialize the PopulationEvalDataset.
- Parameters:
key (Any) β A key used for random number generation or seeding.
dataset_name (str) β The name of the dataset to load. The data should be located in the directory βdata/{dataset_name}β and consist of .npy files.
solver (str) β The solver method used, primarily for plotting or prediction purposes.
wasserstein_metric (int) β Specifies the order of the Wasserstein distance to be used for the error calculation.
label (str, optional) β Specifies whether to load βtest_dataβ or βtrain_dataβ. Default is βtest_dataβ.
- __len__() int[source]ο
Get the number of particles at the first timestep.
- Returns:
The number of particles at the first timestep.
- Return type:
int
- error_wasserstein(trajectory_predicted: ndarray) float[source]ο
Compute the Wasserstein loss between the predicted and true trajectories.
This method calculates the Wasserstein distance (a measure of distance between probability distributions) between the predicted trajectories and the true trajectories over all timesteps.
- Parameters:
trajectory_predicted (np.ndarray) β The predicted trajectory with shape (T, n_particles, n_features).
- Returns:
The cumulative Wasserstein error over all timesteps.
- Return type:
float
- error_wasserstein_cumulative(predictions: Array, model: str, plot_folder_name: str | None = None) Array[source]ο
Compute the cumulative Wasserstein error per timestep.
This method calculates the Wasserstein distance between the predicted and actual trajectories at each timestep and returns the cumulative error.
- Parameters:
predictions (jnp.ndarray) β Array of predicted trajectories with shape (T+1, n_particles, n_features). The predictions should cover the entire timespan from 0 to T.
model (str) β Name of the solver model used. This is primarily used for plotting purposes.
plot_folder_name (Optional[str], default=None) β Directory path where plots should be saved. If None, no plots will be saved.
- Returns:
Array of cumulative Wasserstein errors, with each entry corresponding to the error at a specific timestep. The array has length T.
- Return type:
jnp.ndarray
- error_wasserstein_one_step_ahead(potential: Callable[[Array], float], beta: float, interaction: Callable[[Array], float], key_eval: Array, model: str, plot_folder_name: str | None = None) Array[source]ο
Compute the Wasserstein error for one-step-ahead predictions.
This method evaluates the prediction error by computing the Wasserstein distance between the predicted trajectory and the actual trajectory at each timestep, given the current true population.
- Parameters:
potential (Callable[[jnp.ndarray], float]) β Function that computes the potential based on a JAX array input.
beta (float) β Beta parameter used in the predictions.
interaction (Callable[[jnp.ndarray], float]) β Function that computes the interaction based on a JAX array input.
key_eval (jnp.ndarray) β Random key for JAX-based random number generation.
model (str) β Name of the solver model used. This is primarily used for plotting purposes.
plot_folder_name (Optional[str], default=None) β Directory path where plots should be saved. If None, no plots will be saved.
- Returns:
An array of Wasserstein errors for the one-step-ahead predictions over timesteps. The array has length T, where each entry corresponds to the error at a specific timestep.
- Return type:
jnp.ndarray