1. Setup and run control
Hardware, precision, which steps to compute, and where results go
Before any calculation, a script decides where it runs (CPU or which GPUs), in which precision, which steps to compute and which to read from earlier runs, and which plots to make. These choices are switches: constants in capitals at the top of each example script.
The JAX environment
JAX is initialized once, when the script starts, by initialize_jax (in nlsa_models.core, also available as clim.initialize_jax):
jax_env = clim.initialize_jax(idx_gpu=IDX_GPU, xla_mem_fraction=XLA_MEM_FRACTION, fp=FP)| Argument | Type or values | Default | Description |
|---|---|---|---|
idx_cpu |
int or None |
None |
Which CPU device to use; None takes the first. |
idx_gpu |
int, list of int, or None |
None |
GPU to use, or a list for several GPUs; None (or an empty list) runs on the CPU. |
xla_mem_fraction |
str or None |
None |
Fraction of each GPU’s memory that JAX reserves at start-up, as a string such as "0.95" (sets the environment variable XLA_PYTHON_CLIENT_MEM_FRACTION). None keeps JAX’s default. |
fp |
"f32" or "f64" |
"f32" |
Precision: single (float32, complex64) or double (float64, complex128). Double precision switches on JAX’s 64-bit mode. |
cache_dir |
str, Path or None |
None |
Folder for JAX’s persistent compilation cache, so that later runs skip recompiling. A relative path is taken from the current folder. |
It returns a JaxEnv object with the chosen devices (devices), the CPU device (device_cpu), the real and complex data types (real_dtype, complex_dtype) used throughout the script, and the cache folder. jax_env.tabulate() prints them at the start of a run.
Single precision, which the examples use, needs half the memory of double precision and is faster, by a large factor on GPUs built for graphics, which have little double-precision hardware. Double precision is worth a try when results change noticeably with the precision, for example with very many eigenfunctions or very small kernel eigenvalues.
clim.initialize_matplotlib(backend=MATPLOTLIB_BACKEND) chooses how plots are drawn; set MATPLOTLIB_BACKEND = "Agg" on a server without a display.
Script switches
These are the switches of the two ENSO scripts, with their values in the repository. The run-mode switches are listed with the steps they control below. The Lorenz 63 scripts have the same kind of switches, with run modes named after their own steps.
EXPERIMENT chooses the configuration to run. It is a member of the script’s Experiment enumeration, from which from_experiment() builds the parameters of the run (together with CONE_KERNEL and KERNEL_NORMALIZATION); see Experiments. The kernel analog forecasting script runs ELNINO_2027_FROM_ERA5_NINO34SST, the Koopman script ENSO_FROM_ERA5_IPSST.
| Switch | Type or values | Value in the scripts | Description |
|---|---|---|---|
IDX_GPU |
int or Sequence[int] or None |
KAF: 0Koopman: None |
GPU to use: an index, a list of indices for several GPUs, or None to run on the CPU. |
XLA_MEM_FRACTION |
str or None |
"0.95" |
Fraction of GPU memory that JAX may reserve at start-up (passed to initialize_jax). |
JAX_CACHE_DIR |
str or None |
"jax_cache" |
Folder for JAX’s compilation cache (defined in the scripts but not passed to initialize_jax at present). |
FP |
"f32" or "f64" |
"f32" |
Floating-point precision: "f32" (single, faster) or "f64" (double). |
| Switch | Type or values | Value in the scripts | Description |
|---|---|---|---|
CONE_KERNEL |
bool |
KAF: TrueKoopman: False |
Use the cone kernel. It also switches on the velocity covariates in the experiment settings. |
KERNEL_TUNING_GRAD_METHOD |
"explicit" or "automatic" |
KAF: "automatic"Koopman: "explicit" |
How the tuning curve is differentiated: "explicit" (a closed-form gradient for the exponential shape function) or "automatic" (JAX automatic differentiation). |
KERNEL_NORMALIZATION |
"diffusion_maps" or "bistochastic" |
"diffusion_maps" |
"diffusion_maps" (uses DmKernelPars) or "bistochastic" (uses BsKernelPars). |
| Switch | Value in the scripts | Description |
|---|---|---|
OUTPUT_DATA_DIR |
"examples/enso/data" |
Folder, relative to where the script is run, under which results are saved and read. |
ERA5_DAILY_DATA_DIR |
Koopman script: "/storage/data/era5/daily01" |
Folder with the daily ERA5 files. Defined in the Koopman script but not used by its current experiments, which are monthly. |
ERA5_MONTHLY_DATA_DIR |
Koopman script: "/storage/data/era5/month_nc_1940_2025" |
Folder with the monthly ERA5 files. |
| Switch | Type or values | Value in the scripts | Description |
|---|---|---|---|
PLOT_MODE |
"save" or "show" or "saveshow" or None |
"show" |
"show" (open windows), "save" (write PNG files), "saveshow" (both) or None (no plots). |
WHICH_PLOTS |
set of plot names | KAF: {"running_pred", "pred_timeseries", "skill_scores"}Koopman: {"koopman_modes"} |
Set of plots to make (see the plot list on the Skill and outputs page); {"all"} makes every plot. |
MATPLOTLIB_BACKEND |
"Agg" or None |
None |
Matplotlib backend; set "Agg" on a server without a display. |
DELAY_PLOT_MODE |
"backward" or "central" |
"central" |
Time stamp given to a delay window in plots: its centre ("central") or its last sample ("backward"). |
PLT_DATE_RANGE |
tuple[str, str] or None |
None |
Restrict time-series plots to this date range; None plots the whole period. |
KERNEL_EIGS_PLT |
Sequence[int] or "interactive" or None |
"interactive" |
Kernel eigenfunctions to plot: a list of indices, "interactive" (ask at the prompt) or None. |
KERNEL_MODES_PLT |
Sequence[int] or "interactive" or None |
"interactive" |
Kernel modes (spatial patterns) to plot, as above. |
KOOPMAN_EIGS_PLT |
Sequence[int] or "interactive" or None |
Koopman script: "interactive" |
Koopman eigenfunctions to plot, as above. |
KOOPMAN_MODES_PLT |
Sequence[int] or "interactive" or None |
Koopman script: "interactive" |
Koopman modes (spatial patterns) to plot, as above. |
LEAD_TIMES_PLT |
Sequence[int] or "interactive" or None |
"interactive" |
Lead times for the running-forecast plot, as above. |
INITIALIZATION_TIMES_PLT |
Sequence[int] or "interactive" or None |
"interactive" |
Initialization times for the forecast time-series plot, as above. |
NUM_TABULATE |
int |
40 |
Number of eigenvalues printed in the summary tables. |
Run modes and saved results
Each step of a script has a run-mode switch with three values:
"calc": compute the result, keep it in memory only."calcsave": compute the result and save it."read": read the result saved by an earlier"calcsave"run.
The run mode is applied by wrapping the function that does the step. For example, in the kernel analog forecasting script:
compute_kernel_eigen = timeit(
pickleit(
knl.compute_eigen, # the computation
io=io, # where to save or read
mode=KERNEL_EIGEN_MODE, # "calc", "calcsave" or "read"
fname="kernel_eigen", # file name: kernel_eigen.pkl
cls=KernelEigen, # type checked when reading
)
)compute_kernel_eigen is then called with exactly the arguments of knl.compute_eigen. In "read" mode the arguments are ignored and the file is loaded instead. timeit prints how long each step took.
| Step | Run-mode switch | Wrapper | File |
|---|---|---|---|
| Data | EXTRACT_DATA_MODE |
pickleit |
data.pkl |
| Tuning of the bandwidth function and of the kernel | TUNE_KERNEL_MODE |
pickleit |
tune_info.pkl (one in each folder) |
| Kernel eigenfunctions | KERNEL_EIGEN_MODE |
pickleit |
kernel_eigen.pkl |
| Projection onto kernel eigenfunctions | KERNEL_PROJECTION_MODE |
netcdfit |
kernel_eigen_proj.nc |
| Generator matrix | GENERATOR_MATRIX_MODE |
npyit |
gen_mat.npy |
| Koopman eigendecomposition | KOOPMAN_EIGEN_MODE |
pickleit |
generator_eigen_diff.pkl |
| Values of the forecast Koopman eigenfunction | KOOPMAN_EIG_EVAL_MODE |
pickleit |
koop_eig.pkl |
| Projection onto Koopman eigenfunctions | KOOPMAN_PROJECTION_MODE |
netcdfit |
koopman_eigen_proj.nc |
| KAF expansion coefficients | KAF_EXPANSION_COEFFS_MODE |
pickleit |
kaf_coeffs.pkl |
| Forecasts | KAF_PREDS_MODE |
pickleit |
kaf_preds.pkl |
| Skill scores | SKILL_SCORES_MODE |
h5it |
pred_scores.h5 |
| Plots | PLOT_MODE |
plotit, plotem |
<name>.png, or <name>_<number>.png for a series of plots |
The wrappers are in nlsa.io_actions: pickleit (Python pickle files), h5it (HDF5), npyit (NumPy .npy), netcdfit (NetCDF, for xarray data sets) and csvit (CSV tables); plotit and plotem save or show one figure or a numbered series of figures.
Folder structure
Results are saved under OUTPUT_DATA_DIR (by default examples/enso/data, relative to the folder the script is started from), in a tree of folders whose names are the text forms of the parameter objects. Before each step, main() extends the current path with the settings of that step:
io @= str(pars.train.data.covariate) # start a new path from the root
io /= str(pars.train.data) # go one level deeper
io -= 2 # go back to the folder two steps earlierFor the Niño 3.4 kernel analog forecast, for example, the forecasts end up in
examples/enso/data/
└── sst_190-240-4_-5-5-4_1940-01-01-2019-12-31_monthly_clim1940-01-01-2019-12-31_std predictors
└── ns920_nd12_nb0_na24_vfd4 samples, delays, extra samples, velocities
└── nb128_loglim-3_loglim3_dimNone_scl1 tuning of the bandwidth function
└── zeta0.9900_thresh1e-12 cone kernel
└── nb128_loglim-3_loglim3_dimNone_scl2 kernel tuning
└── fokkerplanck_eigh_neigs512 kernel eigenfunctions
└── sst_190-240_-5-5_…_anom target
└── pred24_eigs0-512 forecast settings
└── ns56_nd12_nb0_na24_vfd4 test samples
├── kaf_preds.pkl
└── pred_scores.h5
Two consequences follow:
- Runs with different settings never overwrite each other, and a
"read"step finds its file only if all the settings that lead to it are unchanged. - Old results are never deleted. Remove folders you no longer need by hand.
Plots
PLOT_MODE sets what happens to plots: "show" opens a window and waits for Enter, "save" writes PNG files next to the results, "saveshow" does both, and None skips plotting. WHICH_PLOTS selects the plots (listed on the Skill and outputs page). For plots that come as a series (eigenfunctions, modes, lead times, initial times), the *_PLT switches give the numbers to plot, or "interactive" to type them in at the prompt.
Several GPUs
With more than one GPU in IDX_GPU, from_experiment() builds a sharding layout: the training samples are split across the GPUs along the sample dimension, while small arrays such as eigenvalues are copied to every GPU. The layout is held in TrainShardings and TestShardings (defined in each script), which contain the toolkit’s L2FnAlgebraShardings, KernelEigenShardings and, in the Koopman script, GeneratorShardings and KoopmanEigenShardings. Nothing needs to be set for one GPU or a CPU.