Skip to content

Use mock sample in tests #489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 5, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Setup environment
run: pip install -e .[test]
- name: Run doctests
run: pytest --doctest-modules --ignore=causalpy/tests/ causalpy/
run: pytest --doctest-modules --ignore=causalpy/tests/ causalpy/ --config-file=causalpy/tests/conftest.py
- name: Run extra tests
run: pytest docs/source/.codespell/test_notebook_to_markdown.py
- name: Run tests
Expand Down
2 changes: 1 addition & 1 deletion causalpy/experiments/prepostnegd.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class PrePostNEGD(BaseExperiment):
... }
... ),
... )
>>> result.summary(round_to=1) # doctest: +NUMBER
>>> result.summary(round_to=1) # doctest: +SKIP
==================Pretest/posttest Nonequivalent Group Design===================
Formula: post ~ 1 + C(group) + pre
<BLANKLINE>
Expand Down
14 changes: 14 additions & 0 deletions causalpy/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,24 @@

import numpy as np
import pytest
from pymc.testing import mock_sample, mock_sample_setup_and_teardown


@pytest.fixture(scope="session")
def rng() -> np.random.Generator:
"""Random number generator that can persist through a pytest session"""
seed: int = sum(map(ord, "causalpy"))
return np.random.default_rng(seed=seed)


mock_pymc_sample = pytest.fixture(mock_sample_setup_and_teardown, scope="session")


@pytest.fixture(autouse=True)
def mock_sample_for_doctest(request):
if not request.config.getoption("--doctest-modules", default=False):
return

import pymc as pm

Check warning on line 41 in causalpy/tests/conftest.py

View check run for this annotation

Codecov / codecov/patch

causalpy/tests/conftest.py#L41

Added line #L41 was not covered by tests

pm.sample = mock_sample

Check warning on line 43 in causalpy/tests/conftest.py

View check run for this annotation

Codecov / codecov/patch

causalpy/tests/conftest.py#L43

Added line #L43 was not covered by tests
32 changes: 16 additions & 16 deletions causalpy/tests/test_integration_pymc_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@


@pytest.mark.integration
def test_did():
def test_did(mock_pymc_sample):
"""
Test Difference in Differences (DID) PyMC experiment.

Expand Down Expand Up @@ -57,7 +57,7 @@ def test_did():


@pytest.mark.integration
def test_did_banks_simple():
def test_did_banks_simple(mock_pymc_sample):
"""
Test simple Differences In Differences Experiment on the 'banks' data set.

Expand Down Expand Up @@ -113,7 +113,7 @@ def test_did_banks_simple():


@pytest.mark.integration
def test_did_banks_multi():
def test_did_banks_multi(mock_pymc_sample):
"""
Test multiple regression Differences In Differences Experiment on the 'banks'
data set.
Expand Down Expand Up @@ -168,7 +168,7 @@ def test_did_banks_multi():


@pytest.mark.integration
def test_rd():
def test_rd(mock_pymc_sample):
"""
Test Regression Discontinuity experiment.

Expand Down Expand Up @@ -199,7 +199,7 @@ def test_rd():


@pytest.mark.integration
def test_rd_bandwidth():
def test_rd_bandwidth(mock_pymc_sample):
"""
Test Regression Discontinuity experiment with bandwidth parameter.

Expand Down Expand Up @@ -229,7 +229,7 @@ def test_rd_bandwidth():


@pytest.mark.integration
def test_rd_drinking():
def test_rd_drinking(mock_pymc_sample):
"""
Test Regression Discontinuity experiment on drinking age data.

Expand Down Expand Up @@ -289,7 +289,7 @@ def reg_kink_function(x, beta, kink):


@pytest.mark.integration
def test_rkink():
def test_rkink(mock_pymc_sample):
"""
Test Regression Kink design.

Expand Down Expand Up @@ -320,7 +320,7 @@ def test_rkink():


@pytest.mark.integration
def test_rkink_bandwidth():
def test_rkink_bandwidth(mock_pymc_sample):
"""
Test Regression Kink experiment with bandwidth parameter.

Expand Down Expand Up @@ -350,7 +350,7 @@ def test_rkink_bandwidth():


@pytest.mark.integration
def test_its():
def test_its(mock_pymc_sample):
"""
Test Interrupted Time-Series experiment.

Expand Down Expand Up @@ -403,7 +403,7 @@ def test_its():


@pytest.mark.integration
def test_its_covid():
def test_its_covid(mock_pymc_sample):
"""
Test Interrupted Time-Series experiment on COVID data.

Expand Down Expand Up @@ -457,7 +457,7 @@ def test_its_covid():


@pytest.mark.integration
def test_sc():
def test_sc(mock_pymc_sample):
"""
Test Synthetic Control experiment.

Expand Down Expand Up @@ -516,7 +516,7 @@ def test_sc():


@pytest.mark.integration
def test_sc_brexit():
def test_sc_brexit(mock_pymc_sample):
"""
Test Synthetic Control experiment on Brexit data.

Expand Down Expand Up @@ -579,7 +579,7 @@ def test_sc_brexit():


@pytest.mark.integration
def test_ancova():
def test_ancova(mock_pymc_sample):
"""
Test Pre-PostNEGD experiment on anova1 data.

Expand Down Expand Up @@ -611,7 +611,7 @@ def test_ancova():


@pytest.mark.integration
def test_geolift1():
def test_geolift1(mock_pymc_sample):
"""
Test Synthetic Control experiment on geo lift data.

Expand Down Expand Up @@ -648,7 +648,7 @@ def test_geolift1():


@pytest.mark.integration
def test_iv_reg():
def test_iv_reg(mock_pymc_sample):
df = cp.load_data("risk")
instruments_formula = "risk ~ 1 + logmort0"
formula = "loggdp ~ 1 + risk"
Expand Down Expand Up @@ -676,7 +676,7 @@ def test_iv_reg():


@pytest.mark.integration
def test_inverse_prop():
def test_inverse_prop(mock_pymc_sample):
"""Test the InversePropensityWeighting class."""
df = cp.load_data("nhefs")
sample_kwargs = {
Expand Down
6 changes: 3 additions & 3 deletions causalpy/tests/test_pymc_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def test_fit_build_not_implemented(self):
argvalues=[None, {"a": 1}],
ids=["None-coords", "dict-coords"],
)
def test_fit_predict(self, coords, rng) -> None:
def test_fit_predict(self, coords, rng, mock_pymc_sample) -> None:
"""
Test fit and predict methods on MyToyModel.

Expand Down Expand Up @@ -122,7 +122,7 @@ def test_fit_predict(self, coords, rng) -> None:
assert isinstance(predictions, az.InferenceData)


def test_idata_property():
def test_idata_property(mock_pymc_sample):
"""Test that we can access the idata property of the model"""
df = cp.load_data("did")
result = cp.DifferenceInDifferences(
Expand All @@ -140,7 +140,7 @@ def test_idata_property():


@pytest.mark.parametrize("seed", seeds)
def test_result_reproducibility(seed):
def test_result_reproducibility(seed, mock_pymc_sample):
"""Test that we can reproduce the results from the model. We could in theory test
this with all the model and experiment types, but what is being targeted is
the PyMCModel.fit method, so we should be safe testing with just one model. Here
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ addopts = [
"--strict-config",
"--cov=causalpy",
"--cov-report=term-missing",
"--doctest-modules",
]
testpaths = "causalpy/tests"
markers = [
Expand Down