Skip to content

Commit

Permalink
refactor(test): adapt to CPCCA class
Browse files Browse the repository at this point in the history
  • Loading branch information
nicrie committed Aug 26, 2024
1 parent ae7d36a commit 9ee4acf
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 45 deletions.
12 changes: 0 additions & 12 deletions tests/models/test_complex_mca.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,18 +231,6 @@ def test_transform_not_implemented(mca_model, mock_data_array, dim):
mca_model.transform(mock_data_array, mock_data_array)


def test_homogeneous_patterns_not_implemented():
mca = ComplexMCA()
with pytest.raises(NotImplementedError):
mca.homogeneous_patterns()


def test_heterogeneous_patterns_not_implemented():
mca = ComplexMCA()
with pytest.raises(NotImplementedError):
mca.heterogeneous_patterns()


@pytest.mark.parametrize(
"dim",
[
Expand Down
10 changes: 4 additions & 6 deletions tests/models/test_complex_mca_rotator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
import numpy as np
import pytest
import xarray as xr

# Import the classes from your modules
Expand Down Expand Up @@ -58,7 +58,7 @@ def test_transform(mca_model, mock_data_array):
mca_rotator.fit(mca_model)

with pytest.raises(NotImplementedError):
mca_rotator.transform(data1=mock_data_array, data2=mock_data_array)
mca_rotator.transform(X=mock_data_array, Y=mock_data_array)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -192,8 +192,7 @@ def test_scores(mca_model, mock_data_array, dim):
def test_homogeneous_patterns(mca_model, mock_data_array, dim):
mca_rotator = ComplexMCARotator(n_modes=2)
mca_rotator.fit(mca_model)
with pytest.raises(NotImplementedError):
_ = mca_rotator.homogeneous_patterns()
mca_rotator.homogeneous_patterns()


@pytest.mark.parametrize(
Expand All @@ -207,8 +206,7 @@ def test_homogeneous_patterns(mca_model, mock_data_array, dim):
def test_heterogeneous_patterns(mca_model, mock_data_array, dim):
mca_rotator = ComplexMCARotator(n_modes=2)
mca_rotator.fit(mca_model)
with pytest.raises(NotImplementedError):
_ = mca_rotator.heterogeneous_patterns()
mca_rotator.heterogeneous_patterns()


@pytest.mark.parametrize(
Expand Down
7 changes: 0 additions & 7 deletions tests/models/test_cpcca.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,6 @@ def test_inverse_transform(cpcca):
assert isinstance(Xrec2, xr.DataArray)


def test_squared_covariance(cpcca):
X, Y = generate_well_conditioned_data()
cpcca.fit(X, Y, "sample")
squared_covariance = cpcca.squared_covariance()
assert isinstance(squared_covariance, xr.DataArray)


def test_squared_covariance_fraction(cpcca):
X, Y = generate_well_conditioned_data()
cpcca.fit(X, Y, "sample")
Expand Down
4 changes: 2 additions & 2 deletions tests/models/test_mca.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test_fit_with_dataarray_list(mca_model, mock_data_array_list, dim):
)
def test_transform(mca_model, mock_data_array, dim):
mca_model.fit(mock_data_array, mock_data_array, dim)
result = mca_model.transform(data1=mock_data_array, data2=mock_data_array)
result = mca_model.transform(X=mock_data_array, Y=mock_data_array)
assert isinstance(result, list)
assert isinstance(result[0], xr.DataArray)

Expand All @@ -111,7 +111,7 @@ def test_transform_unseen_data(mca_model, mock_data_array, dim):
data_unseen = mock_data_array.isel(time=slice(21, None))

mca_model.fit(data, data, dim)
result = mca_model.transform(data1=data_unseen, data2=data_unseen)
result = mca_model.transform(X=data_unseen, Y=data_unseen)
assert isinstance(result, list)
assert isinstance(result[0], xr.DataArray)
# Check that unseen data can be transformed
Expand Down
7 changes: 4 additions & 3 deletions tests/models/test_mca_rotator.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import pytest
import numpy as np
import pytest
import xarray as xr

# Import the classes from your modules
from xeofs.models import MCA, MCARotator

from ..utilities import data_is_dask


@pytest.fixture
def mca_model(mock_data_array, dim):
mca = MCA(n_modes=5)
mca = MCA(n_modes=5, use_pca=False)
mca.fit(mock_data_array, mock_data_array, dim)
return mca


@pytest.fixture
def mca_model_delayed(mock_dask_data_array, dim):
mca = MCA(n_modes=5, compute=False, check_nans=False)
mca = MCA(n_modes=5, compute=False, check_nans=False, use_pca=False)
mca.fit(mock_dask_data_array, mock_dask_data_array, dim)
return mca

Expand Down
58 changes: 43 additions & 15 deletions tests/models/test_orthogonality.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
)


def is_diagonal(matrix, tol=1e-10):
# Check if all off-diagonal elements are close to zero within the specified tolerance
off_diagonal_elements = matrix - np.diag(np.diag(matrix))
return np.all(np.abs(off_diagonal_elements) < tol)


# Orthogonality
# =============================================================================
# EOF
Expand Down Expand Up @@ -319,7 +325,13 @@ def test_rmca_components(dim, use_coslat, power, squared_loadings, mock_data_arr
"""Components are NOT orthogonal"""
data1 = mock_data_array.copy()
data2 = data1.copy() ** 2
model = MCA(n_modes=19, standardize=True, use_coslat=use_coslat)
model = MCA(
n_modes=19,
standardize=True,
use_coslat=use_coslat,
use_pca=True,
n_pca_modes=19,
)
model.fit(data1, data2, dim=dim)
rot = MCARotator(n_modes=5, power=power, squared_loadings=squared_loadings)
rot.fit(model)
Expand Down Expand Up @@ -359,19 +371,27 @@ def test_rmca_scores(dim, use_coslat, power, squared_loadings, mock_data_array):
"""Components are orthogonal only for Varimax rotation"""
data1 = mock_data_array.copy()
data2 = data1.copy() ** 2
model = MCA(n_modes=5, standardize=True, use_coslat=use_coslat)
model = MCA(
n_modes=5,
standardize=True,
use_coslat=use_coslat,
use_pca=False,
n_pca_modes=19,
)
model.fit(data1, data2, dim=dim)
rot = MCARotator(n_modes=5, power=power, squared_loadings=squared_loadings)
rot.fit(model)
W1 = rot.data["norm1"].values
W2 = rot.data["norm2"].values
U1 = rot.data["scores1"].values
U2 = rot.data["scores2"].values
K = U1.conj().T @ U2
target = np.eye(K.shape[0]) * (model.data["input_data1"].sample.size - 1)
K = U1.T @ U2 / (U1.shape[0] - 1)
target = np.diag(W1 * W2)
if power == 1:
# Varimax rotation does guarantee orthogonality
assert np.allclose(K, target, atol=1e-5), "Components are not orthogonal"
np.testing.assert_allclose(K, target, atol=1e-5, rtol=1e-5)
else:
assert not np.allclose(K, target), "Components are orthogonal"
assert not is_diagonal(K), "Components are orthogonal"


# Complex Rotated MCA
Expand All @@ -396,7 +416,9 @@ def test_crmca_components(dim, use_coslat, power, squared_loadings, mock_data_ar
"""Components are NOT orthogonal"""
data1 = mock_data_array.copy()
data2 = data1.copy() ** 2
model = ComplexMCA(n_modes=19, standardize=True, use_coslat=use_coslat)
model = ComplexMCA(
n_modes=19, standardize=True, use_coslat=use_coslat, use_pca=False
)
model.fit(data1, data2, dim=dim)
rot = ComplexMCARotator(n_modes=5, power=power, squared_loadings=squared_loadings)
rot.fit(model)
Expand Down Expand Up @@ -436,17 +458,21 @@ def test_crmca_scores(dim, use_coslat, power, squared_loadings, mock_data_array)
"""Components are orthogonal only for Varimax rotation"""
data1 = mock_data_array.copy()
data2 = data1.copy() ** 2
model = ComplexMCA(n_modes=5, standardize=True, use_coslat=use_coslat)
model = ComplexMCA(
n_modes=5, standardize=True, use_coslat=use_coslat, use_pca=False
)
model.fit(data1, data2, dim=dim)
rot = ComplexMCARotator(n_modes=5, power=power, squared_loadings=squared_loadings)
rot.fit(model)
W1 = rot.data["norm1"].values
W2 = rot.data["norm2"].values
U1 = rot.data["scores1"].values
U2 = rot.data["scores2"].values
K = U1.conj().T @ U2
target = np.eye(K.shape[0]) * (model.data["input_data1"].sample.size - 1)
K = U1.conj().T @ U2 / (U1.shape[0] - 1)
target = np.diag(W1 * W2)
if power == 1:
# Varimax rotation does guarantee orthogonality
assert np.allclose(K, target, atol=1e-5), "Components are not orthogonal"
np.testing.assert_allclose(K, target, atol=1e-5, rtol=1e-5)
else:
assert not np.allclose(K, target), "Components are orthogonal"

Expand Down Expand Up @@ -567,7 +593,7 @@ def test_mca_transform(dim, use_coslat, mock_data_array):
model = MCA(n_modes=5, standardize=True, use_coslat=use_coslat)
model.fit(data1, data2, dim=dim)
scores1, scores2 = model.scores()
pseudo_scores1, pseudo_scores2 = model.transform(data1=data1, data2=data2)
pseudo_scores1, pseudo_scores2 = model.transform(X=data1, Y=data2)
assert np.allclose(
scores1, pseudo_scores1, atol=1e-4
), "Transformed data does not match the scores"
Expand All @@ -593,7 +619,7 @@ def test_cmca_transform(dim, use_coslat, mock_data_array):
model.fit(data1, data2, dim=dim)
scores1, scores2 = model.scores()
with pytest.raises(NotImplementedError):
pseudo_scores1, pseudo_scores2 = model.transform(data1=data1, data2=data2)
pseudo_scores1, pseudo_scores2 = model.transform(X=data1, Y=data2)


# Rotated MCA
Expand Down Expand Up @@ -660,7 +686,7 @@ def test_crmca_transform(dim, use_coslat, power, squared_loadings, mock_data_arr
rot.fit(model)
scores1, scores2 = rot.scores()
with pytest.raises(NotImplementedError):
pseudo_scores1, pseudo_scores2 = rot.transform(data1=data1, data2=data2)
pseudo_scores1, pseudo_scores2 = rot.transform(X=data1, Y=data2)


# Reconstruct
Expand Down Expand Up @@ -931,7 +957,9 @@ def test_crmca_inverse_transform(
# the other tests.
data1 = mock_data_array.copy()
data2 = data1.copy() ** 2
model = ComplexMCA(n_modes=10, standardize=True, use_coslat=use_coslat)
model = ComplexMCA(
n_modes=10, standardize=True, use_coslat=use_coslat, use_pca=False
)
model.fit(data1, data2, dim=dim)
rot = ComplexMCARotator(n_modes=10, power=power, squared_loadings=squared_loadings)
rot.fit(model)
Expand Down

0 comments on commit 9ee4acf

Please sign in to comment.