Skip to content

Commit 9822ce5

Browse files
ArmavicaricardoV94
authored andcommitted
Remove 200 warnings from the test suite
1 parent dd53d8c commit 9822ce5

21 files changed

+629
-341
lines changed

pymc/tests/test_distributions.py

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,12 +1700,14 @@ def test_bernoulli_wrong_arguments(self):
17001700
Bernoulli("x")
17011701

17021702
def test_discrete_weibull(self):
1703-
check_logp(
1704-
DiscreteWeibull,
1705-
Nat,
1706-
{"q": Unit, "beta": NatSmall},
1707-
discrete_weibull_logpmf,
1708-
)
1703+
with warnings.catch_warnings():
1704+
warnings.filterwarnings("ignore", "divide by zero encountered in log", RuntimeWarning)
1705+
check_logp(
1706+
DiscreteWeibull,
1707+
Nat,
1708+
{"q": Unit, "beta": NatSmall},
1709+
discrete_weibull_logpmf,
1710+
)
17091711
check_selfconsistency_discrete_logcdf(
17101712
DiscreteWeibull,
17111713
Nat,
@@ -1732,8 +1734,10 @@ def test_poisson(self):
17321734
)
17331735

17341736
def test_diracdeltadist(self):
1735-
check_logp(DiracDelta, I, {"c": I}, lambda value, c: np.log(c == value))
1736-
check_logcdf(DiracDelta, I, {"c": I}, lambda value, c: np.log(value >= c))
1737+
with warnings.catch_warnings():
1738+
warnings.filterwarnings("ignore", "divide by zero encountered in log", RuntimeWarning)
1739+
check_logp(DiracDelta, I, {"c": I}, lambda value, c: np.log(c == value))
1740+
check_logcdf(DiracDelta, I, {"c": I}, lambda value, c: np.log(value >= c))
17371741

17381742
def test_zeroinflatedpoisson(self):
17391743
def logp_fn(value, psi, mu):
@@ -2370,12 +2374,15 @@ def test_categorical_p_not_normalized(self):
23702374

23712375
@pytest.mark.parametrize("n", [2, 3, 4])
23722376
def test_orderedlogistic(self, n):
2373-
check_logp(
2374-
OrderedLogistic,
2375-
Domain(range(n), dtype="int64", edges=(None, None)),
2376-
{"eta": R, "cutpoints": Vector(R, n - 1)},
2377-
lambda value, eta, cutpoints: orderedlogistic_logpdf(value, eta, cutpoints),
2378-
)
2377+
with warnings.catch_warnings():
2378+
warnings.filterwarnings("ignore", "invalid value encountered in log", RuntimeWarning)
2379+
warnings.filterwarnings("ignore", "divide by zero encountered in log", RuntimeWarning)
2380+
check_logp(
2381+
OrderedLogistic,
2382+
Domain(range(n), dtype="int64", edges=(None, None)),
2383+
{"eta": R, "cutpoints": Vector(R, n - 1)},
2384+
lambda value, eta, cutpoints: orderedlogistic_logpdf(value, eta, cutpoints),
2385+
)
23792386

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

26272635

@@ -2631,8 +2639,14 @@ class TestBound:
26312639
def test_continuous(self):
26322640
with Model() as model:
26332641
dist = Normal.dist(mu=0, sigma=1)
2634-
UnboundedNormal = Bound("unbound", dist, transform=None)
2635-
InfBoundedNormal = Bound("infbound", dist, lower=-np.inf, upper=np.inf, transform=None)
2642+
with warnings.catch_warnings():
2643+
warnings.filterwarnings(
2644+
"ignore", "invalid value encountered in add", RuntimeWarning
2645+
)
2646+
UnboundedNormal = Bound("unbound", dist, transform=None)
2647+
InfBoundedNormal = Bound(
2648+
"infbound", dist, lower=-np.inf, upper=np.inf, transform=None
2649+
)
26362650
LowerNormal = Bound("lower", dist, lower=0, transform=None)
26372651
UpperNormal = Bound("upper", dist, upper=0, transform=None)
26382652
BoundedNormal = Bound("bounded", dist, lower=1, upper=10, transform=None)
@@ -2667,7 +2681,11 @@ def test_continuous(self):
26672681
def test_discrete(self):
26682682
with Model() as model:
26692683
dist = Poisson.dist(mu=4)
2670-
UnboundedPoisson = Bound("unbound", dist)
2684+
with warnings.catch_warnings():
2685+
warnings.filterwarnings(
2686+
"ignore", "invalid value encountered in add", RuntimeWarning
2687+
)
2688+
UnboundedPoisson = Bound("unbound", dist)
26712689
LowerPoisson = Bound("lower", dist, lower=1)
26722690
UpperPoisson = Bound("upper", dist, upper=10)
26732691
BoundedPoisson = Bound("bounded", dist, lower=1, upper=10)
@@ -2714,8 +2732,12 @@ def test_arguments_checks(self):
27142732
msg = "Cannot transform discrete variable."
27152733
with pm.Model() as m:
27162734
x = pm.Poisson.dist(0.5)
2717-
with pytest.raises(ValueError, match=msg):
2718-
pm.Bound("bound", x, transform=pm.distributions.transforms.log)
2735+
with warnings.catch_warnings():
2736+
warnings.filterwarnings(
2737+
"ignore", "invalid value encountered in add", RuntimeWarning
2738+
)
2739+
with pytest.raises(ValueError, match=msg):
2740+
pm.Bound("bound", x, transform=pm.distributions.transforms.log)
27192741

