Skip to content

Clean up some warnings from the test suite #6067

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 1 commit into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
72 changes: 50 additions & 22 deletions pymc/tests/test_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1700,12 +1700,14 @@ def test_bernoulli_wrong_arguments(self):
Bernoulli("x")

def test_discrete_weibull(self):
check_logp(
DiscreteWeibull,
Nat,
{"q": Unit, "beta": NatSmall},
discrete_weibull_logpmf,
)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "divide by zero encountered in log", RuntimeWarning)
check_logp(
DiscreteWeibull,
Nat,
{"q": Unit, "beta": NatSmall},
discrete_weibull_logpmf,
)
check_selfconsistency_discrete_logcdf(
DiscreteWeibull,
Nat,
Expand All @@ -1732,8 +1734,10 @@ def test_poisson(self):
)

def test_diracdeltadist(self):
check_logp(DiracDelta, I, {"c": I}, lambda value, c: np.log(c == value))
check_logcdf(DiracDelta, I, {"c": I}, lambda value, c: np.log(value >= c))
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "divide by zero encountered in log", RuntimeWarning)
check_logp(DiracDelta, I, {"c": I}, lambda value, c: np.log(c == value))
check_logcdf(DiracDelta, I, {"c": I}, lambda value, c: np.log(value >= c))

def test_zeroinflatedpoisson(self):
def logp_fn(value, psi, mu):
Expand Down Expand Up @@ -2370,12 +2374,15 @@ def test_categorical_p_not_normalized(self):

@pytest.mark.parametrize("n", [2, 3, 4])
def test_orderedlogistic(self, n):
check_logp(
OrderedLogistic,
Domain(range(n), dtype="int64", edges=(None, None)),
{"eta": R, "cutpoints": Vector(R, n - 1)},
lambda value, eta, cutpoints: orderedlogistic_logpdf(value, eta, cutpoints),
)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "invalid value encountered in log", RuntimeWarning)
warnings.filterwarnings("ignore", "divide by zero encountered in log", RuntimeWarning)
check_logp(
OrderedLogistic,
Domain(range(n), dtype="int64", edges=(None, None)),
{"eta": R, "cutpoints": Vector(R, n - 1)},
lambda value, eta, cutpoints: orderedlogistic_logpdf(value, eta, cutpoints),
)

@pytest.mark.parametrize("n", [2, 3, 4])
def test_orderedprobit(self, n):
Expand Down Expand Up @@ -2622,6 +2629,7 @@ def ref_logp(value, mu, sigma, steps):
{"mu": R, "sigma": Rplus, "steps": Nat},
ref_logp,
decimal=select_by_precision(float64=6, float32=1),
extra_args={"init_dist": Normal.dist(0, 100)},
)


Expand All @@ -2631,8 +2639,14 @@ class TestBound:
def test_continuous(self):
with Model() as model:
dist = Normal.dist(mu=0, sigma=1)
UnboundedNormal = Bound("unbound", dist, transform=None)
InfBoundedNormal = Bound("infbound", dist, lower=-np.inf, upper=np.inf, transform=None)
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", "invalid value encountered in add", RuntimeWarning
)
UnboundedNormal = Bound("unbound", dist, transform=None)
InfBoundedNormal = Bound(
"infbound", dist, lower=-np.inf, upper=np.inf, transform=None
)
LowerNormal = Bound("lower", dist, lower=0, transform=None)
UpperNormal = Bound("upper", dist, upper=0, transform=None)
BoundedNormal = Bound("bounded", dist, lower=1, upper=10, transform=None)
Expand Down Expand Up @@ -2667,7 +2681,11 @@ def test_continuous(self):
def test_discrete(self):
with Model() as model:
dist = Poisson.dist(mu=4)
UnboundedPoisson = Bound("unbound", dist)
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", "invalid value encountered in add", RuntimeWarning
)
UnboundedPoisson = Bound("unbound", dist)
LowerPoisson = Bound("lower", dist, lower=1)
UpperPoisson = Bound("upper", dist, upper=10)
BoundedPoisson = Bound("bounded", dist, lower=1, upper=10)
Expand Down Expand Up @@ -2714,8 +2732,12 @@ def test_arguments_checks(self):
msg = "Cannot transform discrete variable."
with pm.Model() as m:
x = pm.Poisson.dist(0.5)
with pytest.raises(ValueError, match=msg):
pm.Bound("bound", x, transform=pm.distributions.transforms.log)
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", "invalid value encountered in add", RuntimeWarning
)
with pytest.raises(ValueError, match=msg):
pm.Bound("bound", x, transform=pm.distributions.transforms.log)

