From e08d09ac5923749fe5b5ebee8064887bb06ed671 Mon Sep 17 00:00:00 2001 From: Florian Frantzen Date: Tue, 10 Dec 2024 15:17:28 +0100 Subject: [PATCH] Apply recommended pytest config See scientific python development guide --- .pre-commit-config.yaml | 3 --- pyproject.toml | 13 +++++++++++-- test/nn/cell/test_ccxn.py | 17 +++++------------ test/nn/cell/test_cwn.py | 21 ++++++--------------- test/nn/combinatorial/test_hmc.py | 12 ++++-------- 5 files changed, 26 insertions(+), 40 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f81f52e7..2526fc89 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,3 @@ -default_language_version: - python: python3.11 - repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v5.0.0 diff --git a/pyproject.toml b/pyproject.toml index 942e5706..b88a434a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -100,7 +100,7 @@ ignore = ["E501"] # line too long [tool.ruff.lint.pydocstyle] convention = "numpy" -[tool.ruff.per-file-ignores] +[tool.ruff.lint.per-file-ignores] "__init__.py" = ["F403"] [tool.setuptools.packages.find] @@ -118,7 +118,16 @@ disable_error_code = ["import-untyped"] plugins = "numpy.typing.mypy_plugin" [tool.pytest.ini_options] -addopts = "--capture=no" +minversion = "7.0" +addopts = ["-ra", "--showlocals", "--strict-markers", "--strict-config"] +xfail_strict = true +filterwarnings = [ + "error", + "ignore::scipy.sparse._base.SparseEfficiencyWarning", + "ignore:Sparse CSR tensor support is in beta state:UserWarning", +] +log_cli_level = "info" +testpaths = ["test"] [tool.numpydoc_validation] checks = [ diff --git a/test/nn/cell/test_ccxn.py b/test/nn/cell/test_ccxn.py index 9394864f..432dad2c 100644 --- a/test/nn/cell/test_ccxn.py +++ b/test/nn/cell/test_ccxn.py @@ -19,18 +19,11 @@ def test_forward(self): att=False, ).to(device) - x_0 = torch.rand(2, 2) - x_1 = torch.rand(2, 2) - - adjacency_1 = torch.rand(2, 2) - incidence_2 = torch.rand(2, 2) - - x_0, x_1 = ( - torch.tensor(x_0).float().to(device), - torch.tensor(x_1).float().to(device), - ) - adjacency_1 = adjacency_1.float().to(device) - incidence_2 = incidence_2.float().to(device) + x_0 = torch.rand(2, 2, dtype=torch.float32, device=device) + x_1 = torch.rand(2, 2, dtype=torch.float32, device=device) + + adjacency_1 = torch.rand(2, 2, dtype=torch.float32, device=device) + incidence_2 = torch.rand(2, 2, dtype=torch.float32, device=device) x_0, x_1, x_2 = model(x_0, x_1, adjacency_1, incidence_2) assert x_0.shape == torch.Size([2, 2]) diff --git a/test/nn/cell/test_cwn.py b/test/nn/cell/test_cwn.py index cb0aafd1..9e05a5a1 100644 --- a/test/nn/cell/test_cwn.py +++ b/test/nn/cell/test_cwn.py @@ -19,21 +19,12 @@ def test_forward(self): n_layers=2, ).to(device) - x_0 = torch.rand(2, 2) - x_1 = torch.rand(2, 2) - x_2 = torch.rand(2, 2) - adjacency_1 = torch.rand(2, 2) - incidence_2 = torch.rand(2, 2) - incidence_1_t = torch.rand(2, 2) - - x_0, x_1, x_2 = ( - torch.tensor(x_0).float().to(device), - torch.tensor(x_1).float().to(device), - torch.tensor(x_2).float().to(device), - ) - adjacency_1 = adjacency_1.float().to(device) - incidence_2 = incidence_2.float().to(device) - incidence_1_t = incidence_1_t.float().to(device) + x_0 = torch.rand(2, 2, dtype=torch.float32, device=device) + x_1 = torch.rand(2, 2, dtype=torch.float32, device=device) + x_2 = torch.rand(2, 2, dtype=torch.float32, device=device) + adjacency_1 = torch.rand(2, 2, dtype=torch.float32, device=device) + incidence_2 = torch.rand(2, 2, dtype=torch.float32, device=device) + incidence_1_t = torch.rand(2, 2, dtype=torch.float32, device=device) x_0, x_1, x_2 = model(x_0, x_1, x_2, adjacency_1, incidence_2, incidence_1_t) assert x_0.shape == torch.Size([2, 16]) diff --git a/test/nn/combinatorial/test_hmc.py b/test/nn/combinatorial/test_hmc.py index 67852dd2..60cd4f1b 100644 --- a/test/nn/combinatorial/test_hmc.py +++ b/test/nn/combinatorial/test_hmc.py @@ -1,4 +1,5 @@ """Tests for the HMC class.""" + import numpy as np import torch @@ -17,18 +18,13 @@ def test_forward(self): channels_per_layer = [[in_channels, intermediate_channels, final_channels]] model = HMC(channels_per_layer, negative_slope=0.2).to(device) - x_0 = torch.rand(2, 2) - x_1 = torch.rand(2, 2) - x_2 = torch.rand(2, 2) + x_0 = torch.rand(2, 2, dtype=torch.float32, device=device) + x_1 = torch.rand(2, 2, dtype=torch.float32, device=device) + x_2 = torch.rand(2, 2, dtype=torch.float32, device=device) adjacency_0 = torch.from_numpy( np.random.default_rng().random((2, 2)) ).to_sparse() - x_0, x_1, x_2 = ( - torch.tensor(x_0).float().to(device), - torch.tensor(x_1).float().to(device), - torch.tensor(x_2).float().to(device), - ) adjacency_0 = adjacency_0.float().to(device) x_0, x_1, x_2 = model(