27202742
msg = "Given dims do not exist in model coordinates."
27212743
with pm.Model() as m:
@@ -2784,8 +2806,12 @@ def test_bound_dist(self):
27842806
def test_array_bound(self):
27852807
with Model() as model:
27862808
dist = Normal.dist()
2787-
LowerPoisson = Bound("lower", dist, lower=[1, None], transform=None)
2788-
UpperPoisson = Bound("upper", dist, upper=[np.inf, 10], transform=None)
2809+
with warnings.catch_warnings():
2810+
warnings.filterwarnings(
2811+
"ignore", "invalid value encountered in add", RuntimeWarning
2812+
)
2813+
LowerPoisson = Bound("lower", dist, lower=[1, None], transform=None)
2814+
UpperPoisson = Bound("upper", dist, upper=[np.inf, 10], transform=None)
27892815
BoundedPoisson = Bound("bounded", dist, lower=[1, 2], upper=[9, 10], transform=None)
27902816

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

30863114
import cloudpickle
30873115

pymc/tests/test_distributions_timeseries.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,25 @@ def test_batched_size(self, constant):
293293
beta_tp = np.random.randn(batch_size, ar_order + int(constant))
294294
y_tp = np.random.randn(batch_size, steps)
295295
with Model() as t0:
296-
y = AR("y", beta_tp, shape=(batch_size, steps), initval=y_tp, constant=constant)
296+
y = AR(
297+
"y",
298+
beta_tp,
299+
shape=(batch_size, steps),
300+
initval=y_tp,
301+
constant=constant,
302+
init_dist=Normal.dist(0, 100, shape=(batch_size, steps)),
303+
)
297304
with Model() as t1:
298305
for i in range(batch_size):
299-
AR(f"y_{i}", beta_tp[i], sigma=1.0, shape=steps, initval=y_tp[i], constant=constant)
306+
AR(
307+
f"y_{i}",
308+
beta_tp[i],
309+
sigma=1.0,
310+
shape=steps,
311+
initval=y_tp[i],
312+
constant=constant,
313+
init_dist=Normal.dist(0, 100, shape=steps),
314+
)
300315

301316
assert y.owner.op.ar_order == ar_order
302317

@@ -402,7 +417,14 @@ def test_batched_init_dist(self):
402417
AR("y", beta_tp, sigma=0.01, init_dist=init_dist, steps=steps, initval=y_tp)
403418
with Model() as t1:
404419
for i in range(batch_size):
405-
AR(f"y_{i}", beta_tp, sigma=0.01, shape=steps, initval=y_tp[i])
420+
AR(
421+
f"y_{i}",
422+
beta_tp,
423+
sigma=0.01,
424+
shape=steps,
425+
initval=y_tp[i],
426+
init_dist=Normal.dist(0, 100, shape=steps),
427+
)
406428

