Repository: claritychallenge/clarity (language: python)

Issue:
Run tests conditional on CPU architecture
As investigated in #130 tests fail when GitHub Runners with CPUs of type Intel(R) Xeon(R) CPU E5-2673 v3 @ 2.40GHz (see [example]()).

As [suggested](https://github.com/claritychallenge/clarity/pull/123#issuecomment-1324154208) by @jonbarker68 we could have different tests conditional on the CPU architecture.

This can be achieved using the [@pytest.mark.skipif()](https://docs.pytest.org/en/7.1.x/how-to/skipping.html#id1) fixture in conjunction with [py-cpuinfo](https://github.com/workhorsy/py-cpuinfo) which should report the CPU architecture.


Discussion / hints:

Fix patch:
diff --git a/setup.cfg b/setup.cfg
index b91907504..f134700f5 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -83,10 +83,11 @@ dev =
   isort
   mypy
   pre-commit
+  py-cpuinfo
   pycodestyle
   pytest
-  pytest-regtest
   pytest-cov
+  pytest-regtest
   yamllint
 
 pypi =


Test patch:
diff --git a/.github/workflows/run_tests.yml b/.github/workflows/run_tests.yml
index dbb8dee81..ba14b7d7d 100644
--- a/.github/workflows/run_tests.yml
+++ b/.github/workflows/run_tests.yml
@@ -33,6 +33,9 @@ jobs:
           pip install -e .[tests]
           pip install -e .[docs]
           pip install -e .[dev]
+      - name: CPU information
+        run: |
+          python -m cpuinfo --json
       - name: Run pytest
         run: |
           pytest --cov=clarity .
diff --git a/tests/regression/_regtest_outputs/test_predictors.test_torch_msbg_stoi.out b/tests/regression/_regtest_outputs/test_predictors.test_torch_msbg_stoi_non_xeon_e5_2673_cpu.out
similarity index 100%
rename from tests/regression/_regtest_outputs/test_predictors.test_torch_msbg_stoi.out
rename to tests/regression/_regtest_outputs/test_predictors.test_torch_msbg_stoi_non_xeon_e5_2673_cpu.out
diff --git a/tests/regression/_regtest_outputs/test_predictors.test_torch_msbg_stoi_xeon_e5_2673_cpu.out b/tests/regression/_regtest_outputs/test_predictors.test_torch_msbg_stoi_xeon_e5_2673_cpu.out
new file mode 100644
index 000000000..ee679816e
--- /dev/null
+++ b/tests/regression/_regtest_outputs/test_predictors.test_torch_msbg_stoi_xeon_e5_2673_cpu.out
@@ -0,0 +1,1 @@
+Torch MSBG STOILoss -0.46198, ESTOILoss -0.32999
diff --git a/tests/regression/test_predictors.py b/tests/regression/test_predictors.py
index 49cbcc74b..04d0ba5d2 100644
--- a/tests/regression/test_predictors.py
+++ b/tests/regression/test_predictors.py
@@ -1,12 +1,46 @@
-# Regression test
+""""Regression tests for Predictors"""
+import re
 
+import pytest
 import torch
+from cpuinfo import get_cpu_info
 
 from clarity.predictor.torch_msbg import MSBGHearingModel, torchloudnorm
 from clarity.predictor.torch_stoi import NegSTOILoss
 
+CPUINFO = get_cpu_info()
 
-def test_torch_msbg_stoi(regtest):
+
+@pytest.mark.skipif(
+    re.search("E5-2673", CPUINFO["brand_raw"]),
+    reason="Xeon E5-2673 CPU arch gives a different value",
+)
+def test_torch_msbg_stoi_non_xeon_e5_2673_cpu(regtest):
+
+    torch.manual_seed(0)
+    torch.set_num_threads(1)
+    stoi_loss = NegSTOILoss(sample_rate=44100)
+    estoi_loss = NegSTOILoss(sample_rate=44100, extended=True)
+
+    audiogram = [45, 35, 30, 45, 50, 50]
+    audiometric = [250, 500, 1000, 2000, 4000, 6000]
+    msbg = MSBGHearingModel(audiogram=audiogram, audiometric=audiometric, device="cpu")
+
+    x = torch.randn(2, 44100)
+    y = msbg(x)
+    stoi_loss = stoi_loss.forward(x.cpu(), y.cpu()).mean()
+    estoi_loss = estoi_loss.forward(x.cpu(), y.cpu()).mean()
+
+    regtest.write(
+        f"Torch MSBG STOILoss {stoi_loss:0.5f}, ESTOILoss {estoi_loss:0.5f}\n"
+    )
+
+
+@pytest.mark.skipif(
+    not re.search("E5-2673", CPUINFO["brand_raw"]),
+    reason="Test value obtained with Xeon E5-2673",
+)
+def test_torch_msbg_stoi_xeon_e5_2673_cpu(regtest):
 
     torch.manual_seed(0)
     torch.set_num_threads(1)