Skip to content

Commit

Permalink
Add TestCsrVcorrcoef
Browse files Browse the repository at this point in the history
Test class for unit testing `csr_vcorrcoef`.
  • Loading branch information
WeilerP committed Sep 1, 2021
1 parent 9a80062 commit a26ce8a
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions tests/preprocessing/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import pytest

import numpy as np
from scipy.sparse import csr_matrix, spmatrix

from scvelo.preprocessing.utils import csr_vcorrcoef


class TestCsrVcorrcoef:
@pytest.mark.parametrize(
"X",
(
np.zeros(3),
np.array([1, 0, -4]),
np.array([-0.3, 0.5, 0.93]),
np.zeros(shape=(3, 3)),
np.eye(3),
np.array([[1, 2, 3], [1, -1, 1]]),
np.array([[0.1, -0.3, 7.5], [8.3, 0.4, -0.9]]),
),
)
@pytest.mark.parametrize(
"y",
(
np.zeros(3),
np.ones(3),
np.array([1, 0, 0]),
np.array([1, 2, 3]),
np.array([-0.24, 0.7, 0.4]),
),
)
def test_dense_arrays(self, X: np.ndarray, y: np.ndarray):
pearsonr = csr_vcorrcoef(X=X, y=y)

if X.ndim == 1:
np.testing.assert_almost_equal(np.corrcoef(X, y)[0, 1], pearsonr)
else:
assert all(
np.allclose(np.corrcoef(sample, y)[0, 1], corr, equal_nan=True)
for corr, sample in zip(pearsonr, X)
)

@pytest.mark.parametrize(
"X",
(
csr_matrix(np.zeros(3)),
csr_matrix(np.array([1, 0, -4])),
csr_matrix(np.array([-0.3, 0.5, 0.93])),
csr_matrix(np.zeros(shape=(3, 3))),
csr_matrix(np.eye(3)),
csr_matrix(np.array([[1, 2, 3], [1, -1, 1]])),
csr_matrix(np.array([[0.1, -0.3, 7.5], [8.3, 0.4, -0.9]])),
),
)
@pytest.mark.parametrize(
"y",
(
np.zeros(3),
np.ones(3),
np.array([1, 0, 0]),
np.array([1, 2, 3]),
np.array([-0.24, 0.7, 0.4]),
),
)
def test_sparse_arrays(self, X: spmatrix, y: np.ndarray):
pearsonr = csr_vcorrcoef(X=X, y=y)

X_dense = X.A.squeeze()

if X_dense.ndim == 1:
np.testing.assert_almost_equal(np.corrcoef(X_dense, y)[0, 1], pearsonr)
else:
assert all(
np.allclose(np.corrcoef(sample, y)[0, 1], corr, equal_nan=True)
for corr, sample in zip(pearsonr, X_dense)
)

0 comments on commit a26ce8a

Please sign in to comment.