407429
np.testing.assert_allclose(
408430
t0.compile_logp()(t0.initial_point()),

pymc/tests/test_hmc.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import logging
15+
import warnings
1516

1617
import numpy as np
1718
import numpy.testing as npt
@@ -59,9 +60,11 @@ def test_nuts_tuning():
5960
with pymc.Model():
6061
pymc.Normal("mu", mu=0, sigma=1)
6162
step = pymc.NUTS()
62-
idata = pymc.sample(
63-
10, step=step, tune=5, discard_tuned_samples=False, progressbar=False, chains=1
64-
)
63+
with warnings.catch_warnings():
64+
warnings.filterwarnings("ignore", ".*number of samples.*", UserWarning)
65+
idata = pymc.sample(
66+
10, step=step, tune=5, discard_tuned_samples=False, progressbar=False, chains=1
67+
)
6568

6669
assert not step.tune
6770
ss_tuned = idata.warmup_sample_stats["step_size"][0, -1]

pymc/tests/test_idata_conversion.py

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# pylint: disable=no-member, invalid-name, redefined-outer-name, protected-access, too-many-public-methods
2+
import warnings
23

34
from typing import Dict, Tuple
45

@@ -18,6 +19,7 @@
1819
predictions_to_inference_data,
1920
to_inference_data,
2021
)
22+
from pymc.exceptions import ImputationWarning
2123

2224

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

