Skip to content

Commit

Permalink
test: unify using @pytest.mark.flaky(...) & bump pytest plugins (aw…
Browse files Browse the repository at this point in the history
…slabs#3132)

*Issue #, if available:*

*Description of changes:*
seem that there is mix of few packages and maybe better to use just
`pytest` extension


By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice.


**Please tag this pr with at least one of these labels to make our
release process faster:** BREAKING, new feature, bug fix, other change,
dev setup

cc: @lostella

---------

Co-authored-by: Lorenzo Stella <lorenzostella@gmail.com>
  • Loading branch information
2 people authored and kashif committed Jun 15, 2024
1 parent 82843b5 commit 10a6de6
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/mxnet_nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ jobs:
-r requirements/requirements-extras-autogluon.txt
- name: Test with pytest
run: |
pytest -m 'not (gpu or serial)' --cov src/gluonts --cov-report=term --cov-report xml test
pytest -m 'not (gpu or serial)' --cov=gluonts --cov-report=term --cov-report xml test
2 changes: 1 addition & 1 deletion .github/workflows/test_release_unix_nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ jobs:
- name: Test with pytest
run: |
cd gluon-ts
pytest -m 'not (gpu or serial)' --cov src/gluonts --cov-report=term --cov-report xml test
pytest -m 'not (gpu or serial)' --cov=gluonts --cov-report=term --cov-report xml test
2 changes: 1 addition & 1 deletion .github/workflows/test_release_win32_nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ jobs:
- name: Test with pytest
run: |
cd gluon-ts
pytest -m 'not (gpu or serial)' --cov src/gluonts --cov-report=term --cov-report xml test
pytest -m 'not (gpu or serial)' --cov=gluonts --cov-report=term --cov-report xml test
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ line-length = 79
minversion = "6.0"
timeout = 60
addopts = """
--color=yes
--doctest-modules
--ignore src/gluonts/block.py
--ignore src/gluonts/distribution.py
Expand Down
12 changes: 6 additions & 6 deletions requirements/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pandas>=1.1
flaky~=3.6
pytest-cov==2.6.*
pytest-timeout~=1.3
pytest-xdist~=1.27
pytest>=6.0
pandas >=1.1
pytest >7.0
pytest-cov >4.0,<5.0
pytest-timeout >2.0, <3.0
pytest-xdist >3.0, <4.0
pytest-rerunfailures >=13.0, <14.0
ujson
orjson
requests
Expand Down
3 changes: 1 addition & 2 deletions test/ext/naive_2/test_predictors.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import numpy as np
import pandas as pd
import pytest
from flaky import flaky

from gluonts.dataset.artificial import constant_dataset
from gluonts.dataset.common import Dataset
Expand Down Expand Up @@ -106,7 +105,7 @@ def test_predictor(make_predictor, freq: str):
CONSTANT_DATASET_PREDICTION_LENGTH = dataset_info.prediction_length


@flaky(max_runs=3, min_passes=1)
@pytest.mark.flaky(retries=3)
@pytest.mark.parametrize(
"predictor, accuracy",
[
Expand Down
7 changes: 3 additions & 4 deletions test/mx/distribution/test_distribution_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import mxnet as mx
import numpy as np
import pytest
from flaky import flaky

from gluonts.core.serde import dump_json, load_json
from gluonts.mx.model.tpp.distribution import Loglogistic, Weibull
Expand Down Expand Up @@ -149,7 +148,7 @@

@pytest.mark.parametrize("distr_class, params", test_cases)
@pytest.mark.parametrize("serialize_fn", serialize_fn_list)
@flaky
@pytest.mark.flaky(retries=3)
def test_sampling(distr_class, params, serialize_fn) -> None:
distr = distr_class(**params)
distr = serialize_fn(distr)
Expand Down Expand Up @@ -205,7 +204,7 @@ def test_sampling(distr_class, params, serialize_fn) -> None:
]


@flaky(min_passes=1, max_runs=3)
@pytest.mark.flaky(retries=3)
@pytest.mark.parametrize("distr, params, dim", test_cases_multivariate)
@pytest.mark.parametrize("serialize_fn", serialize_fn_list)
def test_multivariate_sampling(distr, params, dim, serialize_fn) -> None:
Expand Down Expand Up @@ -261,7 +260,7 @@ def test_piecewise_linear_sampling(distr, params, serialize_fn):
assert samples.shape == (num_samples, 2)


@pytest.mark.flaky(max_runs=3, min_passes=1)
@pytest.mark.flaky(retries=3)
@pytest.mark.parametrize("alpha, beta", [(0.3, 0.9), (1.5, 1.7)])
@pytest.mark.parametrize("zero_probability, one_probability", [(0.1, 0.2)])
def test_inflated_beta_sampling(
Expand Down
2 changes: 2 additions & 0 deletions test/mx/distribution/test_mixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ def diff(x: np.ndarray, y: np.ndarray) -> np.ndarray:
def test_mixture(
distr1: Distribution, distr2: Distribution, p: Tensor, serialize_fn
) -> None:
np.random.seed(0)
mx.random.seed(0)
# sample from component distributions, and select samples
samples1 = distr1.sample(num_samples=NUM_SAMPLES_LARGE)
samples2 = distr2.sample(num_samples=NUM_SAMPLES_LARGE)
Expand Down
17 changes: 12 additions & 5 deletions test/mx/distribution/test_mx_distribution_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Test that maximizing likelihood allows to correctly recover distribution parameters for all
distributions exposed to the user.
"""
import random
from functools import reduce

from typing import List, Tuple
Expand Down Expand Up @@ -536,7 +537,7 @@ def test_dirichlet_multinomial(hybridize: bool) -> None:
), f"Covariance did not match: cov = {cov}, cov_hat = {cov_hat}"


