Skip to content

Commit

Permalink
fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
katosh committed Oct 3, 2023
1 parent 1a89b5b commit 776652c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
4 changes: 1 addition & 3 deletions tests/utils_diffusion_maps_from_kernel.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
import numpy as np
import pandas as pd
from scipy.sparse import csr_matrix
Expand Down Expand Up @@ -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)
Expand Down
50 changes: 50 additions & 0 deletions tests/utils_run_diffusion_maps.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 776652c

Please sign in to comment.