-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test class for unit testing `csr_vcorrcoef`.
- Loading branch information
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) |