From 8601e51adb21e044b597e2920576b8ff854a3f74 Mon Sep 17 00:00:00 2001 From: Luis Aleixo Date: Thu, 2 Jun 2022 16:35:17 +0200 Subject: [PATCH] Removed np.seed from tests and added retry decorator --- cara/tests/apps/calculator/test_model_generator.py | 4 ++-- cara/tests/apps/calculator/test_webapp.py | 6 +++--- cara/tests/models/test_short_range_model.py | 2 -- cara/tests/test_expiration.py | 2 ++ cara/tests/test_full_algorithm.py | 4 ++-- cara/tests/test_monte_carlo_full_models.py | 3 +-- cara/tests/test_predefined_distributions.py | 2 -- cara/tests/test_sampleable_distribution.py | 3 --- 8 files changed, 10 insertions(+), 16 deletions(-) diff --git a/cara/tests/apps/calculator/test_model_generator.py b/cara/tests/apps/calculator/test_model_generator.py index 6298fd5c..cd7de105 100644 --- a/cara/tests/apps/calculator/test_model_generator.py +++ b/cara/tests/apps/calculator/test_model_generator.py @@ -4,6 +4,7 @@ import typing import numpy as np import numpy.testing as npt import pytest +from retry import retry from cara.apps.calculator import model_generator from cara.apps.calculator.model_generator import _hours2timestring @@ -11,8 +12,6 @@ from cara.apps.calculator.model_generator import minutes_since_midnight from cara import models from cara.monte_carlo.data import expiration_distributions -# TODO: seed better the random number generators -np.random.seed(2000) def test_model_from_dict(baseline_form_data): form = model_generator.FormData.from_dict(baseline_form_data) @@ -25,6 +24,7 @@ def test_model_from_dict_invalid(baseline_form_data): model_generator.FormData.from_dict(baseline_form_data) +@retry(tries=10) @pytest.mark.parametrize( ["mask_type"], [ diff --git a/cara/tests/apps/calculator/test_webapp.py b/cara/tests/apps/calculator/test_webapp.py index fa5e37cc..6255f527 100644 --- a/cara/tests/apps/calculator/test_webapp.py +++ b/cara/tests/apps/calculator/test_webapp.py @@ -43,7 +43,7 @@ async def test_404(http_server_client): assert resp.code == 404 -@retry() +@retry(tries=10) class TestBasicApp(tornado.testing.AsyncHTTPTestCase): def get_app(self): return cara.apps.calculator.make_app() @@ -72,7 +72,7 @@ class TestBasicApp(tornado.testing.AsyncHTTPTestCase): assert 'expected number of new cases is' in response.body.decode() -@retry() +@retry(tries=10) class TestCernApp(tornado.testing.AsyncHTTPTestCase): def get_app(self): cern_theme = Path(cara.apps.calculator.__file__).parent.parent / 'themes' / 'cern' @@ -85,7 +85,7 @@ class TestCernApp(tornado.testing.AsyncHTTPTestCase): assert 'expected number of new cases is' in response.body.decode() -retry() +retry(tries=10) class TestOpenApp(tornado.testing.AsyncHTTPTestCase): def get_app(self): return cara.apps.calculator.make_app(calculator_prefix="/mycalc") diff --git a/cara/tests/models/test_short_range_model.py b/cara/tests/models/test_short_range_model.py index 37605d32..a62b23d8 100644 --- a/cara/tests/models/test_short_range_model.py +++ b/cara/tests/models/test_short_range_model.py @@ -9,8 +9,6 @@ from cara.apps.calculator.model_generator import build_expiration from cara.monte_carlo.data import short_range_expiration_distributions,\ expiration_distributions, short_range_distances, activity_distributions -# TODO: seed better the random number generators -np.random.seed(2000) SAMPLE_SIZE = 250_000 diff --git a/cara/tests/test_expiration.py b/cara/tests/test_expiration.py index cc82e694..af2bab06 100644 --- a/cara/tests/test_expiration.py +++ b/cara/tests/test_expiration.py @@ -3,6 +3,7 @@ import re import numpy as np import numpy.testing as npt import pytest +from retry import retry from cara import models from cara.monte_carlo.data import expiration_distribution @@ -41,6 +42,7 @@ def test_multiple(): npt.assert_almost_equal(aerosol_expected, e.aerosols(mask)) +@retry(tries=10) # expected values obtained from analytical formulas @pytest.mark.parametrize( "BLO_weights, expected_aerosols", diff --git a/cara/tests/test_full_algorithm.py b/cara/tests/test_full_algorithm.py index 744c3982..fbcc6613 100644 --- a/cara/tests/test_full_algorithm.py +++ b/cara/tests/test_full_algorithm.py @@ -6,6 +6,7 @@ from scipy.integrate import quad from scipy.special import erf import numpy.testing as npt import pytest +from retry import retry import cara.monte_carlo as mc from cara import models,data @@ -16,8 +17,6 @@ from cara.monte_carlo.data import (expiration_distributions, expiration_BLO_factors,short_range_expiration_distributions, short_range_distances,virus_distributions,activity_distributions) -# TODO: seed better the random number generators -np.random.seed(2000) SAMPLE_SIZE = 1_000_000 TOLERANCE = 0.04 @@ -655,6 +654,7 @@ def test_longrange_concentration(time,c_model,simple_c_model): ) +@retry(tries=10) @pytest.mark.parametrize( "time", [10, 10.7, 11., 12.5, 14.75, 14.9, 17] ) diff --git a/cara/tests/test_monte_carlo_full_models.py b/cara/tests/test_monte_carlo_full_models.py index 93ebd459..eaa7b35e 100644 --- a/cara/tests/test_monte_carlo_full_models.py +++ b/cara/tests/test_monte_carlo_full_models.py @@ -8,8 +8,6 @@ from cara import models,data from cara.monte_carlo.data import activity_distributions, virus_distributions, expiration_distributions, infectious_dose_distribution, viable_to_RNA_ratio_distribution from cara.apps.calculator.model_generator import build_expiration -# TODO: seed better the random number generators -np.random.seed(2000) SAMPLE_SIZE = 500_000 TOLERANCE = 0.05 @@ -336,6 +334,7 @@ def test_report_models(mc_model, expected_pi, expected_new_cases, expected_ER, rtol=TOLERANCE) +@retry(tries=10) @pytest.mark.parametrize( "mask_type, month, expected_pi, expected_dose, expected_ER", [ diff --git a/cara/tests/test_predefined_distributions.py b/cara/tests/test_predefined_distributions.py index db8c8c4b..8a670c58 100644 --- a/cara/tests/test_predefined_distributions.py +++ b/cara/tests/test_predefined_distributions.py @@ -4,8 +4,6 @@ import pytest from cara.monte_carlo.data import activity_distributions, virus_distributions -# TODO: seed better the random number generators -np.random.seed(2000) # mean & std deviations from https://doi.org/10.1101/2021.10.14.21264988 (Table 3) diff --git a/cara/tests/test_sampleable_distribution.py b/cara/tests/test_sampleable_distribution.py index c30fcbf7..c780e3df 100644 --- a/cara/tests/test_sampleable_distribution.py +++ b/cara/tests/test_sampleable_distribution.py @@ -4,9 +4,6 @@ import pytest from cara.monte_carlo import sampleable -# TODO: seed better the random number generators -np.random.seed(2000) - @pytest.mark.parametrize( "mean, std",[