msg = "Given dims do not exist in model coordinates."
with pm.Model() as m:
Expand Down Expand Up @@ -2784,8 +2806,12 @@ def test_bound_dist(self):
def test_array_bound(self):
with Model() as model:
dist = Normal.dist()
LowerPoisson = Bound("lower", dist, lower=[1, None], transform=None)
UpperPoisson = Bound("upper", dist, upper=[np.inf, 10], transform=None)
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", "invalid value encountered in add", RuntimeWarning
)
LowerPoisson = Bound("lower", dist, lower=[1, None], transform=None)
UpperPoisson = Bound("upper", dist, upper=[np.inf, 10], transform=None)
BoundedPoisson = Bound("bounded", dist, lower=[1, 2], upper=[9, 10], transform=None)

first, second = joint_logp(LowerPoisson, [0, 0], sum=False)[0].eval()
Expand Down Expand Up @@ -3081,7 +3107,9 @@ def random(rng, size):
with pm.Model():
pm.Normal("x")
y = pm.DensityDist("y", logp=func, random=random)
pm.sample(draws=5, tune=1, mp_ctx="spawn")
with warnings.catch_warnings():
warnings.filterwarnings("ignore", ".*number of samples.*", UserWarning)
pm.sample(draws=5, tune=1, mp_ctx="spawn")

import cloudpickle

Expand Down
28 changes: 25 additions & 3 deletions pymc/tests/test_distributions_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,25 @@ def test_batched_size(self, constant):
beta_tp = np.random.randn(batch_size, ar_order + int(constant))
y_tp = np.random.randn(batch_size, steps)
with Model() as t0:
y = AR("y", beta_tp, shape=(batch_size, steps), initval=y_tp, constant=constant)
y = AR(
"y",
beta_tp,
shape=(batch_size, steps),
initval=y_tp,
constant=constant,
init_dist=Normal.dist(0, 100, shape=(batch_size, steps)),
)
with Model() as t1:
for i in range(batch_size):
AR(f"y_{i}", beta_tp[i], sigma=1.0, shape=steps, initval=y_tp[i], constant=constant)
AR(
f"y_{i}",
beta_tp[i],
sigma=1.0,
shape=steps,
initval=y_tp[i],
constant=constant,
init_dist=Normal.dist(0, 100, shape=steps),
)

assert y.owner.op.ar_order == ar_order

Expand Down Expand Up @@ -402,7 +417,14 @@ def test_batched_init_dist(self):
AR("y", beta_tp, sigma=0.01, init_dist=init_dist, steps=steps, initval=y_tp)
with Model() as t1:
for i in range(batch_size):
AR(f"y_{i}", beta_tp, sigma=0.01, shape=steps, initval=y_tp[i])
AR(
f"y_{i}",
beta_tp,
sigma=0.01,
shape=steps,
initval=y_tp[i],
init_dist=Normal.dist(0, 100, shape=steps),
)

