mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-10-03 07:38:12 +08:00
remove --nan-to-num CLI parameter (#548)
* remove --nan-to-num CLI parameter * factor tests better * lint - remove unused variables
This commit is contained in:
@@ -2,6 +2,9 @@ import json
|
||||
import pytest
|
||||
import unittest
|
||||
import warnings
|
||||
import math
|
||||
|
||||
import decode_fbs
|
||||
|
||||
from server.app.scanpy_engine.scanpy_engine import ScanpyEngine
|
||||
from server.app.util.errors import JSONEncodingValueError
|
||||
@@ -16,24 +19,15 @@ class NaNTest(unittest.TestCase):
|
||||
"obs_names": None,
|
||||
"var_names": None,
|
||||
"diffexp_lfc_cutoff": 0.01,
|
||||
"nan_to_num": False,
|
||||
}
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("ignore", category=UserWarning)
|
||||
self.data = ScanpyEngine("server/test/test_datasets/nan.h5ad", self.args)
|
||||
self.data._create_schema()
|
||||
self.args_nan = dict(self.args)
|
||||
self.args_nan["nan_to_num"] = True
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("ignore", category=UserWarning)
|
||||
self.data_nan = ScanpyEngine(
|
||||
"server/test/test_datasets/nan.h5ad", self.args_nan
|
||||
)
|
||||
self.data_nan._create_schema()
|
||||
|
||||
def test_load(self):
|
||||
with self.assertWarns(UserWarning):
|
||||
ScanpyEngine("server/test/test_datasets/nan.h5ad", self.args_nan)
|
||||
ScanpyEngine("server/test/test_datasets/nan.h5ad", self.args)
|
||||
|
||||
def test_init(self):
|
||||
self.assertEqual(self.data.cell_count, 100)
|
||||
@@ -41,45 +35,38 @@ class NaNTest(unittest.TestCase):
|
||||
epsilon = 0.000_005
|
||||
self.assertTrue(self.data.data.X[0, 0] - -0.171_469_51 < epsilon)
|
||||
|
||||
self.assertEqual(self.data_nan.cell_count, 100)
|
||||
self.assertEqual(self.data_nan.gene_count, 100)
|
||||
epsilon = 0.000_005
|
||||
self.assertTrue(self.data_nan.data.X[0, 0] - -0.171_469_51 < epsilon)
|
||||
|
||||
def test_dataframe(self):
|
||||
data_frame_obs = json.loads(self.data_nan.data_frame(None, "obs"))
|
||||
self.assertEqual(len(data_frame_obs["var"]), 100)
|
||||
self.assertEqual(len(data_frame_obs["obs"]), 100)
|
||||
data_frame_var = json.loads(self.data_nan.data_frame(None, "var"))
|
||||
self.assertEqual(len(data_frame_var["var"]), 100)
|
||||
self.assertEqual(len(data_frame_var["obs"]), 100)
|
||||
with pytest.raises(JSONEncodingValueError):
|
||||
data_frame_obs = json.loads(self.data.data_frame(None, "obs"))
|
||||
with pytest.raises(JSONEncodingValueError):
|
||||
data_frame_var = json.loads(self.data.data_frame(None, "var"))
|
||||
data_frame_var = decode_fbs.decode_matrix_FBS(self.data.data_frame_to_fbs_matrix(None, "var"))
|
||||
self.assertIsNotNone(data_frame_var)
|
||||
self.assertEqual(data_frame_var["n_rows"], 100)
|
||||
self.assertEqual(data_frame_var["n_cols"], 100)
|
||||
self.assertTrue(math.isnan(data_frame_var["columns"][3][3]))
|
||||
|
||||
def test_dataframe_nan_to_0(self):
|
||||
data_frame_obs = json.loads(self.data_nan.data_frame(None, "obs"))
|
||||
self.assertEqual(data_frame_obs["obs"][1][3], 0.0)
|
||||
data_frame_var = json.loads(self.data_nan.data_frame(None, "var"))
|
||||
self.assertEqual(data_frame_var["var"][1][5], 0.0)
|
||||
with pytest.raises(JSONEncodingValueError):
|
||||
json.loads(self.data.data_frame(None, "obs"))
|
||||
with pytest.raises(JSONEncodingValueError):
|
||||
json.loads(self.data.data_frame(None, "var"))
|
||||
|
||||
def test_annotation_nan_to_0(self):
|
||||
annotations_obs = json.loads(self.data_nan.annotation(None, "obs"))
|
||||
self.assertEqual(annotations_obs["data"][0][3], 0.0)
|
||||
annotations_var = json.loads(self.data_nan.annotation(None, "var"))
|
||||
self.assertEqual(annotations_var["data"][0][3], 0.0)
|
||||
def test_dataframe_obs_not_implemented(self):
|
||||
with self.assertRaises(ValueError) as cm:
|
||||
decode_fbs.decode_matrix_FBS(self.data.data_frame_to_fbs_matrix(None, "obs"))
|
||||
self.assertIsNotNone(cm.exception)
|
||||
|
||||
def test_annotation(self):
|
||||
annotations = json.loads(self.data_nan.annotation(None, "obs"))
|
||||
annotations = decode_fbs.decode_matrix_FBS(self.data.annotation_to_fbs_matrix("obs"))
|
||||
self.assertEqual(
|
||||
annotations["names"],
|
||||
["name", "n_genes", "percent_mito", "n_counts", "louvain"],
|
||||
annotations["col_idx"],
|
||||
["name", "n_genes", "percent_mito", "n_counts", "louvain"]
|
||||
)
|
||||
annotations = json.loads(self.data_nan.annotation(None, "var"))
|
||||
self.assertEqual(annotations["names"], ["name", "n_cells", "var_with_nans"])
|
||||
self.assertEqual(len(annotations["data"]), 100)
|
||||
self.assertEqual(annotations["n_rows"], 100)
|
||||
self.assertTrue(math.isnan(annotations["columns"][2][0]))
|
||||
|
||||
annotations = decode_fbs.decode_matrix_FBS(self.data.annotation_to_fbs_matrix("var"))
|
||||
self.assertEqual(annotations["col_idx"], ["name", "n_cells", "var_with_nans"])
|
||||
self.assertEqual(annotations["n_rows"], 100)
|
||||
self.assertTrue(math.isnan(annotations["columns"][2][0]))
|
||||
|
||||
with pytest.raises(JSONEncodingValueError):
|
||||
annotations = json.loads(self.data.annotation(None, "obs"))
|
||||
json.loads(self.data.annotation(None, "obs"))
|
||||
with pytest.raises(JSONEncodingValueError):
|
||||
annotations = json.loads(self.data.annotation(None, "var"))
|
||||
json.loads(self.data.annotation(None, "var"))
|
||||
|
||||
Reference in New Issue
Block a user