Caching Architecture
1 Introduction
In simulation-based NDT reliability assessment, generating a single Probability of Detection (PoD) curve requires computationally expensive operations: Cross-Validation (CV), Gaussian Kernel Smoothing (bandwidth optimization), and Monte Carlo Integration.
To provide a modern, real-time user interface (UI) where users can instantly drag sliders to update threshold limits and slice multidimensional surfaces, digiqual implements a 4-Layer Caching Architecture within the SimulationStudy core class.
This architecture ensures that heavy math is only executed once. Subsequent UI interactions pull pre-calculated matrices directly from memory in microseconds.
2 The 4 Caching Layers
The state of the caches is managed inside core.py. Whenever the physical dataset changes (e.g., a user uploads a new CSV), all caches are instantly wiped to prevent state mismatch.
2.1 Layer 1: The Mean Model Cache (models_cache)
What it does: Caches the fundamental physics regressions. Trigger: Pressing Run models in Model Diagnostics (study.fit_model()), or the first study.pod() call.
Instead of only fitting the single “best” model, the engine evaluates all polynomial degrees (1-10) and the Kriging (Gaussian Process) model via cross-validation, and then fits every one of them to the full dataset. For Kriging, several candidate kernels are first fitted and scored by leave-one-out error, and only the best is cross-validated (details); Kriging is skipped above 1000 runs, because each fit is \(O(N^3)\). The CV scores (cv_scores_cache), their standard errors (cv_se_) and the automatic choice made by the one-standard-error rule (cv_winner_key) are cached with them (how the choice is made).
- Cache Key:
('Polynomial', 3)or('Kriging', None) - Stored Data: The fitted
scikit-learnpipeline. - Benefit: If a user looks at the CV bar chart and switches the Model dropdown from “Auto” (say Polynomial Degree 3) to Degree 4, the app retrieves the pre-trained Degree 4 model instantly without running regression again.
2.2 Layer 2: The Variance Model Cache (variance_cache)
What it does: Caches the heteroscedastic noise profile and parameter sensitivity results. Trigger: study.fit_model() for a specific mean model: pressing Run models (the automatic choice), choosing another model in Model Diagnostics, or any pod() call, which calls fit_model() itself.
In the app, a model change is therefore a Layer-1 hit but can be a Layer-2 miss. It runs in the background with a Fitting the chosen model progress panel, and is quick if that model was used before. fit_model() stores its result in study.model_results.
Once a mean model is selected, the engine must estimate how the noise (variance) changes across the surface. It uses Leave-One-Out Cross-Validation (LOO-CV) to find the optimal smoothing bandwidth for the residuals, and tests candidate statistical distributions to find the best fit. In addition, the engine computes the Total-Order Sobol Sensitivity Indices to quantify parameter importance.
- Cache Key: the
variance_key: the Mean Model key plus the bandwidth setting (e.g.,(('Polynomial', 3), None), whereNonemeans the bandwidth was chosen automatically). Layers 3 and 4 build on this key, so changingbandwidth_rationever reuses a stale variance model. - Stored Data:
residuals,bandwidth,dist_info(name and parameters), andsobol_indices. - Benefit: Bandwidth optimization and Sobol sensitivity computations are slow. By caching them, the system never has to recalculate the noise profile or parameter sensitivities for a model it has already analyzed.
2.3 Layer 3: Individual PoD Curve Cache (pod_curves_cache)
What it does: Caches a specific, integrated S-Curve. Trigger: Evaluating a specific slice of the parameter space.
This layer stores the output of the integration.py engine. It locks in the specific grid coordinates (X_eval) and either performs heavy Monte Carlo integration (if there are active nuisance parameters) or fast vectorization (if all other variables are locked as constant slices).
- Cache Key:
(variance_key, threshold, poi_cols, nuisance_cols, slice_values, nuisance_dists, nuisance_bounds) - Stored Data: The final
pod_curve(1D array),mean_curve, and the mathematical grids used. - Benefit: If a user navigates away from a specific slice and then comes back to it, the fully integrated curve is loaded instantly.
2.4 Layer 4: Threshold Spectrum Cache (threshold_spectrum_cache)
What it does: The “Crown Jewel” that enables real-time threshold scrubbing. Trigger: Pressing Update PoD in the Model Explorer (study.compute_pod_spectrum()), and after each slice change when nothing is integrated.
The integration.py engine is fully vectorized to handle multiple thresholds simultaneously. Instead of calculating one PoD curve, Layer 4 calculates 100 PoD curves across the entire minimum-to-maximum range of the signal data in a single array operation.
- Cache Key: strictly excludes the threshold:
(variance_key, poi_cols, nuisance_cols, slice_values, nuisance_dists, nuisance_bounds) - Stored Data: A massive 2D matrix (
pod_matrix) where rows are X-axis grid points and columns are the 100 different thresholds. - Benefit: When the user drags the “Detection Threshold” slider in the Model Explorer,
pod()performs a 1D linear interpolation across the pre-calculatedpod_matrixand returns the shifted S-Curve in less than a millisecond. - Limits: The spectrum covers the observed signal range only. For a threshold outside it,
pod()ignores the spectrum and computes the PoD directly (Layer 3), since interpolation would just repeat the end values. The spectrum is also never used whenn_boot > 0.
compute_pod_spectrum() builds the spectrum by calling pod() at the median signal, but it keeps any existing study.pod_results and study.model_results: building the cache doesn’t replace the results you are looking at.
3 Example: A User’s Journey Through the Caches
Let’s trace exactly what happens in the engine when a user interacts with the application.
3.1 Step 1: The Initial Fit
The user uploads 500 simulations of an ultrasonic inspection with Length, Angle, and Roughness. They press Run models in Model Diagnostics, which fits the candidate models. Then, in the Model Explorer, they plot Length, slice Angle and Roughness at their medians and press Update PoD.
- Layer 1 Miss (Run models): The engine trains 10 polynomials and 1 Kriging model. It caches all 11 models. It selects “Polynomial (Degree 2)” as the winner.
- Layer 2 Miss: It optimizes the bandwidth and fits a Normal distribution for Poly 2. It caches the variance parameters.
- Layer 3 Miss (Update PoD): It evaluates the curve using the median threshold (e.g., 15.0 dB) and median slices for Angle and Roughness. It caches this base curve.
- Layer 4 Miss (Update PoD): The engine pulls
X_trainand thebandwidthfrom Layer 2, generates a vector of 100 thresholds (from 0 dB to 50 dB), and runs the fast-path vectorized math. The 100-curve matrix is cached.
Total Compute Time: ~2.5 seconds.
3.2 Step 2: The Real-Time Slider
The user rapidly drags the Detection Threshold slider from 15.0 dB to 22.5 dB (inside the observed signal range).
- The
live_updateeffect fires indigiqual/gui/stage_explorer.py. - It calls
study.pod(threshold=22.5, n_boot=0). - Layer 1 Hit: Poly 2 loaded instantly.
- Layer 2 Hit: Bandwidth loaded instantly.
- Layer 4 Hit: The engine sees that
Lengthat the median slices already has a Threshold Spectrum matrix. - It bypasses
integration.pycompletely. It interpolates the matrix at exactly22.5 dBand returns the S-curve.
Total Compute Time: ~0.001 seconds.
3.3 Step 3: Changing a Slice
The user moves the Angle Slice Slider from 0 degrees to 45 degrees.
- The same
live_updateeffect fires. - Layer 1 & 2 Hit: Poly 2 physics loaded instantly.
- Layer 4 Miss: The cache key requires an exact match on
slice_values. Because Angle changed, the old matrix doesn’t apply. - The engine falls back to
integration.py. BecauseAngleis a constant slice (no Monte Carlo required), it takes the Fast Path. - It computes the new 100-threshold spectrum for Angle=45 instantly, saves it to Layer 4, and returns the curve for the current threshold.
Total Compute Time: ~0.05 seconds.
3.4 Step 4: Uncertainty Quantification (Bypassing the Cache)
The user is happy with a threshold of 22.5 dB and clicks Run Bootstrap in the PoD Analysis stage.
- With one plotted variable, the bootstrap task first calls
study.compute_pod_spectrum()for the PoD-vs-threshold plot. This is a Layer 4 hit unless the slice has moved since Update PoD. - It then calls
study.pod(threshold=22.5, n_boot=1000, n_jobs=...), withn_jobs=-1(all cores but two) when Parallel compute is ticked, and1otherwise. - The engine recognizes
n_boot > 0and completely bypasses the Layer 4 slider approximation. - It calls
bootstrap_pod_ci(), using the chosen CPU cores to resample the data 1,000 times, refitting the models and running strict Layer 3 math on every iteration to build robust Confidence Bounds for a range of levels (50%, 90%, 95%, 99%) simultaneously. These bounds are stored in aci_boundsdictionary and a fullreliability_tablematrix.
Total Compute Time: ~6.0 seconds.
4 Conclusion
By cleanly separating the Physics (Layer 1 & 2), the Exploration (Layer 3 & 4), and the Rigorous Confidence Bounds (Bootstrap Bypass), digiqual achieves the interactivity of a dashboard while preserving the mathematical integrity required for safety-critical statistical analysis.