-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e6dfb7
commit 0b36e10
Showing
7 changed files
with
299 additions
and
53 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
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,63 @@ | ||
import numpy as np | ||
import anndata as ad | ||
import scipy.sparse as sps | ||
|
||
KNN_N = 10 | ||
|
||
DIST = np.tile(np.arange(KNN_N), (KNN_N, 1)).astype(float) | ||
CONN = 1 / (DIST + 1) | ||
|
||
KNN = np.diag(np.ones(KNN_N - 1), -1) + np.diag(np.ones(KNN_N - 1), 1) | ||
KNN[0, KNN_N - 1] = 1 | ||
KNN[KNN_N - 1, 0] = 1 | ||
|
||
rng = np.random.default_rng(100) | ||
COUNTS = rng.negative_binomial(5, 0.5, (1000, 10)) | ||
|
||
N = 1000 | ||
BINS = 10 | ||
|
||
EXPRESSION = np.zeros((N, 6), dtype=int) | ||
EXPRESSION[:, 0] = (100 * np.random.default_rng(222222).random(N)).astype(int) | ||
EXPRESSION[:, 1] = EXPRESSION[:, 0] * 1.75 - 0.5 | ||
EXPRESSION[:, 2] = EXPRESSION[:, 0] ** 2 | ||
EXPRESSION[:, 3] = 0 | ||
EXPRESSION[:, 4] = np.arange(N) | ||
EXPRESSION[:, 5] = np.arange(N) * 2 + 10 | ||
|
||
K = 15 | ||
EXPR_KNN = sps.csr_matrix( | ||
( | ||
rng.uniform(low=0.1, high=2., size=K * N), | ||
( | ||
np.repeat(np.arange(N), K), | ||
np.concatenate( | ||
tuple( | ||
rng.choice(np.arange(1000), size=(K, ), replace=False) | ||
for _ in range(N) | ||
) | ||
) | ||
) | ||
), | ||
dtype=np.float32 | ||
) | ||
|
||
EXPRESSION_ADATA = ad.AnnData(EXPRESSION.astype(int)) | ||
|
||
ADATA_UNS_PROGRAM_KEYS = [ | ||
'metric', | ||
'leiden_correlation', | ||
'metric_genes', | ||
'information_distance', | ||
'cluster_program_map', | ||
'n_comps', | ||
'n_programs', | ||
'program_names', | ||
'molecular_cv_loss' | ||
] | ||
|
||
PROGRAMS = ['0', '0', '0', '-1', '1', '1'] | ||
PROGRAMS_ASSIGNED = ['0', '0', '0', '0', '1', '1'] | ||
|
||
TIMES_0 = EXPRESSION[:, 0] / 100 | ||
TIMES_1 = np.arange(N).astype(float) |
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,119 @@ | ||
import unittest | ||
import numpy as np | ||
import numpy.testing as npt | ||
import scipy.sparse as sps | ||
|
||
from ._stubs import ( | ||
EXPR_KNN, | ||
EXPRESSION_ADATA | ||
) | ||
|
||
from inferelator_velocity.denoise import ( | ||
_dist_to_row_stochastic, | ||
denoise | ||
) | ||
|
||
DENOISE_EXPR = np.dot( | ||
_dist_to_row_stochastic(EXPR_KNN).A, | ||
EXPRESSION_ADATA.X | ||
) | ||
|
||
|
||
class TestDenoise(unittest.TestCase): | ||
|
||
threshold = None | ||
|
||
@classmethod | ||
def setUpClass(cls) -> None: | ||
cls.expect = DENOISE_EXPR.copy() | ||
|
||
if cls.threshold is not None: | ||
cls.expect[cls.expect < cls.threshold] = 0 | ||
|
||
return super().setUpClass() | ||
|
||
def setUp(self) -> None: | ||
self.data = EXPRESSION_ADATA.copy() | ||
self.data.X = self.data.X.astype(np.float32) | ||
self.data.obsp['graph'] = EXPR_KNN | ||
|
||
return super().setUp() | ||
|
||
def test_errors(self): | ||
|
||
with self.assertRaises(RuntimeError): | ||
denoise( | ||
self.data, | ||
chunk_size=500, | ||
output_layer='abc', | ||
zero_threshold=self.threshold | ||
) | ||
|
||
self.data.X = self.data.X.astype(int) | ||
with self.assertRaises(RuntimeError): | ||
denoise( | ||
self.data, | ||
chunk_size=500, | ||
graph_key='graph', | ||
output_layer='abc', | ||
zero_threshold=self.threshold | ||
) | ||
|
||
def test_denoise_dense_to_dense(self): | ||
|
||
denoise( | ||
self.data, | ||
chunk_size=500, | ||
graph_key='graph', | ||
output_layer='abc', | ||
zero_threshold=self.threshold | ||
) | ||
|
||
npt.assert_almost_equal( | ||
self.expect, | ||
self.data.layers['abc'], | ||
decimal=2 | ||
) | ||
|
||
def test_denoise_sparse_to_dense(self): | ||
|
||
self.data.X = sps.csr_array(self.data.X) | ||
|
||
denoise( | ||
self.data, | ||
chunk_size=500, | ||
graph_key='graph', | ||
output_layer='abc', | ||
dense=True, | ||
zero_threshold=self.threshold | ||
) | ||
|
||
npt.assert_almost_equal( | ||
self.expect, | ||
self.data.layers['abc'], | ||
decimal=2 | ||
) | ||
|
||
def test_denoise_sparse_to_sparse(self): | ||
|
||
self.data.X = sps.csr_array(self.data.X) | ||
|
||
denoise( | ||
self.data, | ||
chunk_size=500, | ||
graph_key='graph', | ||
output_layer='abc', | ||
dense=False, | ||
zero_threshold=self.threshold | ||
) | ||
|
||
npt.assert_almost_equal( | ||
self.expect, | ||
self.data.layers['abc'].A, | ||
decimal=2 | ||
) | ||
|
||
|
||
class TestDenoiseThreshold(TestDenoise): | ||
|
||
threshold = 100 |
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
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
Empty file.
Oops, something went wrong.