From 00bfab9d2a02504bf9dbee90d659caa45e4832c2 Mon Sep 17 00:00:00 2001 From: Naoto Mizuno Date: Fri, 6 Oct 2023 13:04:10 +0900 Subject: [PATCH 1/3] Add test for _one_side_trunc_norm_sampling --- python_tests/preferential/samplers/test_gp.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 python_tests/preferential/samplers/test_gp.py diff --git a/python_tests/preferential/samplers/test_gp.py b/python_tests/preferential/samplers/test_gp.py new file mode 100644 index 00000000..33f33cfc --- /dev/null +++ b/python_tests/preferential/samplers/test_gp.py @@ -0,0 +1,30 @@ +import sys +from unittest.mock import patch + +import numpy as np +import pytest +import torch + + +if sys.version_info >= (3, 8): + from optuna_dashboard.preferential.samplers.gp import _one_side_trunc_norm_sampling +else: + pytest.skip("BoTorch dropped Python3.7 support", allow_module_level=True) + + +def test_one_side_trunc_norm_sampling() -> None: + for lower in np.linspace(-10, 10, 100): + assert _one_side_trunc_norm_sampling(torch.Tensor([lower])) >= lower + + with patch.object(torch, "rand", return_value=torch.Tensor([0.4])): + assert np.allclose( + _one_side_trunc_norm_sampling(torch.Tensor([0.1])).numpy(), 0.899967154837563 + ) + with patch.object(torch, "rand", return_value=torch.Tensor([0.8])): + assert np.allclose( + _one_side_trunc_norm_sampling(torch.Tensor([-2.3])).numpy(), -0.8113606739551955 + ) + with patch.object(torch, "rand", return_value=torch.Tensor([0.1])): + assert np.allclose( + _one_side_trunc_norm_sampling(torch.Tensor([5])).numpy(), 5.426934003050024 + ) From 843a553ed37fc3c37d3ac0bcd3609e756d6c0f79 Mon Sep 17 00:00:00 2001 From: Naoto Mizuno Date: Fri, 6 Oct 2023 13:29:47 +0900 Subject: [PATCH 2/3] Add test for _orthants_MVN_Gibbs_sampling --- python_tests/preferential/samplers/test_gp.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/python_tests/preferential/samplers/test_gp.py b/python_tests/preferential/samplers/test_gp.py index 33f33cfc..08acca46 100644 --- a/python_tests/preferential/samplers/test_gp.py +++ b/python_tests/preferential/samplers/test_gp.py @@ -8,10 +8,18 @@ import torch if sys.version_info >= (3, 8): from optuna_dashboard.preferential.samplers.gp import _one_side_trunc_norm_sampling + from optuna_dashboard.preferential.samplers.gp import _orthants_MVN_Gibbs_sampling else: pytest.skip("BoTorch dropped Python3.7 support", allow_module_level=True) +def test_orthants_MVN_Gibbs_sampling() -> None: + cov_inv = torch.Tensor([[0.1, 0.3], [0.4, 0.2]]) + initial_sample = torch.Tensor([0.5, 0.6]) + ret = _orthants_MVN_Gibbs_sampling(cov_inv, 2, initial_sample) + assert ret.shape == (3, 2) + + def test_one_side_trunc_norm_sampling() -> None: for lower in np.linspace(-10, 10, 100): assert _one_side_trunc_norm_sampling(torch.Tensor([lower])) >= lower From bb31e36bed4dbb7c52dd92b3bb15d44f1b22c1a5 Mon Sep 17 00:00:00 2001 From: Naoto Mizuno Date: Mon, 18 Dec 2023 16:15:10 +0900 Subject: [PATCH 3/3] flake8 --- python_tests/preferential/samplers/test_gp.py | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/python_tests/preferential/samplers/test_gp.py b/python_tests/preferential/samplers/test_gp.py index 2ba53ce5..e5df9727 100644 --- a/python_tests/preferential/samplers/test_gp.py +++ b/python_tests/preferential/samplers/test_gp.py @@ -20,23 +20,6 @@ def test_orthants_MVN_Gibbs_sampling() -> None: assert ret.shape == (3, 2) -def test_one_side_trunc_norm_sampling() -> None: - for lower in np.linspace(-10, 10, 100): - assert _one_side_trunc_norm_sampling(torch.Tensor([lower])) >= lower - - with patch.object(torch, "rand", return_value=torch.Tensor([0.4])): - assert np.allclose( - _one_side_trunc_norm_sampling(torch.Tensor([0.1])).numpy(), 0.899967154837563 - ) - with patch.object(torch, "rand", return_value=torch.Tensor([0.8])): - assert np.allclose( - _one_side_trunc_norm_sampling(torch.Tensor([-2.3])).numpy(), -0.8113606739551955 - ) - with patch.object(torch, "rand", return_value=torch.Tensor([0.1])): - assert np.allclose( - _one_side_trunc_norm_sampling(torch.Tensor([5])).numpy(), 5.426934003050024 - ) - def test_one_side_trunc_norm_sampling() -> None: for lower in np.linspace(-10, 10, 100): assert _one_side_trunc_norm_sampling(torch.tensor([lower], dtype=torch.float64)) >= lower