Skip to content

Commit

Permalink
test: make XGBoostSampler tests to work also on Windows
Browse files Browse the repository at this point in the history
We had to workaround issue #49 by providing different expected results
for Windows.
  • Loading branch information
marcofavorito committed Mar 18, 2023
1 parent e50a40a commit 83c7a50
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 39 deletions.
119 changes: 81 additions & 38 deletions tests/test_calibrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import glob
import os
import sys
from typing import Any
from unittest.mock import MagicMock, patch

Expand All @@ -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])
Expand Down Expand Up @@ -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,
Expand All @@ -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."""
Expand Down
9 changes: 8 additions & 1 deletion tests/test_samplers/test_xgboost.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""This module contains tests for the xgboost sampler."""
import sys

import numpy as np

from black_it.calibrator import Calibrator
Expand All @@ -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
Expand Down

0 comments on commit 83c7a50

Please sign in to comment.