caskade: building Pythonic scientific simulators

Connor Stone, Alexandre Adam, Adam Coogan, Laurence Perreault-Levasseur, Yashar HezavehThe Journal of Open Source Software 2025

Abstract

Scientific simulators and pipelines form the core of many research projects. Writing high-quality, modular code allows for efficiently scaling a project, but this can be challenging in a research context. Research project goals and solutions to those goals are constantly in flux, requiring many refactoring rounds to meet these changes. The result can be a progressively more unwieldy interconnected code. Here we present a system, caskade, which allows users to focus on modular components of a simulator, which are small and testable to ensure robustness. With caskade, one can turn these modular components into abstracted blocks that connect to form a powerful simulator. caskade manages the flow of parameter values through such a simulator.

Introduction

Scientific research is inherently iterative. As a project evolves, the underlying code often becomes a "spaghetti" of interconnected parameters and functions. This "software entropy" makes it difficult to scale projects, share parameters across different modules, or switch between different types of analysis—such as fixing certain values for one experiment while allowing them to vary in another.

Researchers often find themselves manually "wiring" parameters through deep nests of functions, which is error-prone and makes refactoring difficult. Furthermore, interfacing these complex models with external tools for Markov Chain Monte Carlo (MCMC) or optimization usually requires writing tedious "wrapper" code to flatten and unflatten parameter vectors. caskade is a Python library designed to bring the modularity and ease-of-use of deep learning frameworks like PyTorch to the world of general scientific simulators.

Methodology and Architecture

caskade adopts an object-oriented approach inspired by PyTorch’s nn.Module. Its architecture relies on three core components:

  1. Module base class: Users subclass this to create modular components of a simulator.
  2. Param object: A dedicated object for defining model parameters, which can store metadata like uncertainty or valid ranges.
  3. @forward decorator: Applied to class methods to signal that caskade should manage the flow of Param arguments through that function.

When these modules are combined, caskade automatically constructs a Directed Acyclic Graph (DAG). This graph manages the "cascade" of values, allowing parameters to be synced via arbitrary functional relationships. Crucially, it supports multiple numerical backends, including NumPy, JAX, and PyTorch, allowing researchers to leverage hardware acceleration and automatic differentiation.

Key Features

The DAG as Infrastructure

By treating the simulator as a graph, the "plumbing" of parameters is handled automatically. If multiple parts of a simulator require a shared physical constant (e.g., a specific cosmology), they can all point to a single module, ensuring consistency without manual updates.

Static vs. Dynamic Fluidity

caskade allows parameters to be toggled between "static" (fixed values) and "dynamic" (provided at runtime) without changing the underlying model code. This flexibility is essential for complex inference tasks like Gibbs sampling, where different parameters are updated at different stages.

Vectorization for Inference

The library can automatically represent all parameters in a complex, nested model as a single 1D vector. This allows for near-effortless integration with standard optimization and sampling libraries like scipy.optimize or emcee.

Application: Gravitational Lensing with caustics

The power of caskade is best demonstrated through the caustics package, a gravitational lensing simulator. In this context, caskade manages complex hierarchies including sources, multiple lenses, and a cosmology model, all while keeping the code for each component isolated and testable.

A Directed Acyclic Graph (DAG) generated by caskade for a gravitational lensing simulator, showing the flow of parameters between modules like ExternalShear, SIE, and FlatLambdaCDM.

As shown in the figure above, caskade handles:

  • Redshift Linking: Ensuring that the redshift parameters for different lens components are linked and consistent.
  • Shared Cosmology: Allowing multiple lens objects (e.g., ExternalShear, SIE) to automatically share a single FlatLambdaCDM cosmology module.

Conclusion

caskade provides a lean, Pythonic framework for building complex scientific simulators. By automating parameter management through a DAG structure, it reduces software entropy and simplifies the integration of models with modern inference and optimization tools. While other tools exist for specific niches like differential equations or HPC distribution, caskade serves as a versatile "wrapper" that encourages modular, testable research code.

Summary updated

BibTeX
@article{stone2025caskade,
  author = {Connor Stone and Alexandre Adam and Adam Coogan and Laurence Perreault-Levasseur and Yashar Hezaveh},
  title = {{caskade: building Pythonic scientific simulators}},
  journal = {The Journal of Open Source Software},
  year = {2025},
  doi = {10.21105/joss.08786},
}
Figure