@pytest.mark.flaky(max_runs=3, min_passes=1)
@pytest.mark.flaky(retries=3)
@pytest.mark.parametrize("hybridize", [True, False])
@pytest.mark.parametrize("rank", [0, 1])
def test_lowrank_multivariate_gaussian(hybridize: bool, rank: int) -> None:
Expand Down Expand Up @@ -604,7 +605,7 @@ def test_lowrank_multivariate_gaussian(hybridize: bool, rank: int) -> None:
), f"sigma did not match: sigma = {Sigma}, sigma_hat = {Sigma_hat}"


@pytest.mark.flaky(max_runs=3, min_passes=1)
@pytest.mark.flaky(retries=3)
@pytest.mark.parametrize("hybridize", [True, False])
def test_empirical_distribution(hybridize: bool) -> None:
r"""
Expand Down Expand Up @@ -757,6 +758,9 @@ def test_neg_binomial(mu_alpha: Tuple[float, float], hybridize: bool) -> None:
"""
Test to check that maximizing the likelihood recovers the parameters
"""
random.seed(0)
np.random.seed(0)
mx.random.seed(0)
# test instance
mu, alpha = mu_alpha

Expand Down Expand Up @@ -1243,7 +1247,7 @@ def test_genpareto_likelihood(xi: float, beta: float, hybridize: bool) -> None:


@pytest.mark.timeout(120)
@pytest.mark.flaky(max_runs=6, min_passes=1)
@pytest.mark.flaky(retries=6)
@pytest.mark.parametrize("rate", [50.0])
@pytest.mark.parametrize("zero_probability", [0.8, 0.2, 0.01])
@pytest.mark.parametrize("hybridize", [False, True])
Expand All @@ -1255,8 +1259,11 @@ def test_inflated_poisson_likelihood(
"""
Test to check that maximizing the likelihood recovers the parameters
"""
random.seed(0)
np.random.seed(0)
mx.random.seed(0)
# generate samples
num_samples = 1000 # Required for convergence
num_samples = 2000 # Required for convergence

distr = ZeroInflatedPoissonOutput().distribution(
distr_args=[
Expand Down Expand Up @@ -1291,7 +1298,7 @@ def test_inflated_poisson_likelihood(


@pytest.mark.timeout(150)
@pytest.mark.flaky(max_runs=6, min_passes=1)
@pytest.mark.flaky(retries=6)
@pytest.mark.parametrize("mu", [5.0])
@pytest.mark.parametrize("alpha", [0.05])
@pytest.mark.parametrize("zero_probability", [0.3])
Expand Down
3 changes: 1 addition & 2 deletions test/mx/model/gpvar/test_gpvar.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import mxnet as mx

import pytest
from flaky import flaky

from gluonts.dataset.artificial import constant_dataset
from gluonts.dataset.common import TrainDatasets
Expand Down Expand Up @@ -93,7 +92,7 @@ def test_gpvar_proj():
assert distr.mean.shape == (batch, dim)


@flaky(max_runs=3, min_passes=1)
@pytest.mark.flaky(retries=3)
@pytest.mark.parametrize("hybridize", [True, False])
@pytest.mark.parametrize("target_dim_sample", [None, 2])
@pytest.mark.parametrize("use_marginal_transformation", [True, False])
Expand Down
2 changes: 1 addition & 1 deletion test/mx/model/simple_feedforward/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def hyperparameters():
)


@pytest.mark.flaky(max_runs=3, min_passes=1)
@pytest.mark.flaky(retries=3)
@pytest.mark.parametrize("hybridize", [True, False])
@pytest.mark.parametrize("sampling", [True, False])
def test_accuracy(accuracy_test, hyperparameters, hybridize, sampling):
Expand Down
2 changes: 1 addition & 1 deletion test/mx/model/transformer/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def hyperparameters():
)


@pytest.mark.flaky(max_runs=3, min_passes=1)
@pytest.mark.flaky(retries=3)
@pytest.mark.parametrize("hybridize", [True, False])
def test_accuracy(accuracy_test, hyperparameters, hybridize):
hyperparameters.update(num_batches_per_epoch=80, hybridize=hybridize)
Expand Down
2 changes: 2 additions & 0 deletions test/torch/model/test_estimators.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import pytest
import pandas as pd
import numpy as np
from lightning import seed_everything

from gluonts.dataset.repository import get_dataset
from gluonts.model.predictor import Predictor
Expand Down Expand Up @@ -309,6 +310,7 @@ def test_estimator_constant_dataset(
],
)
def test_estimator_with_features(estimator_constructor):
seed_everything(42)
freq = "1h"
prediction_length = 12

Expand Down
13 changes: 8 additions & 5 deletions test/torch/modules/test_torch_distribution_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import numpy as np
import pytest
from lightning import seed_everything

import torch
import torch.nn as nn
from scipy.special import softmax
Expand Down Expand Up @@ -119,7 +121,7 @@ def compare_logits(
).all(), f"logits did not match: logits_true = {param_true}, logits_hat = {param_hat}"


@pytest.mark.flaky(max_runs=3, min_passes=1)
@pytest.mark.flaky(retries=3)
@pytest.mark.parametrize("concentration1, concentration0", [(3.75, 1.25)])
def test_beta_likelihood(concentration1: float, concentration0: float) -> None:
"""
Expand Down Expand Up @@ -158,7 +160,7 @@ def test_beta_likelihood(concentration1: float, concentration0: float) -> None:
), f"concentration0 did not match: concentration0 = {concentration0}, concentration0_hat = {concentration0_hat}"


