Save and Load
Use save and load when you want to persist a fitted PanelMMM and rebuild it
later without redefining the whole model configuration in code.
Basic round trip
The standard workflow is:
save() writes the model’s InferenceData to NetCDF. load() reads that file,
recreates the PanelMMM configuration from stored metadata, restores
loaded.idata, and rebuilds the PyMC graph from the saved training data.
What Abacus stores
Abacus relies on more than the posterior draws for a full round trip.
| Stored item | Why it matters |
|---|---|
posterior and other InferenceData groups |
Preserve sampled results |
fit_data |
Rebuild the model graph with the original training data |
idata.attrs |
Reconstruct PanelMMM init kwargs and validate compatibility |
The stored attrs include both the shared model metadata and PanelMMM-specific
configuration such as:
date_columnchannel_columnstarget_columntarget_typedimscontrol_columnscontrol_impactsadstockandsaturationadstock_firstyearly_seasonalitytime_varying_interceptandtime_varying_mediascalingmodel_configsampler_config- serialised
mu_effects
save() behaviour
save(fname, **kwargs) is a thin wrapper over self.idata.to_netcdf(...).
Important constraints:
- the model must already be fitted
self.idatamust contain aposteriorgroup- any extra kwargs are passed directly to
InferenceData.to_netcdf(...)
If you call save() before fitting, Abacus raises:
load() and compatibility checks
By default, PanelMMM.load(...) validates that the saved file matches the
current model class and configuration:
With check=True, Abacus verifies:
- the saved model version
- the saved model id derived from the serialised configuration
If those checks fail, Abacus raises DifferentModelError.
If you need to bypass those checks, you can set check=False:
Use that only when you understand why the saved metadata does not match.
Load from an in-memory InferenceData
If you already have an InferenceData object, use load_from_idata(...)
instead of saving to disk first:
This is the same round-trip path that load() uses internally after reading
the NetCDF file.
Where build_from_idata() fits
build_from_idata(idata) is the lower-level rebuild step. It:
- restores supported serialised
mu_effects - reads
idata.fit_data - splits that saved training data back into
Xandy - rebuilds the PyMC graph
You usually do not need to call build_from_idata() yourself because
load() and load_from_idata() already do it.
Round-trip limitations
Not every fitted object can be restored fully.
EventAdditiveEffect does not round-trip
Abacus does not deserialize EventAdditiveEffect because the original
df_events DataFrame is not stored in the saved attrs. In that case,
PanelMMM.load(...) fails fast while rebuilding the model.
Do not drop fit_data if you want to reload
Because rebuild uses idata.fit_data, do not save a partial file that omits
that group if you want to call PanelMMM.load(...) later.
For example, this is valid NetCDF output:
But it is not a full PanelMMM round-trip artefact, because the saved file no
longer includes the training data needed for build_from_idata(...).
Practical advice
- Use the default
save()behaviour for round trips. - Keep
check=Trueunless you have a specific compatibility reason not to. - Prefer
PanelMMM.load(...)over loading NetCDF manually. - Refit or rebuild event effects explicitly rather than expecting saved event state to deserialize.
Next steps
After loading a model, you can go straight to posterior predictive sampling,
diagnostics, decomposition, or optimisation using the restored idata and
rebuilt graph.