-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from pymc-labs/add_basic_tests
- Loading branch information
Showing
5 changed files
with
102 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
[settings] | ||
known_third_party = arviz,matplotlib,numpy,pandas,patsy,pymc,scipy,seaborn,setuptools,sklearn,statsmodels,xarray | ||
known_third_party = arviz,matplotlib,numpy,pandas,patsy,pymc,pytest,scipy,seaborn,setuptools,sklearn,statsmodels,xarray |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def rng() -> np.random.Generator: | ||
seed: int = sum(map(ord, "causalpy")) | ||
return np.random.default_rng(seed=seed) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import arviz as az | ||
import numpy as np | ||
import pandas as pd | ||
import pymc as pm | ||
import pytest | ||
|
||
from causalpy.pymc_models import ModelBuilder | ||
|
||
|
||
class MyToyModel(ModelBuilder): | ||
def build_model(self, X, y, coords): | ||
with self: | ||
X_ = pm.MutableData(name="X", value=X) | ||
y_ = pm.MutableData(name="y", value=y) | ||
beta = pm.Normal("beta", mu=0, sigma=1, shape=X_.shape[1]) | ||
sigma = pm.HalfNormal("sigma", sigma=1) | ||
mu = pm.Deterministic("mu", pm.math.dot(X_, beta)) | ||
pm.Normal("y_hat", mu=mu, sigma=sigma, observed=y_) | ||
|
||
|
||
class TestModelBuilder: | ||
def test_init(self): | ||
mb = ModelBuilder() | ||
assert mb.idata is None | ||
assert mb.sample_kwargs == {} | ||
|
||
@pytest.mark.parametrize( | ||
argnames="coords", argvalues=[{"a": 1}, None], ids=["coords-dict", "coord-None"] | ||
) | ||
@pytest.mark.parametrize( | ||
argnames="y", argvalues=[np.ones(3), None], ids=["y-array", "y-None"] | ||
) | ||
@pytest.mark.parametrize( | ||
argnames="X", argvalues=[np.ones(2), None], ids=["X-array", "X-None"] | ||
) | ||
def test_model_builder(self, X, y, coords) -> None: | ||
with pytest.raises( | ||
NotImplementedError, match="This method must be implemented by a subclass" | ||
): | ||
ModelBuilder().build_model(X=X, y=y, coords=coords) | ||
|
||
def test_fit_build_not_implemented(self): | ||
with pytest.raises( | ||
NotImplementedError, match="This method must be implemented by a subclass" | ||
): | ||
ModelBuilder().fit(X=np.ones(2), y=np.ones(3), coords={"a": 1}) | ||
|
||
@pytest.mark.parametrize( | ||
argnames="coords", | ||
argvalues=[None, {"a": 1}], | ||
ids=["None-coords", "dict-coords"], | ||
) | ||
def test_fit_predict(self, coords, rng) -> None: | ||
X = rng.normal(loc=0, scale=1, size=(20, 2)) | ||
y = rng.normal(loc=0, scale=1, size=(20,)) | ||
model = MyToyModel(sample_kwargs={"chains": 2, "draws": 2}) | ||
model.fit(X, y, coords=coords) | ||
predictions = model.predict(X=X) | ||
score = model.score(X=X, y=y) | ||
assert isinstance(model.idata, az.InferenceData) | ||
assert az.extract(data=model.idata, var_names=["beta"]).shape == (2, 2 * 2) | ||
assert az.extract(data=model.idata, var_names=["sigma"]).shape == (2 * 2,) | ||
assert az.extract(data=model.idata, var_names=["mu"]).shape == (20, 2 * 2) | ||
assert az.extract( | ||
data=model.idata, group="posterior_predictive", var_names=["y_hat"] | ||
).shape == (20, 2 * 2) | ||
assert isinstance(score, pd.Series) | ||
assert score.shape == (2,) | ||
assert isinstance(predictions, az.InferenceData) |