np.testing.assert_allclose(
t0.compile_logp()(t0.initial_point()),
Expand Down
9 changes: 6 additions & 3 deletions pymc/tests/test_hmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import warnings

import numpy as np
import numpy.testing as npt
Expand Down Expand Up @@ -59,9 +60,11 @@ def test_nuts_tuning():
with pymc.Model():
pymc.Normal("mu", mu=0, sigma=1)
step = pymc.NUTS()
idata = pymc.sample(
10, step=step, tune=5, discard_tuned_samples=False, progressbar=False, chains=1
)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", ".*number of samples.*", UserWarning)
idata = pymc.sample(
10, step=step, tune=5, discard_tuned_samples=False, progressbar=False, chains=1
)

assert not step.tune
ss_tuned = idata.warmup_sample_stats["step_size"][0, -1]
Expand Down
83 changes: 54 additions & 29 deletions pymc/tests/test_idata_conversion.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# pylint: disable=no-member, invalid-name, redefined-outer-name, protected-access, too-many-public-methods
import warnings

from typing import Dict, Tuple

Expand All @@ -18,6 +19,7 @@
predictions_to_inference_data,
to_inference_data,
)
from pymc.exceptions import ImputationWarning


@pytest.fixture(scope="module")
Expand Down Expand Up @@ -204,9 +206,13 @@ def test_posterior_predictive_keep_size(self, data, chains, draws, eight_schools

def test_posterior_predictive_warning(self, data, eight_schools_params, caplog):
with data.model:
posterior_predictive = pm.sample_posterior_predictive(
data.obj, 370, return_inferencedata=False, keep_size=False
)
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", ".*smaller than nchains times ndraws.*", UserWarning
)
posterior_predictive = pm.sample_posterior_predictive(
data.obj, 370, return_inferencedata=False, keep_size=False
)
with pytest.warns(UserWarning, match="shape of variables"):
inference_data = to_inference_data(
trace=data.obj,
Expand All @@ -222,7 +228,9 @@ def test_posterior_predictive_thinned(self, data):
with data.model:
draws = 20
thin_by = 4
idata = pm.sample(tune=5, draws=draws, chains=2, return_inferencedata=True)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", ".*number of samples.*", UserWarning)
idata = pm.sample(tune=5, draws=draws, chains=2, return_inferencedata=True)
thinned_idata = idata.sel(draw=slice(None, None, thin_by))
idata.extend(pm.sample_posterior_predictive(thinned_idata))
test_dict = {
Expand Down Expand Up @@ -275,9 +283,13 @@ def test_autodetect_coords_from_model(self, use_context):
step=pm.Metropolis(),
)
if use_context:
idata = to_inference_data(trace=trace)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "More chains .* than draws.*", UserWarning)
idata = to_inference_data(trace=trace)
if not use_context:
idata = to_inference_data(trace=trace, model=model)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "More chains .* than draws.*", UserWarning)
idata = to_inference_data(trace=trace, model=model)

assert "city" in list(idata.posterior.dims)
assert "city" in list(idata.observed_data.dims)
Expand Down Expand Up @@ -320,7 +332,8 @@ def test_missing_data_model(self):
model = pm.Model()
with model:
x = pm.Normal("x", 1, 1)
y = pm.Normal("y", x, 1, observed=data)
with pytest.warns(ImputationWarning):
y = pm.Normal("y", x, 1, observed=data)
inference_data = pm.sample(100, chains=2, return_inferencedata=True)

# make sure that data is really missing
Expand Down Expand Up @@ -349,7 +362,8 @@ def test_mv_missing_data_model(self):
# pylint: disable=unpacking-non-sequence
chol, *_ = pm.LKJCholeskyCov("chol_cov", n=2, eta=1, sd_dist=sd_dist, compute_corr=True)
# pylint: enable=unpacking-non-sequence
y = pm.MvNormal("y", mu=mu, chol=chol, observed=data)
with pytest.warns(ImputationWarning):
y = pm.MvNormal("y", mu=mu, chol=chol, observed=data)
inference_data = pm.sample(100, chains=2, return_inferencedata=True)

