mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-10-06 11:28:11 +08:00
Merge pull request #117 from chanzuckerberg/csweaver/tests
Csweaver/tests
This commit is contained in:
@@ -1,2 +1,3 @@
|
|||||||
recursive-include server/app/web/templates *
|
recursive-include server/app/web/templates *
|
||||||
recursive-include server/app/web/static *
|
recursive-include server/app/web/static *
|
||||||
|
|
||||||
|
|||||||
@@ -61,11 +61,11 @@ class ScanpyEngine(CXGDriver):
|
|||||||
else:
|
else:
|
||||||
min_ = value["query"]["min"]
|
min_ = value["query"]["min"]
|
||||||
max_ = value["query"]["max"]
|
max_ = value["query"]["max"]
|
||||||
if min_:
|
if min_ is not None:
|
||||||
key_idx = np.array((getattr(self.data.obs, key) >= min_).data)
|
key_idx = np.array((getattr(self.data.obs, key) >= min_).data)
|
||||||
cell_idx = np.logical_and(cell_idx, key_idx)
|
cell_idx = np.logical_and(cell_idx, key_idx)
|
||||||
if max_:
|
if max_ is not None:
|
||||||
key_idx = np.array((getattr(self.data.obs, key) <= min_).data)
|
key_idx = np.array((getattr(self.data.obs, key) <= max_).data)
|
||||||
cell_idx = np.logical_and(cell_idx, key_idx)
|
cell_idx = np.logical_and(cell_idx, key_idx)
|
||||||
return self.data[cell_idx, :]
|
return self.data[cell_idx, :]
|
||||||
|
|
||||||
@@ -124,6 +124,7 @@ class ScanpyEngine(CXGDriver):
|
|||||||
expression_1 = self.data.X[cells_idx_1, :]
|
expression_1 = self.data.X[cells_idx_1, :]
|
||||||
expression_2 = self.data.X[cells_idx_2, :]
|
expression_2 = self.data.X[cells_idx_2, :]
|
||||||
diff_exp = stats.ttest_ind(expression_1, expression_2)
|
diff_exp = stats.ttest_ind(expression_1, expression_2)
|
||||||
|
# TODO break this up into functions
|
||||||
set1 = np.logical_and(diff_exp.pvalue < pval, diff_exp.statistic > 0)
|
set1 = np.logical_and(diff_exp.pvalue < pval, diff_exp.statistic > 0)
|
||||||
set2 = np.logical_and(diff_exp.pvalue < pval, diff_exp.statistic < 0)
|
set2 = np.logical_and(diff_exp.pvalue < pval, diff_exp.statistic < 0)
|
||||||
stat1 = diff_exp.statistic[set1]
|
stat1 = diff_exp.statistic[set1]
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
aniso8601==3.0.2
|
aniso8601==3.0.2
|
||||||
anndata==0.6.4
|
anndata==0.6.1
|
||||||
certifi==2018.4.16
|
certifi==2018.4.16
|
||||||
chardet==3.0.4
|
chardet==3.0.4
|
||||||
click==6.7
|
click==6.7
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from unittest.mock import MagicMock
|
from unittest.mock import MagicMock
|
||||||
import sys
|
|
||||||
sys.path.insert(0, "../app")
|
from server.app.util.filter import _convert_variable, parse_filter
|
||||||
from util.filter import _convert_variable, parse_filter
|
|
||||||
|
|
||||||
|
|
||||||
class UtilTest(unittest.TestCase):
|
class UtilTest(unittest.TestCase):
|
||||||
@@ -72,3 +70,6 @@ class UtilTest(unittest.TestCase):
|
|||||||
filterMock.getlist.return_value = ["0,*"]
|
filterMock.getlist.return_value = ["0,*"]
|
||||||
query = parse_filter(filterMock, self.schema)
|
query = parse_filter(filterMock, self.schema)
|
||||||
assert query == {"n_genes": {"variable_type": "continuous", "value_type": "int", "query": {"min": 0, "max": None}}}
|
assert query == {"n_genes": {"variable_type": "continuous", "value_type": "int", "query": {"min": 0, "max": None}}}
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
import unittest
|
||||||
|
|
||||||
|
from server.app.scanpy_engine.scanpy_engine import ScanpyEngine
|
||||||
|
|
||||||
|
|
||||||
|
class UtilTest(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.data = ScanpyEngine("example-dataset/", schema="data_schema.json")
|
||||||
|
|
||||||
|
def test_init(self):
|
||||||
|
self.assertEqual(self.data.cell_count, 2638)
|
||||||
|
self.assertEqual(self.data.gene_count, 1838)
|
||||||
|
epsilon = 0.000005
|
||||||
|
self.assertTrue(self.data.data.X[0,0] - -0.17146951 < epsilon)
|
||||||
|
|
||||||
|
def test_schema(self):
|
||||||
|
self.assertEqual(self.data.schema, {'CellName': {'type': 'string', 'variabletype': 'categorical', 'displayname': 'Name', 'include': True}, 'n_genes': {'type': 'int', 'variabletype': 'continuous', 'displayname': 'Num Genes', 'include': True}, 'percent_mito': {'type': 'float', 'variabletype': 'continuous', 'displayname': 'Mitochondrial Percentage', 'include': True}, 'n_counts': {'type': 'float', 'variabletype': 'continuous', 'displayname': 'Num Counts', 'include': True}, 'louvain': {'type': 'string', 'variabletype': 'categorical', 'displayname': 'Louvain Cluster', 'include': True}})
|
||||||
|
|
||||||
|
def test_cells(self):
|
||||||
|
cells = self.data.cells()
|
||||||
|
self.assertIn("AAACATACAACCAC-1", cells)
|
||||||
|
self.assertEqual(len(cells), 2638)
|
||||||
|
|
||||||
|
def test_genes(self):
|
||||||
|
genes = self.data.genes()
|
||||||
|
self.assertIn("SEPT4", genes)
|
||||||
|
self.assertEqual(len(genes), 1838)
|
||||||
|
|
||||||
|
def test_filter_categorical(self):
|
||||||
|
filter = {"louvain": {"variable_type": "categorical", "value_type": "string", "query": ["B cells"]}}
|
||||||
|
filtered_data = self.data.filter_cells(filter)
|
||||||
|
self.assertEqual(filtered_data.shape, (342, 1838))
|
||||||
|
louvain_vals = filtered_data.obs['louvain'].tolist()
|
||||||
|
self.assertIn("B cells", louvain_vals)
|
||||||
|
self.assertNotIn("NK cells", louvain_vals)
|
||||||
|
|
||||||
|
def test_filter_continuous(self):
|
||||||
|
# print(self.data.data.obs["n_genes"].tolist())
|
||||||
|
filter = {"n_genes": {"variable_type": "continuous", "value_type": "int", "query": {"min": 300, "max": 400}}}
|
||||||
|
filtered_data = self.data.filter_cells(filter)
|
||||||
|
self.assertEqual(filtered_data.shape, (71, 1838))
|
||||||
|
n_genes_vals = filtered_data.obs['n_genes'].tolist()
|
||||||
|
for val in n_genes_vals:
|
||||||
|
self.assertTrue(300 <= val <= 400)
|
||||||
|
|
||||||
|
def test_metadata(self):
|
||||||
|
metadata = self.data.metadata(df=self.data.data)
|
||||||
|
self.assertEqual(len(metadata), 2638)
|
||||||
|
self.assertIn('louvain', metadata[0])
|
||||||
|
|
||||||
|
def test_create_graph(self):
|
||||||
|
graph = self.data.create_graph(df=self.data.data)
|
||||||
|
self.assertEqual(graph[0][1], 0.5545382653143183)
|
||||||
|
self.assertEqual(graph[0][2], 0.6021833809031731)
|
||||||
|
|
||||||
|
def test_diffexp(self):
|
||||||
|
diffexp = self.data.diffexp(["AAACATACAACCAC-1", "AACCGATGGTCATG-1"], ["CCGATAGACCTAAG-1", "GGTGGAGAAGTAGA-1"], 0.5, 7)
|
||||||
|
self.assertEqual(diffexp["celllist1"]["topgenes"], ['EBNA1BP2', 'DIAPH1', 'SLC25A11', 'SNRNP27', 'COMMD8', 'COTL1', 'GTF3A'])
|
||||||
|
|
||||||
|
def test_expression(self):
|
||||||
|
expression = self.data.expression(cells=["AAACATACAACCAC-1"])
|
||||||
|
data_exp = self.data.data[["AAACATACAACCAC-1"], :].X
|
||||||
|
for idx in range(len(expression["cells"][0]["e"])):
|
||||||
|
self.assertEqual(expression["cells"][0]["e"][idx], data_exp[idx])
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
Reference in New Issue
Block a user