autolog#
- pymc_marketing.mlflow.autolog(log_sampler_info=True, log_metadata_info=True, log_model_info=True, sample_error_file='sample-error.txt', summary_var_names=None, arviz_summary_kwargs=None, log_mmm=True, log_clv=True, log_bass=True, idata_format=IdataFormat.NETCDF, disable=False, silent=False)[source]#
Autologging support for PyMC models and PyMC-Marketing models.
Includes logging of sampler diagnostics, model information, data used in the model, and DataTree objects upon sampling the models.
For more information about MLflow, see https://mlflow.org/docs/latest/python_api/mlflow.html
- Parameters:
log_sampler_info (
bool) – Whether to log sampler diagnostics. Default is True.log_metadata_info (
bool) – Whether to log the metadata of inputs used in the model. Default is True.log_model_info (
bool) – Whether to log model information. Default is True.sample_error_file (
str|None) – The name of the file to log the error if an error occurs during sampling. If None, the error will not be logged. Default is “sample-error.txt”.summary_var_names (
list[str] |None) – The names of the variables to include in the ArviZ summary. Default is all the variables in the DataTree object.arviz_summary_kwargs (
dict|None) – Additional keyword arguments to pass toaz.summary.log_mmm (
bool) – Whether to log PyMC-Marketing MMM models. Default is True.log_clv (
bool) – Whether to log PyMC-Marketing CLV models. Default is True.log_bass (
bool) – Whether to log PyMC-Marketing Bass models. Default is True.idata_format (
IdataFormat|str) – Format of the inference-data artifact logged by thefitpatches:"netcdf"writesidata.nc,"zarr"writes the directoryidata.zarr. Default is"netcdf".disable (
bool) – Whether to disable autologging. Default is False.silent (
bool) – Whether to suppress all warnings. Default is False.
- Raises:
ValueError – If
idata_formatis not"netcdf"or"zarr".ImportError – If
idata_format="zarr"andzarris missing or xarray is older than the floor of thezarrextra.RuntimeError – If
idata_format="zarr"and the installed xarray, zarr and pandas cannot write a Zarr store; seepymc_marketing.data.idata.io.check_zarr_writer().
Notes
idata_format="zarr"requires thezarrextra:pip install 'pymc-marketing[zarr]'. A Zarr store is a directory with several files per variable, so remote artifact stores pay per file on upload and download, whereas netCDF is a single file. Names that differ only by letter case (Tandt) cannot share a Zarr directory store on a case-insensitive filesystem (macOS and Windows defaults). zarr-python 3 writes string coordinates with an encoding that is not yet in the Zarr specification, so stores with string coordinates may need re-encoding for a later zarr-python release.Reading a Zarr store back through a proxied artifact server (
mlflow-artifacts:,http:orhttps:artifact URIs) needsmlflow>=3.18.0on the reading side. Older releases silently skip chunks such asc/0/0on download (mlflow/mlflow#27189), so the loaded inference data holds NaN in their place.load_mmm()andload_mmm_from_run()warn about this; other downloads, such asmlflow.artifacts.download_artifacts, do not. Logging is not affected.Calling
autologagain replaces its earlier patches instead of stacking them, so the last call decides what is logged, andautolog(disable=True)switches the patches off until the next call. A wrapper that other code installs on top of anautologpatch is left in place together with the patch beneath it, so a laterautologcall adds a second patch above it and each fit is logged twice. The argument checks run before mlflow records the call, so a rejected call leaves the previous state in place, includingdisable=True; withidata_format="zarr"they write a tiny probe store to the temporary directory.Examples
Autologging for a PyMC model:
import mlflow import pymc as pm import pymc_marketing.mlflow pymc_marketing.mlflow.autolog() # Usual PyMC model code with pm.Model() as model: mu = pm.Normal("mu", mu=0, sigma=1) obs = pm.Normal("obs", mu=mu, sigma=1, observed=[1, 2, 3]) # Incorporate into MLflow workflow mlflow.set_experiment("PyMC Experiment") with mlflow.start_run(): idata = pm.sample(model=model)
Autologging for a PyMC-Marketing MMM:
import pandas as pd import mlflow from pymc_marketing.mmm import ( GeometricAdstock, LogisticSaturation, MMM, ) from pymc_marketing.paths import data_dir import pymc_marketing.mlflow pymc_marketing.mlflow.autolog(log_mmm=True) # Usual PyMC-Marketing model code file_path = data_dir / "mmm_example.csv" data = pd.read_csv(file_path, parse_dates=["date_week"]) X = data.drop("y", axis=1) y = data["y"] mmm = MMM( adstock=GeometricAdstock(l_max=8), saturation=LogisticSaturation(), date_column="date_week", channel_columns=["x1", "x2"], control_columns=[ "event_1", "event_2", "t", ], yearly_seasonality=2, ) # Incorporate into MLflow workflow mlflow.set_experiment("MMM Experiment") with mlflow.start_run(): idata = mmm.fit(X, y) posterior_preds = mmm.sample_posterior_predictive(X) # Additional specific logging fig, _ = mmm.plot.contributions_over_time(var=["channel_contribution"]) mlflow.log_figure(fig, "components.png")
Autologging for a PyMC-Marketing CLV model:
import pandas as pd import mlflow from pymc_marketing.clv import BetaGeoModel from pymc_marketing.paths import data_dir import pymc_marketing.mlflow pymc_marketing.mlflow.autolog(log_clv=True) mlflow.set_experiment("CLV Experiment") file_path = data_dir / "clv_quickstart.csv" data = pd.read_csv(file_path) data["customer_id"] = data.index model = BetaGeoModel() with mlflow.start_run(): model.fit(data=data) with mlflow.start_run(): model.fit(data=data, method="map")
Autologging for a PyMC-Marketing Bass model:
import numpy as np import mlflow from pymc_extras.prior import Prior from pymc_marketing.bass import BassModel import pymc_marketing.mlflow pymc_marketing.mlflow.autolog(log_bass=True) mlflow.set_experiment("Bass Experiment") adoption = np.array([10, 25, 50, 80, 100, 90, 60, 35, 20, 10]) # Positive prior on the market size m keeps the Poisson rate valid model_config = { "m": Prior("Normal", mu=500, sigma=100), } model = BassModel(model_config=model_config) with mlflow.start_run(): idata = model.fit(data=adoption)