# make sure that data is really missing
Expand Down Expand Up @@ -457,7 +471,11 @@ def test_predictions_constant_data(self):
# this should be four chains of 100 samples
# assert predictive_trace["obs"].shape == (400, 2)
# but the shape seems to vary between pymc versions
inference_data = predictions_to_inference_data(predictive_trace, posterior_trace=trace)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "More chains .* than draws.*", UserWarning)
inference_data = predictions_to_inference_data(
predictive_trace, posterior_trace=trace
)
test_dict = {"posterior": ["beta"], "~observed_data": ""}
fails = check_multiple_attrs(test_dict, inference_data)
assert not fails, "Posterior data not copied over as expected."
Expand Down Expand Up @@ -542,7 +560,9 @@ def test_multivariate_observations(self):
p = pm.Beta("p", 1, 1, size=(3,))
p = p / p.sum()
pm.Multinomial("y", 20, p, dims=("experiment", "direction"), observed=data)
idata = pm.sample(draws=50, chains=2, tune=100, return_inferencedata=True)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", ".*number of samples.*", UserWarning)
idata = pm.sample(draws=50, chains=2, tune=100, return_inferencedata=True)
test_dict = {
"posterior": ["p"],
"sample_stats": ["lp"],
Expand Down Expand Up @@ -626,16 +646,19 @@ def test_save_warmup(self, save_warmup, chains, tune, draws):
with pm.Model():
pm.Uniform("u1")
pm.Normal("n1")
idata = pm.sample(
tune=tune,
draws=draws,
chains=chains,
cores=1,
step=pm.Metropolis(),
discard_tuned_samples=False,
return_inferencedata=True,
idata_kwargs={"save_warmup": save_warmup},
)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", ".*number of samples.*", UserWarning)
warnings.filterwarnings("ignore", "More chains .* than draws.*", UserWarning)
idata = pm.sample(
tune=tune,
draws=draws,
chains=chains,
cores=1,
step=pm.Metropolis(),
discard_tuned_samples=False,
return_inferencedata=True,
idata_kwargs={"save_warmup": save_warmup},
)
warmup_prefix = "" if save_warmup and (tune > 0) else "~"
post_prefix = "" if draws > 0 else "~"
test_dict = {
Expand All @@ -659,15 +682,17 @@ def test_save_warmup_issue_1208_after_3_9(self):
with pm.Model():
pm.Uniform("u1")
pm.Normal("n1")
trace = pm.sample(
tune=100,
draws=200,
chains=2,
cores=1,
step=pm.Metropolis(),
discard_tuned_samples=False,
return_inferencedata=False,
)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "Tuning samples will be included.*", UserWarning)
trace = pm.sample(
tune=100,
draws=200,
chains=2,
cores=1,
step=pm.Metropolis(),
discard_tuned_samples=False,
return_inferencedata=False,
)
assert isinstance(trace, pm.backends.base.MultiTrace)
assert len(trace) == 300

Expand Down
9 changes: 7 additions & 2 deletions pymc/tests/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ def test_log1mexp():
)
actual = at.log1mexp(-vals).eval()
npt.assert_allclose(actual, expected)
actual_ = log1mexp_numpy(-vals, negative_input=True)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "divide by zero encountered in log", RuntimeWarning)
warnings.filterwarnings("ignore", "invalid value encountered in log", RuntimeWarning)
actual_ = log1mexp_numpy(-vals, negative_input=True)
npt.assert_allclose(actual_, expected)
# Check that input was not changed in place
npt.assert_allclose(vals, vals_)
Expand Down Expand Up @@ -193,7 +196,9 @@ def test_log1mexp_deprecation_warnings():

def test_logdiffexp():
a = np.log([1, 2, 3, 4])
b = np.log([0, 1, 2, 3])
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "divide by zero encountered in log", RuntimeWarning)
b = np.log([0, 1, 2, 3])

assert np.allclose(logdiffexp_numpy(a, b), 0)
assert np.allclose(logdiffexp(a, b).eval(), 0)
Expand Down
Loading