@@ -1034,3 +1034,54 @@ def mock_pymc_sample():
10341034 if "prior_predictive" in idata :
10351035 del idata ["prior_predictive" ]
10361036 return idata
1037+
1038+
1039+ def mock_sample_setup_and_breakdown ():
1040+ """Set up and tear down mocking of PyMC sampling functions for testing.
1041+
1042+ This function is designed to be used with pytest fixtures to temporarily replace
1043+ PyMC's sampling functionality with faster alternatives for testing purposes.
1044+
1045+ Effects during the fixture's active period:
1046+ * Replaces pm.sample with mock_sample, which uses prior predictive sampling
1047+ instead of MCMC
1048+ * Replaces pm.Flat with pm.Normal to avoid issues with unbounded priors
1049+ * Replaces pm.HalfFlat with pm.HalfNormal to avoid issues with semi-bounded priors
1050+ * Automatically restores all original functions after the test completes
1051+
1052+ Examples
1053+ --------
1054+ .. code-block:: python
1055+
1056+ import pytest
1057+ import pymc as pm
1058+ from pymc.testing import mock_sample_setup_and_breakdown
1059+
1060+ # Register as a pytest fixture
1061+ mock_pymc_sample = pytest.fixture(scope="function")(mock_sample_setup_and_breakdown)
1062+
1063+
1064+ # Use in a test function
1065+ def test_model_inference(mock_pymc_sample):
1066+ with pm.Model() as model:
1067+ x = pm.Normal("x", 0, 1)
1068+ # This will use mock_sample instead of actual MCMC
1069+ idata = pm.sample()
1070+ # Test with the inference data...
1071+
1072+ """
1073+ import pymc as pm
1074+
1075+ original_flat = pm .Flat
1076+ original_half_flat = pm .HalfFlat
1077+ original_sample = pm .sample
1078+
1079+ pm .sample = mock_sample
1080+ pm .Flat = pm .Normal
1081+ pm .HalfFlat = pm .HalfNormal
1082+
1083+ yield
1084+
1085+ pm .sample = original_sample
1086+ pm .Flat = original_flat
1087+ pm .HalfFlat = original_half_flat
0 commit comments