diff --git a/tests/utils_diffusion_maps_from_kernel.py b/tests/utils_diffusion_maps_from_kernel.py index 1732e6f7..c533bc65 100644 --- a/tests/utils_diffusion_maps_from_kernel.py +++ b/tests/utils_diffusion_maps_from_kernel.py @@ -1,3 +1,4 @@ +import pytest import numpy as np import pandas as pd from scipy.sparse import csr_matrix @@ -33,12 +34,9 @@ def test_diffusion_maps_seed(): kernel = create_mock_kernel(50) result1 = diffusion_maps_from_kernel(kernel, seed=0) result2 = diffusion_maps_from_kernel(kernel, seed=0) - result3 = diffusion_maps_from_kernel(kernel, seed=1) # Seed usage should yield the same result assert np.allclose(result1["EigenValues"], result2["EigenValues"]) - # Different seed should yield different results - assert not np.allclose(result1["EigenValues"], result3["EigenValues"]) def test_diffusion_maps_eigen(): kernel = create_mock_kernel(50) diff --git a/tests/utils_run_diffusion_maps.py b/tests/utils_run_diffusion_maps.py new file mode 100644 index 00000000..31b677fb --- /dev/null +++ b/tests/utils_run_diffusion_maps.py @@ -0,0 +1,50 @@ +import pytest +import pandas as pd +import scanpy as sc +from scipy.sparse import csr_matrix, issparse +import numpy as np + +from palantir.utils import run_diffusion_maps + +# Generate mock DataFrame data +def mock_dataframe(rows, cols): + return pd.DataFrame(np.random.rand(rows, cols)) + +# Generate mock sc.AnnData object +def mock_anndata(rows, cols, keys): + ad = sc.AnnData(np.random.rand(rows, cols)) + for key in keys: + ad.obsm[key] = np.random.rand(rows, cols) + return ad + +def test_run_diffusion_maps_dataframe(): + df = mock_dataframe(50, 30) + result = run_diffusion_maps(df) + + assert isinstance(result, dict) + assert set(result.keys()) == {'T', 'EigenVectors', 'EigenValues', 'kernel'} + + assert isinstance(result['kernel'], csr_matrix) + assert isinstance(result['T'], csr_matrix) + assert isinstance(result['EigenVectors'], pd.DataFrame) + assert isinstance(result['EigenValues'], pd.Series) + +def test_run_diffusion_maps_anndata(): + keys = ['X_pca'] + ad = mock_anndata(50, 30, keys) + result = run_diffusion_maps(ad) + + assert 'DM_Kernel' in ad.obsp + assert 'DM_Similarity' in ad.obsp + assert 'DM_EigenVectors' in ad.obsm + assert 'DM_EigenValues' in ad.uns + + assert np.array_equal(ad.obsp['DM_Kernel'].toarray(), result['kernel'].toarray()) + assert np.array_equal(ad.obsp['DM_Similarity'].toarray(), result['T'].toarray()) + assert np.array_equal(ad.obsm['DM_EigenVectors'], result['EigenVectors'].values) + assert np.array_equal(ad.uns['DM_EigenValues'], result['EigenValues']) + +def test_run_diffusion_maps_exceptions(): + # Test with neither pd.DataFrame nor sc.AnnData + with pytest.raises(ValueError): + run_diffusion_maps("invalid_type")