From 83c7a5057be1b653feb4fac44197c7ac6781e02c Mon Sep 17 00:00:00 2001 From: Marco Favorito Date: Sat, 18 Mar 2023 11:42:38 +0100 Subject: [PATCH] test: make XGBoostSampler tests to work also on Windows We had to workaround issue #49 by providing different expected results for Windows. --- tests/test_calibrator.py | 119 +++++++++++++++++++--------- tests/test_samplers/test_xgboost.py | 9 ++- 2 files changed, 89 insertions(+), 39 deletions(-) diff --git a/tests/test_calibrator.py b/tests/test_calibrator.py index 7dd243af..6f95c322 100644 --- a/tests/test_calibrator.py +++ b/tests/test_calibrator.py @@ -18,6 +18,7 @@ import glob import os +import sys from typing import Any from unittest.mock import MagicMock, patch @@ -43,6 +44,78 @@ class TestCalibrate: # pylint: disable=too-many-instance-attributes,attribute-defined-outside-init """Test the Calibrator.calibrate method.""" + expected_params = np.array( + [ + [0.59, 0.36], + [0.63, 0.41], + [0.18, 0.39], + [0.56, 0.37], + [0.83, 0.35], + [0.54, 0.32], + [0.74, 0.32], + [0.53, 0.46], + [0.57, 0.39], + [0.94, 0.42], + [0.32, 0.93], + [0.8, 0.06], + [0.01, 0.02], + [0.04, 0.99], + ] + ) + + expected_losses = [ + 0.33400294, + 0.55274918, + 0.55798021, + 0.61712034, + 0.91962075, + 1.31118518, + 1.51682355, + 1.55503666, + 1.65968375, + 1.78845827, + 1.79905545, + 2.07605975, + 2.28484134, + 3.01432484, + ] + + win32_expected_params = np.array( + [ + [0.59, 0.36], + [0.63, 0.41], + [0.18, 0.39], + [0.56, 0.37], + [0.83, 0.35], + [0.54, 0.32], + [0.74, 0.32], + [0.53, 0.46], + [0.57, 0.39], + [0.32, 0.93], + [0.8, 0.06], + [0.01, 0.02], + [1.0, 0.99], + [0.04, 0.99], + ] + ) + + win32_expected_losses = [ + 0.33400294, + 0.55274918, + 0.55798021, + 0.61712034, + 0.91962075, + 1.31118518, + 1.51682355, + 1.55503666, + 1.65968375, + 1.79905545, + 2.07605975, + 2.28484134, + 2.60093616, + 3.01432484, + ] + def setup(self) -> None: """Set up the tests.""" self.true_params = np.array([0.50, 0.50]) @@ -76,42 +149,6 @@ def setup(self) -> None: @pytest.mark.parametrize("n_jobs", [1, 2]) def test_calibrator_calibrate(self, n_jobs: int) -> None: """Test the Calibrator.calibrate method, positive case, with different number of jobs.""" - expected_params = np.array( - [ - [0.59, 0.36], - [0.63, 0.41], - [0.18, 0.39], - [0.56, 0.37], - [0.83, 0.35], - [0.54, 0.32], - [0.74, 0.32], - [0.53, 0.46], - [0.57, 0.39], - [0.94, 0.42], - [0.32, 0.93], - [0.8, 0.06], - [0.01, 0.02], - [0.04, 0.99], - ] - ) - - expected_losses = [ - 0.33400294, - 0.55274918, - 0.55798021, - 0.61712034, - 0.91962075, - 1.31118518, - 1.51682355, - 1.55503666, - 1.65968375, - 1.78845827, - 1.79905545, - 2.07605975, - 2.28484134, - 3.01432484, - ] - cal = Calibrator( samplers=[ self.random_sampler, @@ -135,8 +172,14 @@ def test_calibrator_calibrate(self, n_jobs: int) -> None: params, losses = cal.calibrate(2) - assert np.allclose(params, expected_params) - assert np.allclose(losses, expected_losses) + # TODO: this is a temporary workaround to make tests to run also on Windows. + # See: https://github.com/bancaditalia/black-it/issues/49 + if sys.platform == "win32": + assert np.allclose(params, self.win32_expected_params) + assert np.allclose(losses, self.win32_expected_losses) + else: + assert np.allclose(params, self.expected_params) + assert np.allclose(losses, self.expected_losses) def test_calibrator_with_check_convergence(self, capsys: Any) -> None: """Test the Calibrator.calibrate method with convergence check.""" diff --git a/tests/test_samplers/test_xgboost.py b/tests/test_samplers/test_xgboost.py index 63f68728..c66d559b 100644 --- a/tests/test_samplers/test_xgboost.py +++ b/tests/test_samplers/test_xgboost.py @@ -14,6 +14,8 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . """This module contains tests for the xgboost sampler.""" +import sys + import numpy as np from black_it.calibrator import Calibrator @@ -24,7 +26,12 @@ from ..fixtures.test_models import BH4 # type: ignore -expected_params = np.array([[0.24, 0.26], [0.37, 0.21], [0.43, 0.14], [0.11, 0.04]]) +# TODO: this is a temporary workaround to make tests to run also on Windows. +# See: https://github.com/bancaditalia/black-it/issues/49 +if sys.platform == "win32": + expected_params = np.array([[0.24, 0.26], [0.19, 0.11], [0.13, 0.22], [0.11, 0.05]]) +else: + expected_params = np.array([[0.24, 0.26], [0.37, 0.21], [0.43, 0.14], [0.11, 0.04]]) MAX_FLOAT32 = np.finfo(np.float32).max MIN_FLOAT32 = np.finfo(np.float32).min