From 17a5efc20383c300c0ee73b3acd8a7e7688e12c5 Mon Sep 17 00:00:00 2001 From: Sam Levang Date: Mon, 7 Oct 2024 09:35:09 -0400 Subject: [PATCH] better naming --- tests/models/cross/test_cca.py | 4 ++-- tests/models/cross/test_cpcca.py | 6 +++--- tests/models/cross/test_hilbert_cpcca.py | 4 ++-- tests/models/cross/test_hilbert_mca_rotator.py | 4 ++-- tests/models/cross/test_mca.py | 4 ++-- tests/models/cross/test_mca_rotator.py | 4 ++-- tests/models/cross/test_rda.py | 4 ++-- tests/models/single/test_eof.py | 4 ++-- tests/models/single/test_eof_rotator.py | 4 ++-- tests/models/single/test_pop.py | 4 ++-- tests/models/single/test_sparse_pca.py | 4 ++-- tests/utilities.py | 11 +++++++---- 12 files changed, 30 insertions(+), 27 deletions(-) diff --git a/tests/models/cross/test_cca.py b/tests/models/cross/test_cca.py index 2afc63d..b95daed 100644 --- a/tests/models/cross/test_cca.py +++ b/tests/models/cross/test_cca.py @@ -5,7 +5,7 @@ from xeofs.cross import CCA -from ...utilities import engine_to_module +from ...utilities import skip_if_missing_engine def generate_random_data(shape, lazy=False, seed=142): @@ -233,7 +233,7 @@ def test_save_load(tmp_path, engine): """Test save/load methods in MCA class, ensuring that we can roundtrip the model and get the same results when transforming data.""" - pytest.importorskip(engine_to_module(engine)) + skip_if_missing_engine(engine) X = generate_random_data((200, 10), seed=123) Y = generate_random_data((200, 20), seed=321) diff --git a/tests/models/cross/test_cpcca.py b/tests/models/cross/test_cpcca.py index 8ae4419..2f6bce8 100644 --- a/tests/models/cross/test_cpcca.py +++ b/tests/models/cross/test_cpcca.py @@ -5,7 +5,7 @@ from xeofs.cross import CPCCA -from ...utilities import engine_to_module +from ...utilities import skip_if_missing_engine def generate_random_data(shape, lazy=False, seed=142): @@ -282,7 +282,7 @@ def test_save_load(tmp_path, engine, alpha): """Test save/load methods in MCA class, ensuring that we can roundtrip the model and get the same results when transforming data.""" - pytest.importorskip(engine_to_module(engine)) + skip_if_missing_engine(engine) X = generate_random_data((200, 10), seed=123) Y = generate_random_data((200, 20), seed=321) @@ -328,7 +328,7 @@ def test_save_load(tmp_path, engine, alpha): def test_save_load_with_data(tmp_path, engine, alpha): """Test save/load methods in CPCCA class, ensuring that we can roundtrip the model and get the same results for SCF.""" - pytest.importorskip(engine_to_module(engine)) + skip_if_missing_engine(engine) X = generate_random_data((200, 10), seed=123) Y = generate_random_data((200, 20), seed=321) diff --git a/tests/models/cross/test_hilbert_cpcca.py b/tests/models/cross/test_hilbert_cpcca.py index bd20ab2..69a2e85 100644 --- a/tests/models/cross/test_hilbert_cpcca.py +++ b/tests/models/cross/test_hilbert_cpcca.py @@ -5,7 +5,7 @@ from xeofs.cross import HilbertCPCCA -from ...utilities import engine_to_module +from ...utilities import skip_if_missing_engine def generate_random_data(shape, lazy=False, seed=142): @@ -72,7 +72,7 @@ def test_singular_values(use_pca): def test_save_load_with_data(tmp_path, engine, alpha): """Test save/load methods in CPCCA class, ensuring that we can roundtrip the model and get the same results.""" - pytest.importorskip(engine_to_module(engine)) + skip_if_missing_engine(engine) X = generate_random_data((200, 10), seed=123) Y = generate_random_data((200, 20), seed=321) diff --git a/tests/models/cross/test_hilbert_mca_rotator.py b/tests/models/cross/test_hilbert_mca_rotator.py index 8e26083..621fedc 100644 --- a/tests/models/cross/test_hilbert_mca_rotator.py +++ b/tests/models/cross/test_hilbert_mca_rotator.py @@ -5,7 +5,7 @@ # Import the classes from your modules from xeofs.cross import HilbertMCA, HilbertMCARotator -from ...utilities import engine_to_module +from ...utilities import skip_if_missing_engine @pytest.fixture @@ -248,7 +248,7 @@ def test_scores_phase(mca_model, mock_data_array, dim): def test_save_load_with_data(tmp_path, engine, mca_model): """Test save/load methods in HilbertMCARotator class, ensuring that we can roundtrip the model and get the same results.""" - pytest.importorskip(engine_to_module(engine)) + skip_if_missing_engine(engine) original = HilbertMCARotator(n_modes=2) original.fit(mca_model) diff --git a/tests/models/cross/test_mca.py b/tests/models/cross/test_mca.py index d06c364..3fdc1b6 100644 --- a/tests/models/cross/test_mca.py +++ b/tests/models/cross/test_mca.py @@ -4,7 +4,7 @@ from xeofs.cross import MCA -from ...utilities import data_is_dask, engine_to_module +from ...utilities import data_is_dask, skip_if_missing_engine @pytest.fixture @@ -381,7 +381,7 @@ def test_save_load(dim, mock_data_array, tmp_path, engine): """Test save/load methods in MCA class, ensuring that we can roundtrip the model and get the same results when transforming data.""" - pytest.importorskip(engine_to_module(engine)) + skip_if_missing_engine(engine) original = MCA() original.fit(mock_data_array, mock_data_array, dim) diff --git a/tests/models/cross/test_mca_rotator.py b/tests/models/cross/test_mca_rotator.py index 22b0dce..1ae7b89 100644 --- a/tests/models/cross/test_mca_rotator.py +++ b/tests/models/cross/test_mca_rotator.py @@ -5,7 +5,7 @@ # Import the classes from your modules from xeofs.cross import MCA, MCARotator -from ...utilities import data_is_dask, engine_to_module +from ...utilities import data_is_dask, skip_if_missing_engine @pytest.fixture @@ -235,7 +235,7 @@ def test_save_load(dim, mock_data_array, tmp_path, engine): """Test save/load methods in MCA class, ensuring that we can roundtrip the model and get the same results when transforming data.""" - pytest.importorskip(engine_to_module(engine)) + skip_if_missing_engine(engine) original_unrotated = MCA() original_unrotated.fit(mock_data_array, mock_data_array, dim) diff --git a/tests/models/cross/test_rda.py b/tests/models/cross/test_rda.py index 1d68c21..9fdfcfd 100644 --- a/tests/models/cross/test_rda.py +++ b/tests/models/cross/test_rda.py @@ -5,7 +5,7 @@ from xeofs.cross import RDA -from ...utilities import engine_to_module +from ...utilities import skip_if_missing_engine def generate_random_data(shape, lazy=False, seed=142): @@ -233,7 +233,7 @@ def test_save_load(tmp_path, engine): """Test save/load methods in MCA class, ensuring that we can roundtrip the model and get the same results when transforming data.""" - pytest.importorskip(engine_to_module(engine)) + skip_if_missing_engine(engine) X = generate_random_data((200, 10), seed=123) Y = generate_random_data((200, 20), seed=321) diff --git a/tests/models/single/test_eof.py b/tests/models/single/test_eof.py index 556ca2d..9294f0d 100644 --- a/tests/models/single/test_eof.py +++ b/tests/models/single/test_eof.py @@ -4,7 +4,7 @@ from xeofs.single import EOF -from ...utilities import engine_to_module +from ...utilities import skip_if_missing_engine def test_init(): @@ -501,7 +501,7 @@ def test_save_load(dim, mock_data_array, tmp_path, engine): """Test save/load methods in EOF class, ensuring that we can roundtrip the model and get the same results when transforming data.""" - pytest.importorskip(engine_to_module(engine)) + skip_if_missing_engine(engine) original = EOF() original.fit(mock_data_array, dim) diff --git a/tests/models/single/test_eof_rotator.py b/tests/models/single/test_eof_rotator.py index 05ae5dd..e47ffa5 100644 --- a/tests/models/single/test_eof_rotator.py +++ b/tests/models/single/test_eof_rotator.py @@ -5,7 +5,7 @@ from xeofs.data_container import DataContainer from xeofs.single import EOF, EOFRotator -from ...utilities import data_is_dask, engine_to_module +from ...utilities import data_is_dask, skip_if_missing_engine @pytest.fixture @@ -208,7 +208,7 @@ def test_save_load(dim, mock_data_array, tmp_path, engine): """Test save/load methods in EOF class, ensuring that we can roundtrip the model and get the same results when transforming data.""" - pytest.importorskip(engine_to_module(engine)) + skip_if_missing_engine(engine) original_unrotated = EOF() original_unrotated.fit(mock_data_array, dim) diff --git a/tests/models/single/test_pop.py b/tests/models/single/test_pop.py index dd352f2..ba91cf6 100644 --- a/tests/models/single/test_pop.py +++ b/tests/models/single/test_pop.py @@ -4,7 +4,7 @@ from xeofs.single import POP -from ...utilities import engine_to_module +from ...utilities import skip_if_missing_engine def test_init(): @@ -161,7 +161,7 @@ def test_save_load(mock_data_array, tmp_path, engine): roundtrip the model and get the same results when transforming data.""" # NOTE: netcdf4 does not support complex data types, so we use only zarr here - pytest.importorskip(engine_to_module(engine)) + skip_if_missing_engine(engine) dim = "time" original = POP() diff --git a/tests/models/single/test_sparse_pca.py b/tests/models/single/test_sparse_pca.py index 88db9c4..d6b6514 100644 --- a/tests/models/single/test_sparse_pca.py +++ b/tests/models/single/test_sparse_pca.py @@ -4,7 +4,7 @@ from xeofs.single import SparsePCA -from ...utilities import engine_to_module +from ...utilities import skip_if_missing_engine def test_init(): @@ -490,7 +490,7 @@ def test_save_load(dim, mock_data_array, tmp_path, engine): """Test save/load methods in SparsePCA class, ensuring that we can roundtrip the model and get the same results when transforming data.""" - pytest.importorskip(engine_to_module(engine)) + skip_if_missing_engine(engine) original = SparsePCA() original.fit(mock_data_array, dim) diff --git a/tests/utilities.py b/tests/utilities.py index f300a62..080ebd1 100644 --- a/tests/utilities.py +++ b/tests/utilities.py @@ -1,3 +1,4 @@ +import pytest import numpy as np import pandas as pd from xeofs.utils.data_types import ( @@ -150,9 +151,11 @@ def assert_expected_coords(data1, data2, policy="all") -> None: ) -def engine_to_module(engine: str) -> str: +def skip_if_missing_engine(engine: str): """ - Required for import skipping because xarray uses `engine="netcdf4"` - but the package itself is called `netCDF4`.""" + Skip save/load tests if missing the i/o backend. + """ + # xarray uses engine="netcdf4" but the package itself is called "netCDF4". mapping = {"netcdf4": "netCDF4"} - return mapping.get(engine, engine) + module = mapping.get(engine, engine) + pytest.importorskip(module)