205207
def test_posterior_predictive_warning(self, data, eight_schools_params, caplog):
206208
with data.model:
207-
posterior_predictive = pm.sample_posterior_predictive(
208-
data.obj, 370, return_inferencedata=False, keep_size=False
209-
)
209+
with warnings.catch_warnings():
210+
warnings.filterwarnings(
211+
"ignore", ".*smaller than nchains times ndraws.*", UserWarning
212+
)
213+
posterior_predictive = pm.sample_posterior_predictive(
214+
data.obj, 370, return_inferencedata=False, keep_size=False
215+
)
210216
with pytest.warns(UserWarning, match="shape of variables"):
211217
inference_data = to_inference_data(
212218
trace=data.obj,
@@ -222,7 +228,9 @@ def test_posterior_predictive_thinned(self, data):
222228
with data.model:
223229
draws = 20
224230
thin_by = 4
225-
idata = pm.sample(tune=5, draws=draws, chains=2, return_inferencedata=True)
231+
with warnings.catch_warnings():
232+
warnings.filterwarnings("ignore", ".*number of samples.*", UserWarning)
233+
idata = pm.sample(tune=5, draws=draws, chains=2, return_inferencedata=True)
226234
thinned_idata = idata.sel(draw=slice(None, None, thin_by))
227235
idata.extend(pm.sample_posterior_predictive(thinned_idata))
228236
test_dict = {
@@ -275,9 +283,13 @@ def test_autodetect_coords_from_model(self, use_context):
275283
step=pm.Metropolis(),
276284
)
277285
if use_context:
278-
idata = to_inference_data(trace=trace)
286+
with warnings.catch_warnings():
287+
warnings.filterwarnings("ignore", "More chains .* than draws.*", UserWarning)
288+
idata = to_inference_data(trace=trace)
279289
if not use_context:
280-
idata = to_inference_data(trace=trace, model=model)
290+
with warnings.catch_warnings():
291+
warnings.filterwarnings("ignore", "More chains .* than draws.*", UserWarning)
292+
idata = to_inference_data(trace=trace, model=model)
281293

282294
assert "city" in list(idata.posterior.dims)
283295
assert "city" in list(idata.observed_data.dims)
@@ -320,7 +332,8 @@ def test_missing_data_model(self):
320332
model = pm.Model()
321333
with model:
322334
x = pm.Normal("x", 1, 1)
323-
y = pm.Normal("y", x, 1, observed=data)
335+
with pytest.warns(ImputationWarning):
336+
y = pm.Normal("y", x, 1, observed=data)
324337
inference_data = pm.sample(100, chains=2, return_inferencedata=True)
325338

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

355369
# make sure that data is really missing
@@ -457,7 +471,11 @@ def test_predictions_constant_data(self):
457471
# this should be four chains of 100 samples
458472
# assert predictive_trace["obs"].shape == (400, 2)
459473
# but the shape seems to vary between pymc versions
460-
inference_data = predictions_to_inference_data(predictive_trace, posterior_trace=trace)
474+
with warnings.catch_warnings():
475+
warnings.filterwarnings("ignore", "More chains .* than draws.*", UserWarning)
476+
inference_data = predictions_to_inference_data(
477+
predictive_trace, posterior_trace=trace
478+
)
461479
test_dict = {"posterior": ["beta"], "~observed_data": ""}
462480
fails = check_multiple_attrs(test_dict, inference_data)
463481
assert not fails, "Posterior data not copied over as expected."
@@ -542,7 +560,9 @@ def test_multivariate_observations(self):
542560
p = pm.Beta("p", 1, 1, size=(3,))
543561
p = p / p.sum()
544562
pm.Multinomial("y", 20, p, dims=("experiment", "direction"), observed=data)
545-
idata = pm.sample(draws=50, chains=2, tune=100, return_inferencedata=True)
563+
with warnings.catch_warnings():
564+
warnings.filterwarnings("ignore", ".*number of samples.*", UserWarning)
565+
idata = pm.sample(draws=50, chains=2, tune=100, return_inferencedata=True)
546566
test_dict = {
547567
"posterior": ["p"],
548568
"sample_stats": ["lp"],
@@ -626,16 +646,19 @@ def test_save_warmup(self, save_warmup, chains, tune, draws):
626646
with pm.Model():
627647
pm.Uniform("u1")
628648
pm.Normal("n1")
629-
idata = pm.sample(
630-
tune=tune,
631-
draws=draws,
632-
chains=chains,
633-
cores=1,
634-
step=pm.Metropolis(),
635-
discard_tuned_samples=False,
636-
return_inferencedata=True,
637-
idata_kwargs={"save_warmup": save_warmup},
638-
)
649+
with warnings.catch_warnings():
650+
warnings.filterwarnings("ignore", ".*number of samples.*", UserWarning)
651+
warnings.filterwarnings("ignore", "More chains .* than draws.*", UserWarning)
652+
idata = pm.sample(
653+
tune=tune,
654+
draws=draws,
655+
chains=chains,
656+
cores=1,
657+
step=pm.Metropolis(),
658+
discard_tuned_samples=False,
659+
return_inferencedata=True,
660+
idata_kwargs={"save_warmup": save_warmup},
661+
)
639662
warmup_prefix = "" if save_warmup and (tune > 0) else "~"
640663
post_prefix = "" if draws > 0 else "~"
641664
test_dict = {
@@ -659,15 +682,17 @@ def test_save_warmup_issue_1208_after_3_9(self):
659682
with pm.Model():
660683
pm.Uniform("u1")
661684
pm.Normal("n1")
662-
trace = pm.sample(
663-
tune=100,
664-
draws=200,
665-
chains=2,
666-
cores=1,
667-
step=pm.Metropolis(),
668-
discard_tuned_samples=False,
669-
return_inferencedata=False,
670-
)
685+
with warnings.catch_warnings():
686+
warnings.filterwarnings("ignore", "Tuning samples will be included.*", UserWarning)
687+
trace = pm.sample(
688+
tune=100,
689+
draws=200,
690+
chains=2,
691+
cores=1,
692+
step=pm.Metropolis(),
693+
discard_tuned_samples=False,
694+
return_inferencedata=False,
695+
)
671696
assert isinstance(trace, pm.backends.base.MultiTrace)
672697
assert len(trace) == 300
673698

pymc/tests/test_math.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,10 @@ def test_log1mexp():
148148
)
149149
actual = at.log1mexp(-vals).eval()
150150
npt.assert_allclose(actual, expected)
151-
actual_ = log1mexp_numpy(-vals, negative_input=True)
151+
with warnings.catch_warnings():
152+
warnings.filterwarnings("ignore", "divide by zero encountered in log", RuntimeWarning)
153+
warnings.filterwarnings("ignore", "invalid value encountered in log", RuntimeWarning)
154+
actual_ = log1mexp_numpy(-vals, negative_input=True)
152155
npt.assert_allclose(actual_, expected)
153156
# Check that input was not changed in place
154157
npt.assert_allclose(vals, vals_)
@@ -193,7 +196,9 @@ def test_log1mexp_deprecation_warnings():
193196

194197
def test_logdiffexp():
195198
a = np.log([1, 2, 3, 4])
196-
b = np.log([0, 1, 2, 3])
199+
with warnings.catch_warnings():
200+
warnings.filterwarnings("ignore", "divide by zero encountered in log", RuntimeWarning)
201+
b = np.log([0, 1, 2, 3])
197202

198203
assert np.allclose(logdiffexp_numpy(a, b), 0)
199204
assert np.allclose(logdiffexp(a, b).eval(), 0)

0 commit comments

Comments
 (0)