@pytest.mark.flaky(max_runs=3, min_passes=1)
@pytest.mark.flaky(retries=3)
@pytest.mark.parametrize("concentration, rate", [(3.75, 1.25)])
def test_gamma_likelihood(concentration: float, rate: float) -> None:
"""
Expand Down Expand Up @@ -193,7 +195,7 @@ def test_gamma_likelihood(concentration: float, rate: float) -> None:
), f"rate did not match: rate = {rate}, rate_hat = {rate_hat}"


@pytest.mark.flaky(max_runs=3, min_passes=1)
@pytest.mark.flaky(retries=3)
@pytest.mark.parametrize("loc, scale,", [(1.0, 0.1)])
def test_normal_likelihood(loc: float, scale: float):
locs = torch.zeros((NUM_SAMPLES,)) + loc
Expand Down Expand Up @@ -223,7 +225,7 @@ def test_normal_likelihood(loc: float, scale: float):
), f"scale did not match: scale = {scale}, scale_hat = {scale_hat}"


@pytest.mark.flaky(max_runs=3, min_passes=1)
@pytest.mark.flaky(retries=3)
@pytest.mark.parametrize("df, loc, scale,", [(6.0, 2.3, 0.7)])
def test_studentT_likelihood(df: float, loc: float, scale: float):
dfs = torch.zeros((NUM_SAMPLES,)) + df
Expand Down Expand Up @@ -258,7 +260,7 @@ def test_studentT_likelihood(df: float, loc: float, scale: float):
), f"scale did not match: scale = {scale}, scale_hat = {scale_hat}"


@pytest.mark.flaky(max_runs=3, min_passes=1)
@pytest.mark.flaky(retries=3)
@pytest.mark.parametrize("rate", [1.0])
def test_poisson(rate: float) -> None:
"""
Expand Down Expand Up @@ -297,6 +299,7 @@ def test_neg_binomial(total_count: float, logit: float) -> None:
"""
Test to check that maximizing the likelihood recovers the parameters
"""
seed_everything(42)
# generate samples
total_counts = torch.zeros((NUM_SAMPLES,)) + total_count
logits = torch.zeros((NUM_SAMPLES,)) + logit
Expand Down

0 comments on commit 10a6de6

Please sign in to comment.