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 to az.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 the fit patches: "netcdf" writes idata.nc, "zarr" writes the directory idata.zarr. Default is "netcdf".

  • disable (bool) – Whether to disable autologging. Default is False.

  • silent (bool) – Whether to suppress all warnings. Default is False.

Raises:

Notes

idata_format="zarr" requires the zarr extra: 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 (T and t) 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: or https: artifact URIs) needs mlflow>=3.18.0 on the reading side. Older releases silently skip chunks such as c/0/0 on download (mlflow/mlflow#27189), so the loaded inference data holds NaN in their place. load_mmm() and load_mmm_from_run() warn about this; other downloads, such as mlflow.artifacts.download_artifacts, do not. Logging is not affected.

Calling autolog again replaces its earlier patches instead of stacking them, so the last call decides what is logged, and autolog(disable=True) switches the patches off until the next call. A wrapper that other code installs on top of an autolog patch is left in place together with the patch beneath it, so a later autolog call 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, including disable=True; with idata_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)